-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_apiclient.py
409 lines (370 loc) · 12.8 KB
/
test_apiclient.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
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
401
402
403
404
405
406
407
408
409
from hexonet.apiconnector.apiclient import APIClient as AC
from hexonet.apiconnector.response import Response as R
from hexonet.apiconnector.responsetemplatemanager import ResponseTemplateManager as RTM
import pytest
import platform
def test_apiclientmethods():
cl = AC()
rtm = RTM.getInstance()
rtm.addTemplate(
'login200',
'[RESPONSE]\r\nPROPERTY[SESSION][0]=h8JLZZHdF2WgWWXlwbKWzEG3XrzoW4y' +
'shhvtqyg0LCYiX55QnhgYX9cB0W4mlpbx\r\nDESCRIPTION=Command completed' +
' successfully\r\nCODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.169\r\nEOF\r' +
'\n'
)
rtm.addTemplate(
'login500',
rtm.generateTemplate('530', 'Authentication failed')
)
rtm.addTemplate(
'OK',
rtm.generateTemplate('200', 'Command completed successfully')
)
rtm.addTemplate(
'listP0',
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=2701\r\nPROPERTY[FIRST][0]=0\r\nP' +
'ROPERTY[DOMAIN][0]=0-60motorcycletimes.com\r\nPROPERTY[DOMAIN][1]=' +
'0-be-s01-0.com\r\nPROPERTY[COUNT][0]=2\r\nPROPERTY[LAST][0]=1\r\nP' +
'ROPERTY[LIMIT][0]=2\r\nDESCRIPTION=Command completed successfully' +
'\r\nCODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.023\r\nEOF\r\n'
)
rtm.addTemplate(
'listP1',
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=2701\r\nPROPERTY[FIRST][0]=2\r\nP' +
'ROPERTY[DOMAIN][0]=0-qas-ao17-0.org\r\nPROPERTY[DOMAIN][1]=0-sunny' +
'da222y.com\r\nPROPERTY[COUNT][0]=2\r\nPROPERTY[LAST][0]=3\r\nPROPE' +
'RTY[LIMIT][0]=2\r\nDESCRIPTION=Command completed successfully\r\nC' +
'ODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.032\r\nEOF\r\n'
)
rtm.addTemplate(
'listFP0',
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=3\r\nPROPERTY[FIRST][0]=0\r\nPROP' +
'ERTY[DOMAIN][0]=0-60motorcycletimes.com\r\nPROPERTY[COUNT][0]=1\r' +
'\nPROPERTY[LAST][0]=1\r\nPROPERTY[LIMIT][0]=1\r\nDESCRIPTION=Comma' +
'nd completed successfully\r\nCODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.' +
'023\r\nEOF\r\n'
)
rtm.addTemplate(
'listFP1',
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=3\r\nPROPERTY[FIRST][0]=1\r\nPROP' +
'ERTY[DOMAIN][0]=0-be-s01-0.com\r\nPROPERTY[COUNT][0]=1\r\nPROPERTY' +
'[LAST][0]=2\r\nPROPERTY[LIMIT][0]=1\r\nDESCRIPTION=Command complet' +
'ed successfully\r\nCODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.032\r\nEOF' +
'\r\n'
)
rtm.addTemplate(
'listFP2',
'[RESPONSE]\r\nPROPERTY[TOTAL][0]=3\r\nPROPERTY[FIRST][0]=2\r\nPROP' +
'ERTY[DOMAIN][0]=0-qas-ao17-0.org\r\nPROPERTY[COUNT][0]=2\r\nPROPER' +
'TY[LAST][0]=3\r\nPROPERTY[LIMIT][0]=1\r\nDESCRIPTION=Command compl' +
'eted successfully\r\nCODE=200\r\nQUEUETIME=0\r\nRUNTIME=0.032\r\nE' +
'OF\r\n'
)
# #.getPOSTData()
# test object input with special chars
validate = (
's_entity=54cd&s_command=AUTH%3Dgwrgwqg%25' +
'%26%5C44t3%2A%0ACOMMAND%3DModifyDomain'
)
enc = cl.getPOSTData({
'COMMAND': 'ModifyDomain',
'AUTH': 'gwrgwqg%&\\44t3*'
})
assert enc == validate
# test string input
enc = cl.getPOSTData('gregergege')
assert enc == 's_entity=54cd&s_command='
# test object input with null value in parameter
validate = 's_entity=54cd&s_command=COMMAND%3DModifyDomain'
enc = cl.getPOSTData({
"COMMAND": 'ModifyDomain',
"AUTH": None
})
assert enc == validate
# #.enableDebugMode()
cl.enableDebugMode()
cl.disableDebugMode()
# #.getSession()
# initial value
session = cl.getSession()
assert session is None
# custom value
sessid = 'testSessionID12345678'
cl.setSession(sessid)
session = cl.getSession()
assert session is sessid
cl.setSession('')
# #.getURL()
assert cl.getURL() == 'https://coreapi.1api.net/api/call.cgi'
# #.getUserAgent()
pid = "PYTHON-SDK"
pyv = platform.python_version()
pf = platform.system()
arch = platform.architecture()[0]
ua = "%s (%s; %s; rv:%s) python/%s" % (pid, pf, arch, cl.getVersion(), pyv)
assert cl.getUserAgent() == ua
# #.setUserAgent()
pid = "WHMCS"
rv = "7.7.0"
pid2 = "python-sdk"
pyv = platform.python_version()
pf = platform.system()
arch = platform.architecture()[0]
ua = "%s (%s; %s; rv:%s) %s/%s python/%s" % (pid, pf, arch, rv, pid2, cl.getVersion(), pyv)
cl2 = cl.setUserAgent(pid, rv)
assert isinstance(cl2, AC) is True
assert cl.getUserAgent() == ua
# #.setURL()
tmp = 'http://coreapi.1api.net/api/call.cgi'
url = cl.setURL(tmp).getURL()
assert url is tmp
cl.setURL('https://coreapi.1api.net/api/call.cgi')
# #.setOTP()
# [otp set]
cl.setOTP('12345678')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = 's_entity=54cd&s_otp=12345678&s_command=COMMAND%3DStatusAccount'
assert tmp == exp
# [otp reset]
cl.setOTP('')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = 's_entity=54cd&s_command=COMMAND%3DStatusAccount'
assert tmp == exp
# #.setSession()
# [session set]
cl.setSession('12345678')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = 's_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount'
assert tmp == exp
# [credentials and session set]
cl.setRoleCredentials('myaccountid', 'myrole', 'mypassword')
cl.setOTP('12345678')
cl.setSession('12345678')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = 's_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount'
assert tmp == exp
# [session reset]
cl.setSession('')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
assert tmp == 's_entity=54cd&s_command=COMMAND%3DStatusAccount'
# #.saveSession/reuseSession
sessionobj = {}
cl.setSession('12345678').saveSession(sessionobj)
cl2 = AC()
cl2.reuseSession(sessionobj)
tmp = cl2.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = 's_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount'
assert tmp == exp
cl.setSession('')
# #.setRemoteIPAddress()
# [ip set]
cl.setRemoteIPAddress('10.10.10.10')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = (
's_entity=54cd&s_remoteaddr=10.10.10.10&s_command=COMMAND%3DStatusAccount'
)
assert tmp == exp
# [ip reset]
cl.setRemoteIPAddress('')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
assert tmp == 's_entity=54cd&s_command=COMMAND%3DStatusAccount'
# #.setCredentials()
# [credentials set]
cl.setCredentials('myaccountid', 'mypassword')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = (
's_entity=54cd&s_login=myaccountid&s_pw=mypassword&s_command=COMMAND%3DStatusAccount'
)
assert tmp == exp
# [session reset]
cl.setCredentials('', '')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
assert tmp == 's_entity=54cd&s_command=COMMAND%3DStatusAccount'
# #.setRoleCredentials()
# [role credentials set]
cl.setRoleCredentials('myaccountid', 'myroleid', 'mypassword')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
exp = (
's_entity=54cd&s_login=myaccountid%21myroleid&s_pw=mypassword&s_command=COMMAND%3DStatusAccount'
)
assert tmp == exp
# [role credentials reset]
cl.setRoleCredentials('', '', '')
tmp = cl.getPOSTData({
'COMMAND': 'StatusAccount'
})
assert tmp == 's_entity=54cd&s_command=COMMAND%3DStatusAccount'
# #.login()
# [login succeeded; no role used]
cl.useOTESystem()
cl.setCredentials('test.user', 'test.passw0rd')
cl.setRemoteIPAddress('1.2.3.4')
r = cl.login()
assert isinstance(r, R) is True
assert r.isSuccess() is True, ("{0} {1}").format(r.getCode(), r.getDescription())
rec = r.getRecord(0)
assert rec is not None
assert rec.getDataByKey('SESSION') is not None
# [login succeeded; role used]
cl.useOTESystem()
cl.setRoleCredentials('test.user', 'testrole', 'test.passw0rd')
r = cl.login()
assert isinstance(r, R)
assert r.isSuccess() is True, ("{0} {1}").format(r.getCode(), r.getDescription())
rec = r.getRecord(0)
assert rec is not None
assert rec.getDataByKey('SESSION') is not None
# [login failed; wrong credentials]
cl.setCredentials('test.user', 'WRONGPASSWORD')
r = cl.login()
assert isinstance(r, R)
assert r.isError() is True
# [login failed; http error]
tpl = rtm.getTemplate('httperror')
old = cl.getURL()
cl.setURL('https://iwontsucceedgregegeg343teagr43.com/api/call.cgi')
cl.setCredentials('test.user', 'WRONGPASSWORD')
r = cl.login()
assert isinstance(r, R)
assert r.isTmpError() is True
assert r.getDescription() == tpl.getDescription()
cl.setURL(old)
# [login succeeded; no session returned]
# TODO: need network mock
# tpl = R(rtm.getTemplate('OK').getPlain())
# cl.useOTESystem()
# cl.setCredentials('test.user', 'test.passw0rd')
# r = cl.login()
# assert isinstance(r, R)
# assert r.isSuccess() is True
# rec = r.getRecord(0)
# assert rec is Node
# #.loginExtended()
# [login succeeded; no role used]
cl.useOTESystem()
cl.setCredentials('test.user', 'test.passw0rd')
r = cl.loginExtended({
'TIMEOUT': 60
})
assert isinstance(r, R) is True
assert r.isSuccess() is True
rec = r.getRecord(0)
assert rec is not None
assert rec.getDataByKey('SESSION') is not None
# #.logout()
# [logout succeeded]
r = cl.logout()
assert isinstance(r, R)
assert r.isSuccess() is True
# [logout failed; session no longer exists]
tpl = R(rtm.getTemplate('login200').getPlain())
cl.enableDebugMode()
cl.setSession(tpl.getRecord(0).getDataByKey('SESSION'))
r = cl.logout()
assert isinstance(r, R) is True
assert r.isError() is True
# #.request()
# [200 < r.statusCode > 299]
# TODO need network mock
# tpl2 = R(rtm.getTemplate('httperror').getPlain())
# cl.setCredentials('test.user', 'test.passw0rd')
# cl.useOTESystem()
# r = cl.request({ 'COMMAND': 'GetUserIndex' })
# assert isinstance(r, R) is True
# assert r.isTmpError() is True
# assert r.getCode() == tpl2.getCode()
# assert r.getDescription() == tpl2.getDescription()
# [200 < r.statusCode > 299, no debug]
# TODO need network mock
# tpl2 = R(rtm.getTemplate('httperror').getPlain())
# cl.disableDebugMode()
# r = cl.request({ 'COMMAND': 'GetUserIndex' })
# assert isinstance(r, R)
# assert r.isTmpError() is True
# assert r.getCode() == tpl2.getCode()
# assert r.getDescription() == tpl2.getDescription()
# .requestNextResponsePage
# [no LAST set]
cl.setCredentials('test.user', 'test.passw0rd')
cl.useOTESystem()
r = R(
rtm.getTemplate('listP0').getPlain(),
{'COMMAND': 'QueryDomainList', 'LIMIT': 2, 'FIRST': 0}
)
nr = cl.requestNextResponsePage(r)
assert r.isSuccess() is True
assert nr.isSuccess() is True
assert r.getRecordsLimitation() == 2
assert nr.getRecordsLimitation() == 2
assert r.getRecordsCount() == 2
assert nr.getRecordsCount() == 2
assert r.getFirstRecordIndex() == 0
assert r.getLastRecordIndex() == 1
assert nr.getFirstRecordIndex() == 2
assert nr.getLastRecordIndex() == 3
# [LAST set]
r = R(rtm.getTemplate(
'listP0').getPlain(),
{'COMMAND': 'QueryDomainList', 'LIMIT': 2, 'FIRST': 0, 'LAST': 1}
)
with pytest.raises(
Exception,
match=r'Parameter LAST in use.'
):
cl.requestNextResponsePage(r)
# [no FIRST set]
cl.disableDebugMode()
r = R(
rtm.getTemplate('listP0').getPlain(),
{'COMMAND': 'QueryDomainList', 'LIMIT': 2}
)
nr = cl.requestNextResponsePage(r)
assert r.isSuccess() is True
assert nr.isSuccess() is True
assert r.getRecordsLimitation() == 2
assert nr.getRecordsLimitation() == 2
assert r.getRecordsCount() == 2
assert nr.getRecordsCount() == 2
assert r.getFirstRecordIndex() == 0
assert r.getLastRecordIndex() == 1
assert nr.getFirstRecordIndex() == 2
assert nr.getLastRecordIndex() == 3
# #.requestAllResponsePages()
# [success case]
nr = cl.requestAllResponsePages(
{'COMMAND': 'QuerySSLCertList', 'FIRST': 0, 'LIMIT': 100}
)
assert len(nr) > 0
# #.setUserView()
cl.setUserView('hexotestman.com')
r = cl.request({'COMMAND': 'GetUserIndex'})
assert isinstance(r, R) is True
assert r.isSuccess() is True
# #.resetUserView()
cl.setUserView('')
r = cl.request({'COMMAND': 'GetUserIndex'})
assert isinstance(r, R) is True
assert r.isSuccess() is True