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