|
1 | 1 | import httpretty |
| 2 | +import pytest |
2 | 3 |
|
3 | 4 | from ..api.init import initialise_api |
4 | 5 | from ..rest import RestClient |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class TestRestClient: |
8 | 9 | @httpretty.activate(allow_net_connect=False, verbose=True) |
| 10 | + @pytest.mark.usefixtures("mock_keyring") |
9 | 11 | def test_implicit_retry_for_status_codes(self): |
10 | 12 | """Assert that the rest client retries certain status codes automatically.""" |
11 | 13 | # initialise_api() needs to be called before RestClient can be instantiated, |
@@ -37,3 +39,32 @@ def test_implicit_retry_for_status_codes(self): |
37 | 39 |
|
38 | 40 | assert len(httpretty.latest_requests()) == 6 |
39 | 41 | assert r.status == 200 |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def mock_keyring(monkeypatch): |
| 46 | + """Mock keyring functions to prevent reading real SSO tokens from the system keyring. |
| 47 | +
|
| 48 | + This is necessary because initialise_api() checks the keyring for SSO tokens, |
| 49 | + and if found, it attempts to refresh them via a network request. When running |
| 50 | + this test in isolation with httpretty mocking enabled, that network request |
| 51 | + will fail because it's not mocked. |
| 52 | + """ |
| 53 | + # Import here to avoid circular imports |
| 54 | + import httpretty.core |
| 55 | + |
| 56 | + from .. import keyring |
| 57 | + |
| 58 | + # Mock all keyring getter functions to return None/False |
| 59 | + monkeypatch.setattr(keyring, "get_access_token", lambda api_host: None) |
| 60 | + monkeypatch.setattr(keyring, "get_refresh_token", lambda api_host: None) |
| 61 | + monkeypatch.setattr(keyring, "should_refresh_access_token", lambda api_host: False) |
| 62 | + |
| 63 | + # Patch httpretty's fake socket to handle shutdown() which urllib3 2.0+ calls |
| 64 | + # This fixes: "Failed to socket.shutdown because a real socket does not exist" |
| 65 | + monkeypatch.setattr( |
| 66 | + httpretty.core.fakesock.socket, |
| 67 | + "shutdown", |
| 68 | + lambda self, how: None, |
| 69 | + raising=False, |
| 70 | + ) |
0 commit comments