cic-internal-integration/apps/cic-eth/tests/test_server.py

172 lines
5.6 KiB
Python
Raw Permalink Normal View History

2021-11-22 10:28:09 +01:00
# coding: utf-8
from __future__ import absolute_import
import logging
import time
import hexathon
2022-01-10 13:43:10 +01:00
import pytest
2022-01-05 10:16:48 +01:00
from cic_eth.server.app import create_app
2022-01-10 13:43:10 +01:00
from cic_eth.server.celery import create_celery_wrapper
2021-12-21 19:15:45 +01:00
from fastapi.testclient import TestClient
2021-11-22 10:28:09 +01:00
log = logging.getLogger(__name__)
2022-01-10 13:43:10 +01:00
@pytest.fixture(scope='function')
def celery_wrapper(api):
""" Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call. Catch allows you to specify how many messages to catch before returning.
"""
def wrapper(method, *args, catch=1, **kwargs):
t = getattr(api, method)(*args, **kwargs)
return t.get()
return wrapper
2021-12-21 19:15:45 +01:00
2022-01-10 13:43:10 +01:00
@pytest.fixture(scope='function')
def client(celery_wrapper):
app = create_app(celery_wrapper)
2022-01-05 10:16:48 +01:00
return TestClient(app)
2022-01-10 13:43:10 +01:00
def test_default_token(client,mock_sync_default_token_api_query):
2021-12-21 19:15:45 +01:00
# Default Token
response = client.get('/default_token')
log.debug(f"balance response {response}")
default_token = response.json()
2022-01-10 13:43:10 +01:00
assert default_token == {'symbol': 'FOO', 'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385', 'name': 'Giftable Token', 'decimals': 6}
2021-11-22 10:28:09 +01:00
2022-01-10 13:43:10 +01:00
def test_token(client, mock_token_api_query):
2021-11-22 10:28:09 +01:00
# Default Token
2022-01-05 10:16:48 +01:00
response = client.get('/token?token_symbol=FOO')
2021-12-21 19:15:45 +01:00
log.debug(f"token response {response}")
token = response.json()
assert token == {
2022-01-10 13:43:10 +01:00
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
2021-12-21 19:15:45 +01:00
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
2022-01-10 13:43:10 +01:00
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'FOO',
2021-12-21 19:15:45 +01:00
}
2022-01-10 13:43:10 +01:00
def test_tokens(client, mock_tokens_api_query):
2021-12-21 19:15:45 +01:00
# Default Token
2022-01-05 10:16:48 +01:00
response = client.get(
2022-01-10 13:43:10 +01:00
'/tokens', params={'token_symbols': ['FOO', 'FOO']})
2021-12-21 19:15:45 +01:00
log.debug(f"tokens response {response}")
tokens = response.json()
2021-12-23 08:36:29 +01:00
assert tokens == [
2022-01-10 13:43:10 +01:00
{
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'FOO',
},
{
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'FOO',
},
2021-12-23 08:36:29 +01:00
]
2021-12-21 19:15:45 +01:00
2022-01-10 13:43:10 +01:00
@pytest.mark.skip("Not implemented")
2022-01-05 10:16:48 +01:00
def test_account(client):
2021-12-21 19:15:45 +01:00
# Default Token
2021-12-23 08:36:29 +01:00
response = client.get('/default_token')
2021-11-22 10:28:09 +01:00
log.debug(f"balance response {response}")
default_token = response.json()
# Create Account 1
params = {
'password': '',
'register': True
}
2021-12-21 19:15:45 +01:00
response = client.post(
'/create_account',
2021-11-22 10:28:09 +01:00
params=params)
address_1 = hexathon.valid(response.json())
# Create Account 2
params = {
'password': '',
'register': True
}
2021-12-21 19:15:45 +01:00
response = client.post('/create_account',
params=params)
2021-11-22 10:28:09 +01:00
address_2 = hexathon.valid(response.json())
time.sleep(30) # Required to allow balance to show
# Balance Account 1
params = {
'address': address_1,
'token_symbol': 'COFE',
'include_pending': True
}
2021-12-21 19:15:45 +01:00
response = client.get('/balance',
params=params)
2021-11-22 10:28:09 +01:00
balance = response.json()
assert (balance[0] == {
"address": default_token.get('address').lower(),
2021-12-02 13:07:49 +01:00
"balance_available": 30000,
2021-11-22 10:28:09 +01:00
"balance_incoming": 0,
2021-12-02 13:07:49 +01:00
"balance_network": 30000,
2021-11-22 10:28:09 +01:00
"balance_outgoing": 0,
"converters": []
})
# Transfer
params = {
'from_address': address_1,
'to_address': address_2,
'value': 100,
'token_symbol': 'COFE'
}
2021-12-21 19:15:45 +01:00
response = client.post('/transfer',
params=params)
2021-11-22 10:28:09 +01:00
transfer = response.json()
# Balance Account 1
params = {
'address': address_1,
'token_symbol': 'COFE',
'include_pending': True
}
2021-12-21 19:15:45 +01:00
response = client.get('/balance',
params=params)
2021-11-22 10:28:09 +01:00
balance_after_transfer = response.json()
assert (balance_after_transfer[0] == {
"address": default_token.get('address').lower(),
2021-12-02 13:07:49 +01:00
"balance_available": 29900,
2021-11-22 10:28:09 +01:00
"balance_incoming": 0,
2021-12-02 13:07:49 +01:00
"balance_network": 30000,
"balance_outgoing": 100,
2021-11-22 10:28:09 +01:00
"converters": []
})
# Transactions Account 1
params = {
'address': address_1,
'limit': 10
}
2021-12-21 19:15:45 +01:00
response = client.get('/transactions',
params=params)
2021-11-22 10:28:09 +01:00
transactions = response.json()
2021-12-21 19:15:45 +01:00
# TODO: What are the other 2 transactions
2021-12-02 13:07:49 +01:00
assert len(transactions) == 3
2021-12-21 19:15:45 +01:00
# Check the transaction is correct
2021-12-02 13:07:49 +01:00
# TODO wtf is READSEND (Ready to send? Or already sent)
assert transactions[0].status == 'READYSEND'