#    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 copy
import json
import uuid

import six

from keystoneclient.auth import token_endpoint
from keystoneclient import exceptions
from keystoneclient import session
from keystoneclient.tests.unit.v3 import client_fixtures
from keystoneclient.tests.unit.v3 import utils
from keystoneclient.v3 import client


class KeystoneClientTest(utils.TestCase):

    def test_unscoped_init(self):
        token = client_fixtures.unscoped_token()
        self.stub_auth(json=token)

        c = client.Client(user_domain_name=token.user_domain_name,
                          username=token.user_name,
                          password='password',
                          auth_url=self.TEST_URL)
        self.assertIsNotNone(c.auth_ref)
        self.assertFalse(c.auth_ref.domain_scoped)
        self.assertFalse(c.auth_ref.project_scoped)
        self.assertEqual(token.user_id, c.auth_user_id)
        self.assertFalse(c.has_service_catalog())

        self.assertEqual(token.user_id, c.get_user_id(session=None))
        self.assertIsNone(c.get_project_id(session=None))

    def test_domain_scoped_init(self):
        token = client_fixtures.domain_scoped_token()
        self.stub_auth(json=token)

        c = client.Client(user_id=token.user_id,
                          password='password',
                          domain_name=token.domain_name,
                          auth_url=self.TEST_URL)
        self.assertIsNotNone(c.auth_ref)
        self.assertTrue(c.auth_ref.domain_scoped)
        self.assertFalse(c.auth_ref.project_scoped)
        self.assertEqual(token.user_id, c.auth_user_id)
        self.assertEqual(token.domain_id, c.auth_domain_id)

    def test_project_scoped_init(self):
        token = client_fixtures.project_scoped_token()
        self.stub_auth(json=token),

        c = client.Client(user_id=token.user_id,
                          password='password',
                          user_domain_name=token.user_domain_name,
                          project_name=token.project_name,
                          auth_url=self.TEST_URL)
        self.assertIsNotNone(c.auth_ref)
        self.assertFalse(c.auth_ref.domain_scoped)
        self.assertTrue(c.auth_ref.project_scoped)
        self.assertEqual(token.user_id, c.auth_user_id)
        self.assertEqual(token.project_id, c.auth_tenant_id)
        self.assertEqual(token.user_id, c.get_user_id(session=None))
        self.assertEqual(token.project_id, c.get_project_id(session=None))

    def test_auth_ref_load(self):
        token = client_fixtures.project_scoped_token()
        self.stub_auth(json=token)

        c = client.Client(user_id=token.user_id,
                          password='password',
                          project_id=token.project_id,
                          auth_url=self.TEST_URL)
        cache = json.dumps(c.auth_ref)
        new_client = client.Client(auth_ref=json.loads(cache))
        self.assertIsNotNone(new_client.auth_ref)
        self.assertFalse(new_client.auth_ref.domain_scoped)
        self.assertTrue(new_client.auth_ref.project_scoped)
        self.assertEqual(token.user_name, new_client.username)
        self.assertIsNone(new_client.password)
        self.assertEqual(new_client.management_url,
                         'http://admin:35357/v3')

    def test_auth_ref_load_with_overridden_arguments(self):
        new_auth_url = 'https://newkeystone.com/v3'

        user_id = uuid.uuid4().hex
        user_name = uuid.uuid4().hex
        project_id = uuid.uuid4().hex

        first = client_fixtures.project_scoped_token(user_id=user_id,
                                                     user_name=user_name,
                                                     project_id=project_id)
        second = client_fixtures.project_scoped_token(user_id=user_id,
                                                      user_name=user_name,
                                                      project_id=project_id)
        self.stub_auth(json=first)
        self.stub_auth(json=second, base_url=new_auth_url)

        c = client.Client(user_id=user_id,
                          password='password',
                          project_id=project_id,
                          auth_url=self.TEST_URL)
        cache = json.dumps(c.auth_ref)
        new_client = client.Client(auth_ref=json.loads(cache),
                                   auth_url=new_auth_url)
        self.assertIsNotNone(new_client.auth_ref)
        self.assertFalse(new_client.auth_ref.domain_scoped)
        self.assertTrue(new_client.auth_ref.project_scoped)
        self.assertEqual(new_auth_url, new_client.auth_url)
        self.assertEqual(user_name, new_client.username)
        self.assertIsNone(new_client.password)
        self.assertEqual(new_client.management_url,
                         'http://admin:35357/v3')

    def test_trust_init(self):
        token = client_fixtures.trust_token()
        self.stub_auth(json=token)

        c = client.Client(user_domain_name=token.user_domain_name,
                          username=token.user_name,
                          password='password',
                          auth_url=self.TEST_URL,
                          trust_id=token.trust_id)
        self.assertIsNotNone(c.auth_ref)
        self.assertFalse(c.auth_ref.domain_scoped)
        self.assertFalse(c.auth_ref.project_scoped)