Repository navigation
Expand file tree
/
Copy pathtest_admin.py
More file actions
299 lines (254 loc) · 11.7 KB
/
Copy pathtest_admin.py
File metadata and controls
299 lines (254 loc) · 11.7 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
from __future__ import absolute_import
import unittest
import warnings
from . import util
import duo_client.admin
class TestAdmin(unittest.TestCase):
def setUp(self):
self.client = duo_client.admin.Admin(
'test_ikey', 'test_akey', 'example.com')
self.client.account_id = 'DA012345678901234567'
# monkeypatch client's _connect()
self.client._connect = lambda: util.MockHTTPConnection()
# if you are wanting to simulate getting lists of objects
# rather than a single object
self.client_list = duo_client.admin.Admin(
'test_ikey', 'test_akey', 'example.com')
self.client_list.account_id = 'DA012345678901234567'
self.client_list._connect = \
lambda: util.MockHTTPConnection(data_response_should_be_list=True)
# if you are wanting to get a response from a call to get
# authentication logs
self.client_authlog = duo_client.admin.Admin(
'test_ikey', 'test_akey', 'example.com')
self.client_authlog.account_id = 'DA012345678901234567'
self.client_authlog._connect = \
lambda: util.MockHTTPConnection(data_response_from_get_authlog=True)
# GET with no params
def test_get_users(self):
response = self.client.get_users()
self.assertEqual(response['method'], 'GET')
self.assertEqual(
response['uri'],
'/admin/v1/users?account_id=%s' % self.client.account_id)
self.assertEqual(response['body'], None)
# GET with params
def test_get_users_by_name(self):
response = self.client.get_users_by_name('foo')
(uri, args) = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/users')
self.assertEqual(
util.params_to_dict(args),
{'username':['foo'],
'account_id':[self.client.account_id]})
self.assertEqual(response['body'], None)
response = self.client.get_users_by_name('foo')
(uri, args) = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/users')
self.assertEqual(
util.params_to_dict(args),
{'username':['foo'],
'account_id':[self.client.account_id]})
self.assertEqual(response['body'], None)
# POST with params
def test_add_user(self):
# all params given
response = self.client.add_user('foo', 'bar', 'active', 'notes', '[email protected]', 'fName', 'lName')
self.assertEqual(response['method'], 'POST')
self.assertEqual(response['uri'], '/admin/v1/users')
self.assertEqual(
util.params_to_dict(response['body']),
{'realname':['bar'],
'notes':['notes'],
'username':['foo'],
'status':['active'],
'email':['foobar%40baz.com'],
'firstname':['fName'],
'lastname':['lName'],
'account_id':[self.client.account_id]})
# defaults
response = self.client.add_user('bar')
self.assertEqual(response['method'], 'POST')
self.assertEqual(response['uri'], '/admin/v1/users')
self.assertEqual(
util.params_to_dict(response['body']),
{'username':['bar'], 'account_id':[self.client.account_id]})
# POST with no params
def test_update_user(self):
response = self.client.update_user('DU012345678901234567')
self.assertEqual(response['method'], 'POST')
self.assertEqual(
response['uri'], '/admin/v1/users/DU012345678901234567')
self.assertEqual(
util.params_to_dict(response['body']),
{'account_id':[self.client.account_id]})
def test_get_endpoints(self):
response = self.client.get_endpoints()
self.assertEqual(response['method'], 'GET')
(uri, args) = response['uri'].split('?')
self.assertEqual(uri, '/admin/v1/endpoints')
self.assertEqual(
util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_user_u2ftokens(self):
""" Test to get u2ftokens by user id.
"""
response = self.client.get_user_u2ftokens('DU012345678901234567')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/users/DU012345678901234567/u2ftokens')
self.assertEqual(util.params_to_dict(args),
{'account_id':[self.client.account_id]})
def test_get_u2ftokens_with_params(self):
""" Test to get u2ftokens with params.
"""
response = list(self.client_list.get_u2ftokens())[0]
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/u2ftokens')
self.assertEqual(util.params_to_dict(args),
{'account_id':[self.client_list.account_id]})
def test_get_u2ftokens_without_params(self):
""" Test to get u2ftokens without params.
"""
response = list(self.client_list.get_u2ftokens())[0]
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/u2ftokens')
self.assertEqual(util.params_to_dict(args),
{'account_id':[self.client_list.account_id]})
def test_get_u2ftoken_by_id(self):
""" Test to get u2ftoken by registration id.
"""
response = self.client.get_u2ftoken_by_id('DU012345678901234567')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/u2ftokens/DU012345678901234567')
self.assertEqual(util.params_to_dict(args),
{'account_id':[self.client.account_id]})
def test_delete_u2ftoken(self):
""" Test to delete u2ftoken by registration id.
"""
response = self.client.delete_u2ftoken('DU012345678901234567')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'DELETE')
self.assertEqual(uri, '/admin/v1/u2ftokens/DU012345678901234567')
self.assertEqual(util.params_to_dict(args),
{'account_id':[self.client.account_id]})
def test_get_user_bypass_codes(self):
""" Test to get bypass codes by user id.
"""
response = self.client.get_user_bypass_codes('DU012345678901234567')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(
uri,
'/admin/v1/users/DU012345678901234567/bypass_codes')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_bypass_codes(self):
""" Test to get bypass codes.
"""
response = list(self.client_list.get_bypass_codes())[0]
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/bypass_codes')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client_list.account_id]})
def test_delete_bypass_code_by_id(self):
""" Test to delete a bypass code by id.
"""
response = self.client.delete_bypass_code_by_id('DU012345678901234567')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'DELETE')
self.assertEqual(uri, '/admin/v1/bypass_codes/DU012345678901234567')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_authentication_log_v1(self):
""" Test to get authentication log on version 1 api.
"""
response = self.client_list.get_authentication_log(api_version=1)[0]
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/logs/authentication')
self.assertEqual(
util.params_to_dict(args)['account_id'],
[self.client_list.account_id])
def test_get_authentication_log_v2(self):
""" Test to get authentication log on version 1 api.
"""
response = self.client_authlog.get_authentication_log(api_version=2)
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v2/logs/authentication')
self.assertEqual(
util.params_to_dict(args)['account_id'],
[self.client_authlog.account_id])
def test_get_groups(self):
""" Test for getting list of all groups
"""
response = self.client.get_groups()
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/groups')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_group_v1(self):
""" Test for v1 API of getting specific group details
"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
response = self.client.get_group('ABC123', api_version=1)
uri, args = response['uri'].split('?')
# Assert deprecation warning generated
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[0].category, DeprecationWarning))
self.assertIn('Please migrate to the v2 API', str(w[0].message))
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v1/groups/ABC123')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_group_v2(self):
""" Test for v2 API of getting specific group details
"""
response = self.client.get_group('ABC123', api_version=2)
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v2/groups/ABC123')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_get_group_users(self):
""" Test for getting list of users associated with a group
"""
response = self.client.get_group_users('ABC123')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'GET')
self.assertEqual(uri, '/admin/v2/groups/ABC123/users')
self.assertEqual(
util.params_to_dict(args),
{
'account_id': [self.client.account_id],
'limit': ['100'],
'offset': ['0'],
})
def test_delete_group(self):
""" Test for deleting a group
"""
response = self.client.delete_group('ABC123')
uri, args = response['uri'].split('?')
self.assertEqual(response['method'], 'DELETE')
self.assertEqual(uri, '/admin/v1/groups/ABC123')
self.assertEqual(util.params_to_dict(args),
{'account_id': [self.client.account_id]})
def test_modify_group(self):
""" Test for modifying a group
"""
response = self.client.modify_group('ABC123')
self.assertEqual(response['method'], 'POST')
self.assertEqual(response['uri'], '/admin/v1/groups/ABC123')
self.assertEqual(util.params_to_dict(response['body']),
{'account_id': [self.client.account_id]})
if __name__ == '__main__':
unittest.main()