Skip to content

Commit 96acd8d

Browse files
authored
ref(seer): Refactor Seer request helper tests (#74670)
This makes a number of changes to the existing tests for `make_signed_seer_api_request`, both for clarity and in anticipation of new tests being added. Changes: - The helper function `url_request` has been renamed `run_test_case`, given a docstring, moved up to the module level, and given the ability to use a custom path when making mock requests. - The mock returned from said function is now captured before it's asserted on, primarily to give it a name so it's clearer what it is. - The default path and body values have been pulled into constants. - The single generically-named test testing three cases has been split into three separate tests, named according to what they're testing.
1 parent ed427e4 commit 96acd8d

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

tests/sentry/seer/test_signed_seer_api.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,62 @@
66
from sentry.seer.signed_seer_api import make_signed_seer_api_request
77
from sentry.testutils.helpers import override_options
88

9+
REQUEST_BODY = b'{"b": 12, "thing": "thing"}'
10+
PATH = "/v0/some/url"
11+
12+
13+
def run_test_case(path: str = PATH, timeout: int | None = None):
14+
"""
15+
Make a mock connection pool, call `make_signed_seer_api_request` on it, and return the
16+
pool's `urlopen` method, so we can make assertions on how `make_signed_seer_api_request`
17+
used it.
18+
"""
19+
mock = Mock()
20+
mock.host = "localhost"
21+
mock.port = None
22+
mock.scheme = "http"
23+
with override_settings(SEER_API_SHARED_SECRET="secret-one"):
24+
make_signed_seer_api_request(
25+
mock,
26+
path=path,
27+
body=REQUEST_BODY,
28+
timeout=timeout,
29+
)
30+
31+
return mock.urlopen
32+
933

1034
@pytest.mark.django_db
11-
def test_make_signed_seer_api_request():
12-
body = b'{"b": 12, "thing": "thing"}'
13-
14-
def url_request(timeout: int | None = None):
15-
mock = Mock()
16-
mock.host = "localhost"
17-
mock.port = None
18-
mock.scheme = "http"
19-
with override_settings(SEER_API_SHARED_SECRET="secret-one"):
20-
make_signed_seer_api_request(
21-
mock,
22-
path="/v0/some/url",
23-
body=body,
24-
timeout=timeout,
25-
)
26-
27-
return mock.urlopen
28-
29-
url_request().assert_called_once_with(
35+
def test_simple():
36+
mock_url_open = run_test_case()
37+
mock_url_open.assert_called_once_with(
3038
"POST",
31-
"/v0/some/url",
32-
body=body,
39+
PATH,
40+
body=REQUEST_BODY,
3341
headers={"content-type": "application/json;charset=utf-8"},
3442
)
3543

36-
url_request(timeout=5).assert_called_once_with(
44+
45+
@pytest.mark.django_db
46+
def test_uses_given_timeout():
47+
mock_url_open = run_test_case(timeout=5)
48+
mock_url_open.assert_called_once_with(
3749
"POST",
38-
"/v0/some/url",
39-
body=body,
50+
PATH,
51+
body=REQUEST_BODY,
4052
headers={"content-type": "application/json;charset=utf-8"},
4153
timeout=5,
4254
)
4355

56+
57+
@pytest.mark.django_db
58+
def test_uses_shared_secret():
4459
with override_options({"seer.api.use-shared-secret": 1.0}):
45-
url_request().assert_called_once_with(
60+
mock_url_open = run_test_case()
61+
mock_url_open.assert_called_once_with(
4662
"POST",
47-
"/v0/some/url",
48-
body=body,
63+
PATH,
64+
body=REQUEST_BODY,
4965
headers={
5066
"content-type": "application/json;charset=utf-8",
5167
"Authorization": "Rpcsignature rpc0:96f23d5b3df807a9dc91f090078a46c00e17fe8b0bc7ef08c9391fa8b37a66b5",

0 commit comments

Comments
 (0)