File: test_users.py

package info (click to toggle)
python-duo-client 5.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: python: 7,105; sh: 6; makefile: 4
file content (174 lines) | stat: -rw-r--r-- 6,557 bytes parent folder | download
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
import json
from .. import util
import duo_client.admin
from .base import TestAdmin


class TestUsers(TestAdmin):
    def test_get_users_generator(self):
        """ Test to get users iterator.
        """
        iterator = self.client_list.get_users_iterator()
        response = next(iterator)
        self.assertEqual(response['method'], 'GET')
        (uri, args) = response['uri'].split('?')
        self.assertEqual(uri, '/admin/v1/users')
        self.assertEqual(
            util.params_to_dict(args),
            {
                'account_id': [self.client.account_id],
                'limit': ['100'],
                'offset': ['0'],
            })

    def test_get_users(self):
        """ Test to get users.
        """
        response = self.client_list.get_users()[0]
        self.assertEqual(response['method'], 'GET')
        (uri, args) = response['uri'].split('?')
        self.assertEqual(uri, '/admin/v1/users')
        self.assertEqual(
            util.params_to_dict(args),
            {
                'account_id': [self.client.account_id],
                'limit': ['100'],
                'offset': ['0'],
            })

    def test_get_users_offset(self):
        """ Test to get users with pagination params.
        """
        response = self.client_list.get_users(offset=30)[0]
        self.assertEqual(response['method'], 'GET')
        (uri, args) = response['uri'].split('?')
        self.assertEqual(uri, '/admin/v1/users')
        self.assertEqual(
            util.params_to_dict(args),
            {
                'account_id': [self.client.account_id],
                'limit': ['100'],
                'offset': ['0'],
            })

    def test_get_users_limit(self):
        """ Test to get users with pagination params.
        """
        response = self.client_list.get_users(limit=30)[0]
        self.assertEqual(response['method'], 'GET')
        (uri, args) = response['uri'].split('?')
        self.assertEqual(uri, '/admin/v1/users')
        self.assertEqual(
            util.params_to_dict(args),
            {
                'account_id': [self.client.account_id],
                'limit': ['30'],
                'offset': ['0'],
            })

    def test_get_users_limit_and_offset(self):
        """ Test to get users with pagination params.
        """
        response = self.client_list.get_users(limit=20, offset=30)[0]
        self.assertEqual(response['method'], 'GET')
        (uri, args) = response['uri'].split('?')
        self.assertEqual(uri, '/admin/v1/users')
        self.assertEqual(
            util.params_to_dict(args),
            {
                'account_id': [self.client.account_id],
                'limit': ['20'],
                'offset': ['30'],
            })

    # 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', realname='bar', status='active', notes='notes',
            email='foobar@baz.com', firstname='fName', lastname='lName',
            alias1='alias1', alias2='alias2', alias3='alias3', alias4='alias4')
        self.assertEqual(response['method'], 'POST')
        self.assertEqual(response['uri'], '/admin/v1/users')
        self.assertEqual(
            json.loads(response['body']),
            {
                'realname': 'bar',
                'notes': 'notes',
                'username': 'foo',
                'status': 'active',
                'email': 'foobar@baz.com',
                'firstname': 'fName',
                'lastname': 'lName',
                'account_id': self.client.account_id,
                'alias1': 'alias1',
                'alias2': 'alias2',
                'alias3': 'alias3',
                'alias4': 'alias4',
            })
        # defaults
        response = self.client.add_user('bar')
        self.assertEqual(response['method'], 'POST')
        self.assertEqual(response['uri'], '/admin/v1/users')
        self.assertEqual(
            json.loads(response['body']),
            {'username':'bar', 'account_id':self.client.account_id})

    def test_update_user(self):
        response = self.client.update_user(
            'DU012345678901234567', username='foo', realname='bar',
            status='active', notes='notes', email='foobar@baz.com',
            firstname='fName', lastname='lName', alias1='alias1',
            alias2='alias2', alias3='alias3', alias4='alias4')
        self.assertEqual(response['method'], 'POST')
        self.assertEqual(
            response['uri'], '/admin/v1/users/DU012345678901234567')
        self.assertEqual(
            json.loads(response['body']),
            {
                'account_id':self.client.account_id,
                'realname': 'bar',
                'notes': 'notes',
                'username': 'foo',
                'status': 'active',
                'email': 'foobar@baz.com',
                'firstname': 'fName',
                'lastname': 'lName',
                'account_id': self.client.account_id,
                'alias1': 'alias1',
                'alias2': 'alias2',
                'alias3': 'alias3',
                'alias4': 'alias4',
            })

    def test_sync_user(self):
        """ Test to synchronize a single user in a directory for a username.
        """
        response = self.client.sync_user('foo', 'test_dir_key')
        self.assertEqual(response['method'], 'POST')
        self.assertEqual(response['uri'],
                         '/admin/v1/users/directorysync/test_dir_key/syncuser')
        self.assertEqual(
           json.loads(response['body']),
            {'username': 'foo', 'account_id': self.client.account_id})