def test_exchange_token(mocker, test_client, init_database,
                        initialised_blockchain_network, mock_sms_apis):
    org = OrganisationFactory(country_code='KE')
    sender = UserFactory(preferred_language="en",
                         phone=phone(),
                         first_name="Bob",
                         last_name="Foo",
                         default_organisation=org)
    sender.set_held_role('GROUP_ACCOUNT', 'group_account')

    token1 = Token.query.filter_by(symbol="SM1").first()
    create_transfer_account_for_user(sender, token1, 20000)

    agent = UserFactory(phone=phone(),
                        first_name="Joe",
                        last_name="Bar",
                        default_organisation=org)
    agent.set_held_role('TOKEN_AGENT', 'token_agent')
    # this is under the assumption that token agent would have default token being the reserve token. is this the case?
    reserve = Token.query.filter_by(symbol="AUD").first()
    create_transfer_account_for_user(agent, reserve, 30000)

    def mock_convert(exchange_contract, from_token, to_token, from_amount,
                     signing_address):
        return from_amount * 1.2

    mocker.patch('server.bt.get_conversion_amount', mock_convert)

    def mock_validate(self, transfer):
        pass

    mocker.patch(
        'server.utils.transfer_limits.MinimumSentLimit.validate_transfer',
        mock_validate)

    TokenProcessor.exchange_token(sender, agent, 1000)
    assert default_transfer_account(sender).balance == 19000
    assert default_transfer_account(agent).balance == 31200

    messages = mock_sms_apis

    assert len(messages) == 2
    sent_message = messages[0]
    assert sent_message['phone'] == sender.phone
    assert 'sent a payment of 10.00 SM1 = 12.00 AUD' in sent_message['message']
    received_message = messages[1]
    assert received_message['phone'] == agent.phone
    assert 'received a payment of 12.00 AUD = 10.00 SM1' in received_message[
        'message']
예제 #2
0
 def exchange_token(sender: User, agent: User, amount: float):
     TokenProcessor.exchange_token(sender, agent, amount)