Skip to content

Commit 19a79ae

Browse files
test(client): add unit tests for OauthClient and Client
1 parent c637ed7 commit 19a79ae

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tests/__init__.py

Whitespace-only changes.

tests/test_client.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import unittest
2+
from unittest.mock import MagicMock, patch
3+
4+
from openapi_python_sdk.client import Client, OauthClient
5+
6+
7+
class TestOauthClient(unittest.TestCase):
8+
9+
@patch("openapi_python_sdk.client.httpx.Client")
10+
def test_create_token(self, mock_httpx):
11+
mock_resp = MagicMock()
12+
mock_resp.json.return_value = {"token": "abc123"}
13+
mock_httpx.return_value.post.return_value = mock_resp
14+
15+
oauth = OauthClient(username="user", apikey="key", test=True)
16+
resp = oauth.create_token(scopes=["GET:test.example.com/api"], ttl=3600)
17+
18+
self.assertEqual(resp["token"], "abc123")
19+
mock_httpx.return_value.post.assert_called_once()
20+
21+
@patch("openapi_python_sdk.client.httpx.Client")
22+
def test_delete_token(self, mock_httpx):
23+
mock_resp = MagicMock()
24+
mock_resp.json.return_value = {"success": True}
25+
mock_httpx.return_value.delete.return_value = mock_resp
26+
27+
oauth = OauthClient(username="user", apikey="key", test=True)
28+
resp = oauth.delete_token(id="abc123")
29+
30+
self.assertTrue(resp["success"])
31+
mock_httpx.return_value.delete.assert_called_once()
32+
33+
@patch("openapi_python_sdk.client.httpx.Client")
34+
def test_get_scopes(self, mock_httpx):
35+
mock_resp = MagicMock()
36+
mock_resp.json.return_value = {"scopes": ["GET:test.example.com/api"]}
37+
mock_httpx.return_value.get.return_value = mock_resp
38+
39+
oauth = OauthClient(username="user", apikey="key")
40+
resp = oauth.get_scopes()
41+
42+
self.assertIn("scopes", resp)
43+
44+
@patch("openapi_python_sdk.client.httpx.Client")
45+
def test_uses_sandbox_url_when_test_true(self, mock_httpx):
46+
oauth = OauthClient(username="user", apikey="key", test=True)
47+
self.assertIn("test.", oauth.url)
48+
49+
@patch("openapi_python_sdk.client.httpx.Client")
50+
def test_uses_production_url_by_default(self, mock_httpx):
51+
oauth = OauthClient(username="user", apikey="key")
52+
self.assertNotIn("test.", oauth.url)
53+
54+
@patch("openapi_python_sdk.client.httpx.Client")
55+
def test_auth_header_is_basic(self, mock_httpx):
56+
oauth = OauthClient(username="user", apikey="key")
57+
self.assertTrue(oauth.auth_header.startswith("Basic "))
58+
59+
60+
class TestClient(unittest.TestCase):
61+
62+
@patch("openapi_python_sdk.client.httpx.Client")
63+
def test_request_get(self, mock_httpx):
64+
mock_resp = MagicMock()
65+
mock_resp.json.return_value = {"data": []}
66+
mock_httpx.return_value.request.return_value = mock_resp
67+
68+
client = Client(token="abc123")
69+
resp = client.request(
70+
method="GET",
71+
url="https://test.imprese.openapi.it/advance",
72+
params={"denominazione": "altravia"},
73+
)
74+
75+
self.assertEqual(resp, {"data": []})
76+
mock_httpx.return_value.request.assert_called_once()
77+
78+
@patch("openapi_python_sdk.client.httpx.Client")
79+
def test_request_post(self, mock_httpx):
80+
mock_resp = MagicMock()
81+
mock_resp.json.return_value = {"result": "ok"}
82+
mock_httpx.return_value.request.return_value = mock_resp
83+
84+
client = Client(token="abc123")
85+
resp = client.request(
86+
method="POST",
87+
url="https://test.postontarget.com/fields/country",
88+
payload={"limit": 0, "query": {"country_code": "IT"}},
89+
)
90+
91+
self.assertEqual(resp["result"], "ok")
92+
93+
@patch("openapi_python_sdk.client.httpx.Client")
94+
def test_auth_header(self, mock_httpx):
95+
client = Client(token="mytoken")
96+
self.assertEqual(client.auth_header, "Bearer mytoken")
97+
self.assertEqual(client.headers["Authorization"], "Bearer mytoken")
98+
99+
@patch("openapi_python_sdk.client.httpx.Client")
100+
def test_defaults_on_empty_request(self, mock_httpx):
101+
mock_resp = MagicMock()
102+
mock_resp.json.return_value = {}
103+
mock_httpx.return_value.request.return_value = mock_resp
104+
105+
client = Client(token="tok")
106+
resp = client.request()
107+
108+
mock_httpx.return_value.request.assert_called_once_with(
109+
method="GET", url="", headers=client.headers, json={}, params={}
110+
)
111+
112+
113+
if __name__ == "__main__":
114+
unittest.main()

0 commit comments

Comments
 (0)