cic-internal-integration/apps/cic-eth/tests/test_server.py
2022-01-05 12:16:48 +03:00

178 lines
5.6 KiB
Python

# coding: utf-8
from __future__ import absolute_import
import logging
import time
import hexathon
from cic_eth.server.app import create_app
from cic_eth_registry.pytest.fixtures_tokens import *
from fastapi.testclient import TestClient
log = logging.getLogger(__name__)
@pytest.fixture(scope='session')
def client(config, default_chain_spec, celery_session_worker):
config.add(str(default_chain_spec), 'CHAIN_SPEC', exists_ok=True)
config.add('cic_eth_test', 'CELERY_QUEUE', exists_ok=True)
chain_spec = config.get('CHAIN_SPEC')
celery_queue = config.get('CELERY_QUEUE')
redis_host = config.get('REDIS_REDIS_HOST')
redis_port = config.get('REDIS_REDIS_PORT')
redis_db = config.get('REDIS_DB')
redis_timeout = 20
app = create_app(str(chain_spec), celery_queue, redis_host,
redis_port, redis_db, redis_timeout)
return TestClient(app)
def test_default_token(client, foo_token, default_chain_spec):
print(foo_token)
print(f"default_chain_spec: {default_chain_spec.asdict()}")
# Default Token
response = client.get('/default_token')
log.debug(f"balance response {response}")
default_token = response.json()
assert default_token == {
'address': '3FF776B6f888980DEf9d4220858803f9dC5e341e',
'decimals': 7,
'name': 'Giftable Token',
'symbol': 'GFT',
}
def test_token(client):
# Default Token
response = client.get('/token?token_symbol=FOO')
log.debug(f"token response {response}")
token = response.json()
assert token == {
'address': '3ff776b6f888980def9d4220858803f9dc5e341e',
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'],
'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'GFT',
}
def test_tokens(client):
# Default Token
response = client.get(
'/tokens', params={'token_symbols': ['GFT', 'COFE', 'FOO']})
log.debug(f"tokens response {response}")
tokens = response.json()
assert tokens == [
{'address': '3ff776b6f888980def9d4220858803f9dc5e341e',
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'],
'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'GFT'},
{'address': '9c9506cf6c50f5b0371be72920fc6060d1a88a6a',
'converters': [],
'decimals': 6,
'name': 'Coffee',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C',
'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'COFE'},
]
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'