Skip to content

Commit 5b189f9

Browse files
committed
Update tests
Signed-off-by: Sampurna Pyne <sampurnapyne1710@gmail.com>
1 parent fe56518 commit 5b189f9

6 files changed

Lines changed: 74 additions & 1042 deletions

File tree

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,91 @@
1-
import json
2-
import os
1+
from datetime import timedelta
32

4-
from vulnerabilities.templatetags.diff_advisory_history import format_diff_for_ui
5-
from vulnerabilities.tests.util_tests import check_results_against_json
3+
import pytest
4+
from django.utils import timezone
65

6+
from vulnerabilities.models import AdvisoryHistoryDiff
7+
from vulnerabilities.models import AdvisorySeverity
8+
from vulnerabilities.models import AdvisoryV2
9+
from vulnerabilities.models import ImpactedPackage
10+
from vulnerabilities.pipelines.v2_improvers.history_diff_backfill import HistoryDiffImproverPipeline
711

12+
13+
@pytest.mark.django_db
814
def test_advisory_history_diffing():
915
"""
1016
Test the diffing logic for historical advisory snapshots.
1117
"""
12-
from vulnerabilities.utils import diff_advisories_v2
13-
14-
base_dir = os.path.join(os.path.dirname(__file__), "test_data", "advisory_history")
15-
16-
# Test GHSA-72hv-8253-57qq (Sample 1)
17-
# See: https://github.com/github/advisory-database/commits/main/advisories/github-reviewed/2026/02/GHSA-72hv-8253-57qq/GHSA-72hv-8253-57qq.json
18-
sample1_dir = os.path.join(base_dir, "GHSA-72hv-8253-57qq")
19-
with open(os.path.join(sample1_dir, "normalised_history_GHSA-72hv-8253-57qq.json"), "r") as f:
20-
sample1_data = json.load(f)
18+
avid1 = "github_osv/GHSA-72hv-8253-57qq"
2119

22-
sample1_diffs = [
23-
diff_advisories_v2(sample1_data[i], sample1_data[i + 1])
24-
for i in range(len(sample1_data) - 1)
25-
]
20+
severity_high = AdvisorySeverity.objects.create(scoring_system="generic_textual", value="HIGH")
21+
severity_moderate = AdvisorySeverity.objects.create(
22+
scoring_system="generic_textual", value="MODERATE"
23+
)
2624

27-
sample1_expected_file = os.path.join(sample1_dir, "expected_diff_GHSA-72hv-8253-57qq.json")
28-
check_results_against_json(sample1_diffs, sample1_expected_file)
25+
old_advisory_snapshot = AdvisoryV2.objects.create(
26+
advisory_id="GHSA-72hv-8253-57qq",
27+
unique_content_id="snap1hash",
28+
date_collected=timezone.now(),
29+
pipeline_id="test",
30+
datasource_id="github_osv",
31+
url="http://test.com",
32+
avid=avid1,
33+
summary="Initial summary",
34+
)
35+
old_advisory_snapshot.severities.add(severity_high)
2936

30-
# Test GHSA-6rw7-vpxm-498p (Sample 2)
31-
# See: https://github.com/github/advisory-database/commits/main/advisories/github-reviewed/2025/12/GHSA-6rw7-vpxm-498p/GHSA-6rw7-vpxm-498p.json
32-
sample2_dir = os.path.join(base_dir, "GHSA-6rw7-vpxm-498p")
33-
with open(os.path.join(sample2_dir, "normalised_history_GHSA-6rw7-vpxm-498p.json"), "r") as f:
34-
sample2_data = json.load(f)
37+
new_advisory_snapshot = AdvisoryV2.objects.create(
38+
advisory_id="GHSA-72hv-8253-57qq",
39+
unique_content_id="snap2hash",
40+
date_collected=timezone.now() + timedelta(days=1),
41+
pipeline_id="test",
42+
datasource_id="github_osv",
43+
url="http://test.com",
44+
avid=avid1,
45+
summary="Updated summary",
46+
)
47+
new_advisory_snapshot.severities.add(severity_moderate)
3548

36-
sample2_diffs = [
37-
diff_advisories_v2(sample2_data[i], sample2_data[i + 1])
38-
for i in range(len(sample2_data) - 1)
39-
]
49+
pkg1 = ImpactedPackage.objects.create(
50+
advisory=old_advisory_snapshot,
51+
base_purl="pkg:maven/tools.jackson.core/jackson-core",
52+
affecting_vers="vers:maven/<=2.18.5",
53+
fixed_vers="vers:maven/2.18.6",
54+
)
55+
pkg2 = ImpactedPackage.objects.create(
56+
advisory=old_advisory_snapshot,
57+
base_purl="pkg:maven/tools.jackson.core/jackson-core",
58+
affecting_vers="vers:maven/>=2.19.0|<2.21.1",
59+
fixed_vers="vers:maven/2.21.1",
60+
)
61+
pkg3 = ImpactedPackage.objects.create(
62+
advisory=old_advisory_snapshot,
63+
base_purl="pkg:maven/com.fasterxml.jackson.core/jackson-core",
64+
affecting_vers="vers:maven/>=3.0.0|<3.1.0",
65+
fixed_vers="vers:maven/3.1.0",
66+
)
4067

41-
sample2_expected_file = os.path.join(sample2_dir, "expected_diff_GHSA-6rw7-vpxm-498p.json")
42-
check_results_against_json(sample2_diffs, sample2_expected_file)
68+
pipeline = HistoryDiffImproverPipeline()
69+
pipeline.execute()
4370

71+
# Run a second time to ensure idempotency
72+
pipeline.execute()
4473

45-
def test_format_diff_for_ui():
46-
"""
47-
Test the template tag logic that formats diffs for the UI.
48-
"""
49-
base_dir = os.path.join(os.path.dirname(__file__), "test_data", "advisory_history")
50-
sample2_dir = os.path.join(base_dir, "GHSA-6rw7-vpxm-498p")
51-
input_file = os.path.join(sample2_dir, "expected_diff_GHSA-6rw7-vpxm-498p.json")
74+
old_advisory_snapshot.refresh_from_db()
75+
new_advisory_snapshot.refresh_from_db()
5276

53-
with open(input_file, "r") as f:
54-
input_diffs = json.load(f)
77+
assert hasattr(old_advisory_snapshot, "history_diff")
78+
assert old_advisory_snapshot.history_diff.advisory_before is None
5579

56-
formatted_diffs = [format_diff_for_ui(diff) for diff in input_diffs]
80+
diff = AdvisoryHistoryDiff.objects.get(advisory_after=new_advisory_snapshot)
81+
assert diff.summary_removed == "Initial summary"
82+
assert diff.summary_added == "Updated summary"
83+
assert severity_moderate in diff.added_severities.all()
84+
assert severity_high in diff.removed_severities.all()
5785

58-
expected_file = os.path.join(sample2_dir, "expected_formatted_diff_GHSA-6rw7-vpxm-498p.json")
59-
check_results_against_json(formatted_diffs, expected_file)
86+
assert diff.added_impacted_packages.count() == 0
87+
assert diff.removed_impacted_packages.count() == 3
88+
assert pkg1 in diff.removed_impacted_packages.all()
89+
assert pkg2 in diff.removed_impacted_packages.all()
90+
assert pkg3 in diff.removed_impacted_packages.all()
91+
assert AdvisoryHistoryDiff.objects.count() == 2

vulnerabilities/tests/test_data/advisory_history/GHSA-6rw7-vpxm-498p/expected_diff_GHSA-6rw7-vpxm-498p.json

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)