From 0f5c74037eede00d1e4e48e1f7ecc3a1501ebd74 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Wed, 7 May 2014 11:16:56 +0300 Subject: [PATCH] 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 --- keystone/tests/test_wsgi.py | 50 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/keystone/tests/test_wsgi.py b/keystone/tests/test_wsgi.py index d784efe05..469ef9ac5 100644 --- a/keystone/tests/test_wsgi.py +++ b/keystone/tests/test_wsgi.py @@ -40,21 +40,15 @@ class FakeAttributeCheckerApp(wsgi.Application): def index(self, context): return context['query_string'] - def has_attribute(self, body, attr): - body_dict = jsonutils.loads(body) - try: - self._require_attribute(body_dict, attr) - return True - except exception.ValidationError as ex: - return (False, ex.message) + def assert_attribute(self, body, attr): + """Asserts that the given request has a certain attribute.""" + ref = jsonutils.loads(body) + self._require_attribute(ref, attr) - def has_attributes(self, body, attr): - body_dict = jsonutils.loads(body) - try: - self._require_attributes(body_dict, attr) - return True - except exception.ValidationError as ex: - return (False, ex.message) + def assert_attributes(self, body, attr): + """Asserts that the given request has a certain set attributes.""" + ref = jsonutils.loads(body) + self._require_attributes(ref, attr) class BaseWSGITest(tests.TestCase): @@ -114,38 +108,40 @@ class ApplicationTest(BaseWSGITest): app = FakeAttributeCheckerApp() req = self._make_request(url='/?1=2') 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() req = self._make_request(url='/?1=2') 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): app = FakeAttributeCheckerApp() req = self._make_request(url='/?a=1&b=2') 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): app = FakeAttributeCheckerApp() req = self._make_request(url='/?a=1&b=2') resp = req.get_response(app) - validation = app.has_attributes(resp.body, ['a', - 'missing_attribute']) - self.assertFalse(validation[0]) - self.assertIn('missing_attribute', validation[1]) + ex = self.assertRaises(exception.ValidationError, + app.assert_attributes, + resp.body, ['a', 'missing_attribute']) + self.assertThat(ex.message, matchers.Contains('missing_attribute')) def test_no_required_attributes_present(self): app = FakeAttributeCheckerApp() req = self._make_request(url='/') resp = req.get_response(app) - validation = app.has_attributes(resp.body, ['missing_attribute1', - 'missing_attribute2']) - self.assertFalse(validation[0]) - self.assertIn('missing_attribute1', validation[1]) - self.assertIn('missing_attribute2', validation[1]) + + ex = self.assertRaises(exception.ValidationError, + app.assert_attributes, resp.body, + ['missing_attribute1', 'missing_attribute2']) + self.assertThat(ex.message, matchers.Contains('missing_attribute1')) + self.assertThat(ex.message, matchers.Contains('missing_attribute2')) def test_render_response_custom_headers(self): resp = wsgi.render_response(headers=[('Custom-Header', 'Some-Value')])