# 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.

from senlin.common import context
from senlin.tests.unit.common import base


class TestRequestContext(base.SenlinTestCase):

    def setUp(self):
        self.ctx = {
            'auth_token': '123',
            'auth_token_info': {'123info': 'woop'},
            'user': 'fooUser',
            'user_name': 'mick',
            'user_domain': 'user-domain',
            'user_domain_name': 'user-domain-name',
            'domain': 'domain-id',
            'domain_name': 'this domain',
            'project': 'project-id',
            'project_name': 'a project',
            'project_domain': 'project-domain-id',
            'project_domain_name': 'a project domain',
            'is_admin': False,
            'is_admin_project': True,
            'password': 'foo',
            'show_deleted': False,
            'read_only': False,
            'roles': ['arole', 'notadmin'],
            'auth_url': 'http://xyz',
            'trusts': None,
            'region_name': 'regionOne'
        }

        super(TestRequestContext, self).setUp()

    def test_request_context_init(self):
        ctx = context.RequestContext(
            auth_token=self.ctx.get('auth_token'),
            auth_token_info=self.ctx.get('auth_token_info'),
            user=self.ctx.get('user'),
            user_name=self.ctx.get('user_name'),
            user_domain=self.ctx.get('user_domain'),
            user_domain_name=self.ctx.get('user_domain_name'),
            domain=self.ctx.get('domain'),
            domain_name=self.ctx.get('domain_name'),
            project_domain=self.ctx.get('project_domain'),
            project_domain_name=self.ctx.get('project_domain_name'),
            project=self.ctx.get('project'),
            project_name=self.ctx.get('project_name'),
            is_admin=self.ctx.get('is_admin'),
            password=self.ctx.get('password'),
            show_deleted=self.ctx.get('show_deleted'),
            roles=self.ctx.get('roles'),
            auth_url=self.ctx.get('auth_url'),
            trusts=self.ctx.get('trusts'),
            region_name=self.ctx.get('region_name'))

        ctx_dict = ctx.to_dict()
        for k, v in self.ctx.items():
            self.assertEqual(v, ctx_dict.get(k))

    def test_request_context_from_dict(self):
        ctx = context.RequestContext.from_dict(self.ctx)

        ctx_dict = ctx.to_dict()
        for k, v in self.ctx.items():
            self.assertEqual(v, ctx_dict.get(k))

    def test_request_context_update(self):
        ctx = context.RequestContext.from_dict(self.ctx)

        for k in self.ctx:
            self.assertEqual(self.ctx.get(k), ctx.to_dict().get(k))
            override = '%s_override' % k
            setattr(ctx, k, override)
            self.assertEqual(override, ctx.to_dict().get(k))

    def test_get_admin_context(self):
        ctx1 = context.get_admin_context()
        self.assertTrue(ctx1.is_admin)

@@ -732,20 +732,25 @@
 uth_
-token': '123
+url': 'http://xyz
 ',%0A 
@@ -804,39 +804,8 @@
 '%7D,%0A
-            'user': 'fooUser',%0A
     
@@ -837,50 +837,8 @@
 k',%0A
-            'user_domain': 'user-domain',%0A
     
@@ -889,85 +889,8 @@
 e',%0A
-            'domain': 'domain-id',%0A            'domain_name': 'this domain',%0A
     
@@ -967,59 +967,8 @@
 t',%0A
-            'project_domain': 'project-domain-id',%0A
     
@@ -1035,310 +1035,193 @@
    '
-is_admin': False,%0A            'is_admin_project': True,%0A            'password': 'foo',%0A            'show_deleted': False,%0A            'read_only': False,%0A            'roles': %5B'arole', 'notadmin'%5D,%0A            'auth_url': 'http://xyz',%0A            'trusts': None,%0A            'region_name': 'regionOne'
+domain_name': 'this domain',%0A            'trusts': None,%0A            'region_name': 'regionOne',%0A            'password': 'foo',%0A            'is_admin': False  # needed for tests to work
 %0A   
@@ -1373,21 +1373,19 @@
    auth_
-token
+url
 =self.ct
@@ -1396,21 +1396,19 @@
 t('auth_
-token
+url
 '),%0A    
@@ -1468,47 +1468,8 @@
 '),%0A
-            user=self.ctx.get('user'),%0A
     
@@ -1517,61 +1517,8 @@
 '),%0A
-            user_domain=self.ctx.get('user_domain'),%0A
     
@@ -1588,22 +1588,23 @@
         
-domain
+project
 =self.ct
@@ -1610,22 +1610,23 @@
 tx.get('
-domain
+project
 '),%0A    
@@ -1633,22 +1633,23 @@
         
-domain
+project
 _name=se
@@ -1660,22 +1660,23 @@
 tx.get('
-domain
+project
 _name'),
@@ -1702,16 +1702,21 @@
 t_domain
+_name
 =self.ct
@@ -1736,16 +1736,21 @@
 t_domain
+_name
 '),%0A    
@@ -1753,32 +1753,24 @@
             
-project_
 domain_name=
@@ -1779,32 +1779,24 @@
 lf.ctx.get('
-project_
 domain_name'
@@ -1802,39 +1802,38 @@
 '),%0A            
-project
+trusts
 =self.ctx.get('p
@@ -1823,39 +1823,38 @@
 s=self.ctx.get('
-project
+trusts
 '),%0A            
@@ -1845,39 +1845,38 @@
 '),%0A            
-project
+region
 _name=self.ctx.g
@@ -1871,39 +1871,38 @@
 e=self.ctx.get('
-project
+region
 _name'),%0A       
@@ -1898,55 +1898,8 @@
 '),%0A
-            is_admin=self.ctx.get('is_admin'),%0A
     
@@ -1953,28 +1953,24 @@
         
-show_deleted
+is_admin
 =self.ct
@@ -1980,207 +1980,45 @@
 et('
-show_deleted'),%0A            roles=self.ctx.get('roles'),%0A            auth_url=self.ctx.get('auth_url'),%0A            trusts=self.ctx.get('trusts'),%0A            region_name=self.ctx.get('region_name'))
+is_admin'))  # need for tests to work
 %0A%0A  

Simplify context tests to senlin only attributes