Debian Codegraph @

keystoneclient/tests/functional/v3/test_users.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import uuid
14
15 from keystoneauth1.exceptions import http
16 from keystoneclient.tests.functional import base
17 from keystoneclient.tests.functional.v3 import client_fixtures as fixtures
18
19
20 class UsersTestCase(base.V3ClientTestCase):
21
22 def check_user(self, user, user_ref=None):
23 self.assertIsNotNone(user.id)
24 self.assertIsNotNone(user.enabled)
25 self.assertIn('self', user.links)
26 self.assertIn('/users/' + user.id, user.links['self'])
27
28 if user_ref:
29 self.assertEqual(user_ref['name'], user.name)
30 self.assertEqual(user_ref['domain'], user.domain_id)
31 # There is no guarantee the attributes below are present in user
32 if hasattr(user_ref, 'description'):
33 self.assertEqual(user_ref['description'], user.description)
34 if hasattr(user_ref, 'email'):
35 self.assertEqual(user_ref['email'], user.email)
36 if hasattr(user_ref, 'default_project'):
37 self.assertEqual(user_ref['default_project'],
38 user.default_project_id)
39 else:
40 # Only check remaining mandatory attributes
41 self.assertIsNotNone(user.name)
42 self.assertIsNotNone(user.domain_id)
43
44 def test_create_user(self):
45 user_ref = {
46 'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex,
47 'domain': self.project_domain_id,
48 'default_project': self.project_id,
49 'password': uuid.uuid4().hex,
50 'description': uuid.uuid4().hex}
51
52 user = self.client.users.create(**user_ref)
53 self.addCleanup(self.client.users.delete, user)
54 self.check_user(user, user_ref)
55
56 def test_get_user(self):
57 user = fixtures.User(self.client, self.project_domain_id)
58 self.useFixture(user)
59
60 user_ret = self.client.users.get(user.id)
61 self.check_user(user_ret, user.ref)
62
63 def test_list_users(self):
64 user_one = fixtures.User(self.client, self.project_domain_id)
65 self.useFixture(user_one)
66
67 user_two = fixtures.User(self.client, self.project_domain_id)
68 self.useFixture(user_two)
69
70 users = self.client.users.list()
71
72 # All users are valid
73 for user in users:
74 self.check_user(user)
75
76 self.assertIn(user_one.entity, users)
77 self.assertIn(user_two.entity, users)
78
79 def test_list_users_with_filters(self):
80 suffix = uuid.uuid4().hex
81 user1_ref = {
82 'name': 'test_user' + suffix,
83 'domain': self.project_domain_id,
84 'default_project': self.project_id,
85 'password': uuid.uuid4().hex,
86 'description': uuid.uuid4().hex}
87
88 user2_ref = {
89 'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex,
90 'domain': self.project_domain_id,
91 'default_project': self.project_id,
92 'password': uuid.uuid4().hex,
93 'description': uuid.uuid4().hex}
94
95 user1 = self.client.users.create(**user1_ref)
96 self.client.users.create(**user2_ref)
97
98 users = self.client.users.list(name__contains=['test_user', suffix])
99 self.assertEqual(1, len(users))
100 self.assertIn(user1, users)
101
102 def test_update_user(self):
103 user = fixtures.User(self.client, self.project_domain_id)
104 self.useFixture(user)
105
106 new_description = uuid.uuid4().hex
107 user_ret = self.client.users.update(user.id,
108 description=new_description)
109
110 user.ref.update({'description': new_description})
111 self.check_user(user_ret, user.ref)
112
113 def test_user_grouping(self):
114 # keystoneclient.v3.users owns user grouping operations, this is why
115 # this test case belongs to this class
116 user = fixtures.User(self.client, self.project_domain_id)
117 group = fixtures.Group(self.client, self.project_domain_id)
118 self.useFixture(user)
119 self.useFixture(group)
120
121 self.assertRaises(http.NotFound,
122 self.client.users.check_in_group,
123 user.id, group.id)
124
125 self.client.users.add_to_group(user.id, group.id)
126 self.client.users.check_in_group(user.id, group.id)
127 self.client.users.remove_from_group(user.id, group.id)
128
129 self.assertRaises(http.NotFound,
130 self.client.users.check_in_group,
131 user.id, group.id)
132
133 def test_delete_user(self):
134 user = self.client.users.create(name=uuid.uuid4().hex,
135 domain=self.project_domain_id)
136
137 self.client.users.delete(user.id)
138 self.assertRaises(http.NotFound,
139 self.client.users.get,
140 user.id)