|
12 | 12 | from django.db import IntegrityError |
13 | 13 | from django.db import connection |
14 | 14 | from django.db.migrations.executor import MigrationExecutor |
| 15 | +from django.db.models import Count |
15 | 16 | from django.test import TestCase |
16 | 17 | from django.utils import timezone |
17 | 18 | from packageurl import PackageURL |
@@ -1031,3 +1032,59 @@ def test_m2m_relationships_work(self): |
1031 | 1032 |
|
1032 | 1033 | self.assertIn(commit1, impacted.affecting_commits.all()) |
1033 | 1034 | self.assertIn(commit2, impacted.fixed_by_commits.all()) |
| 1035 | + |
| 1036 | + |
| 1037 | +class TestLatestAdvisoryV2Migration(TestMigrations): |
| 1038 | + """Tests is_latest field population on existing v2 advisory.""" |
| 1039 | + |
| 1040 | + app_name = "vulnerabilities" |
| 1041 | + migrate_from = "0120_impactedpackage_last_range_unfurl_at_and_more" |
| 1042 | + migrate_to = "0121_advisoryv2_is_latest_alter_advisoryv2_advisory_id_and_more" |
| 1043 | + |
| 1044 | + def setUpBeforeMigration(self, apps): |
| 1045 | + AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2") |
| 1046 | + |
| 1047 | + AdvisoryV2.objects.create( |
| 1048 | + unique_content_id="content_id_old", |
| 1049 | + url="https://old.example.com", |
| 1050 | + summary="Old advisory", |
| 1051 | + advisory_id="test_adv", |
| 1052 | + avid="test_pipeline/test_adv", |
| 1053 | + datasource_id="test_pipeline", |
| 1054 | + ) |
| 1055 | + |
| 1056 | + AdvisoryV2.objects.create( |
| 1057 | + unique_content_id="content_id_old2", |
| 1058 | + url="https://old.example.com", |
| 1059 | + summary="Old 2 advisory", |
| 1060 | + advisory_id="test_adv", |
| 1061 | + avid="test_pipeline/test_adv", |
| 1062 | + datasource_id="test_pipeline", |
| 1063 | + ) |
| 1064 | + |
| 1065 | + AdvisoryV2.objects.create( |
| 1066 | + unique_content_id="content_id_new", |
| 1067 | + url="https://old.example.com", |
| 1068 | + summary="New advisory", |
| 1069 | + advisory_id="test_adv", |
| 1070 | + avid="test_pipeline/test_adv", |
| 1071 | + datasource_id="test_pipeline", |
| 1072 | + ) |
| 1073 | + |
| 1074 | + def test_no_duplicate_is_latest_for_avid(self): |
| 1075 | + AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2") |
| 1076 | + |
| 1077 | + duplicate = ( |
| 1078 | + AdvisoryV2.objects.filter(is_latest=True) |
| 1079 | + .values("avid") |
| 1080 | + .annotate(cnt=Count("id")) |
| 1081 | + .filter(cnt__gt=1) |
| 1082 | + ) |
| 1083 | + |
| 1084 | + self.assertFalse(duplicate.exists()) |
| 1085 | + |
| 1086 | + def test_latest_is_actually_recent(self): |
| 1087 | + AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2") |
| 1088 | + |
| 1089 | + latest = AdvisoryV2.objects.get(avid="test_pipeline/test_adv", is_latest=True) |
| 1090 | + self.assertEqual("New advisory", latest.summary) |
0 commit comments