|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from vulnerabilities.templatetags.diff_advisory_history import format_diff_for_ui |
| 5 | +from vulnerabilities.tests.util_tests import check_results_against_json |
| 6 | + |
| 7 | + |
| 8 | +def test_advisory_history_diffing(): |
| 9 | + """ |
| 10 | + Test the diffing logic for historical advisory snapshots. |
| 11 | + """ |
| 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) |
| 21 | + |
| 22 | + sample1_diffs = [ |
| 23 | + diff_advisories_v2(sample1_data[i], sample1_data[i + 1]) |
| 24 | + for i in range(len(sample1_data) - 1) |
| 25 | + ] |
| 26 | + |
| 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) |
| 29 | + |
| 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) |
| 35 | + |
| 36 | + sample2_diffs = [ |
| 37 | + diff_advisories_v2(sample2_data[i], sample2_data[i + 1]) |
| 38 | + for i in range(len(sample2_data) - 1) |
| 39 | + ] |
| 40 | + |
| 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) |
| 43 | + |
| 44 | + |
| 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") |
| 52 | + |
| 53 | + with open(input_file, "r") as f: |
| 54 | + input_diffs = json.load(f) |
| 55 | + |
| 56 | + formatted_diffs = [format_diff_for_ui(diff) for diff in input_diffs] |
| 57 | + |
| 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) |
0 commit comments