blob: 7621f6eb0db45293009af2ee88c5893094b90839 [file] [log] [blame]
Andrea Frittoli7d707a52014-04-06 11:46:32 +01001# Copyright 2014 Hewlett-Packard Development Company, L.P.
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
Andrea Frittoli2095d242014-03-20 08:36:23 +000016import copy
17
Andrea Frittoli7d707a52014-04-06 11:46:32 +010018from tempest import auth
Andrea Frittoli7d707a52014-04-06 11:46:32 +010019from tempest.common import tempest_fixtures as fixtures
20from tempest import config
21from tempest import exceptions
ghanshyamc0edda02015-02-06 15:51:40 +090022from tempest.services.identity.json import token_client as v2_client
23from tempest.services.identity.v3.json import token_client as v3_client
Andrea Frittoli7d707a52014-04-06 11:46:32 +010024from tempest.tests import base
25from tempest.tests import fake_config
Andrea Frittoli7d707a52014-04-06 11:46:32 +010026from tempest.tests import fake_identity
27
28
29class CredentialsTests(base.TestCase):
30 attributes = {}
31 credentials_class = auth.Credentials
32
33 def _get_credentials(self, attributes=None):
34 if attributes is None:
35 attributes = self.attributes
36 return self.credentials_class(**attributes)
37
Andrea Frittoli9efbe952015-01-29 12:43:09 +000038 def _check(self, credentials, credentials_class, filled):
39 # Check the right version of credentials has been returned
40 self.assertIsInstance(credentials, credentials_class)
41 # Check the id attributes are filled in
42 attributes = [x for x in credentials.ATTRIBUTES if (
43 '_id' in x and x != 'domain_id')]
44 for attr in attributes:
45 if filled:
46 self.assertIsNotNone(getattr(credentials, attr))
47 else:
48 self.assertIsNone(getattr(credentials, attr))
49
Andrea Frittoli7d707a52014-04-06 11:46:32 +010050 def setUp(self):
51 super(CredentialsTests, self).setUp()
Andrea Frittoli7d707a52014-04-06 11:46:32 +010052 self.useFixture(fake_config.ConfigFixture())
53 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
54
Andrea Frittoli2095d242014-03-20 08:36:23 +000055 def test_create(self):
56 creds = self._get_credentials()
57 self.assertEqual(self.attributes, creds._initial)
58
Andrea Frittoli7d707a52014-04-06 11:46:32 +010059 def test_create_invalid_attr(self):
60 self.assertRaises(exceptions.InvalidCredentials,
61 self._get_credentials,
62 attributes=dict(invalid='fake'))
63
Andrea Frittoli7d707a52014-04-06 11:46:32 +010064 def test_is_valid(self):
65 creds = self._get_credentials()
66 self.assertRaises(NotImplementedError, creds.is_valid)
67
68
69class KeystoneV2CredentialsTests(CredentialsTests):
70 attributes = {
71 'username': 'fake_username',
72 'password': 'fake_password',
73 'tenant_name': 'fake_tenant_name'
74 }
75
76 identity_response = fake_identity._fake_v2_response
77 credentials_class = auth.KeystoneV2Credentials
Ken'ichi Ohmichi03010dc2015-01-08 08:16:25 +000078 tokenclient_class = v2_client.TokenClientJSON
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000079 identity_version = 'v2'
Andrea Frittoli7d707a52014-04-06 11:46:32 +010080
81 def setUp(self):
82 super(KeystoneV2CredentialsTests, self).setUp()
Ken'ichi Ohmichi03010dc2015-01-08 08:16:25 +000083 self.stubs.Set(self.tokenclient_class, 'raw_request',
84 self.identity_response)
Andrea Frittoli7d707a52014-04-06 11:46:32 +010085
Andrea Frittoli9efbe952015-01-29 12:43:09 +000086 def _verify_credentials(self, credentials_class, creds_dict, filled=True):
Andrea Frittoli878d5ab2015-01-30 13:22:50 +000087 creds = auth.get_credentials(fill_in=filled,
88 identity_version=self.identity_version,
89 **creds_dict)
Andrea Frittoli9efbe952015-01-29 12:43:09 +000090 self._check(creds, credentials_class, filled)
Andrea Frittoli7d707a52014-04-06 11:46:32 +010091
92 def test_get_credentials(self):
93 self.useFixture(fixtures.LockFixture('auth_version'))
Andrea Frittoli2095d242014-03-20 08:36:23 +000094 self._verify_credentials(credentials_class=self.credentials_class,
95 creds_dict=self.attributes)
96
97 def test_get_credentials_not_filled(self):
98 self.useFixture(fixtures.LockFixture('auth_version'))
99 self._verify_credentials(credentials_class=self.credentials_class,
Andrea Frittoli9efbe952015-01-29 12:43:09 +0000100 creds_dict=self.attributes,
101 filled=False)
Andrea Frittoli7d707a52014-04-06 11:46:32 +0100102
103 def test_is_valid(self):
104 creds = self._get_credentials()
105 self.assertTrue(creds.is_valid())
106
Matthew Treinish6bbc8742014-08-25 18:28:15 -0400107 def _test_is_not_valid(self, ignore_key):
Andrea Frittoli7d707a52014-04-06 11:46:32 +0100108 creds = self._get_credentials()
109 for attr in self.attributes.keys():
Matthew Treinish6bbc8742014-08-25 18:28:15 -0400110 if attr == ignore_key:
111 continue
112 temp_attr = getattr(creds, attr)
Andrea Frittoli7d707a52014-04-06 11:46:32 +0100113 delattr(creds, attr)
114 self.assertFalse(creds.is_valid(),
115 "Credentials should be invalid without %s" % attr)
Matthew Treinish6bbc8742014-08-25 18:28:15 -0400116 setattr(creds, attr, temp_attr)
117
118 def test_is_not_valid(self):
119 # NOTE(mtreinish): A KeystoneV2 credential object is valid without
120 # a tenant_name. So skip that check. See tempest.auth for the valid
121 # credential requirements
122 self._test_is_not_valid('tenant_name')
Andrea Frittoli7d707a52014-04-06 11:46:32 +0100123
Andrea Frittoli2095d242014-03-20 08:36:23 +0000124 def test_reset_all_attributes(self):
125 creds = self._get_credentials()
126 initial_creds = copy.deepcopy(creds)
127 set_attr = creds.__dict__.keys()
128 missing_attr = set(creds.ATTRIBUTES).difference(set_attr)
129 # Set all unset attributes, then reset
130 for attr in missing_attr:
131 setattr(creds, attr, 'fake' + attr)
132 creds.reset()
133 # Check reset credentials are same as initial ones
134 self.assertEqual(creds, initial_creds)
135
136 def test_reset_single_attribute(self):
137 creds = self._get_credentials()
138 initial_creds = copy.deepcopy(creds)
139 set_attr = creds.__dict__.keys()
140 missing_attr = set(creds.ATTRIBUTES).difference(set_attr)
141 # Set one unset attributes, then reset
142 for attr in missing_attr:
143 setattr(creds, attr, 'fake' + attr)
144 creds.reset()
145 # Check reset credentials are same as initial ones
146 self.assertEqual(creds, initial_creds)
147
Andrea Frittolib1b04bb2014-04-06 11:57:07 +0100148
149class KeystoneV3CredentialsTests(KeystoneV2CredentialsTests):
150 attributes = {
151 'username': 'fake_username',
152 'password': 'fake_password',
153 'project_name': 'fake_project_name',
154 'user_domain_name': 'fake_domain_name'
155 }
156
157 credentials_class = auth.KeystoneV3Credentials
158 identity_response = fake_identity._fake_v3_response
Ken'ichi Ohmichi03010dc2015-01-08 08:16:25 +0000159 tokenclient_class = v3_client.V3TokenClientJSON
Andrea Frittoli878d5ab2015-01-30 13:22:50 +0000160 identity_version = 'v3'
Andrea Frittolib1b04bb2014-04-06 11:57:07 +0100161
Matthew Treinish6bbc8742014-08-25 18:28:15 -0400162 def test_is_not_valid(self):
163 # NOTE(mtreinish) For a Keystone V3 credential object a project name
164 # is not required to be valid, so we skip that check. See tempest.auth
165 # for the valid credential requirements
166 self._test_is_not_valid('project_name')
167
Andrea Frittolib1b04bb2014-04-06 11:57:07 +0100168 def test_synced_attributes(self):
169 attributes = self.attributes
170 # Create V3 credentials with tenant instead of project, and user_domain
171 for attr in ['project_id', 'user_domain_id']:
172 attributes[attr] = 'fake_' + attr
173 creds = self._get_credentials(attributes)
174 self.assertEqual(creds.project_name, creds.tenant_name)
175 self.assertEqual(creds.project_id, creds.tenant_id)
176 self.assertEqual(creds.user_domain_name, creds.project_domain_name)
177 self.assertEqual(creds.user_domain_id, creds.project_domain_id)
178 # Replace user_domain with project_domain
179 del attributes['user_domain_name']
180 del attributes['user_domain_id']
181 del attributes['project_name']
182 del attributes['project_id']
183 for attr in ['project_domain_name', 'project_domain_id',
184 'tenant_name', 'tenant_id']:
185 attributes[attr] = 'fake_' + attr
186 self.assertEqual(creds.tenant_name, creds.project_name)
187 self.assertEqual(creds.tenant_id, creds.project_id)
188 self.assertEqual(creds.project_domain_name, creds.user_domain_name)
189 self.assertEqual(creds.project_domain_id, creds.user_domain_id)