Skip to content

Commit 46da06e

Browse files
committed
fix tests
1 parent fad84ba commit 46da06e

File tree

1 file changed

+21
-57
lines changed

1 file changed

+21
-57
lines changed
Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,32 @@
1-
from unittest.mock import Mock
2-
3-
from django.http import HttpResponse, HttpResponseBase
1+
from django.http import HttpResponse
42
from django.test import RequestFactory
53

64
from sentry.middleware.reporting_endpoint import ReportingEndpointMiddleware
75
from sentry.testutils.cases import TestCase
6+
from sentry.testutils.helpers.options import override_options
87

98

109
class ReportingEndpointMiddlewareTestCase(TestCase):
1110
def setUp(self) -> None:
1211
self.middleware = ReportingEndpointMiddleware(lambda request: HttpResponse())
1312
self.factory = RequestFactory()
1413

15-
def _no_header_set(self, result: HttpResponseBase) -> None:
16-
assert "Reporting-Endpoints" not in result
17-
18-
def test_adds_header_for_staff_user(self) -> None:
19-
"""Test that the ReportingEndpoint header is added when user is Sentry staff."""
20-
request = self.factory.get("/")
21-
22-
# Mock staff object with is_active = True
23-
staff_mock = Mock()
24-
staff_mock.is_active = True
25-
setattr(request, "staff", staff_mock)
26-
27-
response = HttpResponse()
28-
result = self.middleware.process_response(request, response)
29-
30-
assert "Reporting-Endpoints" in result
31-
assert (
32-
result["Reporting-Endpoints"]
33-
== "default=https://sentry.my.sentry.io/api/0/reporting-api-experiment/"
34-
)
35-
36-
def test_no_header_for_non_staff_user(self) -> None:
37-
"""Test that the ReportingEndpoint header is not added when user is not Sentry staff."""
38-
request = self.factory.get("/")
39-
40-
# Mock staff object with is_active = False
41-
staff_mock = Mock()
42-
staff_mock.is_active = False
43-
setattr(request, "staff", staff_mock)
44-
45-
response = HttpResponse()
46-
result = self.middleware.process_response(request, response)
47-
48-
self._no_header_set(result)
49-
50-
def test_no_header_when_no_staff_attribute(self) -> None:
51-
"""Test that the ReportingEndpoint header is not added when request has no staff attribute."""
52-
request = self.factory.get("/")
53-
54-
# No staff attribute on request
55-
response = HttpResponse()
56-
result = self.middleware.process_response(request, response)
57-
58-
self._no_header_set(result)
59-
60-
def test_no_header_when_staff_is_none(self) -> None:
61-
"""Test that the ReportingEndpoint header is not added when staff is None."""
62-
request = self.factory.get("/")
63-
setattr(request, "staff", None)
64-
65-
response = HttpResponse()
66-
result = self.middleware.process_response(request, response)
67-
68-
self._no_header_set(result)
14+
def test_obeys_option(self) -> None:
15+
with override_options(
16+
{"issues.browser_reporting.reporting_endpoints_header_enabled": True}
17+
):
18+
request = self.factory.get("/")
19+
response = self.middleware.process_response(request, HttpResponse())
20+
21+
assert (
22+
response.get("Reporting-Endpoints")
23+
== "default=https://sentry.my.sentry.io/api/0/reporting-api-experiment/"
24+
)
25+
26+
with override_options(
27+
{"issues.browser_reporting.reporting_endpoints_header_enabled": False}
28+
):
29+
request = self.factory.get("/")
30+
response = self.middleware.process_response(request, HttpResponse())
31+
32+
assert response.get("Reporting-Endpoints") is None

0 commit comments

Comments
 (0)