Debian Codegraph @

keystoneclient/tests/unit/v2_0/test_tokens.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 import exceptions
16 from keystoneauth1 import fixture
17
18 from keystoneclient import access
19 from keystoneclient.tests.unit.v2_0 import utils
20 from keystoneclient.v2_0 import client
21 from keystoneclient.v2_0 import tokens
22
23
24 class TokenTests(utils.ClientTestCase):
25
26 def test_delete(self):
27 id_ = uuid.uuid4().hex
28 self.stub_url('DELETE', ['tokens', id_], status_code=204)
29 self.client.tokens.delete(id_)
30
31 def test_user_password(self):
32 token_fixture = fixture.V2Token(user_name=self.TEST_USER)
33 self.stub_auth(json=token_fixture)
34
35 password = uuid.uuid4().hex
36 token_ref = self.client.tokens.authenticate(username=self.TEST_USER,
37 password=password)
38
39 self.assertIsInstance(token_ref, tokens.Token)
40 self.assertEqual(token_fixture.token_id, token_ref.id)
41 self.assertEqual(token_fixture.expires_str, token_ref.expires)
42
43 req_body = {
44 'auth': {
45 'passwordCredentials': {
46 'username': self.TEST_USER,
47 'password': password,
48 }
49 }
50 }
51
52 self.assertRequestBodyIs(json=req_body)
53
54 def test_with_token_id(self):
55 token_fixture = fixture.V2Token()
56 self.stub_auth(json=token_fixture)
57
58 token_id = uuid.uuid4().hex
59 token_ref = self.client.tokens.authenticate(token=token_id)
60
61 self.assertIsInstance(token_ref, tokens.Token)
62 self.assertEqual(token_fixture.token_id, token_ref.id)
63 self.assertEqual(token_fixture.expires_str, token_ref.expires)
64
65 req_body = {
66 'auth': {
67 'token': {
68 'id': token_id,
69 }
70 }
71 }
72
73 self.assertRequestBodyIs(json=req_body)
74
75 def test_without_auth_params(self):
76 self.assertRaises(ValueError, self.client.tokens.authenticate)
77 self.assertRaises(ValueError, self.client.tokens.authenticate,
78 tenant_id=uuid.uuid4().hex)
79
80 def test_with_tenant_id(self):
81 token_fixture = fixture.V2Token()
82 token_fixture.set_scope()
83 self.stub_auth(json=token_fixture)
84
85 token_id = uuid.uuid4().hex
86 tenant_id = uuid.uuid4().hex
87 token_ref = self.client.tokens.authenticate(token=token_id,
88 tenant_id=tenant_id)
89
90 self.assertIsInstance(token_ref, tokens.Token)
91 self.assertEqual(token_fixture.token_id, token_ref.id)
92 self.assertEqual(token_fixture.expires_str, token_ref.expires)
93
94 tenant_data = {'id': token_fixture.tenant_id,
95 'name': token_fixture.tenant_name}
96 self.assertEqual(tenant_data, token_ref.tenant)
97
98 req_body = {
99 'auth': {
100 'token': {
101 'id': token_id,
102 },
103 'tenantId': tenant_id
104 }
105 }
106
107 self.assertRequestBodyIs(json=req_body)
108
109 def test_with_tenant_name(self):
110 token_fixture = fixture.V2Token()
111 token_fixture.set_scope()
112 self.stub_auth(json=token_fixture)
113
114 token_id = uuid.uuid4().hex
115 tenant_name = uuid.uuid4().hex
116 token_ref = self.client.tokens.authenticate(token=token_id,
117 tenant_name=tenant_name)
118
119 self.assertIsInstance(token_ref, tokens.Token)
120 self.assertEqual(token_fixture.token_id, token_ref.id)
121 self.assertEqual(token_fixture.expires_str, token_ref.expires)
122
123 tenant_data = {'id': token_fixture.tenant_id,
124 'name': token_fixture.tenant_name}
125 self.assertEqual(tenant_data, token_ref.tenant)
126
127 req_body = {
128 'auth': {
129 'token': {
130 'id': token_id,
131 },
132 'tenantName': tenant_name
133 }
134 }
135
136 self.assertRequestBodyIs(json=req_body)
137
138 def test_authenticate_use_admin_url(self):
139 token_fixture = fixture.V2Token()
140 token_fixture.set_scope()
141 self.stub_auth(json=token_fixture)
142
143 token_ref = self.client.tokens.authenticate(token=uuid.uuid4().hex)
144 self.assertEqual(self.TEST_URL + '/tokens',
145 self.requests_mock.last_request.url)
146 self.assertIsInstance(token_ref, tokens.Token)
147 self.assertEqual(token_fixture.token_id, token_ref.id)
148 self.assertEqual(token_fixture.expires_str, token_ref.expires)
149
150 def test_authenticate_fallback_to_auth_url(self):
151 new_auth_url = 'http://keystone.test:5000/v2.0'
152
153 token_fixture = fixture.V2Token()
154 self.stub_auth(base_url=new_auth_url, json=token_fixture)
155
156 # Creating a HTTPClient not using session is deprecated.
157 with self.deprecations.expect_deprecations_here():
158 c = client.Client(username=self.TEST_USER,
159 auth_url=new_auth_url,
160 password=uuid.uuid4().hex)
161
162 self.assertIsNone(c.management_url)
163
164 token_ref = c.tokens.authenticate(token=uuid.uuid4().hex)
165 self.assertIsInstance(token_ref, tokens.Token)
166 self.assertEqual(token_fixture.token_id, token_ref.id)
167 self.assertEqual(token_fixture.expires_str, token_ref.expires)
168
169 def test_validate_token(self):
170 id_ = uuid.uuid4().hex
171 token_fixture = fixture.V2Token(token_id=id_)
172 self.stub_url('GET', ['tokens', id_], json=token_fixture)
173
174 token_data = self.client.tokens.get_token_data(id_)
175 self.assertEqual(token_fixture, token_data)
176
177 token_ref = self.client.tokens.validate(id_)
178 self.assertIsInstance(token_ref, tokens.Token)
179 self.assertEqual(id_, token_ref.id)
180
181 def test_validate_token_invalid_token(self):
182 # If the token is invalid, typically a NotFound is raised.
183
184 id_ = uuid.uuid4().hex
185 # The server is expected to return 404 if the token is invalid.
186 self.stub_url('GET', ['tokens', id_], status_code=404)
187
188 self.assertRaises(exceptions.NotFound,
189 self.client.tokens.get_token_data, id_)
190 self.assertRaises(exceptions.NotFound,
191 self.client.tokens.validate, id_)
192
193 def test_validate_token_access_info_with_token_id(self):
194 # Can validate a token passing a string token ID.
195 token_id = uuid.uuid4().hex
196 token_fixture = fixture.V2Token(token_id=token_id)
197 self.stub_url('GET', ['tokens', token_id], json=token_fixture)
198 access_info = self.client.tokens.validate_access_info(token_id)
199 self.assertIsInstance(access_info, access.AccessInfoV2)
200 self.assertEqual(token_id, access_info.auth_token)
201
202 def test_validate_token_access_info_with_access_info(self):
203 # Can validate a token passing an access info.
204 token_id = uuid.uuid4().hex
205 token_fixture = fixture.V2Token(token_id=token_id)
206 self.stub_url('GET', ['tokens', token_id], json=token_fixture)
207 token = access.AccessInfo.factory(body=token_fixture)
208 access_info = self.client.tokens.validate_access_info(token)
209 self.assertIsInstance(access_info, access.AccessInfoV2)
210 self.assertEqual(token_id, access_info.auth_token)
211
212 def test_get_revoked(self):
213 sample_revoked_response = {'signed': '-----BEGIN CMS-----\nMIIB...'}
214 self.stub_url('GET', ['tokens', 'revoked'],
215 json=sample_revoked_response)
216 resp = self.client.tokens.get_revoked()
217 self.assertEqual(sample_revoked_response, resp)