async def test_has_logs(self): item_id = uuid.uuid4() operations = [storage_pb2.Operation(create=storage_pb2.OperationCreate(owner_id=1, item_id=item_id.hex, data='{"a": 1}', operation_type='#test_1', base_type='base-type', full_type='full-type')), storage_pb2.Operation(create=storage_pb2.OperationCreate(owner_id=2, item_id=uuid.uuid4().hex, data='{"a": 2}', operation_type='#test_2', base_type='base-type', full_type='full-type')), storage_pb2.Operation(change_owner=storage_pb2.OperationChangeOwner(item_id=item_id.hex, old_owner_id=1, new_owner_id=3, new_storage_id=4, operation_type='#test_3'))] for operation in operations: request = await self.client.post('/apply', data=storage_pb2.ApplyRequest(operations=[operation]).SerializeToString()) await self.check_answer(request, storage_pb2.ApplyResponse) request = await self.client.post('/get-item-logs', data=storage_pb2.GetItemLogsRequest(item_id=item_id.hex).SerializeToString()) data = await self.check_answer(request, storage_pb2.GetItemLogsResponse) self.assertEqual(len(data.logs), 2) self.assertEqual(s11n.from_json(data.logs[0].data)['operation_type'], '#test_1') self.assertEqual(s11n.from_json(data.logs[1].data)['operation_type'], '#test_3')
async def add_event(message, **kwargs): await operations.add_event(tags=frozenset(message.tags), data=s11n.from_json(message.data), turn=message.turn, time=datetime.datetime.fromtimestamp( message.time)) return events_log_pb2.AddEventResponse()
async def test_not_exists(self): request = await self.request(uuid.uuid4()) answer = await self.check_success(request, data_protector_pb2.GetReportResponse) self.assertEqual(answer.report.state, relations.REPORT_STATE.NOT_EXISTS.value) self.assertEqual(s11n.from_json(answer.report.data), [])
def _to_operation_create(pb_operation): return objects.OperationCreate(owner_id=pb_operation.owner_id, item_id=pb_operation.item_id, data=s11n.from_json(pb_operation.data), base_type=pb_operation.base_type, full_type=pb_operation.full_type, operation_type=pb_operation.operation_type, storage_id=pb_operation.storage_id)
async def test_has_data(self): # prepair data await operations.set_properties([ helpers.property(object_id=100, type=200, value='x.1', mode=relations.MODE.APPEND), helpers.property(object_id=200, type=200, value='x.2', mode=relations.MODE.APPEND), helpers.property(object_id=100, type=200, value='y.1', mode=relations.MODE.APPEND), helpers.property(object_id=300, type=200, value='y.2', mode=relations.MODE.APPEND), helpers.property(object_id=100, type=400, value='x.4', mode=relations.MODE.APPEND) ]) # request data secret = helpers.get_config()['custom']['data_protector']['secret'] request = await self.client.post( '/data-protection-collect-data', data=data_protector_pb2.PluginReportRequest( account_id=str('100'), secret=secret).SerializeToString()) response = await self.check_success( request, data_protector_pb2.PluginReportResponse, raw=True) self.assertEqual( response.result, data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS')) report = s11n.from_json(response.data) self.assertCountEqual(report, [['property', { 'type': 200, 'value': 'x.1' }], ['property', { 'type': 200, 'value': 'y.1' }], ['property', { 'type': 400, 'value': 'x.4' }]])
async def check_xsolla_error(self, response, error, details, status_code=400): self.assertEqual(response.status, status_code) content = await response.content.read() data = s11n.from_json(content) self.assertEqual(data['error']['code'], error) self.assertEqual(data['error']['details']['code'], details)
async def xsolla_extractor(request, config, logger): content = await request.content.read() auth_header = request.headers.get('Authorization', '') request_signature = get_auth_signature(auth_header) if request_signature is None: raise tt_exceptions.ApiError( code='INVALID_SIGNATURE', message='signature has not found in headers', details={ 'AuthorisationHeader': auth_header, 'code': 'xsolla.hook.signature_has_not_found' }) expected_signature = get_expected_auth_signature( content, config['custom']['hooks_key']) if request_signature != expected_signature: raise tt_exceptions.ApiError( code='INVALID_SIGNATURE', message='Received signature does not equal to calculated', details={ 'AuthorisationHeader': auth_header, 'code': 'xsolla.hook.unexpected_signature' }) data = s11n.from_json(content) if 'settings' not in data: raise tt_exceptions.ApiError( code='INVALID_PARAMETER', message='settings not found in request body', details={'code': 'xsolla.hook.no_settings_info'}) if data['settings'].get('project_id') != config['custom']['project_id']: raise tt_exceptions.ApiError( code='INVALID_PARAMETER', message='Unexpected project_id on request body', details={'code': 'xsolla.hook.wrong_project_id'}) if data['settings'].get('merchant_id') != config['custom']['merchant_id']: raise tt_exceptions.ApiError( code='INVALID_PARAMETER', message='Unexpected merchant_id on request body', details={'code': 'xsolla.hook.wrong_merchant_id'}) return data
async def test_serialization(self): events = await self.prepair_data() response = await self.client.post('/get-events', data=events_log_pb2.GetEventsRequest(tags=[], page=1, records_on_page=1).SerializeToString()) data = await self.check_success(response, events_log_pb2.GetEventsResponse) event = data.events[0] self.assertEqual(event.id, events[0].id) self.assertEqual(s11n.from_json(event.data), {'turn': 0}) self.assertEqual(set(event.tags), {1, 2}) self.assertEqual(event.turn, 0) self.assertEqual(event.time, time.mktime(events[0].created_at.timetuple())+events[0].created_at.microsecond / 1000000)
async def test_no_data(self): secret = helpers.get_config()['custom']['data_protector']['secret'] request = await self.client.post( '/data-protection-collect-data', data=data_protector_pb2.PluginReportRequest( account_id='777', secret=secret).SerializeToString()) response = await self.check_success( request, data_protector_pb2.PluginReportResponse, raw=True) self.assertEqual( response.result, data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS')) report = s11n.from_json(response.data) self.assertEqual(report, [])
async def test_serialization(self): events = await self.prepair_data() response = await self.client.post( '/get-events', data=events_log_pb2.GetEventsRequest( tags=[], page=1, records_on_page=1).SerializeToString()) data = await self.check_success(response, events_log_pb2.GetEventsResponse) event = data.events[0] self.assertEqual(event.id, events[0].id) self.assertEqual(s11n.from_json(event.data), {'turn': 0}) self.assertEqual(set(event.tags), {1, 2}) self.assertEqual(event.turn, 0) self.assertEqual( event.time, time.mktime(events[0].created_at.timetuple()) + events[0].created_at.microsecond / 1000000)
async def test_has_data(self): # prepair data accounts_infos = await helpers.create_accounts(game_ids=(666, 777, 888) ) await helpers.force_bind(accounts_infos[1].id, discord_id=100500) await helpers.force_bind(accounts_infos[2].id, discord_id=100501) code_1_1 = await operations.get_bind_code(accounts_infos[0].id, expire_timeout=60) code_2 = await operations.get_bind_code(accounts_infos[2].id, expire_timeout=60) code_1_2 = await operations.get_bind_code(accounts_infos[0].id, expire_timeout=60) new_info = await operations.get_account_info_by_id(accounts_infos[2].id ) # request data secret = helpers.get_config()['custom']['data_protector']['secret'] request = await self.client.post( '/data-protection-collect-data', data=data_protector_pb2.PluginReportRequest( account_id='888', secret=secret).SerializeToString()) response = await self.check_success( request, data_protector_pb2.PluginReportResponse, raw=True) self.assertEqual( response.result, data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS')) report = s11n.from_json(response.data) self.assertEqual( report, [['discord_id', 100501], ['bind_code', code_2.data()]])
async def test_has_data(self): infos, invoices = await self.prepair_complex_data() secret = helpers.get_config()['custom']['data_protector']['secret'] request = await self.client.post( '/data-protection-collect-data', data=data_protector_pb2.PluginReportRequest( account_id=str(infos[0].id), secret=secret).SerializeToString()) response = await self.check_success( request, data_protector_pb2.PluginReportResponse, raw=True) self.assertEqual( response.result, data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS')) report = s11n.from_json(response.data) self.assertCountEqual(report, [ ['account_info', { 'email': infos[0].email, 'name': infos[0].name }], [ 'invoice', { 'purchased_amount': invoices[0].purchased_amount, 'xsolla_id': invoices[0].xsolla_id } ], [ 'invoice', { 'purchased_amount': invoices[2].purchased_amount, 'xsolla_id': invoices[2].xsolla_id } ] ])
async def fill_subreport(self, subreport): logger = log.ContextLogger() logger.info('fill subreport for %s', subreport) answer = await self.request_report(str(subreport.data['id']), logger=logger) if answer is None: logger.error('no answer received, stop processing') return None if answer.result == data_protector_pb2.PluginReportResponse.ResultType.Value( 'FAILED'): logger.error('answer FAILED, stop processing') return None if answer.result != data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS'): raise NotImplementedError('unknowm result type') if answer.data: report = s11n.from_json(answer.data) else: report = [] data = copy.copy(subreport.data) new_data = [(subreport.source, data_type, data_value) for data_type, data_value in report] data['report'].extend(new_data) logger.error('subreport updated, collected data: %s', len(new_data)) return subreport.replace(state=relations.SUBREPORT_STATE.READY, data=data)
async def test_success(self): report_id = await operations.create_report_base([("test_source_1", 2), ("test_source_2", 666) ]) await logic.process_all(helpers.get_config()['custom']) request = await self.request(report_id) answer = await self.check_success(request, data_protector_pb2.GetReportResponse) report = await operations.get_report(report_id) self.assertEqual(answer.report.state, relations.REPORT_STATE.READY.value) self.assertEqual(s11n.from_json(answer.report.data), [['test_source_1', 'type_3', 'data_3'], ['test_source_2', 'xxx', 666]]) self.assertEqual(answer.report.completed_at, utils.postgres_time_to_timestamp(report.completed_at)) self.assertEqual(answer.report.expire_at, utils.postgres_time_to_timestamp(report.expire_at))
async def test_has_data(self): # prepair data message_1_id = await operations.send_message(sender_id=1, recipients_ids=[2, 3, 4], body='1') message_2_id = await operations.send_message(sender_id=2, recipients_ids=[3, 4, 5], body='2') message_3_id = await operations.send_message(sender_id=3, recipients_ids=[4, 5, 6], body='3') message_4_id = await operations.send_message(sender_id=4, recipients_ids=[2], body='4') messages_ids = [message_1_id, message_2_id, message_3_id, message_4_id] for message_id, account_id in [(messages_ids[0], 1), (messages_ids[0], 2), (messages_ids[0], 3), (messages_ids[0], 4), (messages_ids[1], 2), (messages_ids[2], 4), (messages_ids[2], 5), (messages_ids[2], 6), (messages_ids[3], 4), (messages_ids[3], 2)]: await operations.hide_message(account_id, message_id) removed_messages_ids = await operations.candidates_to_remove_ids() self.assertNotEqual(removed_messages_ids, []) await operations.remove_messages(removed_messages_ids) messages = [] test_account_id = 3 for message_id in messages_ids: message = await operations.load_message(test_account_id, message_id, visibility=None) messages.append(message) # request data secret = helpers.get_config()['custom']['data_protector']['secret'] request = await self.client.post( '/data-protection-collect-data', data=data_protector_pb2.PluginReportRequest( account_id=str(test_account_id), secret=secret).SerializeToString()) response = await self.check_success( request, data_protector_pb2.PluginReportResponse, raw=True) self.assertEqual( response.result, data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS')) report = s11n.from_json(response.data) self.assertCountEqual( report, [['message', messages[message_index].data_of(test_account_id)] for message_index in [1, 2]])
def to_restrictions(pb_restrictions): if not pb_restrictions: return objects.Restrictions() return objects.Restrictions(**s11n.from_json(pb_restrictions))
async def add_event(message, **kwargs): await operations.add_event(tags=frozenset(message.tags), data=s11n.from_json(message.data), turn=message.turn, time=datetime.datetime.fromtimestamp(message.time)) return events_log_pb2.AddEventResponse()