Debian Codegraph @

keystoneclient/tests/functional/v3/test_credentials.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
17 from keystoneclient.tests.functional import base
18 from keystoneclient.tests.functional.v3 import client_fixtures as fixtures
19
20
21 class CredentialsTestCase(base.V3ClientTestCase):
22
23 def setUp(self):
24 super(CredentialsTestCase, self).setUp()
25 self.test_domain = fixtures.Domain(self.client)
26 self.useFixture(self.test_domain)
27
28 def check_credential(self, credential, credential_ref=None):
29 self.assertIsNotNone(credential.id)
30 self.assertIn('self', credential.links)
31 self.assertIn('/credentials/' + credential.id,
32 credential.links['self'])
33
34 if credential_ref:
35 self.assertEqual(credential_ref['user'], credential.user_id)
36 self.assertEqual(credential_ref['type'], credential.type)
37 self.assertEqual(credential_ref['blob'], credential.blob)
38
39 # There is no guarantee below attributes are present in credential
40 if credential_ref['type'] == 'ec2' or hasattr(credential_ref,
41 'project'):
42 self.assertEqual(credential_ref['project'],
43 credential.project_id)
44
45 else:
46 # Only check remaining mandatory attributes
47 self.assertIsNotNone(credential.user_id)
48 self.assertIsNotNone(credential.type)
49 self.assertIsNotNone(credential.blob)
50 if credential.type == 'ec2':
51 self.assertIsNotNone(credential.project_id)
52
53 def test_create_credential_of_cert_type(self):
54 user = fixtures.User(self.client, self.test_domain.id)
55 self.useFixture(user)
56
57 credential_ref = {'user': user.id,
58 'type': 'cert',
59 'blob': uuid.uuid4().hex}
60 credential = self.client.credentials.create(**credential_ref)
61
62 self.addCleanup(self.client.credentials.delete, credential)
63 self.check_credential(credential, credential_ref)
64
65 def test_create_credential_of_ec2_type(self):
66 user = fixtures.User(self.client, self.test_domain.id)
67 self.useFixture(user)
68
69 # project is mandatory attribute if the credential type is ec2
70 credential_ref = {'user': user.id,
71 'type': 'ec2',
72 'blob': ("{\"access\":\"" + uuid.uuid4().hex +
73 "\",\"secret\":\"secretKey\"}")}
74 self.assertRaises(http.BadRequest,
75 self.client.credentials.create,
76 **credential_ref)
77
78 project = fixtures.Project(self.client, self.test_domain.id)
79 self.useFixture(project)
80
81 credential_ref = {'user': user.id,
82 'type': 'ec2',
83 'blob': ("{\"access\":\"" + uuid.uuid4().hex +
84 "\",\"secret\":\"secretKey\"}"),
85 'project': project.id}
86 credential = self.client.credentials.create(**credential_ref)
87
88 self.addCleanup(self.client.credentials.delete, credential)
89 self.check_credential(credential, credential_ref)
90
91 def test_create_credential_of_totp_type(self):
92 user = fixtures.User(self.client, self.test_domain.id)
93 self.useFixture(user)
94
95 credential_ref = {'user': user.id,
96 'type': 'totp',
97 'blob': uuid.uuid4().hex}
98 credential = self.client.credentials.create(**credential_ref)
99
100 self.addCleanup(self.client.credentials.delete, credential)
101 self.check_credential(credential, credential_ref)
102
103 def test_get_credential(self):
104 user = fixtures.User(self.client, self.test_domain.id)
105 self.useFixture(user)
106 project = fixtures.Project(self.client, self.test_domain.id)
107 self.useFixture(project)
108
109 for credential_type in ['cert', 'ec2', 'totp']:
110 credential = fixtures.Credential(self.client, user=user.id,
111 type=credential_type,
112 project=project.id)
113 self.useFixture(credential)
114
115 credential_ret = self.client.credentials.get(credential.id)
116 self.check_credential(credential_ret, credential.ref)
117
118 def test_list_credentials(self):
119 user = fixtures.User(self.client, self.test_domain.id)
120 self.useFixture(user)
121
122 cert_credential = fixtures.Credential(self.client, user=user.id,
123 type='cert')
124 self.useFixture(cert_credential)
125
126 project = fixtures.Project(self.client, self.test_domain.id)
127 self.useFixture(project)
128 ec2_credential = fixtures.Credential(self.client, user=user.id,
129 type='ec2', project=project.id)
130 self.useFixture(ec2_credential)
131
132 totp_credential = fixtures.Credential(self.client, user=user.id,
133 type='totp')
134 self.useFixture(totp_credential)
135
136 credentials = self.client.credentials.list()
137
138 # All credentials are valid
139 for credential in credentials:
140 self.check_credential(credential)
141
142 self.assertIn(cert_credential.entity, credentials)
143 self.assertIn(ec2_credential.entity, credentials)
144 self.assertIn(totp_credential.entity, credentials)
145
146 def test_update_credential(self):
147 user = fixtures.User(self.client, self.test_domain.id)
148 self.useFixture(user)
149
150 new_user = fixtures.User(self.client, self.test_domain.id)
151 self.useFixture(new_user)
152 new_project = fixtures.Project(self.client, self.test_domain.id)
153 self.useFixture(new_project)
154
155 credential = fixtures.Credential(self.client, user=user.id,
156 type='cert')
157 self.useFixture(credential)
158
159 new_type = 'ec2'
160 new_blob = ("{\"access\":\"" + uuid.uuid4().hex +
161 "\",\"secret\":\"secretKey\"}")
162
163 credential_ret = self.client.credentials.update(credential.id,
164 user=new_user.id,
165 type=new_type,
166 blob=new_blob,
167 project=new_project.id)
168
169 credential.ref.update({'user': new_user.id, 'type': new_type,
170 'blob': new_blob, 'project': new_project.id})
171 self.check_credential(credential_ret, credential.ref)
172
173 def test_delete_credential(self):
174 user = fixtures.User(self.client, self.test_domain.id)
175 self.useFixture(user)
176 project = fixtures.Project(self.client, self.test_domain.id)
177 self.useFixture(project)
178
179 for credential_type in ['cert', 'ec2', 'totp']:
180
181 if credential_type == 'ec2':
182 blob_value = ("{\"access\":\"" + uuid.uuid4().hex +
183 "\",\"secret\":\"secretKey\"}")
184 else:
185 blob_value = uuid.uuid4().hex
186
187 credential = self.client.credentials.create(user=user.id,
188 type=credential_type,
189 blob=blob_value,
190 project=project.id)
191 self.client.credentials.delete(credential.id)
192 self.assertRaises(http.NotFound,
193 self.client.credentials.get,
194 credential.id)