|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +from django.apps import apps |
| 24 | +from django.db import connection |
| 25 | +from django.db.migrations.executor import MigrationExecutor |
| 26 | +from django.test import TestCase |
| 27 | + |
| 28 | +from vulnerabilities import severity_systems |
| 29 | +from vulnerabilities.models import VulnerabilityReference |
| 30 | +from vulnerabilities.models import VulnerabilitySeverity |
| 31 | + |
| 32 | + |
| 33 | +class TestMigrations(TestCase): |
| 34 | + @property |
| 35 | + def app(self): |
| 36 | + return apps.get_containing_app_config(type(self).__module__).name |
| 37 | + |
| 38 | + migrate_from = None |
| 39 | + migrate_to = None |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + assert ( |
| 43 | + self.migrate_from and self.migrate_to |
| 44 | + ), "TestCase '{}' must define migrate_from and migrate_to properties".format( |
| 45 | + type(self).__name__ |
| 46 | + ) |
| 47 | + self.migrate_from = [(self.app, self.migrate_from)] |
| 48 | + self.migrate_to = [(self.app, self.migrate_to)] |
| 49 | + executor = MigrationExecutor(connection) |
| 50 | + old_apps = executor.loader.project_state(self.migrate_from).apps |
| 51 | + |
| 52 | + # # Reverse to the original migration |
| 53 | + executor.migrate(self.migrate_from) |
| 54 | + |
| 55 | + self.setUpBeforeMigration(old_apps) |
| 56 | + |
| 57 | + # Run the migration to test |
| 58 | + executor = MigrationExecutor(connection) |
| 59 | + executor.loader.build_graph() # reload. |
| 60 | + executor.migrate(self.migrate_to) |
| 61 | + |
| 62 | + self.apps = executor.loader.project_state(self.migrate_to).apps |
| 63 | + |
| 64 | + def setUpBeforeMigration(self, apps): |
| 65 | + pass |
| 66 | + |
| 67 | + |
| 68 | +class DuplicateSeverityTestCase(TestMigrations): |
| 69 | + |
| 70 | + migrate_from = "0013_auto_20220503_0941" |
| 71 | + migrate_to = "0014_remove_duplicate_severities" |
| 72 | + |
| 73 | + def setUpBeforeMigration(self, apps): |
| 74 | + # using get_model to avoid circular import |
| 75 | + VulnerabilityReference = apps.get_model("vulnerabilities", "VulnerabilityReference") |
| 76 | + Severities = apps.get_model("vulnerabilities", "VulnerabilitySeverity") |
| 77 | + Vulnerability = apps.get_model("vulnerabilities", "Vulnerability") |
| 78 | + |
| 79 | + reference = VulnerabilityReference.objects.create( |
| 80 | + reference_id="CVE-TEST", url="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-TEST" |
| 81 | + ) |
| 82 | + self.reference = reference |
| 83 | + vulnerability1 = Vulnerability(vulnerability_id=1, summary="test-1") |
| 84 | + vulnerability1.save() |
| 85 | + vulnerability2 = Vulnerability(vulnerability_id=2, summary="test-2") |
| 86 | + vulnerability2.save() |
| 87 | + vulnerability3 = Vulnerability(vulnerability_id=3, summary="test-3") |
| 88 | + vulnerability3.save() |
| 89 | + Severities.objects.update_or_create( |
| 90 | + vulnerability=vulnerability1, |
| 91 | + scoring_system=severity_systems.REDHAT_AGGREGATE.identifier, |
| 92 | + reference=reference, |
| 93 | + defaults={"value": str("TEST")}, |
| 94 | + ) |
| 95 | + Severities.objects.update_or_create( |
| 96 | + vulnerability=vulnerability2, |
| 97 | + scoring_system=severity_systems.REDHAT_AGGREGATE.identifier, |
| 98 | + reference=reference, |
| 99 | + defaults={"value": str("TEST")}, |
| 100 | + ) |
| 101 | + Severities.objects.update_or_create( |
| 102 | + vulnerability=vulnerability3, |
| 103 | + scoring_system=severity_systems.REDHAT_AGGREGATE.identifier, |
| 104 | + reference=reference, |
| 105 | + defaults={"value": str("TEST")}, |
| 106 | + ) |
| 107 | + |
| 108 | + def test_remove_duplicate_rows(self): |
| 109 | + VulnerabilitySeverity = self.apps.get_model("vulnerabilities", "VulnerabilitySeverity") |
| 110 | + assert len(VulnerabilitySeverity.objects.filter(reference=self.reference.id)) == 1 |
| 111 | + |
| 112 | + |
| 113 | +class DropVulnerabilityFromSeverityTestCase(TestMigrations): |
| 114 | + |
| 115 | + migrate_from = "0014_remove_duplicate_severities" |
| 116 | + migrate_to = "0015_alter_vulnerabilityseverity_unique_together_and_more" |
| 117 | + |
| 118 | + def test_dropping_vulnerability_from_severity(self): |
| 119 | + # using get_model to avoid circular import |
| 120 | + VulnerabilityReference = self.apps.get_model("vulnerabilities", "VulnerabilityReference") |
| 121 | + VulnerabilitySeverity = self.apps.get_model("vulnerabilities", "VulnerabilitySeverity") |
| 122 | + |
| 123 | + reference = VulnerabilityReference.objects.create( |
| 124 | + reference_id="CVE-TEST", url="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-TEST" |
| 125 | + ) |
| 126 | + VulnerabilitySeverity.objects.update_or_create( |
| 127 | + scoring_system=severity_systems.REDHAT_AGGREGATE.identifier, |
| 128 | + reference=reference, |
| 129 | + defaults={"value": str("TEST")}, |
| 130 | + ) |
0 commit comments