def test_build_messages_multiple_event_msg(setup): context.event_handler_manager = EventHandlerManager() single_request = EventRequestSingle(1, 1, {}, 1442502890000) request = EventRequestBatch([single_request]) messages = request.build_messages() print messages assert len(messages) == 2 assert set([message['message_id'] for message in messages]) == {1, 2}
def test_build_messages_single_event_msg(setup): context.event_handler_manager = EventHandlerManager() single_request = EventRequestSingle(1, 2, {}, 1442502890000) request = EventRequestBatch([single_request]) messages = request.build_messages() print messages assert len(messages) == 1 assert messages[0]['message_id'] == 3
def create_request(self, requests): valid_requests = [] validator = JsonEventValidator() for request in requests: if validator.validate_single(request): valid_requests.append(EventRequestSingle(request['user_id'], request['event_id'], request['pairs'] if 'pairs' in request else {}, request['timestamp'])) return EventRequestBatch(valid_requests)
def create_request(self, requests): valid_requests = [] validator = ProtoEventValidator() for request in requests: if validator.validate_single(request): valid_requests.append(EventRequestSingle(request.user_id, request.event_id, {pair.key:pair.value for pair in request.pairs}, request.timestamp)) return EventRequestBatch(valid_requests)
def test_login_proto_without_platform_id(mock_log): '''Test that a login proto without platform id fails validation.''' event_proto = EventMessage_pb2.Event() pair = event_proto.pairs.add() pair.key = 'deviceToken' pair.value = '1234' pair = event_proto.pairs.add() pair.key = 'applicationVersion' pair.value = '1' params = dict((pair.key, pair.value) for pair in event_proto.pairs) event = EventRequestSingle(123, config.login_event_id, params, 12345) assert not LoginEventHandler().validate(event, params)
def test_login_and_then_other_event(setup, mocker): '''Tests that both devices are persisted if they have different tokens''' context.event_handler_manager = EventHandlerManager() event_batch_platform1 = create_batch_with_login_event( user_id=1338, platform_id=1, device_token='str_device_token_1') event_request_platform1 = EventRequestBatch([event_batch_platform1]) event_request_platform1.process() # test no message event1 = EventRequestSingle(1, -1, None, 1442502890000) event_request_other1 = EventRequestBatch([event1]) messages1 = event_request_other1.build_messages() assert len(messages1) == 0 # test parameter from event params = {'title_param': 'param title', 'text_param': 'param content'} event2 = EventRequestSingle(user_id=1, event_id=3, pairs=params, timestamp=2442502890000) event_request_other2 = EventRequestBatch([event2]) messages2 = event_request_other2.build_messages() assert len(messages2) == 1 assert messages2[0]['message_id'] == 4 assert messages2[0]['content'] == 'text param content' assert messages2[0]['title'] == 'title param title' # test missing parameter which is required by localization event3 = EventRequestSingle(user_id=1, event_id=3, pairs=None, timestamp=1442502890000) event_request_other3 = EventRequestBatch([event3]) messages3 = event_request_other3.build_messages() assert len(messages3) == 0
def test_valid_login_proto(mock_log): '''Test that a valid login proto is validated correctly.''' event_proto = EventMessage_pb2.Event() pair = event_proto.pairs.add() pair.key = 'platformId' pair.value = '1' pair = event_proto.pairs.add() pair.key = 'deviceToken' pair.value = '1234' pair = event_proto.pairs.add() pair.key = 'applicationVersion' pair.value = '1' params = dict((pair.key, pair.value) for pair in event_proto.pairs) event = EventRequestSingle(123, config.login_event_id, params, 12345) assert LoginEventHandler().validate(event, params)
def create_request(self, requests): valid_requests = [] validator = ProtoEventValidator() for request in requests: if validator.validate_single(request): valid_requests.append( EventRequestSingle( request.user_id, request.event_id, {pair.key: pair.value for pair in request.pairs}, request.timestamp)) else: context.main_logger.error("Request not valid: {req}".format( req=str(request.__dict__))) return EventRequestBatch(valid_requests)
def create_batch_with_login_event(user_id, platform_id, device_token): event_proto = EventMessage_pb2.Event() # add required parameters pair = event_proto.pairs.add() pair.key = 'platformId' pair.value = str(platform_id) pair = event_proto.pairs.add() pair.key = 'deviceToken' pair.value = device_token pair = event_proto.pairs.add() pair.key = 'applicationVersion' pair.value = '1' params = dict((pair.key, pair.value) for pair in event_proto.pairs) event = EventRequestSingle(user_id, config.login_event_id, params, 1442502890000) return event
def test_login_and_then_other_event(setup, mocker): '''Tests that both devices are persisted if they have different tokens''' context.event_handler_manager = EventHandlerManager() event_batch_platform1 = create_batch_with_login_event( user_id=1338, platform_id=1, device_id='str_device_id_1', device_token='str_device_token_1') event_request_platform1 = EventRequestBatch([event_batch_platform1]) event_request_platform1.process() # test no message event3 = EventRequestSingle(1, -1, None, 1442502890000) event_request_other3 = EventRequestBatch([event3]) messages3 = event_request_other3.build_messages() assert len(messages3) == 0
def test_event_proto_without_timestamp(mock_log): '''Test that an event proto without timestamp fails validation''' event = EventRequestSingle(None, 1, None) event_handler = EventHandler(1) assert not event_handler.validate(event, {})
def test_event_proto_without_user_id(mock_log): '''Test that an event proto without user_id fails validation''' event = EventRequestSingle(None, 1, None, 12345) event_handler = EventHandler(1) assert not event_handler.validate(event, {})
def test_valid_event_proto(mock_log, ): '''Test that a valid event proto is validated correctly.''' event = EventRequestSingle(123, 1, None, 12345) event_handler = EventHandler(1) assert event_handler.validate(event, {})
def test_build_messages_missing_event(setup): context.event_handler_manager = EventHandlerManager() single_request = EventRequestSingle(1, 5, {}, 1442502890000) request = EventRequestBatch([single_request]) assert len(request.build_messages()) == 0