|
|
@@ -1,4 +1,5 @@
|
|
|
import json
|
|
|
+import sys
|
|
|
from contextlib import contextmanager
|
|
|
from types import SimpleNamespace
|
|
|
from unittest.mock import Mock, patch
|
|
|
@@ -10,7 +11,7 @@ from django.urls import reverse
|
|
|
from rest_framework import status
|
|
|
|
|
|
from netbox.middleware import CoreMiddleware, MaintenanceModeMiddleware
|
|
|
-from utilities.testing import TestCase
|
|
|
+from utilities.testing import TestCase, disable_warnings
|
|
|
|
|
|
|
|
|
class CoreMiddlewareTestCase(TestCase):
|
|
|
@@ -156,3 +157,49 @@ class CoreMiddlewareTestCase(TestCase):
|
|
|
response = self.process_internal_error(request, 'Simulated maintenance mode GraphQL error')
|
|
|
|
|
|
self.assert_json_500_response(response)
|
|
|
+
|
|
|
+
|
|
|
+@override_settings(SENTRY_ENABLED=True)
|
|
|
+class SentryNotFoundReportingTestCase(TestCase):
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ super().setUp()
|
|
|
+ self.sentry_sdk = Mock()
|
|
|
+ patcher = patch.dict(sys.modules, {'sentry_sdk': self.sentry_sdk})
|
|
|
+ patcher.start()
|
|
|
+ self.addCleanup(patcher.stop)
|
|
|
+
|
|
|
+ def test_404_reported_to_sentry(self):
|
|
|
+ with disable_warnings('django.request'):
|
|
|
+ response = self.client.get('/nonexistent-path/')
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 404)
|
|
|
+ self.sentry_sdk.capture_message.assert_called_once_with('Page not found', level='error')
|
|
|
+
|
|
|
+ def test_append_slash_redirect_not_reported_to_sentry(self):
|
|
|
+ url = reverse('api-status')
|
|
|
+
|
|
|
+ for method in ('get', 'head'):
|
|
|
+ with self.subTest(method=method):
|
|
|
+ response = getattr(self.client, method)(url.rstrip('/'))
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 301)
|
|
|
+ self.assertEqual(response.headers['Location'], url)
|
|
|
+ self.sentry_sdk.capture_message.assert_not_called()
|
|
|
+
|
|
|
+ def test_unsafe_api_request_without_slash_reported_to_sentry(self):
|
|
|
+ with disable_warnings('django.request'):
|
|
|
+ response = self.client.post(
|
|
|
+ reverse('api-status').rstrip('/'), data={}, content_type='application/json'
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 404)
|
|
|
+ self.sentry_sdk.capture_message.assert_called_once_with('Page not found', level='error')
|
|
|
+
|
|
|
+ @override_settings(SENTRY_ENABLED=False)
|
|
|
+ def test_404_not_reported_when_sentry_disabled(self):
|
|
|
+ with disable_warnings('django.request'):
|
|
|
+ response = self.client.get('/nonexistent-path/')
|
|
|
+
|
|
|
+ self.assertEqual(response.status_code, 404)
|
|
|
+ self.sentry_sdk.capture_message.assert_not_called()
|