-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathapi_tests.py
More file actions
400 lines (331 loc) · 16.1 KB
/
api_tests.py
File metadata and controls
400 lines (331 loc) · 16.1 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
SoftLayer.tests.api_tests
~~~~~~~~~~~~~~~~~~~~~~~~~
:license: MIT, see LICENSE for more details.
"""
import io
import os
import requests
from unittest import mock as mock
import SoftLayer
import SoftLayer.API
from SoftLayer import auth as slauth
from SoftLayer import exceptions
from SoftLayer import testing
from SoftLayer import transports
class Initialization(testing.TestCase):
def test_init(self):
client = SoftLayer.Client(username='doesnotexist',
api_key='issurelywrong',
timeout=10,
endpoint_url='http://example.com/v3/xmlrpc/')
self.assertIsInstance(client.auth, SoftLayer.BasicAuthentication)
self.assertEqual(client.auth.username, 'doesnotexist')
self.assertEqual(client.auth.api_key, 'issurelywrong')
self.assertIsNotNone(client.transport)
self.assertIsInstance(client.transport, transports.XmlRpcTransport)
self.assertEqual(client.transport.timeout, 10)
def test_init_with_rest_url(self):
client = SoftLayer.Client(username='doesnotexist',
api_key='issurelywrong',
timeout=10,
endpoint_url='http://example.com/v3/rest/')
self.assertIsInstance(client.auth, SoftLayer.BasicHTTPAuthentication)
self.assertEqual(client.auth.username, 'doesnotexist')
self.assertEqual(client.auth.api_key, 'issurelywrong')
self.assertIsNotNone(client.transport)
self.assertIsInstance(client.transport, transports.RestTransport)
self.assertEqual(client.transport.endpoint_url,
'http://example.com/v3/rest')
self.assertEqual(client.transport.timeout, 10)
@mock.patch('SoftLayer.config.get_client_settings')
def test_env(self, get_client_settings):
auth = mock.Mock()
get_client_settings.return_value = {
'timeout': 10,
'endpoint_url': 'http://endpoint_url/',
}
client = SoftLayer.Client(auth=auth)
self.assertEqual(client.auth.get_headers(), auth.get_headers())
self.assertEqual(client.transport.timeout, 10)
self.assertEqual(client.transport.endpoint_url, 'http://endpoint_url')
class ClientMethods(testing.TestCase):
def test_repr(self):
client = SoftLayer.Client(
username='doesnotexist',
api_key='issurelywrong'
)
self.assertIn("Client", repr(client))
def test_service_repr(self):
client = SoftLayer.Client(
username='doesnotexist',
api_key='issurelywrong'
)
self.assertIn("Service", repr(client['SERVICE']))
def test_len(self):
client = SoftLayer.Client(
username='doesnotexist',
api_key='issurelywrong'
)
self.assertEqual(len(client), 0)
class APIClient(testing.TestCase):
def test_simple_call(self):
mock = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mock.return_value = {"test": "result"}
resp = self.client['SERVICE'].METHOD()
self.assertEqual(resp, {"test": "result"})
self.assert_called_with('SoftLayer_SERVICE', 'METHOD',
mask=None,
filter=None,
identifier=None,
args=tuple(),
limit=None,
offset=None,
)
def test_simple_call_2(self):
mock = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mock.return_value = {"test": "result"}
resp = self.client.call('SERVICE', 'METHOD', {'networkComponents': [{'maxSpeed': 100}]})
self.assertEqual(resp, {"test": "result"})
self.assert_called_with('SoftLayer_SERVICE', 'METHOD',
mask=None, filter=None, identifier=None,
args=({'networkComponents': [{'maxSpeed': 100}]},), limit=None, offset=None,
)
def test_verify_request_false(self):
client = SoftLayer.BaseClient(transport=self.mocks)
mock = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mock.return_value = {"test": "result"}
resp = client.call('SERVICE', 'METHOD', verify=False)
self.assertEqual(resp, {"test": "result"})
self.assert_called_with('SoftLayer_SERVICE', 'METHOD', verify=False)
def test_verify_request_true(self):
client = SoftLayer.BaseClient(transport=self.mocks)
mock = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mock.return_value = {"test": "result"}
resp = client.call('SERVICE', 'METHOD', verify=True)
self.assertEqual(resp, {"test": "result"})
self.assert_called_with('SoftLayer_SERVICE', 'METHOD', verify=True)
def test_verify_request_not_specified(self):
client = SoftLayer.BaseClient(transport=self.mocks)
mock = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mock.return_value = {"test": "result"}
resp = client.call('SERVICE', 'METHOD')
self.assertEqual(resp, {"test": "result"})
self.assert_called_with('SoftLayer_SERVICE', 'METHOD', verify=None)
@mock.patch('SoftLayer.API.BaseClient.iter_call')
def test_iterate(self, _iter_call):
self.client['SERVICE'].METHOD(iter=True)
_iter_call.assert_called_with('SERVICE', 'METHOD')
@mock.patch('SoftLayer.API.BaseClient.iter_call')
def test_service_iter_call(self, _iter_call):
self.client['SERVICE'].iter_call('METHOD', 'ARG')
_iter_call.assert_called_with('SERVICE', 'METHOD', 'ARG')
@mock.patch('SoftLayer.API.BaseClient.iter_call')
def test_service_iter_call_with_chunk(self, _iter_call):
self.client['SERVICE'].iter_call('METHOD', 'ARG', chunk=2)
_iter_call.assert_called_with('SERVICE', 'METHOD', 'ARG', chunk=2)
@mock.patch('SoftLayer.API.BaseClient.call')
def test_iter_call(self, _call):
# chunk=100, no limit
_call.side_effect = [
transports.SoftLayerListResult(range(100), 125),
transports.SoftLayerListResult(range(100, 125), 125)
]
result = list(self.client.iter_call('SERVICE', 'METHOD', iter=True))
self.assertEqual(list(range(125)), result)
_call.assert_has_calls([
mock.call('SERVICE', 'METHOD', limit=100, iter=False, offset=0, filter=mock.ANY),
mock.call('SERVICE', 'METHOD', limit=100, iter=False, offset=100, filter=mock.ANY),
])
_call.reset_mock()
# chunk=100, no limit. Requires one extra request.
_call.side_effect = [
transports.SoftLayerListResult(range(100), 201),
transports.SoftLayerListResult(range(100, 200), 201),
transports.SoftLayerListResult([], 201)
]
result = list(self.client.iter_call('SERVICE', 'METHOD', iter=True))
self.assertEqual(list(range(200)), result)
_call.assert_has_calls([
mock.call('SERVICE', 'METHOD', limit=100, iter=False, offset=0, filter=mock.ANY),
mock.call('SERVICE', 'METHOD', limit=100, iter=False, offset=100, filter=mock.ANY),
mock.call('SERVICE', 'METHOD', limit=100, iter=False, offset=200, filter=mock.ANY),
])
_call.reset_mock()
# chunk=25, limit=30
_call.side_effect = [
transports.SoftLayerListResult(range(0, 25), 30),
transports.SoftLayerListResult(range(25, 30), 30)
]
result = list(self.client.iter_call('SERVICE', 'METHOD', iter=True, limit=25))
self.assertEqual(list(range(30)), result)
_call.assert_has_calls([
mock.call('SERVICE', 'METHOD', iter=False, limit=25, offset=0, filter=mock.ANY),
mock.call('SERVICE', 'METHOD', iter=False, limit=25, offset=25, filter=mock.ANY),
])
_call.reset_mock()
# A non-list was returned
_call.side_effect = ["test"]
result = list(self.client.iter_call('SERVICE', 'METHOD', iter=True))
self.assertEqual(["test"], result)
_call.assert_has_calls([
mock.call('SERVICE', 'METHOD', iter=False, limit=100, offset=0, filter=mock.ANY),
])
_call.reset_mock()
_call.side_effect = [
transports.SoftLayerListResult(range(0, 25), 30),
transports.SoftLayerListResult(range(25, 30), 30)
]
result = list(
self.client.iter_call('SERVICE', 'METHOD', 'ARG', iter=True, limit=25, offset=12)
)
self.assertEqual(list(range(30)), result)
_call.assert_has_calls([
mock.call('SERVICE', 'METHOD', 'ARG', iter=False, limit=25, offset=12, filter=mock.ANY),
mock.call('SERVICE', 'METHOD', 'ARG', iter=False, limit=25, offset=37, filter=mock.ANY),
])
# Chunk size of 0 is invalid
self.assertRaises(
AttributeError,
lambda: list(self.client.iter_call('SERVICE', 'METHOD', iter=True, limit=0, filter=mock.ANY)))
def test_call_invalid_arguments(self):
self.assertRaises(
TypeError,
self.client.call, 'SERVICE', 'METHOD', invalid_kwarg='invalid')
def test_call_compression_disabled(self):
mocked = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mocked.return_value = {}
self.client['SERVICE'].METHOD(compress=False)
calls = self.calls('SoftLayer_SERVICE', 'METHOD')
self.assertEqual(len(calls), 1)
headers = calls[0].transport_headers
self.assertEqual(headers.get('accept-encoding'), 'identity')
def test_call_compression_enabled(self):
mocked = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mocked.return_value = {}
self.client['SERVICE'].METHOD(compress=True)
calls = self.calls('SoftLayer_SERVICE', 'METHOD')
self.assertEqual(len(calls), 1)
headers = calls[0].transport_headers
self.assertEqual(headers.get('accept-encoding'),
'gzip, deflate, compress')
def test_call_compression_override(self):
# raw_headers should override compress=False
mocked = self.set_mock('SoftLayer_SERVICE', 'METHOD')
mocked.return_value = {}
self.client['SERVICE'].METHOD(
compress=False,
raw_headers={'Accept-Encoding': 'gzip'})
calls = self.calls('SoftLayer_SERVICE', 'METHOD')
self.assertEqual(len(calls), 1)
headers = calls[0].transport_headers
self.assertEqual(headers.get('accept-encoding'), 'gzip')
def test_special_services(self):
# Tests for the special classes that don't need to start with SoftLayer_
self.client.call('BluePages_Search', 'findBluePagesProfile')
self.assert_called_with('BluePages_Search', 'findBluePagesProfile')
class UnauthenticatedAPIClient(testing.TestCase):
def set_up(self):
self.client = SoftLayer.Client(endpoint_url="ENDPOINT")
@mock.patch('SoftLayer.config.get_client_settings')
def test_init(self, get_client_settings):
get_client_settings.return_value = {}
client = SoftLayer.Client()
self.assertIsNone(client.auth)
@mock.patch('SoftLayer.config.get_client_settings')
def test_init_with_proxy(self, get_client_settings):
get_client_settings.return_value = {'proxy': 'http://localhost:3128'}
client = SoftLayer.Client()
self.assertEqual(client.transport.proxy, 'http://localhost:3128')
@mock.patch('SoftLayer.API.BaseClient.call')
def test_authenticate_with_password(self, _call):
_call.return_value = {
'userId': 12345,
'hash': 'TOKEN',
}
self.client.authenticate_with_password('USERNAME', 'PASSWORD')
_call.assert_called_with(
'User_Customer',
'getPortalLoginToken',
'USERNAME',
'PASSWORD',
None,
None)
self.assertIsNotNone(self.client.auth)
self.assertEqual(self.client.auth.user_id, 12345)
self.assertEqual(self.client.auth.auth_token, 'TOKEN')
class EmployeeClientTests(testing.TestCase):
@staticmethod
def setup_response(filename, status_code=200, total_items=1):
basepath = os.path.dirname(__file__)
body = b''
with open(f"{basepath}/../SoftLayer/fixtures/xmlrpc/{filename}.xml", 'rb') as fixture:
body = fixture.read()
response = requests.Response()
list_body = body
response.raw = io.BytesIO(list_body)
response.headers['SoftLayer-Total-Items'] = total_items
response.status_code = status_code
return response
def set_up(self):
self.client = SoftLayer.API.EmployeeClient(config_file='./tests/testconfig')
@mock.patch('SoftLayer.transports.xmlrpc.requests.Session.request')
def test_auth_with_pass_failure(self, api_response):
api_response.return_value = self.setup_response('invalidLogin')
exception = self.assertRaises(
exceptions.SoftLayerAPIError,
self.client.authenticate_with_password, 'testUser', 'testPassword', '123456')
self.assertEqual(exception.faultCode, "SoftLayer_Exception_Public")
@mock.patch('SoftLayer.transports.xmlrpc.requests.Session.request')
def test_auth_with_pass_success(self, api_response):
api_response.return_value = self.setup_response('successLogin')
result = self.client.authenticate_with_internal('testUser', 'testPassword', '123456')
print(result)
self.assertEqual(result['userId'], 1234)
self.assertEqual(self.client.settings['softlayer']['userid'], '1234')
self.assertIn('x'*200, self.client.settings['softlayer']['access_token'])
def test_auth_with_hash(self):
self.client.auth = None
self.client.authenticate_with_hash(5555, 'abcdefg')
self.assertEqual(self.client.auth.user_id, 5555)
self.assertEqual(self.client.auth.hash, 'abcdefg')
@mock.patch('SoftLayer.transports.xmlrpc.requests.Session.request')
def test_refresh_token(self, api_response):
api_response.return_value = self.setup_response('refreshSuccess')
self.client.refresh_token(9999, 'qweasdzxcqweasdzxcqweasdzxc')
self.assertEqual(self.client.auth.user_id, 9999)
self.assertIn('REFRESHEDTOKENaaaa', self.client.auth.hash)
@mock.patch('SoftLayer.transports.xmlrpc.requests.Session.request')
def test_expired_token_is_refreshed(self, api_response):
api_response.side_effect = [
self.setup_response('expiredToken'),
self.setup_response('refreshSuccess'),
self.setup_response('Employee_getObject')
]
self.client.auth = slauth.EmployeeAuthentication(5555, 'aabbccee')
self.client.settings['softlayer']['userid'] = '5555'
result = self.client.call('SoftLayer_User_Employee', 'getObject', id=5555)
self.assertIn('REFRESHEDTOKENaaaa', self.client.auth.hash)
self.assertEqual('testUser', result['username'])
@mock.patch('SoftLayer.transports.xmlrpc.requests.Session.request')
def test_expired_token_is_really_expired(self, api_response):
api_response.side_effect = [
self.setup_response('expiredToken'),
self.setup_response('expiredToken')
]
self.client.auth = slauth.EmployeeAuthentication(5555, 'aabbccee')
self.client.settings['softlayer']['userid'] = '5555'
exception = self.assertRaises(
exceptions.SoftLayerAPIError,
self.client.call, 'SoftLayer_User_Employee', 'getObject', id=5555)
self.assertEqual(exception.faultCode, "SoftLayer_Exception_EncryptedToken_Expired")
@mock.patch('SoftLayer.API.BaseClient.call')
def test_account_check(self, _call):
self.client.transport = self.mocks
self.client.account_id = 1234
self.client.call("SoftLayer_Account", "getObject")
self.client.call("SoftLayer_Account", "getObject1", id=9999)
_call.assert_has_calls([
mock.call(self.client, 'SoftLayer_Account', 'getObject', id=1234),
mock.call(self.client, 'SoftLayer_Account', 'getObject1', id=9999),
])