# Copyright 2015 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import fixtures
from oslo_config import cfg

from tempest.common import credentials_factory as credentials
from tempest import config
from tempest.tests import base
from tempest.tests import fake_config


class TestAltAvailable(base.TestCase):

    identity_version = 'v2'

    def setUp(self):
        super(TestAltAvailable, self).setUp()
        self.useFixture(fake_config.ConfigFixture())
        self.patchobject(config, 'TempestConfigPrivate',
                         fake_config.FakePrivate)

    def run_test(self, dynamic_creds, use_accounts_file, creds):

        cfg.CONF.set_default('use_dynamic_credentials',
                             dynamic_creds, group='auth')
        if use_accounts_file:
            accounts = [dict(username="u%s" % ii,
                             project_name="t%s" % ii,
                             password="p") for ii in creds]
            self.useFixture(fixtures.MockPatch(
                'tempest.lib.common.preprov_creds.read_accounts_yaml',
                return_value=accounts))
            cfg.CONF.set_default('test_accounts_file',
                                 use_accounts_file, group='auth')
            self.useFixture(fixtures.MockPatch('os.path.isfile',
                                               return_value=True))
        else:
            self.useFixture(fixtures.MockPatch('os.path.isfile',
                                               return_value=False))
        expected = len(set(creds)) > 1 or dynamic_creds
        observed = credentials.is_alt_available(
            identity_version=self.identity_version)
        self.assertEqual(expected, observed)

    # Dynamic credentials implies alt so only one test case for True
    def test__dynamic_creds__accounts_file__one_user(self):
        self.run_test(dynamic_creds=True,
                      use_accounts_file=False,
                      creds=['1', '2'])

    def test__no_dynamic_creds__accounts_file__one_user(self):
        self.run_test(dynamic_creds=False,
                      use_accounts_file=True,
                      creds=['1'])

    def test__no_dynamic_creds__accounts_file__two_users(self):
        self.run_test(dynamic_creds=False,
                      use_accounts_file=True,
                      creds=['1', '2'])

    def test__no_dynamic_creds__accounts_file__two_users_identical(self):
        self.run_test(dynamic_creds=False,
                      use_accounts_file=True,
                      creds=['1', '1'])


class TestAltAvailableV3(TestAltAvailable):

    identity_version = 'v3'
