172 lines
5.6 KiB
Python
172 lines
5.6 KiB
Python
# coding: utf-8
|
|
from __future__ import absolute_import
|
|
|
|
import logging
|
|
import time
|
|
|
|
import hexathon
|
|
import pytest
|
|
from cic_eth.server.app import create_app
|
|
from cic_eth.server.celery import create_celery_wrapper
|
|
from fastapi.testclient import TestClient
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
@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
|
|
|
|
@pytest.fixture(scope='function')
|
|
def client(celery_wrapper):
|
|
app = create_app(celery_wrapper)
|
|
return TestClient(app)
|
|
|
|
|
|
def test_default_token(client,mock_sync_default_token_api_query):
|
|
# Default Token
|
|
response = client.get('/default_token')
|
|
log.debug(f"balance response {response}")
|
|
default_token = response.json()
|
|
assert default_token == {'symbol': 'FOO', 'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385', 'name': 'Giftable Token', 'decimals': 6}
|
|
|
|
def test_token(client, mock_token_api_query):
|
|
# Default Token
|
|
response = client.get('/token?token_symbol=FOO')
|
|
log.debug(f"token response {response}")
|
|
token = response.json()
|
|
assert token == {
|
|
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
|
|
'converters': [],
|
|
'decimals': 6,
|
|
'name': 'Giftable Token',
|
|
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
|
|
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
|
|
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
|
|
'symbol': 'FOO',
|
|
}
|
|
|
|
def test_tokens(client, mock_tokens_api_query):
|
|
# Default Token
|
|
response = client.get(
|
|
'/tokens', params={'token_symbols': ['FOO', 'FOO']})
|
|
|
|
log.debug(f"tokens response {response}")
|
|
tokens = response.json()
|
|
assert tokens == [
|
|
{
|
|
'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',
|
|
},
|
|
]
|
|
|
|
@pytest.mark.skip("Not implemented")
|
|
def test_account(client):
|
|
# Default Token
|
|
response = client.get('/default_token')
|
|
log.debug(f"balance response {response}")
|
|
default_token = response.json()
|
|
|
|
# Create Account 1
|
|
params = {
|
|
'password': '',
|
|
'register': True
|
|
}
|
|
response = client.post(
|
|
'/create_account',
|
|
params=params)
|
|
address_1 = hexathon.valid(response.json())
|
|
|
|
# Create Account 2
|
|
params = {
|
|
'password': '',
|
|
'register': True
|
|
}
|
|
response = client.post('/create_account',
|
|
params=params)
|
|
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
|
|
}
|
|
response = client.get('/balance',
|
|
params=params)
|
|
balance = response.json()
|
|
|
|
assert (balance[0] == {
|
|
"address": default_token.get('address').lower(),
|
|
"balance_available": 30000,
|
|
"balance_incoming": 0,
|
|
"balance_network": 30000,
|
|
"balance_outgoing": 0,
|
|
"converters": []
|
|
})
|
|
|
|
# Transfer
|
|
params = {
|
|
'from_address': address_1,
|
|
'to_address': address_2,
|
|
'value': 100,
|
|
'token_symbol': 'COFE'
|
|
}
|
|
response = client.post('/transfer',
|
|
params=params)
|
|
transfer = response.json()
|
|
|
|
# Balance Account 1
|
|
params = {
|
|
'address': address_1,
|
|
'token_symbol': 'COFE',
|
|
'include_pending': True
|
|
}
|
|
response = client.get('/balance',
|
|
params=params)
|
|
balance_after_transfer = response.json()
|
|
assert (balance_after_transfer[0] == {
|
|
"address": default_token.get('address').lower(),
|
|
"balance_available": 29900,
|
|
"balance_incoming": 0,
|
|
"balance_network": 30000,
|
|
"balance_outgoing": 100,
|
|
"converters": []
|
|
})
|
|
|
|
# Transactions Account 1
|
|
params = {
|
|
'address': address_1,
|
|
'limit': 10
|
|
}
|
|
response = client.get('/transactions',
|
|
params=params)
|
|
transactions = response.json()
|
|
# TODO: What are the other 2 transactions
|
|
assert len(transactions) == 3
|
|
# Check the transaction is correct
|
|
# TODO wtf is READSEND (Ready to send? Or already sent)
|
|
assert transactions[0].status == 'READYSEND'
|