From 11fc21fa26d1641b86e5df6cb2068222623c0e04 Mon Sep 17 00:00:00 2001 From: Anantha Arunachalam Date: Thu, 28 May 2015 15:52:54 -0400 Subject: [PATCH 1/4] Added basic getter for tenant and user details --- openstackinabox/models/keystone/model.py | 35 +++++++++++++++++ openstackinabox/tests/test_model_basic.py | 47 +++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 openstackinabox/tests/test_model_basic.py diff --git a/openstackinabox/models/keystone/model.py b/openstackinabox/models/keystone/model.py index b02a43a..7493a6e 100644 --- a/openstackinabox/models/keystone/model.py +++ b/openstackinabox/models/keystone/model.py @@ -305,6 +305,37 @@ def bool_to_database(value): return 1 return 0 + def add_admin_tenant_details(self): + self.__admin_tenant_args = { + 'name': 'system', + 'description': 'system administrator', + } + + def add_admin_user_details(self): + self.__admin_user_args = { + 'tenantid': self.__admin_tenant_id, + 'username': 'system', + 'email': 'system@stackinabox', + 'password': 'stackinabox', + 'apikey': '537461636b496e41426f78', + } + + @property + def get_admin_tenant_details(self): + return self.__admin_tenant_args + + @property + def get_admin_user_details(self): + return self.__admin_user_args + + def get_admin_tenant_details_by_id(self, tenantid): + tenant_details = self.get_tenant_by_id(self, tenantid) + return tenant_details + + def get_admin_user_details_by_id(self, tenantid, userid): + user_details = self.get_user_by_id(self, tenantid, userid) + return user_details + def init_database(self): self.log_info('Initializing database') dbcursor = self.database.cursor() @@ -314,11 +345,15 @@ def init_database(self): # Create an admin user and add the admin token to that user self.__admin_tenant_id = self.add_tenant('system', 'system administrator') + self.add_admin_tenant_details() + self.__admin_user_id = self.add_user(self.__admin_tenant_id, 'system', 'system@stackinabox', 'stackinabox', '537461636b496e41426f78') + self.add_admin_user_details() + roles = [ KeystoneModel.IDENTITY_ADMIN_ROLE, KeystoneModel.IDENTITY_VIEWER_ROLE, diff --git a/openstackinabox/tests/test_model_basic.py b/openstackinabox/tests/test_model_basic.py new file mode 100644 index 0000000..99db384 --- /dev/null +++ b/openstackinabox/tests/test_model_basic.py @@ -0,0 +1,47 @@ +""" +Stack-In-A-Box: Basic Test +""" +import json +import unittest + +import httpretty +import mock +import requests +import stackinabox.util_httpretty +from stackinabox.stack import StackInABox + +from openstackinabox.models.keystone.model import KeystoneModel +from openstackinabox.services.keystone import KeystoneV2Service + + +@httpretty.activate +class TestKeystoneModel(unittest.TestCase): + + def setUp(self): + super(TestKeystoneModel, self).setUp() + self.keystone = KeystoneV2Service() + self.headers = { + 'x-auth-token': self.keystone.model.get_admin_token() + } + StackInABox.register_service(self.keystone) + + def tearDown(self): + super(TestKeystoneModel, self).tearDown() + StackInABox.reset_services() + + def test_keystone_set_model(self): + with self.assertRaises(TypeError): + self.keystone.model = None + + self.keystone.model = KeystoneModel() + + def test_add_user(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + tenant_details = self.keystone.model.get_admin_tenant_details + self.assertEqual(tenant_details['name'], 'system') + self.assertEqual(tenant_details['description'], 'system administrator') + user_details = self.keystone.model.get_admin_user_details + self.assertEqual(user_details['username'], 'system') + self.assertEqual(user_details['email'], 'system@stackinabox') + self.assertEqual(user_details['password'], 'stackinabox') + self.assertEqual(user_details['apikey'], '537461636b496e41426f78') From a9c4d8357ed070ddf97ab9123fd89e5e2dcb8d3c Mon Sep 17 00:00:00 2001 From: Anantha Arunachalam Date: Thu, 28 May 2015 18:03:17 -0400 Subject: [PATCH 2/4] Get Admin User Implemented --- openstackinabox/services/keystone/v2.py | 67 ++++ openstackinabox/tests/test_get_admin_user.py | 315 +++++++++++++++++++ 2 files changed, 382 insertions(+) create mode 100644 openstackinabox/tests/test_get_admin_user.py diff --git a/openstackinabox/services/keystone/v2.py b/openstackinabox/services/keystone/v2.py index ab79148..3becea0 100644 --- a/openstackinabox/services/keystone/v2.py +++ b/openstackinabox/services/keystone/v2.py @@ -38,6 +38,10 @@ class KeystoneV2Service(BaseService): '^\/users\/{0}/OS-KSADM/credentials$' .format(USER_ID_REGEX)) + USER_ID_ADMIN_PATH_REGEX = re.compile( + '^\/users\/{0}/RAX-AUTH/admins$' + .format(USER_ID_REGEX)) + @staticmethod def get_user_id_from_path(uri_path): uri_matcher = None @@ -45,6 +49,7 @@ def get_user_id_from_path(uri_path): regexes = [ KeystoneV2Service.USER_ID_PATH_REGEX, KeystoneV2Service.USER_ID_KSADM_CREDENTIAL_PATH_REGEX, + KeystoneV2Service.USER_ID_ADMIN_PATH_REGEX, ] for r in regexes: @@ -81,6 +86,9 @@ def __init__(self): self.register(BaseService.POST, KeystoneV2Service.USER_ID_KSADM_CREDENTIAL_PATH_REGEX, KeystoneV2Service.handle_add_credentials_to_user) + self.register(BaseService.GET, + KeystoneV2Service.USER_ID_ADMIN_PATH_REGEX, + KeystoneV2Service.handle_get_admin_user) self.log_info('initialized') @property @@ -579,3 +587,62 @@ def handle_add_credentials_to_user(self, request, uri, headers): return (503, headers, 'Server error') return (201, headers, '') + + def handle_get_admin_user(self, request, uri, headers): + ''' + 200 -> OK + 400 -> Bad Request + 403 -> Forbidden + 404 -> Not Found + 405 -> Invalid Method + 413 -> Over Limit + 415 -> Bad Media Type + 503 -> Service Fault + + No body + + Response + { + "users": [ + { + "RAX-AUTH:defaultRegion": , + "RAX-AUTH:domainId": , + "email": + "enabled": True/False, + "id": , + "username": + } + ] + } + ''' + self.log_request(uri, request) + req_headers = request.headers + + user_data = self.helper_authenticate(req_headers, + headers, + True, + False) + if isinstance(user_data, tuple): + return user_data + + try: + user_id = KeystoneV2Service.get_user_id_from_path(uri) + self.log_debug('Lookup of user id {0} requested' + .format(user_id)) + + except Exception as ex: # pragma: no cover + self.log_exception('Failed to get user id from path') + return (400, headers, 'bad request') + + try: + user_info = self.model.get_user_by_id(user_data['tenantid'], + user_id) + except: + self.log_exception('failed to get user data') + return (404, headers, 'Not found') + + del user_info['password'] + del user_info['apikey'] + user_info['RAX-AUTH:DomainID'] = user_info['tenantid'] + + return (200, headers, json.dumps(user_info)) diff --git a/openstackinabox/tests/test_get_admin_user.py b/openstackinabox/tests/test_get_admin_user.py new file mode 100644 index 0000000..29677ba --- /dev/null +++ b/openstackinabox/tests/test_get_admin_user.py @@ -0,0 +1,315 @@ +""" +Stack-In-A-Box: Basic Test +""" +import json +import unittest + +import httpretty +import mock +import requests +import stackinabox.util_httpretty +from stackinabox.stack import StackInABox + +from openstackinabox.models.keystone.model import KeystoneModel +from openstackinabox.services.keystone import KeystoneV2Service + + +@httpretty.activate +class TestKeystoneV2GetAdmin(unittest.TestCase): + + def setUp(self): + super(TestKeystoneV2GetAdmin, self).setUp() + self.keystone = KeystoneV2Service() + + self.headers = { + 'x-auth-token': self.keystone.model.get_admin_token() + } + self.tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + self.user_info = { + 'user': { + 'username': 'trinity', + 'enabled': True, + 'email': 'trinity@theone.matrix', + 'password': 'Inl0veWithNeo' + } + } + self.user_info['user']['userid'] =\ + self.keystone.model.add_user(tenantid=self.tenant_id, + username=self.user_info['user'][ + 'username'], + email=self.user_info['user']['email'], + password=self.user_info['user'][ + 'password'], + enabled=self.user_info['user'][ + 'enabled']) + self.keystone.model.add_token(self.tenant_id, + self.user_info['user']['userid']) + self.keystone.model.add_user_role_by_rolename( + tenantid=self.tenant_id, + userid=self.user_info['user']['userid'], + rolename=self.keystone.model.IDENTITY_ADMIN_ROLE) + StackInABox.register_service(self.keystone) + + + def tearDown(self): + super(TestKeystoneV2GetAdmin, self).tearDown() + StackInABox.reset_services() + + @staticmethod + def get_userid_url(host, userid): + return 'http://{0}/keystone/v2.0/users/{1}/RAX-AUTH/admins'\ + .format(host, userid) + + def test_get_admin_user_basic(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + + user_data = self.keystone.model.get_token_by_userid( + self.user_info['user']['userid']) + + url = TestKeystoneV2GetAdmin.get_userid_url( + 'localhost', + self.user_info['user']['userid']) + + self.headers['x-auth-token'] = user_data['token'] + res = requests.get(url, + headers=self.headers, + data='') + self.assertEqual(res.status_code, 200) + + def test_get_admin_user_incorrect_request(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + + user_data = self.keystone.model.get_token_by_userid( + self.user_info['user']['userid']) + + url = TestKeystoneV2GetAdmin.get_userid_url( + 'localhost', + self.user_info['user']['userid']) + + res = requests.get(url, + headers=self.headers, + data='') + self.assertEqual(res.status_code, 404) + + def test_get_admin_user_no_token(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + + url = TestKeystoneV2GetAdmin.get_userid_url( + 'localhost', + self.user_info['user']['userid']) + + res = requests.get(url, headers=None, data='') + self.assertEqual(res.status_code, 403) + + def test_get_admin_user_invalid_token(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + + url = TestKeystoneV2GetAdmin.get_userid_url( + 'localhost', + self.user_info['user']['userid']) + self.headers['x-auth-token'] = 'new_token' + res = requests.get(url, + headers=self.headers, + data='') + self.assertEqual(res.status_code, 401) + + '''def test_get_admin_no_token(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + + res = requests.post('http://localhost/keystone/v2.0/users', + data=json_data) + self.assertEqual(res.status_code, 403) + + def test_user_add_invalid_token(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + json_data = json.dumps(self.user_info) + self.headers['x-auth-token'] = 'new_token' + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 401) + + def test_user_add_bad_request(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + del self.user_info['user']['username'] + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 400) + + def test_user_add_same_user(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + self.user_info['user']['username'] = 'tom' + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 409) + + def test_user_add_invalid_username(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + self.user_info['user']['username'] = 'trinity$' + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 400) + + def test_user_add_no_password(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + del self.user_info['user']['OS-KSADM:password'] + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 201) + + def test_user_add_invalid_password(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + self.user_info['user']['OS-KSADM:password'] = 'Inl0veWithNeo$' + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 400) + + def __fail_add_user(self, *args, **kwargs): + raise Exception('mock error') + + def test_user_add_failed(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + + with mock.patch( + 'openstackinabox.models.keystone.model.' + 'KeystoneModel.add_user') as mok_keystone_model: + mok_keystone_model.side_effect = Exception('mock error') + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 404) + + def test_user_add(self): + stackinabox.util_httpretty.httpretty_registration('localhost') + neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', + description='The One') + tom = self.keystone.model.add_user(neo_tenant_id, + 'tom', + 'tom@theone.matrix', + 'bluepill', + 'iamnottheone', + enabled=True) + self.keystone.model.add_user_role_by_rolename(neo_tenant_id, + tom, + 'identity:user-admin') + + self.keystone.model.add_token(neo_tenant_id, tom) + json_data = json.dumps(self.user_info) + user_data = self.keystone.model.get_token_by_userid(tom) + self.headers['x-auth-token'] = user_data['token'] + res = requests.post('http://localhost/keystone/v2.0/users', + headers=self.headers, + data=json_data) + self.assertEqual(res.status_code, 201) + + user_info = self.keystone.model.get_user_by_id(neo_tenant_id, + tom) + self.assertEqual(user_info['userid'], tom) + self.assertEqual(user_info['username'], 'tom') + self.assertEqual(user_info['email'], 'tom@theone.matrix') + self.assertEqual(user_info['password'], 'bluepill') + self.assertEqual(user_info['apikey'], 'iamnottheone') + self.assertTrue(user_info['enabled']) +''' \ No newline at end of file From ebdf04a724ff9b41321bb549c7aa32cde22271be Mon Sep 17 00:00:00 2001 From: Anantha Arunachalam Date: Thu, 28 May 2015 18:06:28 -0400 Subject: [PATCH 3/4] Get Admin User Implemented- with small corrections --- openstackinabox/tests/test_get_admin_user.py | 200 +------------------ 1 file changed, 1 insertion(+), 199 deletions(-) diff --git a/openstackinabox/tests/test_get_admin_user.py b/openstackinabox/tests/test_get_admin_user.py index 29677ba..51a75dc 100644 --- a/openstackinabox/tests/test_get_admin_user.py +++ b/openstackinabox/tests/test_get_admin_user.py @@ -114,202 +114,4 @@ def test_get_admin_user_invalid_token(self): data='') self.assertEqual(res.status_code, 401) - '''def test_get_admin_no_token(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - - res = requests.post('http://localhost/keystone/v2.0/users', - data=json_data) - self.assertEqual(res.status_code, 403) - - def test_user_add_invalid_token(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - json_data = json.dumps(self.user_info) - self.headers['x-auth-token'] = 'new_token' - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 401) - - def test_user_add_bad_request(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - del self.user_info['user']['username'] - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 400) - - def test_user_add_same_user(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - self.user_info['user']['username'] = 'tom' - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 409) - - def test_user_add_invalid_username(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - self.user_info['user']['username'] = 'trinity$' - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 400) - - def test_user_add_no_password(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - del self.user_info['user']['OS-KSADM:password'] - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 201) - - def test_user_add_invalid_password(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - self.user_info['user']['OS-KSADM:password'] = 'Inl0veWithNeo$' - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 400) - - def __fail_add_user(self, *args, **kwargs): - raise Exception('mock error') - - def test_user_add_failed(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - - with mock.patch( - 'openstackinabox.models.keystone.model.' - 'KeystoneModel.add_user') as mok_keystone_model: - mok_keystone_model.side_effect = Exception('mock error') - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 404) - - def test_user_add(self): - stackinabox.util_httpretty.httpretty_registration('localhost') - neo_tenant_id = self.keystone.model.add_tenant(tenantname='neo', - description='The One') - tom = self.keystone.model.add_user(neo_tenant_id, - 'tom', - 'tom@theone.matrix', - 'bluepill', - 'iamnottheone', - enabled=True) - self.keystone.model.add_user_role_by_rolename(neo_tenant_id, - tom, - 'identity:user-admin') - - self.keystone.model.add_token(neo_tenant_id, tom) - json_data = json.dumps(self.user_info) - user_data = self.keystone.model.get_token_by_userid(tom) - self.headers['x-auth-token'] = user_data['token'] - res = requests.post('http://localhost/keystone/v2.0/users', - headers=self.headers, - data=json_data) - self.assertEqual(res.status_code, 201) - - user_info = self.keystone.model.get_user_by_id(neo_tenant_id, - tom) - self.assertEqual(user_info['userid'], tom) - self.assertEqual(user_info['username'], 'tom') - self.assertEqual(user_info['email'], 'tom@theone.matrix') - self.assertEqual(user_info['password'], 'bluepill') - self.assertEqual(user_info['apikey'], 'iamnottheone') - self.assertTrue(user_info['enabled']) -''' \ No newline at end of file + \ No newline at end of file From 78aeca98bbeefac162612ab66f65653bb4519bae Mon Sep 17 00:00:00 2001 From: Anantha Arunachalam Date: Fri, 29 May 2015 10:14:03 -0400 Subject: [PATCH 4/4] Get admin user implemented. Corrected Get User/Admin details --- openstackinabox/models/keystone/model.py | 14 ++------------ openstackinabox/tests/test_model_basic.py | 5 +++-- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/openstackinabox/models/keystone/model.py b/openstackinabox/models/keystone/model.py index 4a095d7..6a61adc 100644 --- a/openstackinabox/models/keystone/model.py +++ b/openstackinabox/models/keystone/model.py @@ -322,19 +322,11 @@ def add_admin_user_details(self): @property def get_admin_tenant_details(self): - return self.__admin_tenant_args + return self.get_tenant_by_id(self.__admin_tenant_id) @property def get_admin_user_details(self): - return self.__admin_user_args - - def get_admin_tenant_details_by_id(self, tenantid): - tenant_details = self.get_tenant_by_id(self, tenantid) - return tenant_details - - def get_admin_user_details_by_id(self, tenantid, userid): - user_details = self.get_user_by_id(self, tenantid, userid) - return user_details + return self.get_user_by_id(self.__admin_tenant_id, self.__admin_user_id) def init_database(self): self.log_info('Initializing database') @@ -345,14 +337,12 @@ def init_database(self): # Create an admin user and add the admin token to that user self.__admin_tenant_id = self.add_tenant('system', 'system administrator') - self.add_admin_tenant_details() self.__admin_user_id = self.add_user(self.__admin_tenant_id, 'system', 'system@stackinabox', 'stackinabox', '537461636b496e41426f78') - self.add_admin_user_details() roles = [ KeystoneModel.IDENTITY_ADMIN_ROLE, diff --git a/openstackinabox/tests/test_model_basic.py b/openstackinabox/tests/test_model_basic.py index 99db384..f447ae4 100644 --- a/openstackinabox/tests/test_model_basic.py +++ b/openstackinabox/tests/test_model_basic.py @@ -23,7 +23,6 @@ def setUp(self): self.headers = { 'x-auth-token': self.keystone.model.get_admin_token() } - StackInABox.register_service(self.keystone) def tearDown(self): super(TestKeystoneModel, self).tearDown() @@ -35,11 +34,13 @@ def test_keystone_set_model(self): self.keystone.model = KeystoneModel() - def test_add_user(self): + def test_get_tenant_details(self): stackinabox.util_httpretty.httpretty_registration('localhost') tenant_details = self.keystone.model.get_admin_tenant_details self.assertEqual(tenant_details['name'], 'system') self.assertEqual(tenant_details['description'], 'system administrator') + + def test_get_user_details(self): user_details = self.keystone.model.get_admin_user_details self.assertEqual(user_details['username'], 'system') self.assertEqual(user_details['email'], 'system@stackinabox')