blob: 4c72d828f714e6e38f55a973ea6bab6a0e6994b4 [file] [log] [blame]
Chris Hoge4f6117a2015-03-20 12:39:33 -05001# Copyright 2015 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Jane Zadorozhnaf7b39732015-06-10 14:19:33 +030016from oslo_utils import timeutils
17import six
Megan Guiney3a34df72017-05-23 23:04:17 -070018
Chris Hoge4f6117a2015-03-20 12:39:33 -050019from tempest.api.identity import base
Ken'ichi Ohmichi44f01272017-01-27 18:44:14 -080020from tempest.lib import decorators
Megan Guiney3a34df72017-05-23 23:04:17 -070021from tempest.lib import exceptions as lib_exc
Chris Hoge4f6117a2015-03-20 12:39:33 -050022
23
24class TokensV3Test(base.BaseIdentityV3Test):
25
Megan Guiney3a34df72017-05-23 23:04:17 -070026 @decorators.idempotent_id('a9512ac3-3909-48a4-b395-11f438e16260')
27 def test_validate_token(self):
28 creds = self.os_primary.credentials
29 user_id = creds.user_id
30 username = creds.username
31 password = creds.password
32 user_domain_id = creds.user_domain_id
33 # GET and validate token
34 subject_token, token_body = self.non_admin_token.get_token(
35 user_id=user_id,
36 username=username,
37 user_domain_id=user_domain_id,
38 password=password,
39 auth_data=True)
40 authenticated_token = self.non_admin_client.show_token(
41 subject_token)['token']
42 # sanity checking to make sure they are indeed the same token
43 self.assertEqual(authenticated_token, token_body)
44 # test to see if token has been properly authenticated
45 self.assertEqual(authenticated_token['user']['id'], user_id)
46 self.assertEqual(authenticated_token['user']['name'], username)
47 self.non_admin_client.delete_token(subject_token)
48 self.assertRaises(
49 lib_exc.NotFound, self.non_admin_client.show_token, subject_token)
50
Ken'ichi Ohmichi44f01272017-01-27 18:44:14 -080051 @decorators.idempotent_id('6f8e4436-fc96-4282-8122-e41df57197a9')
Chris Hoge4f6117a2015-03-20 12:39:33 -050052 def test_create_token(self):
53
Jordan Pittier8160d312017-04-18 11:52:23 +020054 creds = self.os_primary.credentials
Chris Hoge4f6117a2015-03-20 12:39:33 -050055 user_id = creds.user_id
56 username = creds.username
57 password = creds.password
Tom Cocozzello5e3cff12016-02-23 14:18:56 -060058 user_domain_id = creds.user_domain_id
Chris Hoge4f6117a2015-03-20 12:39:33 -050059
Marc Koderer8afdf6c2016-04-28 17:24:15 -050060 # 'user_domain_id' needs to be specified otherwise tempest.lib assumes
Tom Cocozzello5e3cff12016-02-23 14:18:56 -060061 # it to be 'default'
62 token_id, resp = self.non_admin_token.get_token(
63 user_id=user_id,
Brant Knudsondd9f8052016-10-21 13:38:10 -050064 username=username,
Tom Cocozzello5e3cff12016-02-23 14:18:56 -060065 user_domain_id=user_domain_id,
66 password=password,
67 auth_data=True)
Jane Zadorozhnaf7b39732015-06-10 14:19:33 +030068
69 self.assertNotEmpty(token_id)
70 self.assertIsInstance(token_id, six.string_types)
71
72 now = timeutils.utcnow()
73 expires_at = timeutils.normalize_time(
74 timeutils.parse_isotime(resp['expires_at']))
75 self.assertGreater(resp['expires_at'],
76 resp['issued_at'])
77 self.assertGreater(expires_at, now)
78
79 subject_id = resp['user']['id']
Brant Knudsondd9f8052016-10-21 13:38:10 -050080 if user_id:
81 self.assertEqual(subject_id, user_id)
82 else:
83 # Expect a user ID, but don't know what it will be.
Masayuki Igawaf9009b42017-04-10 14:49:29 +090084 self.assertIsNotNone(subject_id, 'Expected user ID in token.')
Jane Zadorozhnaf7b39732015-06-10 14:19:33 +030085
86 subject_name = resp['user']['name']
Brant Knudsondd9f8052016-10-21 13:38:10 -050087 if username:
88 self.assertEqual(subject_name, username)
89 else:
90 # Expect a user name, but don't know what it will be.
Masayuki Igawaf9009b42017-04-10 14:49:29 +090091 self.assertIsNotNone(subject_name, 'Expected user name in token.')
Jane Zadorozhnaf7b39732015-06-10 14:19:33 +030092
93 self.assertEqual(resp['methods'][0], 'password')