Refactor tests regarding required attributes

Some tests regarding the _required_attribute(s) functions in the wsgi
module seemed to be somewhat confusing, so I refactored them trying to
increase readability.

Change-Id: Id9aedcb05063cf0d618399847f6afe73c800aa93
This commit is contained in:
Juan Antonio Osorio 2014-05-07 11:16:56 +03:00
parent fa7812e3d4
commit 0f5c74037e

View File

@ -40,21 +40,15 @@ class FakeAttributeCheckerApp(wsgi.Application):
def index(self, context): def index(self, context):
return context['query_string'] return context['query_string']
def has_attribute(self, body, attr): def assert_attribute(self, body, attr):
body_dict = jsonutils.loads(body) """Asserts that the given request has a certain attribute."""
try: ref = jsonutils.loads(body)
self._require_attribute(body_dict, attr) self._require_attribute(ref, attr)
return True
except exception.ValidationError as ex:
return (False, ex.message)
def has_attributes(self, body, attr): def assert_attributes(self, body, attr):
body_dict = jsonutils.loads(body) """Asserts that the given request has a certain set attributes."""
try: ref = jsonutils.loads(body)
self._require_attributes(body_dict, attr) self._require_attributes(ref, attr)
return True
except exception.ValidationError as ex:
return (False, ex.message)
class BaseWSGITest(tests.TestCase): class BaseWSGITest(tests.TestCase):
@ -114,38 +108,40 @@ class ApplicationTest(BaseWSGITest):
app = FakeAttributeCheckerApp() app = FakeAttributeCheckerApp()
req = self._make_request(url='/?1=2') req = self._make_request(url='/?1=2')
resp = req.get_response(app) resp = req.get_response(app)
self.assertTrue(app.has_attribute(resp.body, '1')) app.assert_attribute(resp.body, '1')
def test_require_attribute(self): def test_require_attribute_fail_if_attribute_not_present(self):
app = FakeAttributeCheckerApp() app = FakeAttributeCheckerApp()
req = self._make_request(url='/?1=2') req = self._make_request(url='/?1=2')
resp = req.get_response(app) resp = req.get_response(app)
self.assertFalse(app.has_attribute(resp.body, 'a')[0]) self.assertRaises(exception.ValidationError,
app.assert_attribute, resp.body, 'a')
def test_successful_require_multiple_attributes(self): def test_successful_require_multiple_attributes(self):
app = FakeAttributeCheckerApp() app = FakeAttributeCheckerApp()
req = self._make_request(url='/?a=1&b=2') req = self._make_request(url='/?a=1&b=2')
resp = req.get_response(app) resp = req.get_response(app)
self.assertTrue(app.has_attributes(resp.body, ['a', 'b'])) app.assert_attributes(resp.body, ['a', 'b'])
def test_attribute_missing_from_request(self): def test_attribute_missing_from_request(self):
app = FakeAttributeCheckerApp() app = FakeAttributeCheckerApp()
req = self._make_request(url='/?a=1&b=2') req = self._make_request(url='/?a=1&b=2')
resp = req.get_response(app) resp = req.get_response(app)
validation = app.has_attributes(resp.body, ['a', ex = self.assertRaises(exception.ValidationError,
'missing_attribute']) app.assert_attributes,
self.assertFalse(validation[0]) resp.body, ['a', 'missing_attribute'])
self.assertIn('missing_attribute', validation[1]) self.assertThat(ex.message, matchers.Contains('missing_attribute'))
def test_no_required_attributes_present(self): def test_no_required_attributes_present(self):
app = FakeAttributeCheckerApp() app = FakeAttributeCheckerApp()
req = self._make_request(url='/') req = self._make_request(url='/')
resp = req.get_response(app) resp = req.get_response(app)
validation = app.has_attributes(resp.body, ['missing_attribute1',
'missing_attribute2']) ex = self.assertRaises(exception.ValidationError,
self.assertFalse(validation[0]) app.assert_attributes, resp.body,
self.assertIn('missing_attribute1', validation[1]) ['missing_attribute1', 'missing_attribute2'])
self.assertIn('missing_attribute2', validation[1]) self.assertThat(ex.message, matchers.Contains('missing_attribute1'))
self.assertThat(ex.message, matchers.Contains('missing_attribute2'))
def test_render_response_custom_headers(self): def test_render_response_custom_headers(self):
resp = wsgi.render_response(headers=[('Custom-Header', 'Some-Value')]) resp = wsgi.render_response(headers=[('Custom-Header', 'Some-Value')])