PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ee/spec/lib/ee/gitlab/background_migration/remove_duplicated_cs_findings_without_vulnerability_id_spec.rb

https://gitlab.com/axil/gitlab
Ruby | 106 lines | 86 code | 19 blank | 1 comment | 0 complexity | bd64f40931f304e7f5064ad646450858 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Gitlab::BackgroundMigration::RemoveDuplicatedCsFindingsWithoutVulnerabilityId, :migration, schema: 20200917135802 do
  4. let(:migration) { 'RemoveDuplicatedCsFindingsWithoutVulnerabilityId'}
  5. let(:namespaces) { table(:namespaces) }
  6. let(:notes) { table(:notes) }
  7. let(:group) { namespaces.create!(name: 'foo', path: 'foo') }
  8. let(:projects) { table(:projects) }
  9. let(:findings) { table(:vulnerability_occurrences) }
  10. let(:scanners) { table(:vulnerability_scanners) }
  11. let(:identifiers) { table(:vulnerability_identifiers) }
  12. let(:finding_identifiers) { table(:vulnerability_occurrence_identifiers) }
  13. let!(:project) { projects.create!(id: 12058473, namespace_id: group.id, name: 'gitlab', path: 'gitlab') }
  14. let!(:scanner) do
  15. scanners.create!(id: 6, project_id: project.id, external_id: 'clair', name: 'Security Scanner')
  16. end
  17. it 'removes duplicate findings and vulnerabilities' do
  18. allow(::Gitlab).to receive(:com?).and_return(true)
  19. ids = [231411, 231412, 231413, 231500, 231600, 231700, 231800]
  20. fingerprints = %w(
  21. 6c871440eb9f7618b9aef25e5246acddff6ed7a1
  22. 9d1a47927875f1aee1e2b9f16c25a8ff7586f1a6
  23. d7da2cc109c18d890ab239e833524d451cc45246
  24. 6c871440eb9f7618b9aef25e5246acddff6ed7a1
  25. 9d1a47927875f1aee1e2b9f16c25a8ff7586f1a6
  26. d7da2cc109c18d890ab239e833524d451cc45246
  27. d7da2cc109c18d890ab239e833524d453cd45246
  28. )
  29. expected_fingerprints = %w(
  30. 6c871440eb9f7618b9aef25e5246acddff6ed7a1
  31. 9d1a47927875f1aee1e2b9f16c25a8ff7586f1a6
  32. d7da2cc109c18d890ab239e833524d451cc45246
  33. d7da2cc109c18d890ab239e833524d453cd45246
  34. )
  35. 7.times.each { |x| identifiers.create!(vulnerability_identifer_params(x, project.id)) }
  36. 3.times.each { |x| findings.create!(finding_params(x, project.id).merge({ id: ids[x], location_fingerprint: fingerprints[x], vulnerability_id: nil })) }
  37. findings.create!(finding_params(0, project.id).merge({ id: ids[3], location_fingerprint: Gitlab::Database::ShaAttribute.new.serialize(fingerprints[3]).to_s, vulnerability_id: nil }))
  38. findings.create!(finding_params(1, project.id).merge({ id: ids[4], location_fingerprint: Gitlab::Database::ShaAttribute.new.serialize(fingerprints[4]).to_s, vulnerability_id: nil }))
  39. findings.create!(finding_params(2, project.id).merge({ id: ids[5], location_fingerprint: Gitlab::Database::ShaAttribute.new.serialize(fingerprints[5]).to_s, vulnerability_id: nil }))
  40. findings.create!(finding_params(3, project.id).merge({ id: ids[6], location_fingerprint: Gitlab::Database::ShaAttribute.new.serialize(fingerprints[6]).to_s, vulnerability_id: nil }))
  41. 7.times.each { |x| finding_identifiers.create!(occurrence_id: ids[x], identifier_id: x ) }
  42. expect(finding_identifiers.all.count). to eq(7)
  43. described_class.new.perform(231411, 231413)
  44. expect(findings.ids).to match_array([231411, 231412, 231413, 231800])
  45. expect(findings.where(report_type: 2).count). to eq(4)
  46. expect(finding_identifiers.all.count). to eq(4)
  47. location_fingerprints = findings.pluck(:location_fingerprint).flat_map { |x| Gitlab::Database::ShaAttribute.new.deserialize(x) }
  48. expect(location_fingerprints).to match_array(expected_fingerprints)
  49. end
  50. def vulnerability_identifer_params(id, project_id)
  51. {
  52. id: id,
  53. project_id: project_id,
  54. fingerprint: 'd432c2ad2953e8bd587a3a43b3ce309b5b0154c' + id.to_s,
  55. external_type: 'SECURITY_ID',
  56. external_id: 'SECURITY_0',
  57. name: 'SECURITY_IDENTIFIER 0'
  58. }
  59. end
  60. def finding_params(primary_identifier_id, project_id)
  61. attrs = attributes_for(:vulnerabilities_finding) # rubocop: disable RSpec/FactoriesInMigrationSpecs
  62. {
  63. severity: 0,
  64. confidence: 5,
  65. report_type: 2,
  66. project_id: project_id,
  67. scanner_id: 6,
  68. primary_identifier_id: primary_identifier_id,
  69. project_fingerprint: attrs[:project_fingerprint],
  70. location_fingerprint: Digest::SHA1.hexdigest(SecureRandom.hex(10)),
  71. uuid: SecureRandom.uuid,
  72. name: attrs[:name],
  73. metadata_version: '1.3',
  74. raw_metadata: attrs[:raw_metadata]
  75. }
  76. end
  77. def create_identifier(number_of)
  78. (1..number_of).each do |identifier_id|
  79. identifiers.create!(id: identifier_id,
  80. project_id: 123,
  81. fingerprint: 'd432c2ad2953e8bd587a3a43b3ce309b5b0154c' + identifier_id.to_s,
  82. external_type: 'SECURITY_ID',
  83. external_id: 'SECURITY_0',
  84. name: 'SECURITY_IDENTIFIER 0')
  85. end
  86. end
  87. end