Skip to content

Commit 7765e7e

Browse files
ccassellxdesai
authored andcommitted
Add get_endpoints_iterator and paginate get_endpoints (duosecurity#76)
1 parent 6435f3d commit 7765e7e

2 files changed

Lines changed: 93 additions & 6 deletions

File tree

duo_client/admin.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,14 +943,30 @@ def delete_user_group(self, user_id, group_id):
943943
params = {}
944944
return self.json_api_call('DELETE', path, params)
945945

946-
def get_endpoints(self):
946+
def get_endpoints_iterator(self):
947947
"""
948-
Returns list of endpoints.
948+
Returns iterator of endpoints objects.
949949
950950
Raises RuntimeError on error.
951951
"""
952-
response = self.json_api_call('GET', '/admin/v1/endpoints', {})
953-
return response
952+
return self.json_paging_api_call('GET', '/admin/v1/endpoints', {})
953+
954+
def get_endpoints(self, limit=None, offset=0):
955+
"""
956+
Returns a list of endpoints.
957+
958+
Params:
959+
limit - The maximum number of records to return. (Optional)
960+
offset - The offset of the first record to return. (Optional)
961+
962+
Raises RuntimeError on error.
963+
"""
964+
(limit, offset) = self.normalize_paging_args(limit, offset)
965+
if limit:
966+
return self.json_api_call('GET', '/admin/v1/endpoints',
967+
{'limit': limit, 'offset': offset})
968+
969+
return list(self.get_endpoints_iterator())
954970

955971
def get_phones_generator(self):
956972
"""

tests/admin/test_endpoints.py

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,82 @@
44

55

66
class TestEndpoints(TestAdmin):
7+
def test_get_endpoints_iterator(self):
8+
""" Test to get endpoints iterator
9+
"""
10+
iterator = self.client_list.get_endpoints_iterator()
11+
response = next(iterator)
12+
self.assertEqual(response['method'], 'GET')
13+
(uri, args) = response['uri'].split('?')
14+
self.assertEqual(uri, '/admin/v1/endpoints')
15+
self.assertEqual(
16+
util.params_to_dict(args),
17+
{
18+
'account_id': [self.client.account_id],
19+
'limit': ['100'],
20+
'offset': ['0'],
21+
})
22+
723
def test_get_endpoints(self):
8-
response = self.client.get_endpoints()
24+
""" Test to get endpoints.
25+
"""
26+
response = self.client_list.get_endpoints()[0]
27+
self.assertEqual(response['method'], 'GET')
28+
(uri, args) = response['uri'].split('?')
29+
self.assertEqual(uri, '/admin/v1/endpoints')
30+
self.assertEqual(
31+
util.params_to_dict(args),
32+
{
33+
'account_id': [self.client.account_id],
34+
'limit': ['100'],
35+
'offset': ['0'],
36+
})
37+
38+
def test_get_endpoints_offset(self):
39+
""" Test to get endpoints with pagination params.
40+
"""
41+
response = self.client_list.get_endpoints(offset=20)[0]
42+
self.assertEqual(response['method'], 'GET')
43+
(uri, args) = response['uri'].split('?')
44+
self.assertEqual(uri, '/admin/v1/endpoints')
45+
self.assertEqual(
46+
util.params_to_dict(args),
47+
{
48+
'account_id': [self.client.account_id],
49+
'limit': ['100'],
50+
'offset': ['0'],
51+
})
52+
53+
def test_get_endpoints_limit(self):
54+
""" Test to get endpoints with pagination params.
55+
"""
56+
response = self.client_list.get_endpoints(limit=20)[0]
957
self.assertEqual(response['method'], 'GET')
1058
(uri, args) = response['uri'].split('?')
1159
self.assertEqual(uri, '/admin/v1/endpoints')
1260
self.assertEqual(
1361
util.params_to_dict(args),
14-
{'account_id': [self.client.account_id]})
62+
{
63+
'account_id': [self.client.account_id],
64+
'limit': ['20'],
65+
'offset': ['0'],
66+
})
67+
68+
def test_get_endpoints_limit_and_offset(self):
69+
""" Test to get endpoints with pagination params.
70+
"""
71+
response = self.client_list.get_endpoints(limit=35, offset=20)[0]
72+
self.assertEqual(response['method'], 'GET')
73+
(uri, args) = response['uri'].split('?')
74+
self.assertEqual(uri, '/admin/v1/endpoints')
75+
self.assertEqual(
76+
util.params_to_dict(args),
77+
{
78+
'account_id': [self.client.account_id],
79+
'limit': ['35'],
80+
'offset': ['20'],
81+
})
82+
83+
84+
if __name__ == '__main__':
85+
unittest.main()

0 commit comments

Comments
 (0)