|
3 | 3 | from django.test import Client, TestCase |
4 | 4 | from django.urls import reverse |
5 | 5 |
|
| 6 | +from shared.listeners.automatic_linkage import build_new_links |
| 7 | +from shared.listeners.notify_users import create_package_subscription_notifications |
| 8 | +from shared.models.cve import ( |
| 9 | + AffectedProduct, |
| 10 | + CveRecord, |
| 11 | + Description, |
| 12 | + Metric, |
| 13 | + Organization, |
| 14 | + Version, |
| 15 | +) |
| 16 | +from shared.models.linkage import CVEDerivationClusterProposal |
6 | 17 | from shared.models.nix_evaluation import ( |
7 | 18 | NixChannel, |
8 | 19 | NixDerivation, |
@@ -338,3 +349,54 @@ def test_user_unsubscribes_from_empty_package_name_fails_htmx(self) -> None: |
338 | 349 | # Verify empty subscriptions in context |
339 | 350 | self.assertIn("package_subscriptions", response.context) |
340 | 351 | self.assertEqual(response.context["package_subscriptions"], []) |
| 352 | + |
| 353 | + def test_user_receives_notification_for_subscribed_package_suggestion(self) -> None: |
| 354 | + """Test that users receive notifications when suggestions affect their subscribed packages""" |
| 355 | + # User subscribes to firefox package |
| 356 | + add_url = reverse("webview:subscriptions:add") |
| 357 | + self.client.post(add_url, {"package_name": "firefox"}, HTTP_HX_REQUEST="true") |
| 358 | + |
| 359 | + # Create CVE and container - this should trigger automatic linkage and then notifications |
| 360 | + assigner = Organization.objects.create(uuid=1, short_name="test_org") |
| 361 | + cve_record = CveRecord.objects.create( |
| 362 | + cve_id="CVE-2025-0001", |
| 363 | + assigner=assigner, |
| 364 | + ) |
| 365 | + |
| 366 | + description = Description.objects.create(value="Test firefox vulnerability") |
| 367 | + metric = Metric.objects.create(format="cvssV3_1", raw_cvss_json={}) |
| 368 | + affected_product = AffectedProduct.objects.create(package_name="firefox") |
| 369 | + affected_product.versions.add( |
| 370 | + Version.objects.create(status=Version.Status.AFFECTED, version="120.0") |
| 371 | + ) |
| 372 | + |
| 373 | + container = cve_record.container.create( |
| 374 | + provider=assigner, |
| 375 | + title="Firefox Security Issue", |
| 376 | + ) |
| 377 | + |
| 378 | + container.affected.set([affected_product]) |
| 379 | + container.descriptions.set([description]) |
| 380 | + container.metrics.set([metric]) |
| 381 | + |
| 382 | + # Trigger the linkage and notification system manually since pgpubsub triggers won't work in tests |
| 383 | + linkage_created = build_new_links(container) |
| 384 | + |
| 385 | + if linkage_created: |
| 386 | + # Get the created proposal and trigger notifications |
| 387 | + suggestion = CVEDerivationClusterProposal.objects.get(cve=cve_record) |
| 388 | + create_package_subscription_notifications(suggestion) |
| 389 | + |
| 390 | + # Verify notification appears in notification center context |
| 391 | + response = self.client.get(reverse("webview:notifications:center")) |
| 392 | + self.assertEqual(response.status_code, 200) |
| 393 | + |
| 394 | + # Check that notification appears in context |
| 395 | + notifications = response.context["notifications"] |
| 396 | + self.assertEqual(len(notifications), 1) |
| 397 | + |
| 398 | + notification = notifications[0] |
| 399 | + self.assertEqual(notification.user, self.user) |
| 400 | + self.assertIn("firefox", notification.title) |
| 401 | + self.assertIn("CVE-2025-0001", notification.message) |
| 402 | + self.assertFalse(notification.is_read) # Should be unread initially |
0 commit comments