|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from unittest.mock import MagicMock, call, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from requests.exceptions import HTTPError |
| 6 | + |
| 7 | +from controller.sentry.webservices.sentry import BearerAuth, PaginatedSentryClient |
| 8 | + |
| 9 | + |
| 10 | +class Response: |
| 11 | + def __init__(self, status_code, data, links=None, headers=None) -> None: |
| 12 | + self.status_code = status_code |
| 13 | + self.data = data |
| 14 | + self.links = links if links else {} |
| 15 | + self.headers = headers if headers else {} |
| 16 | + |
| 17 | + def json(self): |
| 18 | + return self.data |
| 19 | + |
| 20 | + def raise_for_status(self): |
| 21 | + if self.status_code > 399: |
| 22 | + raise HTTPError |
| 23 | + |
| 24 | + |
| 25 | +def test_auth(): |
| 26 | + auth = BearerAuth("my_token") |
| 27 | + request_mock = MagicMock() |
| 28 | + auth(request_mock) |
| 29 | + |
| 30 | + request_mock.headers.__setitem__.assert_called_once_with("authorization", "Bearer my_token") |
| 31 | + |
| 32 | + |
| 33 | +@patch("controller.sentry.webservices.sentry.request") |
| 34 | +def test_client(mock_request: MagicMock): |
| 35 | + client = PaginatedSentryClient() |
| 36 | + |
| 37 | + res = client.list_projects() |
| 38 | + mock_request.assert_not_called() |
| 39 | + return_value = [ |
| 40 | + Response(200, [object(), object()], links={"next": {"results": True, "url": "http://next.sentry"}}), |
| 41 | + Response(200, [object(), object()]), |
| 42 | + ] |
| 43 | + |
| 44 | + mock_request.side_effect = return_value |
| 45 | + |
| 46 | + for chunk, expected in zip(res, return_value): |
| 47 | + assert chunk == expected.data |
| 48 | + |
| 49 | + call_1 = call("GET", "https://sentry.io/api/0/projects/", timeout=20, auth=client.auth) |
| 50 | + call_2 = call("GET", "http://next.sentry", timeout=20, auth=client.auth) |
| 51 | + mock_request.assert_has_calls((call_1, call_2)) |
| 52 | + |
| 53 | + |
| 54 | +@patch("controller.sentry.webservices.sentry.request") |
| 55 | +def test_client_rate_limited(mock_request: MagicMock): |
| 56 | + client = PaginatedSentryClient() |
| 57 | + |
| 58 | + res = client.list_projects() |
| 59 | + mock_request.assert_not_called() |
| 60 | + reset = datetime.now() + timedelta(seconds=5) |
| 61 | + return_value = [ |
| 62 | + Response(429, [object(), object()], headers={"x-sentry-rate-limit-reset": reset.timestamp()}), |
| 63 | + Response(500, [object(), object()]), |
| 64 | + ] |
| 65 | + |
| 66 | + mock_request.side_effect = return_value |
| 67 | + |
| 68 | + with pytest.raises(HTTPError): |
| 69 | + next(res) |
| 70 | + |
| 71 | + call_1 = call("GET", "https://sentry.io/api/0/projects/", timeout=20, auth=client.auth) |
| 72 | + call_2 = call("GET", "https://sentry.io/api/0/projects/", timeout=20, auth=client.auth) |
| 73 | + mock_request.assert_has_calls((call_1, call_2)) |
| 74 | + |
| 75 | + |
| 76 | +@patch("controller.sentry.webservices.sentry.request") |
| 77 | +def test_client_rate_limited_rest_in_past(mock_request: MagicMock): |
| 78 | + client = PaginatedSentryClient() |
| 79 | + |
| 80 | + res = client.list_projects() |
| 81 | + mock_request.assert_not_called() |
| 82 | + |
| 83 | + reset = datetime.now() - timedelta(seconds=5) |
| 84 | + return_value = [ |
| 85 | + Response(429, [object(), object()], headers={"x-sentry-rate-limit-reset": reset.timestamp()}), |
| 86 | + Response(500, [object(), object()]), |
| 87 | + ] |
| 88 | + |
| 89 | + mock_request.side_effect = return_value |
| 90 | + |
| 91 | + with pytest.raises(HTTPError): |
| 92 | + next(res) |
| 93 | + |
| 94 | + call_1 = call("GET", "https://sentry.io/api/0/projects/", timeout=20, auth=client.auth) |
| 95 | + call_2 = call("GET", "https://sentry.io/api/0/projects/", timeout=20, auth=client.auth) |
| 96 | + mock_request.assert_has_calls((call_1, call_2)) |
0 commit comments