|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django.test import override_settings |
| 3 | +from django.utils import timezone |
| 4 | +from rest_framework.authtoken.models import Token |
| 5 | +from rest_framework.test import APIClient |
| 6 | +from watson import search as watson |
| 7 | + |
| 8 | +from dojo.models import Development_Environment, Engagement, Finding, Product, Product_Type, UserContactInfo |
| 9 | + |
| 10 | +from .dojo_test_case import DojoAPITestCase |
| 11 | + |
| 12 | + |
| 13 | +class TestWatsonAsyncSearchIndex(DojoAPITestCase): |
| 14 | + |
| 15 | + """Test Watson search indexing works correctly for both sync and async updates.""" |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + """Set up test data and API client.""" |
| 19 | + super().setUp() |
| 20 | + |
| 21 | + self.testuser = User.objects.create(username="admin", is_staff=True, is_superuser=True) |
| 22 | + UserContactInfo.objects.create(user=self.testuser, block_execution=True) |
| 23 | + |
| 24 | + self.system_settings(enable_webhooks_notifications=False) |
| 25 | + self.system_settings(enable_product_grade=False) |
| 26 | + self.system_settings(enable_github=False) |
| 27 | + |
| 28 | + # Create API client with authentication |
| 29 | + self.token = Token.objects.create(user=self.testuser) |
| 30 | + self.client = APIClient() |
| 31 | + self.client.force_login(self.testuser) |
| 32 | + |
| 33 | + # Create test product type and product |
| 34 | + self.product_type = Product_Type.objects.create(name="Test Product Type") |
| 35 | + self.product = Product.objects.create( |
| 36 | + name="Test Product", |
| 37 | + description="Test product for Watson indexing", |
| 38 | + prod_type=self.product_type, |
| 39 | + ) |
| 40 | + self.engagement = Engagement.objects.create( |
| 41 | + name="Test Engagement", |
| 42 | + product=self.product, |
| 43 | + target_start=timezone.now(), |
| 44 | + target_end=timezone.now(), |
| 45 | + ) |
| 46 | + |
| 47 | + # Create Development Environment |
| 48 | + Development_Environment.objects.get_or_create(name="Development") |
| 49 | + |
| 50 | + def _import_acunetix_scan(self): |
| 51 | + """Import an Acunetix scan and return the response.""" |
| 52 | + return self.import_scan_with_params( |
| 53 | + filename="scans/acunetix/watson_test_unique.xml", |
| 54 | + scan_type="Acunetix Scan", |
| 55 | + engagement=self.engagement.id, |
| 56 | + ) |
| 57 | + |
| 58 | + def _search_watson_for_finding(self, search_term): |
| 59 | + """Search Watson index for findings containing the search term.""" |
| 60 | + # Search the Watson index |
| 61 | + return watson.search(search_term, models=(Finding,)) |
| 62 | + |
| 63 | + def _import_and_check_watson_index(self, expected_message): |
| 64 | + """Common test logic for importing scan and verifying Watson indexing works.""" |
| 65 | + # Verify no findings exist before import |
| 66 | + search_results = self._search_watson_for_finding("WatsonUniqueReportItem2025") |
| 67 | + found_finding_ids_before = [result.pk for result in search_results] |
| 68 | + self.assertEqual(len(found_finding_ids_before), 0, "Should have no findings before import") |
| 69 | + |
| 70 | + # Import the scan |
| 71 | + response_data = self._import_acunetix_scan() |
| 72 | + |
| 73 | + # Get test ID from response |
| 74 | + test_id = response_data["test_id"] |
| 75 | + |
| 76 | + # Verify finding was created |
| 77 | + findings = Finding.objects.filter(test_id=test_id) |
| 78 | + self.assertEqual(findings.count(), 1, "Should have created exactly one finding") |
| 79 | + finding = findings.first() |
| 80 | + |
| 81 | + self.assertIn("WatsonUniqueReportItem2025", finding.title, "Finding should contain 'WatsonUniqueReportItem2025' in title") |
| 82 | + |
| 83 | + # Search Watson index for the finding |
| 84 | + search_results = self._search_watson_for_finding("WatsonUniqueReportItem2025") |
| 85 | + |
| 86 | + # Verify finding is in search index |
| 87 | + found_finding_ids = [result.object.pk for result in search_results] |
| 88 | + |
| 89 | + self.assertIn(finding.pk, found_finding_ids, expected_message.format(finding_id=finding.pk)) |
| 90 | + |
| 91 | + return finding |
| 92 | + |
| 93 | + def test_sync_watson_indexing_single_finding(self): |
| 94 | + """Test that single finding import uses sync indexing and finding is searchable.""" |
| 95 | + # Default threshold is 100, so single finding should use sync indexing |
| 96 | + self._import_and_check_watson_index( |
| 97 | + "Finding {finding_id} should be found in Watson search index", |
| 98 | + ) |
| 99 | + |
| 100 | + @override_settings(WATSON_ASYNC_INDEX_UPDATE_THRESHOLD=0) |
| 101 | + def test_async_watson_indexing_single_finding(self): |
| 102 | + """Test that with threshold=0, single finding uses async indexing and is searchable.""" |
| 103 | + # With threshold=0, even single finding should trigger async indexing |
| 104 | + self._import_and_check_watson_index( |
| 105 | + "Finding {finding_id} should be found in Watson search index after async update", |
| 106 | + ) |
| 107 | + |
| 108 | + @override_settings(WATSON_ASYNC_INDEX_UPDATE_THRESHOLD=-1) |
| 109 | + def test_disabled_async_watson_indexing(self): |
| 110 | + """Test that with threshold=-1, async is disabled and sync indexing works.""" |
| 111 | + # With threshold=-1, async should be completely disabled |
| 112 | + self._import_and_check_watson_index( |
| 113 | + "Finding {finding_id} should be found in Watson search index with sync update", |
| 114 | + ) |
0 commit comments