|
| 1 | +import uuid |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from ws_api.session import WSAPISession |
| 7 | +from ws_api.wealthsimple_api import WealthsimpleAPI, WealthsimpleAPIBase |
| 8 | + |
| 9 | + |
| 10 | +def test_wealthsimple_api_set_user_agent(): |
| 11 | + WealthsimpleAPI.set_user_agent("Test User Agent") |
| 12 | + assert WealthsimpleAPI.user_agent == "Test User Agent" |
| 13 | + |
| 14 | + |
| 15 | +def test_wealthsimple_api_uuidv4(): |
| 16 | + uuid_str = WealthsimpleAPI.uuidv4() |
| 17 | + assert isinstance(uuid_str, str) |
| 18 | + assert len(uuid_str) == 36 |
| 19 | + uuid.UUID(uuid_str, version=4) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_session(): |
| 24 | + sess = WSAPISession() |
| 25 | + sess.client_id = "test_client_id" |
| 26 | + sess.access_token = "test_access_token" |
| 27 | + sess.refresh_token = "test_refresh_token" |
| 28 | + sess.session_id = "test_session_id" |
| 29 | + sess.wssdi = "test_wssdi" |
| 30 | + return sess |
| 31 | + |
| 32 | + |
| 33 | +def test_wealthsimple_api_init_with_session(mock_session): |
| 34 | + api = WealthsimpleAPI(mock_session) |
| 35 | + assert api.session.client_id == "test_client_id" |
| 36 | + assert api.session.access_token == "test_access_token" |
| 37 | + assert api.session.refresh_token == "test_refresh_token" |
| 38 | + assert api.session.session_id == "test_session_id" |
| 39 | + assert api.session.wssdi == "test_wssdi" |
| 40 | + |
| 41 | + |
| 42 | +@patch("requests.request") |
| 43 | +def test_send_http_request_post(mock_request, mock_session): |
| 44 | + mock_resp = MagicMock() |
| 45 | + mock_resp.json.return_value = {"status": "ok"} |
| 46 | + mock_request.return_value = mock_resp |
| 47 | + |
| 48 | + api = WealthsimpleAPIBase(mock_session) |
| 49 | + |
| 50 | + result = api.send_http_request( |
| 51 | + "https://test.example.com/api", |
| 52 | + "POST", |
| 53 | + {"grant_type": "password", "username": "test", "password": "test"}, |
| 54 | + ) |
| 55 | + |
| 56 | + assert result == {"status": "ok"} |
| 57 | + |
| 58 | + mock_request.assert_called_once() |
| 59 | + args, kwargs = mock_request.call_args |
| 60 | + assert args[0] == "POST" |
| 61 | + assert args[1] == "https://test.example.com/api" |
| 62 | + assert kwargs["json"] == { |
| 63 | + "grant_type": "password", |
| 64 | + "username": "test", |
| 65 | + "password": "test", |
| 66 | + } |
| 67 | + headers = kwargs["headers"] |
| 68 | + assert headers["Content-Type"] == "application/json" |
| 69 | + assert headers["x-ws-session-id"] == "test_session_id" |
| 70 | + assert headers["Authorization"] == "Bearer test_access_token" |
| 71 | + assert headers["x-ws-device-id"] == "test_wssdi" |
| 72 | + |
| 73 | + |
| 74 | +@patch("requests.request") |
| 75 | +def test_send_get_request(mock_request, mock_session): |
| 76 | + mock_resp = MagicMock() |
| 77 | + mock_resp.json.return_value = {"status": "ok"} |
| 78 | + mock_request.return_value = mock_resp |
| 79 | + |
| 80 | + api = WealthsimpleAPIBase(mock_session) |
| 81 | + |
| 82 | + result = api.send_get("https://test.example.com/get") |
| 83 | + |
| 84 | + assert result == {"status": "ok"} |
| 85 | + |
| 86 | + mock_request.assert_called_once() |
| 87 | + args, kwargs = mock_request.call_args |
| 88 | + assert args[0] == "GET" |
| 89 | + assert args[1] == "https://test.example.com/get" |
| 90 | + headers = kwargs["headers"] |
| 91 | + assert "x-ws-session-id" in headers |
| 92 | + assert headers["x-ws-session-id"] == "test_session_id" |
0 commit comments