|
15 | 15 | from unittest import mock |
16 | 16 | from lean.constants import API_BASE_URL |
17 | 17 | from lean.components.api.api_client import APIClient |
| 18 | +from lean.components.api.auth0_client import Auth0Client |
18 | 19 | from lean.components.util.http_client import HTTPClient |
19 | 20 |
|
20 | 21 |
|
@@ -49,6 +50,127 @@ def test_auth0client_trade_station() -> None: |
49 | 50 | assert len(result.get_account_ids()) > 0 |
50 | 51 |
|
51 | 52 |
|
| 53 | +def test_auth0client_authorize_with_user_name() -> None: |
| 54 | + with mock.patch("webbrowser.open") as mock_open: |
| 55 | + Auth0Client.authorize("charles-schwab", mock.Mock(), 123, user_name="test_login") |
| 56 | + mock_open.assert_called_once() |
| 57 | + called_url = mock_open.call_args[0][0] |
| 58 | + assert "&userId=test_login" in called_url |
| 59 | + |
| 60 | + |
| 61 | +def test_auth0client_authorize_without_user_name() -> None: |
| 62 | + with mock.patch("webbrowser.open") as mock_open: |
| 63 | + Auth0Client.authorize("charles-schwab", mock.Mock(), 123) |
| 64 | + mock_open.assert_called_once() |
| 65 | + called_url = mock_open.call_args[0][0] |
| 66 | + assert "userId" not in called_url |
| 67 | + |
| 68 | + |
| 69 | +@responses.activate |
| 70 | +def test_auth0client_read_with_user_name() -> None: |
| 71 | + api_clint = APIClient(mock.Mock(), HTTPClient(mock.Mock()), user_id="123", api_token="abc") |
| 72 | + |
| 73 | + responses.add( |
| 74 | + responses.POST, |
| 75 | + f"{API_BASE_URL}live/auth0/read", |
| 76 | + json={ |
| 77 | + "authorization": { |
| 78 | + "charles-schwab-access-token": "abc123", |
| 79 | + "accounts": [{"id": "ACC001", "name": "ACC001 | Individual | USD"}] |
| 80 | + }, |
| 81 | + "success": "true"}, |
| 82 | + status=200 |
| 83 | + ) |
| 84 | + |
| 85 | + result = api_clint.auth0.read("charles-schwab", user_name="test_login") |
| 86 | + |
| 87 | + assert result |
| 88 | + assert result.authorization |
| 89 | + sent_body = responses.calls[0].request.body.decode() |
| 90 | + assert "userId" in sent_body |
| 91 | + assert "test_login" in sent_body |
| 92 | + |
| 93 | + |
| 94 | +@responses.activate |
| 95 | +def test_auth0client_read_without_user_name() -> None: |
| 96 | + api_clint = APIClient(mock.Mock(), HTTPClient(mock.Mock()), user_id="123", api_token="abc") |
| 97 | + |
| 98 | + responses.add( |
| 99 | + responses.POST, |
| 100 | + f"{API_BASE_URL}live/auth0/read", |
| 101 | + json={ |
| 102 | + "authorization": { |
| 103 | + "charles-schwab-access-token": "abc123", |
| 104 | + "accounts": [{"id": "ACC001", "name": "ACC001 | Individual | USD"}] |
| 105 | + }, |
| 106 | + "success": "true"}, |
| 107 | + status=200 |
| 108 | + ) |
| 109 | + |
| 110 | + result = api_clint.auth0.read("charles-schwab") |
| 111 | + |
| 112 | + assert result |
| 113 | + assert result.authorization |
| 114 | + sent_body = responses.calls[0].request.body.decode() |
| 115 | + assert "userId" not in sent_body |
| 116 | + |
| 117 | + |
| 118 | +@responses.activate |
| 119 | +def test_auth0client_read_caches_without_user_name() -> None: |
| 120 | + api_clint = APIClient(mock.Mock(), HTTPClient(mock.Mock()), user_id="123", api_token="abc") |
| 121 | + |
| 122 | + responses.add( |
| 123 | + responses.POST, |
| 124 | + f"{API_BASE_URL}live/auth0/read", |
| 125 | + json={ |
| 126 | + "authorization": { |
| 127 | + "charles-schwab-access-token": "abc123", |
| 128 | + "accounts": [{"id": "ACC001", "name": "ACC001 | Individual | USD"}] |
| 129 | + }, |
| 130 | + "success": "true"}, |
| 131 | + status=200 |
| 132 | + ) |
| 133 | + |
| 134 | + api_clint.auth0.read("charles-schwab") |
| 135 | + api_clint.auth0.read("charles-schwab") |
| 136 | + |
| 137 | + assert len(responses.calls) == 1 |
| 138 | + |
| 139 | + |
| 140 | +@responses.activate |
| 141 | +def test_auth0client_read_caches_per_user_name() -> None: |
| 142 | + api_clint = APIClient(mock.Mock(), HTTPClient(mock.Mock()), user_id="123", api_token="abc") |
| 143 | + |
| 144 | + responses.add( |
| 145 | + responses.POST, |
| 146 | + f"{API_BASE_URL}live/auth0/read", |
| 147 | + json={ |
| 148 | + "authorization": { |
| 149 | + "charles-schwab-access-token": "abc123", |
| 150 | + "accounts": [{"id": "ACC001", "name": "ACC001 | Individual | USD"}] |
| 151 | + }, |
| 152 | + "success": "true"}, |
| 153 | + status=200 |
| 154 | + ) |
| 155 | + responses.add( |
| 156 | + responses.POST, |
| 157 | + f"{API_BASE_URL}live/auth0/read", |
| 158 | + json={ |
| 159 | + "authorization": { |
| 160 | + "charles-schwab-access-token": "xyz789", |
| 161 | + "accounts": [{"id": "ACC002", "name": "ACC002 | Individual | USD"}] |
| 162 | + }, |
| 163 | + "success": "true"}, |
| 164 | + status=200 |
| 165 | + ) |
| 166 | + |
| 167 | + api_clint.auth0.read("charles-schwab", user_name="user_a") |
| 168 | + api_clint.auth0.read("charles-schwab", user_name="user_a") # cache hit |
| 169 | + api_clint.auth0.read("charles-schwab", user_name="user_b") # different user — new call |
| 170 | + |
| 171 | + assert len(responses.calls) == 2 |
| 172 | + |
| 173 | + |
52 | 174 | @responses.activate |
53 | 175 | def test_auth0client_alpaca() -> None: |
54 | 176 | api_clint = APIClient(mock.Mock(), HTTPClient(mock.Mock()), user_id="123", api_token="abc") |
|
0 commit comments