def test_peer_hr_with_data(self): """ Check human readable format for a peer with data. """ peer = manufacture.makeIPv4Address() entry = LogEntry(None, None, None, peer) expected_peer_string = (u'%s:%d' % (peer.host, peer.port)) self.assertEqual(expected_peer_string, entry.peer_hr)
def test_log(self): """ `log` method is here for transition and used the old Logger.log interface to emit an event. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() peer = manufacture.makeIPv4Address() avatar = manufacture.makeFilesystemApplicationAvatar() data = {} handler.log(100, message, peer=peer, avatar=avatar, data=data) log_message = self.popLog() self.assertEqual(100, log_message[0]) self.assertTrue('Some message' in log_message[1]) self.assertEqual(avatar, log_message[2]) self.assertEqual(peer, log_message[3]) self.assertEqual(data, log_message[4])
def test_init(self): """ Check LogEntry initialization. """ log_id = 999999 log_text = manufacture.getUniqueString() log_avatar = manufacture.makeFilesystemApplicationAvatar() log_peer = manufacture.makeIPv4Address() entry = LogEntry(log_id, log_text, log_avatar, log_peer) now = time() self.assertEqual(log_id, entry.message_id) self.assertEqual(log_text, entry.text) self.assertEqual(log_avatar, entry.avatar) self.assertEqual(log_peer, entry.peer) # We just check that human representation of time not empty. self.assertTrue(entry.timestamp_hr) # We just test that the entry's timestamp is very recent. time_diff = now - entry.timestamp self.assertTrue(time_diff <= 0.05, 'Timediff is %f' % time_diff) self.assertTrue(time_diff >= 0.0, 'Timediff is %f' % time_diff)
def test_init(self): """ Check Event initialization. """ event_id = manufacture.getUniqueString() message = manufacture.getUniqueString() data = { 'attr1': 'value1', 'attr2': 'value2', 'peer': manufacture.makeIPv4Address(), 'avatar': manufacture.makeFilesystemApplicationAvatar()} event = Event( id=event_id, message=message, data=data, ) self.assertProvides(IEvent, event) self.assertEqual(event_id, event.id) self.assertEqual(message, event.message) self.assertEqual(data, event.data)
def test_POST_internal_error(self): """ Internal server errors are reported as errors. """ data = ( '{"jsonrpc": "2.0", "id": 6, ' '"method": "public_method_internal_error", "params": []}') resource = ImplementedJSONRPCResource() peer = manufacture.makeIPv4Address() request = manufacture.makeTwistedWebRequest( resource=resource, data=data, peer=peer) result = resource.render_POST(request) self.assertEqual(server.NOT_DONE_YET, result) self.runDeferred(resource._deferred) response = json.loads(request.test_response_content) self.assertIsNone(response['result']) self.assertEqual(-32603, response['error']['code']) self.assertEqual(6, response['id']) self.assertTrue(resource._logInternalError_called) self.assertTrue(u'internal' in resource._logInternalError_value) self.assertEqual(peer, resource._logInternalError_peer)