-
Notifications
You must be signed in to change notification settings - Fork 601
/
test_rest.py
235 lines (194 loc) · 7.27 KB
/
test_rest.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import json
import requests
from requests.auth import HTTPBasicAuth
import sys
from tabpy.tabpy_tools.rest import RequestsNetworkWrapper, ServiceClient
import unittest
from unittest.mock import Mock
class TestRequestsNetworkWrapper(unittest.TestCase):
def test_init(self):
RequestsNetworkWrapper()
def test_init_with_session(self):
session = {}
rnw = RequestsNetworkWrapper(session=session)
self.assertIs(session, rnw.session)
def mock_response(self, status_code):
response = Mock(requests.Response())
response.json.return_value = "json"
response.status_code = status_code
return response
def setUp(self):
session = Mock(requests.session())
session.get.return_value = self.mock_response(200)
session.post.return_value = self.mock_response(200)
session.put.return_value = self.mock_response(200)
session.delete.return_value = self.mock_response(204)
self.rnw = RequestsNetworkWrapper(session=session)
def test_GET(self):
url = "abc"
data = {"foo": "bar"}
self.assertEqual(self.rnw.GET(url, data), "json")
self.rnw.session.get.assert_called_once_with(
url, params=data, timeout=None, auth=None
)
def test_GET_InvalidData(self):
url = "abc"
data = {"cat"}
with self.assertRaises(TypeError):
self.rnw.session.get.return_value = self.mock_response(404)
self.rnw.GET(url, data)
def test_GET_InvalidURL(self):
url = ""
data = {"foo": "bar"}
with self.assertRaises(TypeError):
self.rnw.session.get.return_value = self.mock_response(404)
self.rnw.GET(url, data)
def test_POST(self):
url = "abc"
data = {"foo": "bar"}
self.assertEqual(self.rnw.POST(url, data), "json")
self.rnw.session.post.assert_called_once_with(
url,
data=json.dumps(data),
headers={"content-type": "application/json"},
timeout=None,
auth=None,
)
def test_POST_InvalidURL(self):
url = ""
data = {"foo": "bar"}
with self.assertRaises(TypeError):
self.rnw.session.post.return_value = self.mock_response(404)
self.rnw.POST(url, data)
def test_POST_InvalidData(self):
url = "url"
data = {"cat"}
with self.assertRaises(TypeError):
self.rnw.POST(url, data)
def test_PUT(self):
url = "abc"
data = {"foo": "bar"}
self.assertEqual(self.rnw.PUT(url, data), "json")
self.rnw.session.put.assert_called_once_with(
url,
data=json.dumps(data),
headers={"content-type": "application/json"},
timeout=None,
auth=None,
)
def test_PUT_InvalidData(self):
url = "url"
data = {"cat"}
with self.assertRaises(TypeError):
self.rnw.PUT(url, data)
def test_PUT_InvalidURL(self):
url = ""
data = {"foo:bar"}
with self.assertRaises(TypeError):
self.rnw.PUT(url, data)
def test_DELETE(self):
url = "abc"
data = {"foo": "bar"}
self.assertIs(self.rnw.DELETE(url, data), None)
self.rnw.session.delete.assert_called_once_with(
url, data=json.dumps(data), timeout=None, auth=None
)
def test_DELETE_InvalidData(self):
url = "abc"
data = {"cat"}
with self.assertRaises(TypeError):
self.rnw.DELETE(url, data)
def test_DELETE_InvalidURL(self):
url = ""
data = {"foo:bar"}
with self.assertRaises(TypeError):
self.rnw.DELETE(url, data)
def test_set_credentials(self):
expected_auth = None
self.assertEqual(self.rnw.auth, expected_auth)
username, password = "username", "password"
expected_auth = HTTPBasicAuth(username, password)
self.rnw.set_credentials(username, password)
self.assertEqual(self.rnw.auth, expected_auth)
def _test_METHOD_with_credentials(
self,
http_method_function,
http_session_method_function,
headers=None,
params=False,
data=False,
response=None,
):
username, password = "username", "password"
self.rnw.set_credentials(username, password)
url = "url"
_data = {"foo": "bar"}
self.assertEqual(http_method_function(url, _data), response)
pargs = {url}
kwargs = {"timeout": None, "auth": self.rnw.auth}
if data:
kwargs["data"] = json.dumps(_data)
if headers:
kwargs["headers"] = headers
if params:
kwargs["params"] = _data
http_session_method_function.assert_called_once_with(*pargs, **kwargs)
self.assertEqual(self.rnw.auth, HTTPBasicAuth(username, password))
def test_GET_with_credentials(self):
self._test_METHOD_with_credentials(
self.rnw.GET, self.rnw.session.get, params=True, response="json"
)
def test_POST_with_credentials(self):
self._test_METHOD_with_credentials(
self.rnw.POST,
self.rnw.session.post,
headers={"content-type": "application/json"},
data=True,
response="json",
)
def test_PUT_with_credentials(self):
self._test_METHOD_with_credentials(
self.rnw.PUT,
self.rnw.session.put,
data=True,
headers={"content-type": "application/json"},
response="json",
)
def test_DELETE_with_credentials(self):
self._test_METHOD_with_credentials(
self.rnw.DELETE, self.rnw.session.delete, data=True
)
class TestServiceClient(unittest.TestCase):
def setUp(self):
nw = Mock(RequestsNetworkWrapper())
nw.GET.return_value = "GET"
nw.POST.return_value = "POST"
nw.PUT.return_value = "PUT"
nw.DELETE.return_value = "DELETE"
self.sc = ServiceClient("endpoint/", network_wrapper=nw)
self.scClientDoesNotEndWithSlash = ServiceClient("endpoint", network_wrapper=nw)
def test_GET(self):
self.assertEqual(self.sc.GET("test"), "GET")
self.sc.network_wrapper.GET.assert_called_once_with("endpoint/test", None, None)
def test_POST(self):
self.assertEqual(self.sc.POST("test"), "POST")
self.sc.network_wrapper.POST.assert_called_once_with(
"endpoint/test", None, None
)
def test_PUT(self):
self.assertEqual(self.sc.PUT("test"), "PUT")
self.sc.network_wrapper.PUT.assert_called_once_with("endpoint/test", None, None)
def test_DELETE(self):
self.assertEqual(self.sc.DELETE("test"), None)
self.sc.network_wrapper.DELETE.assert_called_once_with(
"endpoint/test", None, None
)
def test_FixEndpoint(self):
self.assertEqual(self.scClientDoesNotEndWithSlash.GET("test"), "GET")
self.sc.network_wrapper.GET.assert_called_once_with("endpoint/test", None, None)
def test_set_credentials(self):
username, password = "username", "password"
self.sc.set_credentials(username, password)
self.sc.network_wrapper.set_credentials.assert_called_once_with(
username, password
)