"Fossies" - the Fresh Open Source Software Archive

Member "keystone-18.0.0/keystone/tests/unit/credential/test_backend_sql.py" (14 Oct 2020, 4550 Bytes) of package /linux/misc/openstack/keystone-18.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_backend_sql.py": 17.0.0_vs_18.0.0.

    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 oslo_config import fixture as config_fixture
   16 
   17 from keystone.common import provider_api
   18 from keystone.credential.providers import fernet as credential_provider
   19 from keystone.tests import unit
   20 from keystone.tests.unit import default_fixtures
   21 from keystone.tests.unit import ksfixtures
   22 from keystone.tests.unit.ksfixtures import database
   23 
   24 from keystone.credential.backends import sql as credential_sql
   25 from keystone import exception
   26 
   27 PROVIDERS = provider_api.ProviderAPIs
   28 
   29 
   30 class SqlTests(unit.SQLDriverOverrides, unit.TestCase):
   31 
   32     def setUp(self):
   33         super(SqlTests, self).setUp()
   34         self.useFixture(database.Database())
   35         self.load_backends()
   36         # populate the engine with tables & fixtures
   37         self.load_fixtures(default_fixtures)
   38         # defaulted by the data load
   39         self.user_foo['enabled'] = True
   40 
   41     def config_files(self):
   42         config_files = super(SqlTests, self).config_files()
   43         config_files.append(unit.dirs.tests_conf('backend_sql.conf'))
   44         return config_files
   45 
   46 
   47 class SqlCredential(SqlTests):
   48 
   49     def _create_credential_with_user_id(self, user_id=None):
   50         if not user_id:
   51             user_id = uuid.uuid4().hex
   52         credential = unit.new_credential_ref(user_id=user_id,
   53                                              extra=uuid.uuid4().hex,
   54                                              type=uuid.uuid4().hex)
   55         PROVIDERS.credential_api.create_credential(
   56             credential['id'], credential
   57         )
   58         return credential
   59 
   60     def _validate_credential_list(self, retrieved_credentials,
   61                                   expected_credentials):
   62         self.assertEqual(len(expected_credentials), len(retrieved_credentials))
   63         retrieved_ids = [c['id'] for c in retrieved_credentials]
   64         for cred in expected_credentials:
   65             self.assertIn(cred['id'], retrieved_ids)
   66 
   67     def setUp(self):
   68         super(SqlCredential, self).setUp()
   69         self.useFixture(
   70             ksfixtures.KeyRepository(
   71                 self.config_fixture,
   72                 'credential',
   73                 credential_provider.MAX_ACTIVE_KEYS
   74             )
   75         )
   76         self.credentials = []
   77         self.user_credentials = []
   78         # setup 3 credentials with random user ids
   79         for _ in range(3):
   80             cred = self._create_credential_with_user_id()
   81             self.user_credentials.append(cred)
   82             self.credentials.append(cred)
   83         # setup 3 credentials with specific user ids
   84         for _ in range(3):
   85             cred = self._create_credential_with_user_id(self.user_foo['id'])
   86             self.user_credentials.append(cred)
   87             self.credentials.append(cred)
   88 
   89     def test_backend_credential_sql_hints_none(self):
   90         credentials = PROVIDERS.credential_api.list_credentials(hints=None)
   91         self._validate_credential_list(credentials, self.user_credentials)
   92 
   93     def test_backend_credential_sql_no_hints(self):
   94         credentials = PROVIDERS.credential_api.list_credentials()
   95         self._validate_credential_list(credentials, self.user_credentials)
   96 
   97     def test_backend_credential_sql_encrypted_string(self):
   98         cred_dict = {
   99             'id': uuid.uuid4().hex,
  100             'type': uuid.uuid4().hex,
  101             'hash': uuid.uuid4().hex,
  102             'encrypted_blob': b'randomdata'
  103         }
  104         ref = credential_sql.CredentialModel.from_dict(cred_dict)
  105         # Make sure CredentialModel is handing over a text string
  106         # to the database. To avoid encoding issues
  107         self.assertIsInstance(ref.encrypted_blob, str)
  108 
  109     def test_credential_limits(self):
  110         config_fixture_ = self.user = self.useFixture(config_fixture.Config())
  111         config_fixture_.config(group='credential', user_limit=4)
  112         self._create_credential_with_user_id(self.user_foo['id'])
  113         self.assertRaises(exception.CredentialLimitExceeded,
  114                           self._create_credential_with_user_id,
  115                           self.user_foo['id'])