Ejemplo n.º 1
0
 def test_from_msgpack_store(self):
     """
     Ensures a valid store message is correctly parsed.
     """
     mock_message = msgpack.packb({
         'message': 'store',
         'uuid': self.uuid,
         'node': self.node,
         'key': self.key,
         'value': self.value,
         'timestamp': self.timestamp,
         'expires': self.expires,
         'created_with': self.version,
         'public_key': self.public_key,
         'name': self.name,
         'meta': self.meta,
         'sig': self.sig,
         'version': self.version
     })
     result = from_msgpack(mock_message)
     self.assertIsInstance(result, Store)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.node, self.node)
     self.assertEqual(result.key, self.key)
     self.assertEqual(result.value, self.value)
     self.assertEqual(result.timestamp, self.timestamp)
     self.assertEqual(result.expires, self.expires)
     self.assertEqual(result.created_with, self.version)
     self.assertEqual(result.public_key, self.public_key)
     self.assertEqual(result.name, self.name)
     self.assertEqual(result.meta, self.meta)
     self.assertEqual(result.sig, self.sig)
     self.assertEqual(result.version, self.version)
Ejemplo n.º 2
0
 def test_from_msgpack_unknown_request(self):
     """
     Ensures the correct exception is raised if the message is not
     recognised.
     """
     # "pang" is "bang" in Swedish (apparently).
     mock_message = msgpack.packb({
         'message': 'pang',
         'uuid': self.uuid,
         'node': self.node,
         'version': self.version
     })
     with self.assertRaises(ValueError) as cm:
         from_msgpack(mock_message)
     ex = cm.exception
     self.assertEqual(2, ex.args[0])
     self.assertEqual(ERRORS[2], ex.args[1])
     self.assertEqual('pang is not a valid message type.',
                      ex.args[2]['context'])
Ejemplo n.º 3
0
 def test_from_msgpack_pong(self):
     """
     Ensures a valid pong message is correctly parsed.
     """
     mock_message = msgpack.packb({
         'message': 'pong',
         'uuid': self.uuid,
         'node': self.node,
         'version': self.version
     })
     result = from_msgpack(mock_message)
     self.assertIsInstance(result, Pong)
     self.assertEqual(result.node, self.node)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.version, self.version)
Ejemplo n.º 4
0
 def test_from_msgpack_findvalue(self):
     """
     Ensures a valid findvalue message is correctly parsed.
     """
     mock_message = msgpack.packb({
         'message': 'findvalue',
         'uuid': self.uuid,
         'node': self.node,
         'key': self.key,
         'version': self.version
     })
     result = from_msgpack(mock_message)
     self.assertIsInstance(result, FindValue)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.node, self.node)
     self.assertEqual(result.key, self.key)
     self.assertEqual(result.version, self.version)
Ejemplo n.º 5
0
 def test_from_msgpack_value(self):
     """
     Ensures a valid value message is correctly parsed.
     """
     mock_message = to_msgpack(self.mock_message)
     result = from_msgpack(mock_message)
     self.assertIsInstance(result, Value)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.node, self.node)
     self.assertEqual(result.key, self.key)
     self.assertEqual(result.value, self.value)
     self.assertEqual(result.timestamp, self.timestamp)
     self.assertEqual(result.public_key, self.public_key)
     self.assertEqual(result.name, self.name)
     self.assertEqual(result.meta, self.meta)
     self.assertEqual(result.sig, self.sig)
     self.assertEqual(result.version, self.version)
Ejemplo n.º 6
0
 def test_string_received_except_to_error(self):
     """
     Sanity test to check error handling works as expected.
     """
     # Mock
     self.transport.loseConnection = MagicMock()
     # Send bad message
     self.protocol.dataReceived('1:a,')
     # Check we receive the expected error in return
     raw_response = self.transport.value()
     msgpack_response = self._from_netstring(raw_response)
     err = from_msgpack(msgpack_response)
     self.assertEqual(3, err.code)
     self.assertEqual(ERRORS[3], err.title)
     self.assertEqual({}, err.details)
     uuidMatch = ('[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-' +
                  '[a-f0-9]{12}')
     self.assertTrue(re.match(uuidMatch, err.uuid))
     # Ensure the loseConnection method was also called.
     self.transport.loseConnection.assert_called_once_with()
Ejemplo n.º 7
0
 def test_from_msgpack_error(self):
     """
     Ensures a valid error message is correctly parsed.
     """
     mock_message = msgpack.packb({
         'message': 'error',
         'uuid': self.uuid,
         'node': self.node,
         'code': 1,
         'title': ERRORS[1],
         'details': {'key': 'value'},
         'version': self.version
     })
     result = from_msgpack(mock_message)
     self.assertIsInstance(result, Error)
     self.assertEqual(result.uuid, self.uuid)
     self.assertEqual(result.node, self.node)
     self.assertEqual(result.code, 1)
     self.assertEqual(result.title, ERRORS[1])
     self.assertEqual(result.details, {'key': 'value'})
     self.assertEqual(result.version, self.version)