Refactor REST API tests for common code patterns

Move common patterns out into a helper class and refactor
tests to use that base helper.

Change-Id: Icd2512da3902d8dcf6d28af3d09fc4ee0bf1f33a
This commit is contained in:
Richard Jones 2015-02-25 11:47:33 +11:00
parent aed603d618
commit 243d2a5a44
9 changed files with 104 additions and 188 deletions

View File

@ -12,21 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import testtools
from openstack_dashboard.api.rest import cinder
from openstack_dashboard.test.api_tests import rest_test_utils as utils
from openstack_dashboard.test import helpers as test
class CinderRestTestCase(testtools.TestCase):
def assertStatusCode(self, response, expected_code):
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
class CinderRestTestCase(test.TestCase):
def test_volumes_get(self):
self._test_volumes_get(False, {})
@ -40,9 +31,9 @@ class CinderRestTestCase(testtools.TestCase):
@mock.patch.object(cinder.api, 'cinder')
def _test_volumes_get(self, all, filters, cc):
if all:
request = utils.construct_request(GET={'all_projects': 'true'})
request = self.mock_rest_request(GET={'all_projects': 'true'})
else:
request = utils.construct_request(**{'GET': filters})
request = self.mock_rest_request(**{'GET': filters})
cc.volume_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'id': 'one'}}),
mock.Mock(**{'to_dict.return_value': {'id': 'two'}}),
@ -60,7 +51,7 @@ class CinderRestTestCase(testtools.TestCase):
@mock.patch.object(cinder.api, 'cinder')
def test_volume_snaps_get(self, cc):
request = utils.construct_request(**{'GET': {}})
request = self.mock_rest_request(**{'GET': {}})
cc.volume_snapshot_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'id': 'one'}}),
mock.Mock(**{'to_dict.return_value': {'id': 'two'}}),
@ -75,7 +66,7 @@ class CinderRestTestCase(testtools.TestCase):
@mock.patch.object(cinder.api, 'cinder')
def test_volume_snaps_get_with_filters(self, cc):
filters = {'status': 'available'}
request = utils.construct_request(**{'GET': dict(filters)})
request = self.mock_rest_request(**{'GET': dict(filters)})
cc.volume_snapshot_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'id': 'one'}}),
mock.Mock(**{'to_dict.return_value': {'id': 'two'}}),

View File

@ -17,12 +17,10 @@ import mock
from horizon import conf
from openstack_dashboard.api.rest import config
from openstack_dashboard.test.api_tests import rest_test_utils as util
from openstack_dashboard.test import helpers as test
class ConfigRestTestCase(test.RestAPITestCase):
class ConfigRestTestCase(test.TestCase):
def assertContains(self, response, expected_content):
if response.find(expected_content) > 0:
return
@ -39,7 +37,7 @@ class ConfigRestTestCase(test.RestAPITestCase):
user_config = {"modal_backdrop": "static"}
content = '"modal_backdrop": "static"'
with mock.patch.dict(conf.HORIZON_CONFIG, user_config):
request = util.construct_request()
request = self.mock_rest_request()
response = config.DefaultUserConfigs().get(request)
self.assertStatusCode(response, 200)
self.assertContains(response.content, content)
@ -48,7 +46,7 @@ class ConfigRestTestCase(test.RestAPITestCase):
admin_config = {"user_home": "somewhere.com"}
content = '"user_home": "somewhere.com"'
with mock.patch.dict(conf.HORIZON_CONFIG, admin_config):
request = util.construct_request()
request = self.mock_rest_request()
response = config.AdminConfigs().get(request)
self.assertStatusCode(response, 200)
self.assertContains(response.content, content)
@ -57,7 +55,7 @@ class ConfigRestTestCase(test.RestAPITestCase):
ignore_config = {"password_validator": "someobject"}
content = '"password_validator": "someobject"'
with mock.patch.dict(conf.HORIZON_CONFIG, ignore_config):
request = util.construct_request()
request = self.mock_rest_request()
response = config.AdminConfigs().get(request)
self.assertStatusCode(response, 200)
self.assertNotContains(response.content, content)

