From a73ce2bf6190e538a44e1fc24b8d7ff5d04a5546 Mon Sep 17 00:00:00 2001 From: Eva Balycheva Date: Wed, 24 Feb 2016 01:26:14 +0300 Subject: [PATCH] Fix validation in websocket unit tests Validation is not performed for many Zaqar responses in these files: zaqar.tests.unit.transport.websocket.v2.test_queue_lifecycle.py zaqar.tests.unit.transport.websocket.v2.test_subscriptions.py For example, it's possible to modify asserted response code in "test_no_metadata" test case in "test_queue_lifecycle.py" and the test will still pass. This patch fixes all places where validation is not performed. Not all tests pass after the change, so this patch fixes these tests. Change-Id: I597084a16f4a796ffa74df7e299013e80d9cf6a3 Closes-Bug: 1549012 --- .../websocket/v2/test_queue_lifecycle.py | 87 +++++++++---------- .../websocket/v2/test_subscriptions.py | 4 +- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py b/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py index 985cbb48..ced7ed0f 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_queue_lifecycle.py @@ -64,7 +64,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -72,7 +72,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(404, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Create @@ -91,7 +91,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Fetch metadata @@ -107,9 +107,9 @@ class QueueLifecycleBaseTest(base.V2Base): def validator(resp, isBinary): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - self.assertEqual(json.dumps(meta), json.dumps(resp['body'])) + self.assertEqual(meta, resp['body']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Stats empty queue @@ -121,7 +121,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Delete @@ -133,7 +133,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(204, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Get non-existent stats @@ -145,7 +145,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(404, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_name_restrictions(self): @@ -165,7 +165,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -173,7 +173,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) body["queue_name"] = "m@rsb@r" @@ -183,7 +183,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) body["queue_name"] = "marsbar" * 10 @@ -200,7 +200,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -208,7 +208,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) headers['X-Project-ID'] = 'test-project' @@ -218,7 +218,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_non_ascii_name(self): @@ -234,7 +234,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -242,7 +242,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) body = {"queue_name": test_params[1]} @@ -260,7 +260,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -268,14 +268,14 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def validator(resp, isBinary): resp = json.loads(resp) self.assertEqual(204, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) @ddt.data('{', '[]', '.', ' ') @@ -290,7 +290,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -298,7 +298,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_too_much_metadata(self): @@ -317,7 +317,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -325,7 +325,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_way_too_much_metadata(self): @@ -344,7 +344,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, headers) @@ -352,7 +352,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_update_metadata(self): @@ -366,7 +366,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() # Create req = test_utils.create_request(action, body, headers) @@ -375,7 +375,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Set meta @@ -388,7 +388,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(204, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Get @@ -400,9 +400,9 @@ class QueueLifecycleBaseTest(base.V2Base): def validator(resp, isBinary): resp = json.loads(resp) self.assertEqual(204, resp['headers']['status']) - self.assertEqual(json.dumps(meta1), json.dumps(resp['body'])) + self.assertEqual(meta1, resp['body']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Update @@ -416,7 +416,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(204, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Get again @@ -428,9 +428,9 @@ class QueueLifecycleBaseTest(base.V2Base): def validator(resp, isBinary): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - self.assertEqual(json.dumps(meta2), json.dumps(resp['body'])) + self.assertEqual(meta2, resp['body']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_list(self): @@ -444,7 +444,7 @@ class QueueLifecycleBaseTest(base.V2Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() # NOTE(kgriffs): It's important that this one sort after the one # above. This is in order to prove that bug/1236605 is fixed, and @@ -463,7 +463,7 @@ class QueueLifecycleBaseTest(base.V2Base): self.assertEqual(200, resp['headers']['status']) self.assertEqual([], resp['body']['queues']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Payload exceeded @@ -474,7 +474,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(400, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # Create some @@ -492,7 +492,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(201, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) create_queue(project_id, 'q1', {"node": 31}) @@ -509,7 +509,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(2, len(resp['body']['queues'])) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # List (no metadata, get all) @@ -522,7 +522,7 @@ class QueueLifecycleBaseTest(base.V2Base): # Ensure we didn't pick up the queue from the alt project. self.assertEqual(3, len(resp['body']['queues'])) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # List with metadata @@ -533,7 +533,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) action = "queue_get" @@ -543,10 +543,9 @@ class QueueLifecycleBaseTest(base.V2Base): def validator(resp, isBinary): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - self.assertEqual(json.dumps({"node": 31}), - json.dumps(resp['body'])) + self.assertEqual({"node": 31}, resp['body']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # List tail @@ -558,7 +557,7 @@ class QueueLifecycleBaseTest(base.V2Base): resp = json.loads(resp) self.assertEqual(200, resp['headers']['status']) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) # List manually-constructed tail diff --git a/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py b/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py index 556d1a5b..707dbb93 100644 --- a/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py +++ b/zaqar/tests/unit/transport/websocket/v2/test_subscriptions.py @@ -56,7 +56,7 @@ class SubscriptionTest(base.V1_1Base): send_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(send_mock.stop) - send_mock.start() + sender = send_mock.start() req = test_utils.create_request(action, body, self.headers) @@ -64,7 +64,7 @@ class SubscriptionTest(base.V1_1Base): resp = json.loads(resp) self.assertEqual(resp['headers']['status'], 204) - send_mock.side_effect = validator + sender.side_effect = validator self.protocol.onMessage(req, False) def test_subscription_create(self):