|
6 | 6 | from sentry.seer.signed_seer_api import make_signed_seer_api_request |
7 | 7 | from sentry.testutils.helpers import override_options |
8 | 8 |
|
| 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 | + |
9 | 33 |
|
10 | 34 | @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( |
30 | 38 | "POST", |
31 | | - "/v0/some/url", |
32 | | - body=body, |
| 39 | + PATH, |
| 40 | + body=REQUEST_BODY, |
33 | 41 | headers={"content-type": "application/json;charset=utf-8"}, |
34 | 42 | ) |
35 | 43 |
|
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( |
37 | 49 | "POST", |
38 | | - "/v0/some/url", |
39 | | - body=body, |
| 50 | + PATH, |
| 51 | + body=REQUEST_BODY, |
40 | 52 | headers={"content-type": "application/json;charset=utf-8"}, |
41 | 53 | timeout=5, |
42 | 54 | ) |
43 | 55 |
|
| 56 | + |
| 57 | +@pytest.mark.django_db |
| 58 | +def test_uses_shared_secret(): |
44 | 59 | 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( |
46 | 62 | "POST", |
47 | | - "/v0/some/url", |
48 | | - body=body, |
| 63 | + PATH, |
| 64 | + body=REQUEST_BODY, |
49 | 65 | headers={ |
50 | 66 | "content-type": "application/json;charset=utf-8", |
51 | 67 | "Authorization": "Rpcsignature rpc0:96f23d5b3df807a9dc91f090078a46c00e17fe8b0bc7ef08c9391fa8b37a66b5", |
|
0 commit comments