View File

@ -15,21 +15,13 @@
import mock
from openstack_dashboard.api.rest import glance
from openstack_dashboard.test.api_tests import rest_test_utils # noqa
from openstack_dashboard.test import helpers as test
class ImagesRestTestCase(test.APITestCase):
def assertStatusCode(self, response, expected_code):
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
class ImagesRestTestCase(test.TestCase):
@mock.patch.object(glance.api, 'glance')
def test_image_get_single(self, gc):
request = rest_test_utils.construct_request()
request = self.mock_rest_request()
gc.image_get.return_value.to_dict.return_value = {'name': '1'}
response = glance.Image().get(request, "1")
@ -45,7 +37,7 @@ class ImagesRestTestCase(test.APITestCase):
'paginate': False,
}
filters = {'name': 'fedora'}
request = rest_test_utils.construct_request(
request = self.mock_rest_request(
**{'GET': dict(kwargs, **filters)})
gc.image_list_detailed.return_value = ([
mock.Mock(**{'to_dict.return_value': {'name': 'fedora'}}),
@ -63,7 +55,7 @@ class ImagesRestTestCase(test.APITestCase):
@mock.patch.object(glance.api, 'glance')
def test_namespace_get_list(self, gc):
request = rest_test_utils.construct_request(**{'GET': {}})
request = self.mock_rest_request(**{'GET': {}})
gc.metadefs_namespace_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'namespace': '1'}}),
mock.Mock(**{'to_dict.return_value': {'namespace': '2'}})
@ -87,7 +79,7 @@ class ImagesRestTestCase(test.APITestCase):
'paginate': False,
}
filters = {'resource_types': 'type'}
request = rest_test_utils.construct_request(
request = self.mock_rest_request(
**{'GET': dict(kwargs, **filters)})
gc.metadefs_namespace_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'namespace': '1'}}),
@ -106,7 +98,7 @@ class ImagesRestTestCase(test.APITestCase):
@mock.patch.object(glance.api, 'glance')
def test_namespace_get_namespace(self, gc):
kwargs = {'resource_type': ['OS::Nova::Flavor']}
request = rest_test_utils.construct_request(**{'GET': dict(kwargs)})
request = self.mock_rest_request(**{'GET': dict(kwargs)})
gc.metadefs_namespace_get.return_value\
.to_dict.return_value = {'namespace': '1'}
@ -128,26 +120,26 @@ class ImagesRestTestCase(test.APITestCase):
# Combined
request_params = dict(kwargs)
request_params.update(filters)
request = rest_test_utils.construct_request(
request = self.mock_rest_request(
**{'GET': dict(request_params)})
output_filters, output_kwargs = glance._parse_filters_kwargs(request)
self.assertDictEqual(kwargs, output_kwargs)
self.assertDictEqual(filters, output_filters)
# Empty Filters
request = rest_test_utils.construct_request(**{'GET': dict(kwargs)})
request = self.mock_rest_request(**{'GET': dict(kwargs)})
output_filters, output_kwargs = glance._parse_filters_kwargs(request)
self.assertDictEqual(kwargs, output_kwargs)
self.assertDictEqual({}, output_filters)
# Emtpy keywords
request = rest_test_utils.construct_request(**{'GET': dict(filters)})
request = self.mock_rest_request(**{'GET': dict(filters)})
output_filters, output_kwargs = glance._parse_filters_kwargs(request)
self.assertDictEqual({}, output_kwargs)
self.assertDictEqual(filters, output_filters)
# Empty both
request = rest_test_utils.construct_request(**{'GET': dict()})
request = self.mock_rest_request(**{'GET': dict()})
output_filters, output_kwargs = glance._parse_filters_kwargs(request)
self.assertDictEqual({}, output_kwargs)
self.assertDictEqual({}, output_filters)

View File

@ -12,31 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import testtools
from django.conf import settings
from oslo_serialization import jsonutils
from openstack_dashboard.api.rest import keystone
from rest_test_utils import construct_request # noqa
from openstack_dashboard.test import helpers as test
class KeystoneRestTestCase(testtools.TestCase):
def assertStatusCode(self, response, expected_code):
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
class KeystoneRestTestCase(test.TestCase):
#
# Users
#
@mock.patch.object(keystone.api, 'keystone')
def test_user_get(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.user_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.User().get(request, 'the_id')
self.assertStatusCode(response, 200)
@ -45,7 +36,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_get_current(self, kc):
request = construct_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!'}
response = keystone.User().get(request, 'current')
self.assertStatusCode(response, 200)
@ -55,7 +46,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_get_list(self, kc):
request = construct_request(**{
request = self.mock_rest_request(**{
'session.get': mock.Mock(return_value='the_domain'),
'GET': {},
})
@ -115,7 +106,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def _test_user_create(self, supplied_body, expected_call, kc):
request = construct_request(body=supplied_body)
request = self.mock_rest_request(body=supplied_body)
kc.get_default_domain.return_value = mock.Mock(**{'id': 'the_domain'})
kc.user_create.return_value.id = 'user123'
kc.user_create.return_value = mock.Mock(**{
@ -133,7 +124,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_delete_many(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
["id1", "id2", "id3"]
''')
@ -148,7 +139,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_delete(self, kc):
request = construct_request()
request = self.mock_rest_request()
response = keystone.User().delete(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -156,7 +147,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_patch_password(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"password": "sekrit"}
''')
response = keystone.User().patch(request, 'user123')
@ -168,7 +159,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_patch_enabled(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"enabled": false}
''')
response = keystone.User().patch(request, 'user123')
@ -180,7 +171,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_patch_project(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"project_id": "other123"}
''')
response = keystone.User().patch(request, 'user123')
@ -192,7 +183,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_user_patch_multiple(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"project_id": "other123", "enabled": false}
''')
response = keystone.User().patch(request, 'user123')
@ -209,7 +200,7 @@ class KeystoneRestTestCase(testtools.TestCase):
#
@mock.patch.object(keystone.api, 'keystone')
def test_role_get(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.role_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Role().get(request, 'the_id')
self.assertStatusCode(response, 200)
@ -218,7 +209,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_get_default(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.get_default_role.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Role().get(request, 'default')
self.assertStatusCode(response, 200)
@ -228,7 +219,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_get_list(self, kc):
request = construct_request(**{'GET': {}})
request = self.mock_rest_request(**{'GET': {}})
kc.role_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
@ -241,8 +232,8 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_get_for_user(self, kc):
request = construct_request(**{'GET': {'user_id': 'user123',
'project_id': 'project123'}})
request = self.mock_rest_request(**{'GET': {'user_id': 'user123',
'project_id': 'project123'}})
kc.roles_for_user.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
@ -256,7 +247,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_create(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"name": "bob"}
''')
kc.role_create.return_value.id = 'role123'
@ -273,7 +264,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_grant(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"action": "grant", "data": {"user_id": "user123",
"role_id": "role123", "project_id": "project123"}}
''')
@ -286,7 +277,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_delete_many(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
["id1", "id2", "id3"]
''')
@ -301,7 +292,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_delete(self, kc):
request = construct_request()
request = self.mock_rest_request()
response = keystone.Role().delete(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -309,7 +300,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_role_patch(self, kc):
request = construct_request(body='{"name": "spam"}')
request = self.mock_rest_request(body='{"name": "spam"}')
response = keystone.Role().patch(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -322,7 +313,7 @@ class KeystoneRestTestCase(testtools.TestCase):
#
@mock.patch.object(keystone.api, 'keystone')
def test_domain_get(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.domain_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Domain().get(request, 'the_id')
self.assertStatusCode(response, 200)
@ -331,7 +322,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_domain_get_default(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.get_default_domain.return_value.to_dict.return_value = {
'name': 'Ni!'
}
@ -343,7 +334,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_domain_get_list(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.domain_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
@ -375,7 +366,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def _test_domain_create(self, supplied_body, expected_call, kc):
request = construct_request(body=supplied_body)
request = self.mock_rest_request(body=supplied_body)
kc.domain_create.return_value.id = 'domain123'
kc.domain_create.return_value.to_dict.return_value = {
'id': 'domain123', 'name': 'bob'
@ -392,7 +383,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_domain_delete_many(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
["id1", "id2", "id3"]
''')
@ -407,7 +398,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_domain_delete(self, kc):
request = construct_request()
request = self.mock_rest_request()
response = keystone.Domain().delete(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -415,7 +406,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_domain_patch(self, kc):
request = construct_request(body='{"name": "spam"}')
request = self.mock_rest_request(body='{"name": "spam"}')
response = keystone.Domain().patch(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -430,7 +421,7 @@ class KeystoneRestTestCase(testtools.TestCase):
#
@mock.patch.object(keystone.api, 'keystone')
def test_project_get(self, kc):
request = construct_request()
request = self.mock_rest_request()
kc.tenant_get.return_value.to_dict.return_value = {'name': 'Ni!'}
response = keystone.Project().get(request, 'the_id')
self.assertStatusCode(response, 200)
@ -439,7 +430,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_project_get_list(self, kc):
request = construct_request(**{'GET': {}})
request = self.mock_rest_request(**{'GET': {}})
kc.tenant_list.return_value = ([
mock.Mock(**{'to_dict.return_value': {'name': 'Ni!'}}),
mock.Mock(**{'to_dict.return_value': {'name': 'Ptang!'}})
@ -477,7 +468,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def _test_project_create(self, supplied_body, expected_call, kc):
request = construct_request(body=supplied_body)
request = self.mock_rest_request(body=supplied_body)
kc.tenant_create.return_value.id = 'project123'
kc.tenant_create.return_value.to_dict.return_value = {
'id': 'project123', 'name': 'bob'
@ -494,7 +485,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_project_delete_many(self, kc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
["id1", "id2", "id3"]
''')
@ -509,7 +500,7 @@ class KeystoneRestTestCase(testtools.TestCase):
@mock.patch.object(keystone.api, 'keystone')
def test_project_delete(self, kc):
request = construct_request()
request = self.mock_rest_request()
response = keystone.Project().delete(request, 'the_id')
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -519,7 +510,7 @@ class KeystoneRestTestCase(testtools.TestCase):
def test_project_patch(self, kc):
# nothing in the Horizon code documents what additional parameters are
# allowed, so we'll just assume GIGO
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"name": "spam", "domain_id": "domain123", "foo": "bar"}
''')
response = keystone.Project().patch(request, 'spam123')
@ -537,7 +528,7 @@ class KeystoneRestTestCase(testtools.TestCase):
#
@mock.patch.object(keystone.api, 'keystone')
def test_service_catalog_get(self, kc):
request = construct_request()
request = self.mock_rest_request()
response = keystone.ServiceCatalog().get(request)
self.assertStatusCode(response, 200)
content = jsonutils.dumps(request.user.service_catalog,

View File

@ -14,15 +14,14 @@
import mock
from openstack_dashboard.api.rest import network
from openstack_dashboard.test.api_tests import rest_test_utils
from openstack_dashboard.test import helpers as test
class RestNetworkApiSecurityGroupTests(test.RestAPITestCase):
class RestNetworkApiSecurityGroupTests(test.TestCase):
@mock.patch.object(network.api, 'network')
def test_security_group_detailed(self, client):
request = rest_test_utils.construct_request()
request = self.mock_rest_request()
client.security_group_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'name': 'default'}}),
]

View File

@ -12,29 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import testtools
from django.conf import settings
from openstack_dashboard.api.rest import nova
from rest_test_utils import construct_request # noqa
from openstack_dashboard.test import helpers as test
class NovaRestTestCase(testtools.TestCase):
def assertStatusCode(self, response, expected_code):
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
class NovaRestTestCase(test.TestCase):
#
# Keypairs
#
@mock.patch.object(nova.api, 'nova')
def test_keypair_get(self, nc):
request = construct_request()
request = self.mock_rest_request()
nc.keypair_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'id': 'one'}}),
mock.Mock(**{'to_dict.return_value': {'id': 'two'}}),
@ -47,7 +38,7 @@ class NovaRestTestCase(testtools.TestCase):
@mock.patch.object(nova.api, 'nova')
def test_keypair_create(self, nc):
request = construct_request(body='''{"name": "Ni!"}''')
request = self.mock_rest_request(body='''{"name": "Ni!"}''')
new = nc.keypair_create.return_value
new.to_dict.return_value = {'name': 'Ni!', 'public_key': 'sekrit'}
new.name = 'Ni!'
@ -61,7 +52,7 @@ class NovaRestTestCase(testtools.TestCase):
@mock.patch.object(nova.api, 'nova')
def test_keypair_import(self, nc):
request = construct_request(body='''
request = self.mock_rest_request(body='''
{"name": "Ni!", "public_key": "hi"}
''')
new = nc.keypair_import.return_value
@ -87,9 +78,9 @@ class NovaRestTestCase(testtools.TestCase):
@mock.patch.object(nova.api, 'nova')
def _test_availzone_get(self, detail, nc):
if detail:
request = construct_request(GET={'detailed': 'true'})
request = self.mock_rest_request(GET={'detailed': 'true'})
else:
request = construct_request(GET={})
request = self.mock_rest_request(GET={})
nc.availability_zone_list.return_value = [
mock.Mock(**{'to_dict.return_value': {'id': 'one'}}),
mock.Mock(**{'to_dict.return_value': {'id': 'two'}}),
@ -112,9 +103,9 @@ class NovaRestTestCase(testtools.TestCase):
@mock.patch.object(nova.api, 'nova')
def _test_limits_get(self, reserved, nc):
if reserved:
request = construct_request(GET={'reserved': 'true'})
request = self.mock_rest_request(GET={'reserved': 'true'})
else:
request = construct_request(GET={})
request = self.mock_rest_request(GET={})
nc.tenant_absolute_limits.return_value = {'id': 'one'}
response = nova.Limits().get(request)
self.assertStatusCode(response, 200)
@ -126,7 +117,7 @@ class NovaRestTestCase(testtools.TestCase):
#
@mock.patch.object(nova.api, 'nova')
def test_server_create_missing(self, nc):
request = construct_request(body='''{"name": "hi"}''')
request = self.mock_rest_request(body='''{"name": "hi"}''')
response = nova.Servers().post(request)
self.assertStatusCode(response, 400)
self.assertEqual(response.content,
@ -135,7 +126,7 @@ class NovaRestTestCase(testtools.TestCase):
@mock.patch.object(nova.api, 'nova')
def test_server_create_basic(self, nc):
request = construct_request(body='''{"name": "Ni!",
request = self.mock_rest_request(body='''{"name": "Ni!",
"source_id": "image123", "flavor_id": "flavor123",
"key_name": "sekrit", "user_data": "base64 yes",
"security_groups": [{"name": "root"}]}

View File

@ -1,29 +0,0 @@
# Copyright 2014, Rackspace, US, Inc.
#
# 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 mock
def mock_obj_to_dict(r):
return mock.Mock(**{'to_dict.return_value': r})
def construct_request(**args):
mock_args = {
'user.is_authenticated.return_value': True,
'is_ajax.return_value': True,
'policy.check.return_value': True,
'body': ''
}
mock_args.update(args)
return mock.Mock(**mock_args)

View File

@ -11,35 +11,16 @@
# 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 mock
import testtools
from openstack_dashboard.api.rest import utils
from openstack_dashboard.test import helpers as test
class RestUtilsTestCase(testtools.TestCase):
def assertStatusCode(self, response, expected_code):
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
def _construct_request(self, **args):
mock_args = {
'user.is_authenticated.return_value': True,
'is_ajax.return_value': True,
'policy.check.return_value': True,
'body': ''
}
mock_args.update(args)
return mock.Mock(**mock_args)
class RestUtilsTestCase(test.TestCase):
def test_api_success(self):
@utils.ajax()
def f(self, request):
return 'ok'
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
request.is_authenticated.assert_called_once()
self.assertStatusCode(response, 200)
@ -49,7 +30,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax(authenticated=False)
def f(self, request):
return 'ok'
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
request.is_authenticated.assert_not_called_once()
self.assertStatusCode(response, 200)
@ -59,7 +40,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
return 'ok'
request = self._construct_request(**{
request = self.mock_rest_request(**{
'user.is_authenticated.return_value': False
})
response = f(None, request)
@ -71,7 +52,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
pass
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
self.assertStatusCode(response, 204)
self.assertEqual(response.content, '')
@ -80,7 +61,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
raise utils.AjaxError(500, 'b0rk')
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
self.assertStatusCode(response, 500)
self.assertEqual(response.content, '"b0rk"')
@ -89,7 +70,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
assert False, "don't get here"
request = self._construct_request(**{'body': 'spam'})
request = self.mock_rest_request(**{'body': 'spam'})
response = f(None, request)
self.assertStatusCode(response, 400)
self.assertEqual(response.content, '"malformed JSON request: No JSON '
@ -99,7 +80,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
raise utils.AjaxError(404, 'b0rk')
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
self.assertStatusCode(response, 404)
self.assertEqual(response.content, '"b0rk"')
@ -108,7 +89,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax(data_required=True)
def f(self, request):
assert False, "don't get here"
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
self.assertStatusCode(response, 400)
self.assertEqual(response.content, '"request requires JSON body"')
@ -117,7 +98,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax(data_required=True)
def f(self, request):
return 'OK'
request = self._construct_request(**{'body': '''
request = self.mock_rest_request(**{'body': '''
{"current": true, "update": true}
'''})
response = f(None, request)
@ -128,7 +109,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
return utils.CreatedResponse('/api/spam/spam123')
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
request.is_authenticated.assert_called_once()
self.assertStatusCode(response, 201)
@ -139,7 +120,7 @@ class RestUtilsTestCase(testtools.TestCase):
@utils.ajax()
def f(self, request):
return utils.CreatedResponse('/api/spam/spam123', 'spam!')
request = self._construct_request()
request = self.mock_rest_request()
response = f(None, request)
request.is_authenticated.assert_called_once()
self.assertStatusCode(response, 201)

View File

@ -20,7 +20,6 @@ import collections
import copy
from functools import wraps # noqa
import os
import testtools
from ceilometerclient.v2 import client as ceilometer_client
from cinderclient import client as cinder_client
@ -268,6 +267,28 @@ class TestCase(horizon_helpers.TestCase):
else:
assert len(errors) > 0, "No errors were found on the form"
def assertStatusCode(self, response, expected_code):
"""Validates an expected status code.
Matches camel case of other assert functions
"""
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
@staticmethod
def mock_rest_request(**args):
mock_args = {
'user.is_authenticated.return_value': True,
'is_ajax.return_value': True,
'policy.check.return_value': True,
'body': ''
}
mock_args.update(args)
return mock.Mock(**mock_args)
class BaseAdminViewTests(TestCase):
"""Sets an active user with the "admin" role.
@ -425,25 +446,6 @@ class APITestCase(TestCase):
return self.saharaclient
class RestAPITestCase(testtools.TestCase):
"""Testing APIs.
For use with tests which deal with the underlying clients rather than
stubbing out the openstack_dashboard.api.* methods.
"""
def assertStatusCode(self, response, expected_code):
"""Validates an expected status code.
Matches camel case of other assert functions
"""
if response.status_code == expected_code:
return
self.fail('status code %r != %r: %s' % (response.status_code,
expected_code,
response.content))
@unittest.skipUnless(os.environ.get('WITH_SELENIUM', False),
"The WITH_SELENIUM env variable is not set.")
class SeleniumTestCase(horizon_helpers.SeleniumTestCase):