def test_on_message_with_invalid_input_binary(self): dumps, loads, create_req = test_utils.get_pack_tools(binary=True) send_mock = mock.Mock() self.protocol.sendMessage = send_mock # Test error response, when the request can't be deserialized. req = "123" self.protocol.onMessage(req, True) resp = loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) self.assertIn('Can\'t decode binary', resp['body']['error']) # Test error response, when request body is not a dictionary. req = dumps("Apparently, I'm not a dictionary") self.protocol.onMessage(req, True) resp = loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) self.assertIn('Unexpected body type. Expected dict', resp['body']['error']) # Test error response, when validation fails. action = 'queue_glorify' body = {} req = create_req(action, body, self.headers) self.protocol.onMessage(req, True) resp = loads(send_mock.call_args[0][0]) self.assertEqual(400, resp['headers']['status']) self.assertEqual('queue_glorify is not a valid action', resp['body']['error'])
def test_on_message_with_input_in_different_format(self, in_binary): dumps, loads, create_req = test_utils.get_pack_tools(binary=in_binary) action = 'queue_get' body = {'queue_name': 'beautiful-non-existing-queue'} req = create_req(action, body, self.headers) send_mock = mock.Mock() self.protocol.sendMessage = send_mock self.protocol.onMessage(req, in_binary) resp = loads(send_mock.call_args[0][0]) self.assertEqual(200, resp['headers']['status'])
def test_auth_response_serialization_format(self, in_binary): dumps, loads, create_req = test_utils.get_pack_tools(binary=in_binary) headers = self.headers.copy() headers['X-Auth-Token'] = 'mytoken1' req = create_req("authenticate", {}, headers) msg_mock = mock.patch.object(self.protocol, 'sendMessage') self.addCleanup(msg_mock.stop) msg_mock = msg_mock.start() # Depending on onMessage method's second argument, auth response should # be in binary or text format. self.protocol.onMessage(req, in_binary) self.assertEqual(in_binary, self.protocol._auth_in_binary) self.protocol._auth_response('401 error', 'Failed') self.assertEqual(1, msg_mock.call_count) resp = loads(msg_mock.call_args[0][0]) self.assertEqual(401, resp['headers']['status'])
def _test_post(self, sample_messages, in_binary=False): body = {"queue_name": "kitkat", "messages": sample_messages} send_mock = mock.Mock() self.protocol.sendMessage = send_mock dumps, loads, create_req = test_utils.get_pack_tools(binary=in_binary) req = create_req(consts.MESSAGE_POST, body, self.headers) self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] if not in_binary: arg = arg.decode() resp = loads(arg) self.assertEqual(201, resp['headers']['status']) self.msg_ids = resp['body']['message_ids'] self.assertEqual(len(sample_messages), len(self.msg_ids)) lookup = dict([(m['ttl'], m['body']) for m in sample_messages]) # Test GET on the message resource directly # NOTE(cpp-cabrera): force the passing of time to age a message timeutils_utcnow = 'oslo_utils.timeutils.utcnow' now = timeutils.utcnow() + datetime.timedelta(seconds=10) with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now for msg_id in self.msg_ids: headers = self.headers.copy() headers['X-Project-ID'] = '777777' # Wrong project ID action = consts.MESSAGE_GET body = {"queue_name": "kitkat", "message_id": msg_id} req = create_req(action, body, headers) self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] if not in_binary: arg = arg.decode() resp = loads(arg) self.assertEqual(404, resp['headers']['status']) # Correct project ID req = create_req(action, body, self.headers) self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] if not in_binary: arg = arg.decode() resp = loads(arg) self.assertEqual(200, resp['headers']['status']) # Check message properties message = resp['body']['messages'] self.assertEqual(lookup[message['ttl']], message['body']) self.assertEqual(msg_id, message['id']) # no negative age # NOTE(cpp-cabrera): testtools lacks # GreaterThanEqual on py26 self.assertThat(message['age'], matchers.GreaterThan(-1)) # Test bulk GET action = consts.MESSAGE_GET_MANY body = {"queue_name": "kitkat", "message_ids": self.msg_ids} req = create_req(action, body, self.headers) self.protocol.onMessage(req, in_binary) arg = send_mock.call_args[0][0] if not in_binary: arg = arg.decode() resp = loads(arg) self.assertEqual(200, resp['headers']['status']) expected_ttls = set(m['ttl'] for m in sample_messages) actual_ttls = set(m['ttl'] for m in resp['body']['messages']) self.assertFalse(expected_ttls - actual_ttls) actual_ids = set(m['id'] for m in resp['body']['messages']) self.assertFalse(set(self.msg_ids) - actual_ids)