From 3c3745228e09b37c06e40ddf2093e0dc9577f196 Mon Sep 17 00:00:00 2001 From: William Luke Date: Mon, 22 Nov 2021 12:28:09 +0300 Subject: [PATCH] convert api tests to uwsgi --- .../cic_eth/runnable/daemons/server.py | 5 +- .../cic_eth/server/UWSGIOpenAPIRequest.py | 1 + .../cic_eth/server/openapi/server.yaml | 52 +++--- apps/cic-eth/test_requirements.txt | 1 + .../tests/server/test_account_controller.py | 159 ------------------ .../tests/server/test_token_controller.py | 43 ----- apps/cic-eth/tests/test_server.py | 115 +++++++++++++ docker-compose.yml | 6 +- 8 files changed, 147 insertions(+), 235 deletions(-) delete mode 100644 apps/cic-eth/tests/server/test_account_controller.py delete mode 100644 apps/cic-eth/tests/server/test_token_controller.py create mode 100644 apps/cic-eth/tests/test_server.py diff --git a/apps/cic-eth/cic_eth/runnable/daemons/server.py b/apps/cic-eth/cic_eth/runnable/daemons/server.py index 4e4da49d..e6f66084 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/server.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/server.py @@ -19,10 +19,9 @@ spec = create_spec(spec_dict) logging.basicConfig(level=logging.WARNING) log = logging.getLogger() +# TODO Implement wei conversions # uwsgi application - - def application(env, start_response): oAPIRequest = UWSGIOpenAPIRequest(env) validator = RequestValidator(spec) @@ -46,7 +45,7 @@ def application(env, start_response): # Setup Channel to receive the result - if path == '/list': + if path == '/transactions': address = params.pop('address') print('address', address, ) # address, limit=10 diff --git a/apps/cic-eth/cic_eth/server/UWSGIOpenAPIRequest.py b/apps/cic-eth/cic_eth/server/UWSGIOpenAPIRequest.py index 9e93b9c8..0161c8a6 100644 --- a/apps/cic-eth/cic_eth/server/UWSGIOpenAPIRequest.py +++ b/apps/cic-eth/cic_eth/server/UWSGIOpenAPIRequest.py @@ -58,4 +58,5 @@ class UWSGIOpenAPIRequestFactory: body=body, mimetype=mimetype, ) + UWSGIOpenAPIRequest = UWSGIOpenAPIRequestFactory.create \ No newline at end of file diff --git a/apps/cic-eth/cic_eth/server/openapi/server.yaml b/apps/cic-eth/cic_eth/server/openapi/server.yaml index 0eef8139..c43bd27a 100644 --- a/apps/cic-eth/cic_eth/server/openapi/server.yaml +++ b/apps/cic-eth/cic_eth/server/openapi/server.yaml @@ -13,7 +13,7 @@ info: servers: - url: / paths: - /token: + /default_token: description: Retrieves the default fallback token of the custodial network. get: tags: @@ -62,7 +62,7 @@ paths: type: array items: $ref: "#/components/schemas/TokenBalance" - + x-openapi-router-controller: cic_eth.server.controllers.account_controller /transactions: description: @@ -218,78 +218,78 @@ components: Transaction: type: object properties: - block_number: + block_number: type: integer format: int32 example: 24531 - date_checked: + date_checked: type: string example: 2021-11-12T09:36:40.725296 - date_created: + date_created: type: string example: 2021-11-12T09:36:40.131292 - date_updated: + date_updated: type: string example: 2021-11-12T09:36:40.131292 - destination_token: + destination_token: type: string example: 0x3ff776b6f888980def9d4220858803f9dc5e341e - destination_token_decimals: + destination_token_decimals: type: integer format: int32 example: 6 - destination_token_symbol: + destination_token_symbol: type: string example: COFE - from_value: + from_value: type: integer format: int32 example: 100000000 - hash: + hash: type: string example: 0xc7d160b4f1c89f09cbccbc2c4f6a72760bc3c1634a88438870c31b2e4d9e2bf3 - nonce: + nonce: type: integer format: int32 example: 1 - recipient: + recipient: type: string example: 872e1ec9d499b242ebfcfd0a279a4c3e0cd472c0 - sender: + sender: type: string example: 1a92b05e0b880127a4c26ac0f68a52df3ac6b89d - signed_tx: + signed_tx: type: string example: 0xf8aa018310c8e0837a1200943ff776b6f888980def9d4220858803f9dc5e341e80b844a9059cbb000000000000000000000000872e1ec9d499b242ebfcfd0a279a4c3e0cd472c00000000000000000000000000000000000000000000000000000000005f5e10082466ca0617d50ea726dfe61d6dc5e8a4a85cf7469514f394250cecb019006317cfb94d3a04930e14524f0a87db623a80e0f841ab613f693f5031c6a136873052ae7bba08e - source_token: + source_token: type: string example: 0x3ff776b6f888980def9d4220858803f9dc5e341e - source_token_decimals: + source_token_decimals: type: integer format: int32 example: 6 - source_token_symbol: + source_token_symbol: type: string example: COFE - status: + status: type: string example: SUCCESS - status_code: + status_code: type: integer format: int32 example: 4104 - timestamp: + timestamp: type: integer format: int32 example: 1636709800 - to_value: + to_value: type: integer format: int32 example: 100000000 - tx_hash: + tx_hash: type: string example: 0xc7d160b4f1c89f09cbccbc2c4f6a72760bc3c1634a88438870c31b2e4d9e2bf3 - tx_index: + tx_index: type: integer format: int32 example: 0 @@ -310,8 +310,8 @@ components: type: integer description: Decimals example: - symbol: 'GTF' - address: '3FF776B6f888980DEf9d4220858803f9dC5e341e' + symbol: "GTF" + address: "3FF776B6f888980DEf9d4220858803f9dC5e341e" decimals: 6 name: "Gift Token" TokenBalance: diff --git a/apps/cic-eth/test_requirements.txt b/apps/cic-eth/test_requirements.txt index 67462190..de8c7c01 100644 --- a/apps/cic-eth/test_requirements.txt +++ b/apps/cic-eth/test_requirements.txt @@ -2,6 +2,7 @@ pytest==6.0.1 pytest-celery==0.0.0a1 pytest-mock==3.3.1 pytest-cov==2.10.1 +pytest-localserver==0.5.1 pytest-redis==2.0.0 redis==3.5.3 eth-tester==0.5.0b3 diff --git a/apps/cic-eth/tests/server/test_account_controller.py b/apps/cic-eth/tests/server/test_account_controller.py deleted file mode 100644 index 91828753..00000000 --- a/apps/cic-eth/tests/server/test_account_controller.py +++ /dev/null @@ -1,159 +0,0 @@ -# coding: utf-8 -from __future__ import absolute_import - -import logging -import time - -import hexathon -import pytest -from cic_eth.server.app import create_app - -log = logging.getLogger(__name__) - - -@pytest.fixture -def client(): - log.debug("Here") - app = create_app({'TESTING': True}) - with app.app.test_client() as client: - # with app.app_context(): - # init_db() - yield client - - -def test_account(client): - # Default Token - query_string = [] - response = client.open( - '/token', - method='GET', - query_string=query_string) - default_token = response.get_json() - - # Create Account 1 - query_string = [('password', ''), - ('register', True)] - response = client.open( - '/create_account', - method='POST', - query_string=query_string) - address_1 = hexathon.valid(response.get_json()) - - # Create Account 2 - query_string = [('password', ''), - ('register', True)] - response = client.open( - '/create_account', - method='POST', - query_string=query_string) - address_2 = hexathon.valid(response.get_json()) - time.sleep(30) # Required to allow balance to show - - # Balance Account 1 - query_string = [('address', address_1), - ('token_symbol', 'COFE'), - ('include_pending', True)] - response = client.open( - '/balance', - method='GET', - query_string=query_string) - balance = response.get_json() - - assert (balance[0] == { - "address": default_token.get('address').lower(), - "balance_available": 30000000000, - "balance_incoming": 0, - "balance_network": 30000000000, - "balance_outgoing": 0, - "converters": [] - }) - - # Transfer - query_string = [('from_address', address_1), - ('to_address', address_2), - ('value', 100), - ('token_symbol', 'COFE')] - response = client.open( - '/transfer', - method='POST', - query_string=query_string) - transfer = response.get_json() - - # Balance Account 1 - query_string = [('address', address_1), - ('token_symbol', 'COFE'), - ('include_pending', True)] - response = client.open( - '/balance', - method='GET', - query_string=query_string) - balance_after_transfer = response.get_json() - assert (balance_after_transfer[0] == { - "address": default_token.get('address').lower(), - "balance_available": 29900000000, - "balance_incoming": 0, - "balance_network": 30000000000, - "balance_outgoing": 100000000, - "converters": [] - }) - - # Transactions Account 1 - query_string = [('address', address_1), - ('limit', 10)] - response = client.open( - '/transactions', - method='GET', - query_string=query_string) - transactions = response.get_json() - log.debug(transactions) - exit(1) - - -# def test_list_transactions(client): -# """Test case for list_transactions - - -# """ -# query_string = [('address', "test_address"), -# ('limit', 10)] -# response = client.open( -# '/transactions', -# method='GET', -# query_string=query_string) -# self.assert200(response, -# 'Response body is : ' + response.data.decode('utf-8')) - - -# def test_transfer(client): -# """Test case for transfer - - -# """ -# query_string = [('from_address', "test_address"), -# ('to_address', "test_address"), -# ('value', 56), -# ('token_symbol', 'token_symbol_example')] -# response = client.open( -# '/transfer', -# method='POST', -# query_string=query_string) -# self.assert200(response, -# 'Response body is : ' + response.data.decode('utf-8')) - - -# def test_transfer_from(client): -# """Test case for transfer_from - - -# """ -# query_string = [('from_address', "test_address"), -# ('to_address', "test_address"), -# ('value', 56), -# ('token_symbol', 'token_symbol_example'), -# ('spender_address', "test_address")] -# response = client.open( -# '/transfer_from', -# method='POST', -# query_string=query_string) -# self.assert200(response, -# 'Response body is : ' + response.data.decode('utf-8')) diff --git a/apps/cic-eth/tests/server/test_token_controller.py b/apps/cic-eth/tests/server/test_token_controller.py deleted file mode 100644 index 426ab0e1..00000000 --- a/apps/cic-eth/tests/server/test_token_controller.py +++ /dev/null @@ -1,43 +0,0 @@ -# # coding: utf-8 - -# from __future__ import absolute_import - -# import logging - -# import connexion -# from cic_eth.server.encoder import JSONEncoder -# from cic_eth.server.models.token import Token -# from flask import json -# from flask_testing import TestCase -# from six import BytesIO - - -# class BaseTestCase(TestCase): - -# def create_app(self): -# logging.getLogger('connexion.operation').setLevel('ERROR') -# app = connexion.App( -# __name__, specification_dir='../../cic_eth/server/openapi') -# app.app.json_encoder = JSONEncoder -# app.add_api('server.yaml') -# return app.app - - -# class TestTokenController(BaseTestCase): -# """TokenController integration test stubs""" - -# def test_get_default_token(self): -# """Test case for get_default_token - - -# """ -# response = self.client.open( -# '/token', -# method='GET') -# self.assert200(response, -# 'Response body is : ' + response.data.decode('utf-8')) - - -# if __name__ == '__main__': -# import unittest -# unittest.main() diff --git a/apps/cic-eth/tests/test_server.py b/apps/cic-eth/tests/test_server.py new file mode 100644 index 00000000..0b675000 --- /dev/null +++ b/apps/cic-eth/tests/test_server.py @@ -0,0 +1,115 @@ +# coding: utf-8 +from __future__ import absolute_import + +import logging +import time + +import hexathon +import pytest +import requests +from cic_eth.runnable.daemons.server import application +from pytest_localserver.http import WSGIServer + +log = logging.getLogger(__name__) + + +@pytest.fixture +def testserver(request): + """Defines the testserver funcarg""" + server = WSGIServer(application=application) + server.start() + request.addfinalizer(server.stop) + return server + + +def test_account(testserver): + # Default Token + response = requests.get( + testserver.url + '/default_token', + ) + log.debug(f"balance response {response}") + default_token = response.json() + + # Create Account 1 + params = { + 'password': '', + 'register': True + } + response = requests.post( + testserver.url + '/create_account', + params=params) + address_1 = hexathon.valid(response.json()) + + # Create Account 2 + params = { + 'password': '', + 'register': True + } + response = requests.post( + testserver.url + '/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 = requests.get( + testserver.url + '/balance', + params=params) + balance = response.json() + + assert (balance[0] == { + "address": default_token.get('address').lower(), + "balance_available": 30000000000, + "balance_incoming": 0, + "balance_network": 30000000000, + "balance_outgoing": 0, + "converters": [] + }) + + # Transfer + params = { + 'from_address': address_1, + 'to_address': address_2, + 'value': 100, + 'token_symbol': 'COFE' + } + response = requests.post( + testserver.url + '/transfer', + params=params) + transfer = response.json() + + # Balance Account 1 + params = { + 'address': address_1, + 'token_symbol': 'COFE', + 'include_pending': True + } + response = requests.get( + testserver.url + '/balance', + params=params) + balance_after_transfer = response.json() + assert (balance_after_transfer[0] == { + "address": default_token.get('address').lower(), + "balance_available": 29900000000, + "balance_incoming": 0, + "balance_network": 30000000000, + "balance_outgoing": 100000000, + "converters": [] + }) + + # Transactions Account 1 + params = { + 'address': address_1, + 'limit': 10 + } + response = requests.get( + testserver.url + '/transactions', + params=params) + transactions = response.json() + log.debug(transactions) + exit(1) diff --git a/docker-compose.yml b/docker-compose.yml index 8ee2c667..f81f66a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -220,10 +220,8 @@ services: set -a if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi set +a - /usr/local/bin/uwsgi \ - --wsgi-file /root/cic_eth/runnable/daemons/server.py \ - --http :5000 \ - --pyargv "-vv" + pip install -r test_requirements.txt + tail -F ./requirements.txt cic-eth-tracker: image: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:${TAG:-latest}