Improve mocking in REST API tests (test_keystone)

The current REST API tests have a lot of rooms to be improved like:
- mocking the whole module of the API wrapper

Change-Id: I799b0bce11bb06f6da64b8cc76f490f644fd457f
Partial-Bug: #1752917
This commit is contained in:
Akihiro Motoki 2018-03-12 06:56:07 +09:00
parent cd06fe813e
commit dd7e281844
2 changed files with 251 additions and 220 deletions

View File

@ -216,7 +216,7 @@ def domain_get(request, domain_id):
@profiler.trace @profiler.trace
def domain_delete(request, domain_id): def domain_delete(request, domain_id):
manager = keystoneclient(request, admin=True).domains manager = keystoneclient(request, admin=True).domains
return manager.delete(domain_id) manager.delete(domain_id)
@profiler.trace @profiler.trace
@ -348,7 +348,7 @@ def tenant_get(request, project, admin=True):
@profiler.trace @profiler.trace
def tenant_delete(request, project): def tenant_delete(request, project):
manager = VERSIONS.get_project_manager(request, admin=True) manager = VERSIONS.get_project_manager(request, admin=True)
return manager.delete(project) manager.delete(project)
@profiler.trace @profiler.trace
@ -451,7 +451,7 @@ def user_create(request, name=None, email=None, password=None, project=None,
@profiler.trace @profiler.trace
def user_delete(request, user_id): def user_delete(request, user_id):
return keystoneclient(request, admin=True).users.delete(user_id) keystoneclient(request, admin=True).users.delete(user_id)
@profiler.trace @profiler.trace
@ -513,9 +513,9 @@ def user_update(request, user, **data):
def user_update_enabled(request, user, enabled): def user_update_enabled(request, user, enabled):
manager = keystoneclient(request, admin=True).users manager = keystoneclient(request, admin=True).users
if VERSIONS.active < 3: if VERSIONS.active < 3:
return manager.update_enabled(user, enabled) manager.update_enabled(user, enabled)
else: else:
return manager.update(user, enabled=enabled) manager.update(user, enabled=enabled)
@profiler.trace @profiler.trace
@ -527,9 +527,9 @@ def user_update_password(request, user, password, admin=True):
manager = keystoneclient(request, admin=admin).users manager = keystoneclient(request, admin=admin).users
if VERSIONS.active < 3: if VERSIONS.active < 3:
return manager.update_password(user, password) manager.update_password(user, password)
else: else:
return manager.update(user, password=password) manager.update(user, password=password)
def user_verify_admin_password(request, admin_password): def user_verify_admin_password(request, admin_password):
@ -708,7 +708,7 @@ def role_update(request, role_id, name=None):
@profiler.trace @profiler.trace
def role_delete(request, role_id): def role_delete(request, role_id):
manager = keystoneclient(request, admin=True).roles manager = keystoneclient(request, admin=True).roles
return manager.delete(role_id) manager.delete(role_id)
@profiler.trace @profiler.trace
@ -806,10 +806,10 @@ def add_tenant_user_role(request, project=None, user=None, role=None,
"""Adds a role for a user on a tenant.""" """Adds a role for a user on a tenant."""
manager = keystoneclient(request, admin=True).roles manager = keystoneclient(request, admin=True).roles
if VERSIONS.active < 3: if VERSIONS.active < 3:
return manager.add_user_role(user, role, project) manager.add_user_role(user, role, project)
else: else:
return manager.grant(role, user=user, project=project, manager.grant(role, user=user, project=project,
group=group, domain=domain) group=group, domain=domain)
@profiler.trace @profiler.trace

View File

@ -17,6 +17,7 @@ import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six import six
from openstack_dashboard import api
from openstack_dashboard.api.rest import keystone from openstack_dashboard.api.rest import keystone
from openstack_dashboard.test import helpers as test from openstack_dashboard.test import helpers as test
@ -25,43 +26,43 @@ class KeystoneRestTestCase(test.TestCase):
# #
# Version # Version
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['get_version']})
def test_version_get(self, kc): def test_version_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.get_version.return_value = '2.0' self.mock_get_version.return_value = '2.0'
response = keystone.Version().get(request) response = keystone.Version().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"version": "2.0"}) self.assertEqual(response.json, {"version": "2.0"})
kc.get_version.assert_called_once_with() self.mock_get_version.assert_called_once_with()
# #
# Users # Users
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get']})
def test_user_get(self, kc): def test_user_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.user_get.return_value.to_dict.return_value = {'name': 'Ni!'} self.mock_user_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.User().get(request, 'the_id') response = keystone.User().get(request, 'the_id')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.user_get.assert_called_once_with(request, 'the_id') self.mock_user_get.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get']})
def test_user_get_current(self, kc): def test_user_get_current(self):
request = self.mock_rest_request(**{'user.id': 'current_id'}) request = self.mock_rest_request(**{'user.id': 'current_id'})
kc.user_get.return_value.to_dict.return_value = {'name': 'Ni!'} self.mock_user_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.User().get(request, 'current') response = keystone.User().get(request, 'current')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.user_get.assert_called_once_with(request, 'current_id') self.mock_user_get.assert_called_once_with(request, 'current_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_list']})
def test_user_get_list(self, kc): def test_user_get_list(self):
request = self.mock_rest_request(**{ request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'), 'session.get': mock.Mock(return_value='the_domain'),
'GET': {}, 'GET': {},
}) })
kc.user_list.return_value = [ self.mock_user_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
] ]
@ -69,18 +70,19 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) {"items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.user_list.assert_called_once_with(request, project=None, self.mock_user_list.assert_called_once_with(request, project=None,
domain='the_domain', group=None, domain='the_domain',
filters=None) group=None,
filters=None)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_list']})
def test_user_get_list_with_filters(self, kc): def test_user_get_list_with_filters(self):
filters = {'enabled': True} filters = {'enabled': True}
request = self.mock_rest_request(**{ request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'), 'session.get': mock.Mock(return_value='the_domain'),
'GET': dict(**filters), 'GET': dict(**filters),
}) })
kc.user_list.return_value = [ self.mock_user_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
] ]
@ -88,9 +90,10 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) {"items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.user_list.assert_called_once_with(request, project=None, self.mock_user_list.assert_called_once_with(request, project=None,
domain='the_domain', group=None, domain='the_domain',
filters=filters) group=None,
filters=filters)
def test_user_create_full(self): def test_user_create_full(self):
self._test_user_create( self._test_user_create(
@ -155,12 +158,13 @@ class KeystoneRestTestCase(test.TestCase):
} }
) )
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['get_default_domain',
def _test_user_create(self, supplied_body, add_user_call, kc): 'user_create']})
def _test_user_create(self, supplied_body, add_user_call):
request = self.mock_rest_request(body=supplied_body) request = self.mock_rest_request(body=supplied_body)
kc.get_default_domain.return_value = mock.Mock(**{'id': 'the_domain'}) self.mock_get_default_domain.return_value = \
kc.user_create.return_value.id = 'user123' mock.Mock(**{'id': 'the_domain'})
kc.user_create.return_value = mock.Mock(**{ self.mock_user_create.return_value = mock.Mock(**{
'id': 'user123', 'id': 'user123',
'to_dict.return_value': {'id': 'user123', 'name': 'bob'} 'to_dict.return_value': {'id': 'user123', 'name': 'bob'}
}) })
@ -171,115 +175,124 @@ class KeystoneRestTestCase(test.TestCase):
'/api/keystone/users/user123') '/api/keystone/users/user123')
self.assertEqual(response.json, self.assertEqual(response.json,
{"id": "user123", "name": "bob"}) {"id": "user123", "name": "bob"})
kc.user_create.assert_called_once_with(request, **add_user_call) self.mock_user_create.assert_called_once_with(request, **add_user_call)
self.mock_get_default_domain.assert_called_once_with(request)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_delete']})
def test_user_delete_many(self, kc): def test_user_delete_many(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
["id1", "id2", "id3"] ["id1", "id2", "id3"]
''') ''')
self.mock_user_delete.return_value = None
response = keystone.Users().delete(request) response = keystone.Users().delete(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_delete.assert_has_calls([ self.mock_user_delete.assert_has_calls([
mock.call(request, 'id1'), mock.call(request, 'id1'),
mock.call(request, 'id2'), mock.call(request, 'id2'),
mock.call(request, 'id3'), mock.call(request, 'id3'),
]) ])
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_delete']})
def test_user_delete(self, kc): def test_user_delete(self):
request = self.mock_rest_request() request = self.mock_rest_request()
self.mock_user_delete.return_value = None
response = keystone.User().delete(request, 'the_id') response = keystone.User().delete(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_delete.assert_called_once_with(request, 'the_id') self.mock_user_delete.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get',
def test_user_patch_password(self, kc): 'user_update_password']})
def test_user_patch_password(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"password": "sekrit"} {"password": "sekrit"}
''') ''')
user = keystone.User() user = keystone.User()
kc.user_get = mock.MagicMock(return_value=user) self.mock_user_get.return_value = mock.sentinel.user
self.mock_user_update_password.return_value = None
response = user.patch(request, 'user123') response = user.patch(request, 'user123')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_update_password.assert_called_once_with(request, self.mock_user_get.assert_called_once_with(request, 'user123')
user, self.mock_user_update_password.assert_called_once_with(
'sekrit') request, mock.sentinel.user, 'sekrit')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get',
def test_user_patch_enabled(self, kc): 'user_update_enabled']})
def test_user_patch_enabled(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"enabled": false} {"enabled": false}
''') ''')
user = keystone.User() user = keystone.User()
kc.user_get = mock.MagicMock(return_value=user) self.mock_user_get.return_value = mock.sentinel.user
self.mock_user_update_enabled.return_value = None
response = user.patch(request, 'user123') response = user.patch(request, 'user123')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_get.assert_called_once_with(request, 'user123') self.mock_user_get.assert_called_once_with(request, 'user123')
kc.user_update_enabled.assert_called_once_with(request, self.mock_user_update_enabled.assert_called_once_with(
user, request, mock.sentinel.user, False)
False)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get',
def test_user_patch_project(self, kc): 'user_update']})
def test_user_patch_project(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"project": "other123"} {"project": "other123"}
''') ''')
user = keystone.User() user = keystone.User()
kc.user_get = mock.MagicMock(return_value=user) self.mock_user_get.return_value = mock.sentinel.user
self.mock_user_update.return_value = self.users.first()
response = user.patch(request, 'user123') response = user.patch(request, 'user123')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_update.assert_called_once_with(request, self.mock_user_get.assert_called_once_with(request, 'user123')
user, self.mock_user_update.assert_called_once_with(
project='other123') request, mock.sentinel.user, project='other123')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['user_get',
def test_user_patch_multiple(self, kc): 'user_update']})
def test_user_patch_multiple(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"project": "other123", "name": "something"} {"project": "other123", "name": "something"}
''') ''')
user = keystone.User() user = keystone.User()
kc.user_get = mock.MagicMock(return_value=user) self.mock_user_get.return_value = mock.sentinel.user
self.mock_user_update.return_value = self.users.first()
response = user.patch(request, 'user123') response = user.patch(request, 'user123')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.user_update.assert_called_once_with(request, self.mock_user_get.assert_called_once_with(request, 'user123')
user, self.mock_user_update.assert_called_once_with(
project='other123', request, mock.sentinel.user, project='other123', name='something')
name='something')
# #
# Roles # Roles
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_get']})
def test_role_get(self, kc): def test_role_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.role_get.return_value.to_dict.return_value = {'name': 'Ni!'} self.mock_role_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Role().get(request, 'the_id') response = keystone.Role().get(request, 'the_id')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.role_get.assert_called_once_with(request, 'the_id') self.mock_role_get.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['get_default_role']})
def test_role_get_default(self, kc): def test_role_get_default(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.get_default_role.return_value.to_dict.return_value = {'name': 'Ni!'} ret_val_role = self.mock_get_default_role.return_value
ret_val_role.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Role().get(request, 'default') response = keystone.Role().get(request, 'default')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.get_default_role.assert_called_once_with(request) self.mock_get_default_role.assert_called_once_with(request)
kc.role_get.assert_not_called()
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_list']})
def test_role_get_list(self, kc): def test_role_get_list(self):
request = self.mock_rest_request(**{'GET': {}}) request = self.mock_rest_request(**{'GET': {}})
kc.role_list.return_value = [ self.mock_role_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
] ]
@ -287,13 +300,13 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) {"items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.role_list.assert_called_once_with(request) self.mock_role_list.assert_called_once_with(request)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['roles_for_user']})
def test_role_get_for_user(self, kc): def test_role_get_for_user(self):
request = self.mock_rest_request(**{'GET': {'user_id': 'user123', request = self.mock_rest_request(**{'GET': {'user_id': 'user123',
'project_id': 'project123'}}) 'project_id': 'project123'}})
kc.roles_for_user.return_value = [ self.mock_roles_for_user.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
] ]
@ -301,16 +314,16 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) {"items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.roles_for_user.assert_called_once_with(request, 'user123', self.mock_roles_for_user.assert_called_once_with(request, 'user123',
'project123') 'project123')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_create']})
def test_role_create(self, kc): def test_role_create(self):
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"name": "bob"} {"name": "bob"}
''') ''')
kc.role_create.return_value.id = 'role123' self.mock_role_create.return_value.id = 'role123'
kc.role_create.return_value.to_dict.return_value = { self.mock_role_create.return_value.to_dict.return_value = {
'id': 'role123', 'name': 'bob' 'id': 'role123', 'name': 'bob'
} }
@ -319,10 +332,11 @@ class KeystoneRestTestCase(test.TestCase):
self.assertEqual(response['location'], self.assertEqual(response['location'],
'/api/keystone/roles/role123') '/api/keystone/roles/role123')
self.assertEqual(response.json, {"id": "role123", "name": "bob"}) self.assertEqual(response.json, {"id": "role123", "name": "bob"})
kc.role_create.assert_called_once_with(request, 'bob') self.mock_role_create.assert_called_once_with(request, 'bob')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['add_tenant_user_role']})
def test_role_grant(self, kc): def test_role_grant(self):
self.mock_add_tenant_user_role.return_value = None
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"action": "grant", "data": {"user_id": "user123", {"action": "grant", "data": {"user_id": "user123",
"role_id": "role123", "project_id": "project123"}} "role_id": "role123", "project_id": "project123"}}
@ -331,11 +345,12 @@ class KeystoneRestTestCase(test.TestCase):
"user3") "user3")
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.add_tenant_user_role.assert_called_once_with(request, 'project1', self.mock_add_tenant_user_role.assert_called_once_with(
'user3', 'role2') request, 'project1', 'user3', 'role2')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_delete']})
def test_role_delete_many(self, kc): def test_role_delete_many(self):
self.mock_role_delete.return_value = None
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
["id1", "id2", "id3"] ["id1", "id2", "id3"]
''') ''')
@ -343,65 +358,70 @@ class KeystoneRestTestCase(test.TestCase):
response = keystone.Roles().delete(request) response = keystone.Roles().delete(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.role_delete.assert_has_calls([ self.mock_role_delete.assert_has_calls([
mock.call(request, 'id1'), mock.call(request, 'id1'),
mock.call(request, 'id2'), mock.call(request, 'id2'),
mock.call(request, 'id3'), mock.call(request, 'id3'),
]) ])
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_delete']})
def test_role_delete(self, kc): def test_role_delete(self):
self.mock_role_delete.return_value = None
request = self.mock_rest_request() request = self.mock_rest_request()
response = keystone.Role().delete(request, 'the_id') response = keystone.Role().delete(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.role_delete.assert_called_once_with(request, 'the_id') self.mock_role_delete.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['role_update']})
def test_role_patch(self, kc): def test_role_patch(self):
self.mock_role_update.return_value = self.roles.first()
request = self.mock_rest_request(body='{"name": "spam"}') request = self.mock_rest_request(body='{"name": "spam"}')
response = keystone.Role().patch(request, 'the_id') response = keystone.Role().patch(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.role_update.assert_called_once_with(request, self.mock_role_update.assert_called_once_with(request,
'the_id', 'the_id',
'spam') 'spam')
# #
# Domains # Domains
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['get_default_domain']})
def test_default_domain_get(self, kc): def test_default_domain_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
domain = api.base.APIDictWrapper({'id': 'the_id', 'name': 'the_name'})
self.mock_get_default_domain.return_value = domain
response = keystone.DefaultDomain().get(request) response = keystone.DefaultDomain().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {}) self.assertEqual(response.json, domain.to_dict())
self.mock_get_default_domain.assert_called_once_with(request)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_get']})
def test_domain_get(self, kc): def test_domain_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.domain_get.return_value.to_dict.return_value = {'name': 'Ni!'} ret_val_domain = self.mock_domain_get.return_value
ret_val_domain.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Domain().get(request, 'the_id') response = keystone.Domain().get(request, 'the_id')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.domain_get.assert_called_once_with(request, 'the_id') self.mock_domain_get.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['get_default_domain']})
def test_domain_get_default(self, kc): def test_domain_get_default(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.get_default_domain.return_value.to_dict.return_value = { self.mock_get_default_domain.return_value.to_dict.return_value = {
'name': 'Ni!' 'name': 'Ni!'
} }
response = keystone.Domain().get(request, 'default') response = keystone.Domain().get(request, 'default')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.get_default_domain.assert_called_once_with(request) self.mock_get_default_domain.assert_called_once_with(request)
kc.domain_get.assert_not_called()
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_list']})
def test_domain_get_list(self, kc): def test_domain_get_list(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.domain_list.return_value = [ self.mock_domain_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
] ]
@ -409,7 +429,7 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) {"items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.domain_list.assert_called_once_with(request) self.mock_domain_list.assert_called_once_with(request)
def test_domain_create_full(self): def test_domain_create_full(self):
self._test_domain_create( self._test_domain_create(
@ -430,11 +450,12 @@ class KeystoneRestTestCase(test.TestCase):
} }
) )
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_create']})
def _test_domain_create(self, supplied_body, expected_call, kc): def _test_domain_create(self, supplied_body, expected_call):
request = self.mock_rest_request(body=supplied_body) request = self.mock_rest_request(body=supplied_body)
kc.domain_create.return_value.id = 'domain123' ret_val_domain = self.mock_domain_create.return_value
kc.domain_create.return_value.to_dict.return_value = { ret_val_domain.id = 'domain123'
ret_val_domain.to_dict.return_value = {
'id': 'domain123', 'name': 'bob' 'id': 'domain123', 'name': 'bob'
} }
@ -443,11 +464,12 @@ class KeystoneRestTestCase(test.TestCase):
self.assertEqual(response['location'], self.assertEqual(response['location'],
'/api/keystone/domains/domain123') '/api/keystone/domains/domain123')
self.assertEqual(response.json, {"id": "domain123", "name": "bob"}) self.assertEqual(response.json, {"id": "domain123", "name": "bob"})
kc.domain_create.assert_called_once_with(request, 'bob', self.mock_domain_create.assert_called_once_with(request, 'bob',
**expected_call) **expected_call)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_delete']})
def test_domain_delete_many(self, kc): def test_domain_delete_many(self):
self.mock_domain_delete.return_value = None
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
["id1", "id2", "id3"] ["id1", "id2", "id3"]
''') ''')
@ -455,43 +477,46 @@ class KeystoneRestTestCase(test.TestCase):
response = keystone.Domains().delete(request) response = keystone.Domains().delete(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.domain_delete.assert_has_calls([ self.mock_domain_delete.assert_has_calls([
mock.call(request, 'id1'), mock.call(request, 'id1'),
mock.call(request, 'id2'), mock.call(request, 'id2'),
mock.call(request, 'id3'), mock.call(request, 'id3'),
]) ])
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_delete']})
def test_domain_delete(self, kc): def test_domain_delete(self):
self.mock_domain_delete.return_value = None
request = self.mock_rest_request() request = self.mock_rest_request()
response = keystone.Domain().delete(request, 'the_id') response = keystone.Domain().delete(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.domain_delete.assert_called_once_with(request, 'the_id') self.mock_domain_delete.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['domain_update']})
def test_domain_patch(self, kc): def test_domain_patch(self):
self.mock_domain_update.return_value = self.domains.first()
request = self.mock_rest_request(body='{"name": "spam"}') request = self.mock_rest_request(body='{"name": "spam"}')
response = keystone.Domain().patch(request, 'the_id') response = keystone.Domain().patch(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.domain_update.assert_called_once_with(request, self.mock_domain_update.assert_called_once_with(request,
'the_id', 'the_id',
name='spam', name='spam',
description=None, description=None,
enabled=None) enabled=None)
# #
# Projects # Projects
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_get']})
def test_project_get(self, kc): def test_project_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.tenant_get.return_value.to_dict.return_value = {'name': 'Ni!'} ret_val_tenant = self.mock_tenant_get.return_value
ret_val_tenant.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Project().get(request, 'the_id') response = keystone.Project().get(request, 'the_id')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "Ni!"}) self.assertEqual(response.json, {"name": "Ni!"})
kc.tenant_get.assert_called_once_with(request, 'the_id') self.mock_tenant_get.assert_called_once_with(request, 'the_id')
def test_project_get_list(self): def test_project_get_list(self):
self._test_project_get_list( self._test_project_get_list(
@ -538,10 +563,10 @@ class KeystoneRestTestCase(test.TestCase):
} }
) )
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_list']})
def _test_project_get_list(self, params, expected_call, kc): def _test_project_get_list(self, params, expected_call):
request = self.mock_rest_request(**{'GET': dict(**params)}) request = self.mock_rest_request(**{'GET': dict(**params)})
kc.tenant_list.return_value = ([ self.mock_tenant_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
], False) ], False)
@ -551,13 +576,13 @@ class KeystoneRestTestCase(test.TestCase):
self.assertEqual(response.json, self.assertEqual(response.json,
{"has_more": False, {"has_more": False,
"items": [{"name": "Ni!"}, {"name": "Ptang!"}]}) "items": [{"name": "Ni!"}, {"name": "Ptang!"}]})
kc.tenant_list.assert_called_once_with(request, **expected_call) self.mock_tenant_list.assert_called_once_with(request, **expected_call)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_list']})
def test_project_get_list_with_filters(self, kc): def test_project_get_list_with_filters(self):
filters = {'name': 'Ni!'} filters = {'name': 'Ni!'}
request = self.mock_rest_request(**{'GET': dict(**filters)}) request = self.mock_rest_request(**{'GET': dict(**filters)})
kc.tenant_list.return_value = ([ self.mock_tenant_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}})
], False) ], False)
@ -567,10 +592,10 @@ class KeystoneRestTestCase(test.TestCase):
self.assertEqual(response.json, self.assertEqual(response.json,
{"has_more": False, {"has_more": False,
"items": [{"name": "Ni!"}, {"name": "Ni!"}]}) "items": [{"name": "Ni!"}, {"name": "Ni!"}]})
kc.tenant_list.assert_called_once_with(request, paginate=False, self.mock_tenant_list.assert_called_once_with(request, paginate=False,
marker=None, domain=None, marker=None, domain=None,
user=None, admin=True, user=None, admin=True,
filters=filters) filters=filters)
def test_project_create_full(self): def test_project_create_full(self):
self._test_project_create( self._test_project_create(
@ -594,11 +619,11 @@ class KeystoneRestTestCase(test.TestCase):
} }
) )
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_create']})
def _test_project_create(self, supplied_body, expected_call, kc): def _test_project_create(self, supplied_body, expected_call):
request = self.mock_rest_request(body=supplied_body) request = self.mock_rest_request(body=supplied_body)
kc.tenant_create.return_value.id = 'project123' self.mock_tenant_create.return_value.id = 'project123'
kc.tenant_create.return_value.to_dict.return_value = { self.mock_tenant_create.return_value.to_dict.return_value = {
'id': 'project123', 'name': 'bob' 'id': 'project123', 'name': 'bob'
} }
@ -608,11 +633,12 @@ class KeystoneRestTestCase(test.TestCase):
'/api/keystone/projects/project123') '/api/keystone/projects/project123')
self.assertEqual(response.json, self.assertEqual(response.json,
{"id": "project123", "name": "bob"}) {"id": "project123", "name": "bob"})
kc.tenant_create.assert_called_once_with(request, 'bob', self.mock_tenant_create.assert_called_once_with(request, 'bob',
**expected_call) **expected_call)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_delete']})
def test_project_delete_many(self, kc): def test_project_delete_many(self):
self.mock_tenant_delete.return_value = None
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
["id1", "id2", "id3"] ["id1", "id2", "id3"]
''') ''')
@ -620,42 +646,43 @@ class KeystoneRestTestCase(test.TestCase):
response = keystone.Projects().delete(request) response = keystone.Projects().delete(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.tenant_delete.assert_has_calls([ self.mock_tenant_delete.assert_has_calls([
mock.call(request, 'id1'), mock.call(request, 'id1'),
mock.call(request, 'id2'), mock.call(request, 'id2'),
mock.call(request, 'id3'), mock.call(request, 'id3'),
]) ])
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_delete']})
def test_project_delete(self, kc): def test_project_delete(self):
self.mock_tenant_delete.return_value = None
request = self.mock_rest_request() request = self.mock_rest_request()
response = keystone.Project().delete(request, 'the_id') response = keystone.Project().delete(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.tenant_delete.assert_called_once_with(request, 'the_id') self.mock_tenant_delete.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['tenant_update']})
def test_project_patch(self, kc): def test_project_patch(self):
# nothing in the Horizon code documents what additional parameters are # nothing in the Horizon code documents what additional parameters are
# allowed, so we'll just assume GIGO # allowed, so we'll just assume GIGO
self.mock_tenant_update.return_value = self.tenants.first()
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
{"name": "spam", "domain_id": "domain123", "foo": "bar"} {"name": "spam", "domain_id": "domain123", "foo": "bar"}
''') ''')
response = keystone.Project().patch(request, 'spam123') response = keystone.Project().patch(request, 'spam123')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.tenant_update.assert_called_once_with(request, self.mock_tenant_update.assert_called_once_with(request,
'spam123', 'spam123',
name='spam', foo='bar', name='spam', foo='bar',
description=None, description=None,
domain='domain123', domain='domain123',
enabled=None) enabled=None)
# #
# Service Catalog # Service Catalog
# #
@mock.patch.object(keystone.api, 'keystone') def test_service_catalog_get(self):
def test_service_catalog_get(self, kc):
request = self.mock_rest_request() request = self.mock_rest_request()
response = keystone.ServiceCatalog().get(request) response = keystone.ServiceCatalog().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
@ -668,8 +695,7 @@ class KeystoneRestTestCase(test.TestCase):
# #
# User Session # User Session
# #
@mock.patch.object(keystone.api, 'keystone') def test_user_session_get(self):
def test_user_session_get(self, kc):
request = self.mock_rest_request() request = self.mock_rest_request()
request.user = mock.Mock( request.user = mock.Mock(
services_region='some region', services_region='some region',
@ -688,13 +714,13 @@ class KeystoneRestTestCase(test.TestCase):
# #
# Groups # Groups
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_list']})
def test_group_get_list(self, kc): def test_group_get_list(self):
request = self.mock_rest_request(**{ request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'), 'session.get': mock.Mock(return_value='the_domain'),
'GET': {}, 'GET': {},
}) })
kc.group_list.return_value = [ self.mock_group_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'uno!'}}), mock.Mock(**{'to_dict.return_value': {'name': 'uno!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'dos!'}}) mock.Mock(**{'to_dict.return_value': {'name': 'dos!'}})
] ]
@ -702,17 +728,18 @@ class KeystoneRestTestCase(test.TestCase):
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "uno!"}, {"name": "dos!"}]}) {"items": [{"name": "uno!"}, {"name": "dos!"}]})
kc.group_list.assert_called_once_with(request, domain='the_domain') self.mock_group_list.assert_called_once_with(request,
domain='the_domain')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_create']})
def test_group_create(self, kc): def test_group_create(self):
request = self.mock_rest_request(**{ request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'), 'session.get': mock.Mock(return_value='the_domain'),
'GET': {}, 'GET': {},
'body': '{"name": "bug!", "description": "bugaboo!!"}', 'body': '{"name": "bug!", "description": "bugaboo!!"}',
}) })
kc.group_create.return_value.id = 'group789' self.mock_group_create.return_value.id = 'group789'
kc.group_create.return_value.to_dict.return_value = { self.mock_group_create.return_value.to_dict.return_value = {
'id': 'group789', 'name': 'bug!', 'description': 'bugaboo!!' 'id': 'group789', 'name': 'bug!', 'description': 'bugaboo!!'
} }
@ -724,18 +751,18 @@ class KeystoneRestTestCase(test.TestCase):
{"id": "group789", {"id": "group789",
"name": "bug!", "name": "bug!",
"description": "bugaboo!!"}) "description": "bugaboo!!"})
kc.group_create.assert_called_once_with(request, 'the_domain', self.mock_group_create.assert_called_once_with(request, 'the_domain',
'bug!', 'bugaboo!!') 'bug!', 'bugaboo!!')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_create']})
def test_group_create_without_description(self, kc): def test_group_create_without_description(self):
request = self.mock_rest_request(**{ request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'), 'session.get': mock.Mock(return_value='the_domain'),
'GET': {}, 'GET': {},
'body': '{"name": "bug!"}', 'body': '{"name": "bug!"}',
}) })
kc.group_create.return_value.id = 'group789' self.mock_group_create.return_value.id = 'group789'
kc.group_create.return_value.to_dict.return_value = { self.mock_group_create.return_value.to_dict.return_value = {
'id': 'group789', 'name': 'bug!' 'id': 'group789', 'name': 'bug!'
} }
@ -746,42 +773,45 @@ class KeystoneRestTestCase(test.TestCase):
self.assertEqual(response.json, self.assertEqual(response.json,
{"id": "group789", {"id": "group789",
"name": "bug!"}) "name": "bug!"})
kc.group_create.assert_called_once_with(request, 'the_domain', self.mock_group_create.assert_called_once_with(request, 'the_domain',
'bug!', None) 'bug!', None)
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_get']})
def test_group_get(self, kc): def test_group_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
kc.group_get.return_value.to_dict.return_value = { self.mock_group_get.return_value.to_dict.return_value = {
'name': 'bug!', 'description': 'bugaboo!!'} 'name': 'bug!', 'description': 'bugaboo!!'}
response = keystone.Group().get(request, 'the_id') response = keystone.Group().get(request, 'the_id')
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, {"name": "bug!", self.assertEqual(response.json, {"name": "bug!",
"description": "bugaboo!!"}) "description": "bugaboo!!"})
kc.group_get.assert_called_once_with(request, 'the_id') self.mock_group_get.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_delete']})
def test_group_delete(self, kc): def test_group_delete(self):
self.mock_group_delete.return_value = None
request = self.mock_rest_request() request = self.mock_rest_request()
response = keystone.Group().delete(request, 'the_id') response = keystone.Group().delete(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.group_delete.assert_called_once_with(request, 'the_id') self.mock_group_delete.assert_called_once_with(request, 'the_id')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_update']})
def test_group_patch(self, kc): def test_group_patch(self):
self.mock_group_update.return_value = self.groups.first()
request = self.mock_rest_request( request = self.mock_rest_request(
body='{"name": "spam_i_am", "description": "Sir Spam"}') body='{"name": "spam_i_am", "description": "Sir Spam"}')
response = keystone.Group().patch(request, 'the_id') response = keystone.Group().patch(request, 'the_id')
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.group_update.assert_called_once_with(request, self.mock_group_update.assert_called_once_with(request,
'the_id', 'the_id',
'spam_i_am', 'spam_i_am',
'Sir Spam') 'Sir Spam')
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['group_delete']})
def test_group_delete_many(self, kc): def test_group_delete_many(self):
self.mock_group_delete.return_value = None
request = self.mock_rest_request(body=''' request = self.mock_rest_request(body='''
["id1", "id2", "id3"] ["id1", "id2", "id3"]
''') ''')
@ -789,7 +819,7 @@ class KeystoneRestTestCase(test.TestCase):
response = keystone.Groups().delete(request) response = keystone.Groups().delete(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
self.assertEqual(response.content, b'') self.assertEqual(response.content, b'')
kc.group_delete.assert_has_calls([ self.mock_group_delete.assert_has_calls([
mock.call(request, 'id1'), mock.call(request, 'id1'),
mock.call(request, 'id2'), mock.call(request, 'id2'),
mock.call(request, 'id3'), mock.call(request, 'id3'),
@ -799,8 +829,8 @@ class KeystoneRestTestCase(test.TestCase):
# Services # Services
# #
@mock.patch.object(keystone.api, 'keystone') @test.create_mocks({api.keystone: ['Service']})
def test_services_get(self, kc): def test_services_get(self):
request = self.mock_rest_request() request = self.mock_rest_request()
mock_service = { mock_service = {
"name": "srv_name", "name": "srv_name",
@ -811,7 +841,8 @@ class KeystoneRestTestCase(test.TestCase):
service_catalog=[mock_service], service_catalog=[mock_service],
services_region='some region' services_region='some region'
) )
self.mock_Service.return_value.to_dict.return_value = mock_service
response = keystone.Services().get(request) response = keystone.Services().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
kc.Service.assert_called_once_with(mock_service, "some region") self.mock_Service.assert_called_once_with(mock_service, "some region")