From c0a495c910113b98797f4b867c8f0b4a672252a6 Mon Sep 17 00:00:00 2001 From: Min Pae Date: Tue, 21 Apr 2015 09:35:37 -0700 Subject: [PATCH] added unit tests for cue.common.context - added unit tests for cue.common.context - also modified cue.common.context to pass some arguments to parent class that were previously omitted Change-Id: I8f07be5567923284c0aa7505b7af0182d27b7079 --- cue/common/context.py | 21 ++++++++---- cue/tests/unit/common/test_context.py | 48 ++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/cue/common/context.py b/cue/common/context.py index 4c21810b..7e4edc15 100644 --- a/cue/common/context.py +++ b/cue/common/context.py @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# Copyright 2014-2015 Hewlett-Packard Development Company, L.P. # # 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 @@ -11,6 +12,7 @@ # 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 oslo_context import context @@ -24,24 +26,29 @@ class RequestContext(context.RequestContext): is_public_api=False, domain_id=None, domain_name=None): """Stores several additional request parameters: + :param roles: :param domain_id: The ID of the domain. :param domain_name: The name of the domain. :param is_public_api: Specifies whether the request should be processed without authentication. """ + super(RequestContext, self).__init__(auth_token=auth_token, user=user, + tenant=tenant, domain=domain, + user_domain=user_domain, + project_domain=project_domain, + is_admin=is_admin, + read_only=read_only, + show_deleted=show_deleted, + request_id=request_id, + resource_uuid=resource_uuid, + overwrite=overwrite) + self.roles = roles or [] self.is_public_api = is_public_api self.domain_id = domain_id self.domain_name = domain_name - super(RequestContext, self).__init__(auth_token=auth_token, - user=user, tenant=tenant, - is_admin=is_admin, - read_only=read_only, - show_deleted=show_deleted, - request_id=request_id) - @property def project_id(self): return self.tenant diff --git a/cue/tests/unit/common/test_context.py b/cue/tests/unit/common/test_context.py index 1928435a..6cec8620 100644 --- a/cue/tests/unit/common/test_context.py +++ b/cue/tests/unit/common/test_context.py @@ -1,4 +1,5 @@ -# Copyright 2014, Hewlett-Packard +# -*- coding: utf-8 -*- +# Copyright 2014-2015 Hewlett-Packard Development Company, L.P. # # 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 @@ -12,12 +13,51 @@ # License for the specific language governing permissions and limitations # under the License. +import uuid + from cue.common import context from cue.tests.unit import base -class TestConfig(base.UnitTestCase): +class TestContext(base.UnitTestCase): - def test_sanity(self): + def test_context(self): + original_context = { + 'auth_token': str(uuid.uuid4()), + 'user': str(uuid.uuid4()), + 'tenant': str(uuid.uuid4()), + 'domain': str(uuid.uuid4()), + 'user_domain': str(uuid.uuid4()), + 'project_domain': str(uuid.uuid4()), + 'is_admin': False, + 'read_only': False, + 'show_deleted': False, + 'request_id': str(uuid.uuid4()), + 'resource_uuid': str(uuid.uuid4()), + 'roles': str(uuid.uuid4()), + 'is_public_api': False, + 'domain_id': str(uuid.uuid4()), + 'domain_name': str(uuid.uuid4()) + } + + req_context = context.RequestContext(**original_context) + context_dict = req_context.to_dict() + + # user_identity is an attribute generated by oslo_context based on + # inputs received at context construction time, rather than duplicate + # that logic here to verify it, don't check it + del context_dict['user_identity'] + + self.assertDictEqual(original_context, context_dict) + + def test_tenant_id(self): + tenant_id = str(uuid.uuid4()) req_context = context.RequestContext() - req_context.to_dict() + req_context.tenant_id = tenant_id + self.assertEqual(tenant_id, req_context.tenant_id) + + def test_user_id(self): + user_id = str(uuid.uuid4()) + req_context = context.RequestContext() + req_context.user_id = user_id + self.assertEqual(user_id, req_context.user_id)