Esempio n. 1
0
    async def test_rollback(self):
        await helpers.call_change_balance(account_id=666,
                                          currency=1,
                                          amount=1000)
        await helpers.call_change_balance(account_id=667,
                                          currency=1,
                                          amount=1000)

        request = await self.client.post('/transactions/start',
                                         data=bank_pb2.StartTransactionRequest(
                                             operations=TEST_OPERATIONS,
                                             lifetime=100).SerializeToString())
        answer = await self.check_success(request,
                                          bank_pb2.StartTransactionResponse)

        request = await self.client.post(
            '/transactions/rollback',
            data=bank_pb2.RollbackTransactionRequest(
                transaction_id=answer.transaction_id).SerializeToString())
        answer = await self.check_success(request,
                                          bank_pb2.StartTransactionResponse)

        results = await db.sql('SELECT * FROM transactions')
        self.assertEqual(results[0]['state'],
                         relations.TRANSACTION_STATE.ROLLBACKED.value)

        results = await db.sql(
            'SELECT * FROM operations ORDER BY created_at ASC')
        self.assertEqual(results, [])

        await self.check_balance(account_id=666, expected_balance={1: 1000})
        await self.check_balance(account_id=667, expected_balance={1: 1000})
Esempio n. 2
0
    async def test_restrictions_saved(self):
        await helpers.call_change_balance(account_id=666,
                                          currency=1,
                                          amount=1000)
        await helpers.call_change_balance(account_id=667,
                                          currency=1,
                                          amount=1000)

        restrictions_data = {
            'hard_minimum': 0,
            'hard_maximum': 10000,
            'soft_minimum': 7,
            'soft_maximum': 7000
        }

        restrictions = s11n.to_json(restrictions_data)

        request = await self.client.post(
            '/transactions/start',
            data=bank_pb2.StartTransactionRequest(
                operations=TEST_OPERATIONS,
                lifetime=0,
                restrictions=restrictions).SerializeToString())

        answer = await self.check_success(request,
                                          bank_pb2.StartTransactionResponse)

        results = await db.sql('SELECT * FROM transactions WHERE id=%(id)s',
                               {'id': answer.transaction_id})

        self.assertEqual(results[0]['data']['restrictions'], restrictions_data)
Esempio n. 3
0
    async def test_started(self):
        await helpers.call_change_balance(account_id=666,
                                          currency=1,
                                          amount=1000)
        await helpers.call_change_balance(account_id=667,
                                          currency=1,
                                          amount=1000)

        lifetime = datetime.timedelta(seconds=100)

        request = await self.client.post('/transactions/start',
                                         data=bank_pb2.StartTransactionRequest(
                                             operations=TEST_OPERATIONS,
                                             lifetime=100).SerializeToString())
        answer = await self.check_success(request,
                                          bank_pb2.StartTransactionResponse)

        results = await db.sql('SELECT * FROM transactions WHERE id=%(id)s',
                               {'id': answer.transaction_id})

        self.assertEqual(results[0]['state'],
                         relations.TRANSACTION_STATE.OPENED.value)
        self.assertEqual(results[0]['lifetime'], lifetime)

        results = await db.sql('SELECT transaction FROM operations')

        loaded_operations = await load_operations()

        self.assertEqual(TEST_OPERATIONS, loaded_operations)

        # only withdraws applied on transaction start
        await self.check_balance(account_id=666, expected_balance={1: 0})
        await self.check_balance(account_id=667, expected_balance={1: 900})
Esempio n. 4
0
 async def test_no_operations(self):
     request = await self.client.post('/transactions/start',
                                      data=bank_pb2.StartTransactionRequest(
                                          operations=[],
                                          lifetime=0).SerializeToString())
     await self.check_error(
         request, error='bank.start_transaction.no_operations_specified')
Esempio n. 5
0
    async def apply_operations(self, test_operations):
        request = await self.client.post('/transactions/start',
                                         data=bank_pb2.StartTransactionRequest(
                                             operations=test_operations,
                                             lifetime=0).SerializeToString())
        answer = await self.check_success(request,
                                          bank_pb2.StartTransactionResponse)

        await self.client.post(
            '/transactions/commit',
            data=bank_pb2.CommitTransactionRequest(
                transaction_id=answer.transaction_id).SerializeToString())
Esempio n. 6
0
    async def test_small_balance(self):
        request = await self.client.post('/transactions/start', data=bank_pb2.StartTransactionRequest(operations=TEST_OPERATIONS,
                                                                                                      lifetime=0).SerializeToString())
        await self.check_error(request, error='bank.start_transaction.no_enough_currency')

        results = await db.sql('SELECT * FROM transactions')
        self.assertEqual(results, [])

        results = await db.sql('SELECT * FROM operations ORDER BY created_at ASC')
        self.assertEqual(results, [])

        await self.check_balance(account_id=666, expected_balance={})
        await self.check_balance(account_id=667, expected_balance={})
Esempio n. 7
0
def change_energy_balance(account_id, type, energy, async=False, autocommit=False):

    if async and not autocommit:
        raise exceptions.AutocommitRequiredForAsyncTransaction()

    operations = [bank_pb2.Operation(account_id=account_id,
                                     currency=ENERGY_CURRENCY_ID,
                                     amount=energy,
                                     type=type)]

    if not async:
        try:
            answer = tt_api.sync_request(url=conf.game_settings.TT_ENERGY_START_TRANSACTION,
                                         data=bank_pb2.StartTransactionRequest(lifetime=conf.game_settings.ENERGY_TRANSACTION_LIFETIME,
                                                                               operations=operations,
                                                                               autocommit=autocommit),
                                         AnswerType=bank_pb2.StartTransactionResponse)
        except utils_exceptions.TTAPIUnexpectedAPIStatus:
            return False, None

        return True, answer.transaction_id

    tt_api.async_request(url=conf.game_settings.TT_ENERGY_START_TRANSACTION,
                         data=bank_pb2.StartTransactionRequest(lifetime=conf.game_settings.ENERGY_TRANSACTION_LIFETIME,
                                                               operations=operations,
                                                               autocommit=autocommit))

    return True, None