2021-11-22 10:28:09 +01:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
import hexathon
|
2022-01-05 10:16:48 +01:00
|
|
|
from cic_eth.server.app import create_app
|
|
|
|
from cic_eth_registry.pytest.fixtures_tokens import *
|
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-05 10:16:48 +01:00
|
|
|
@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
|
2021-12-21 19:15:45 +01:00
|
|
|
|
2022-01-05 10:16:48 +01:00
|
|
|
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()}")
|
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()
|
|
|
|
assert default_token == {
|
|
|
|
'address': '3FF776B6f888980DEf9d4220858803f9dC5e341e',
|
2022-01-05 10:16:48 +01:00
|
|
|
'decimals': 7,
|
2021-12-21 19:15:45 +01:00
|
|
|
'name': 'Giftable Token',
|
|
|
|
'symbol': 'GFT',
|
|
|
|
}
|
2021-11-22 10:28:09 +01:00
|
|
|
|
|
|
|
|
2022-01-05 10:16:48 +01:00
|
|
|
def test_token(client):
|
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 == {
|
|
|
|
'address': '3ff776b6f888980def9d4220858803f9dc5e341e',
|
|
|
|
'converters': [],
|
|
|
|
'decimals': 6,
|
|
|
|
'name': 'Giftable Token',
|
|
|
|
'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'],
|
|
|
|
'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328',
|
|
|
|
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
|
|
|
|
'symbol': 'GFT',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-05 10:16:48 +01:00
|
|
|
def test_tokens(client):
|
2021-12-21 19:15:45 +01:00
|
|
|
# Default Token
|
2022-01-05 10:16:48 +01:00
|
|
|
response = client.get(
|
|
|
|
'/tokens', params={'token_symbols': ['GFT', 'COFE', '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 == [
|
|
|
|
{'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'},
|
|
|
|
]
|
2021-12-21 19:15:45 +01:00
|
|
|
|
|
|
|
|
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'
|