From 9f4e3684e2f501ffffb8daabcba65c6b843cab1b Mon Sep 17 00:00:00 2001 From: Giacomo Licari Date: Sat, 9 Mar 2024 11:06:31 +0100 Subject: [PATCH] Fix tests --- api/api/services/database.py | 3 +- api/api/services/validator.py | 5 +- api/tests/conftest.py | 64 +++++++++++++++----------- api/tests/test_api.py | 32 +++++++------ api/tests/test_api_claim_rate_limit.py | 19 +++++--- api/tests/test_api_cli.py | 13 ++++-- api/tests/test_database.py | 21 ++++++--- 7 files changed, 99 insertions(+), 58 deletions(-) diff --git a/api/api/services/database.py b/api/api/services/database.py index 7d07761..a667776 100644 --- a/api/api/services/database.py +++ b/api/api/services/database.py @@ -1,9 +1,10 @@ import sqlite3 from datetime import datetime +from flask_sqlalchemy import SQLAlchemy + from api.const import (DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, DEFAULT_NATIVE_MAX_AMOUNT_PER_DAY, FaucetRequestType) -from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() diff --git a/api/api/services/validator.py b/api/api/services/validator.py index a47fb68..e84d608 100644 --- a/api/api/services/validator.py +++ b/api/api/services/validator.py @@ -1,9 +1,11 @@ import datetime +import pdb -from api.const import TokenType from flask import current_app, request from web3 import Web3 +from api.const import TokenType + from .captcha import captcha_verify from .database import AccessKeyConfig, Token, Transaction from .rate_limit import Strategy @@ -27,6 +29,7 @@ def __init__(self, request_data, validate_captcha, access_key=None, *args, **kwa self.validate_captcha = validate_captcha self.access_key = access_key self.ip_address = request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr) + self.errors = [] def validate(self): self.data_validation() diff --git a/api/tests/conftest.py b/api/tests/conftest.py index 2b19928..3622fd5 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -1,11 +1,13 @@ import os -from unittest import TestCase, mock +from unittest import TestCase, TestResult, mock +from flask.testing import FlaskClient + +from api import create_app from api.services import Strategy from api.services.database import Token, db -from temp_env_var import FAUCET_ENABLED_TOKENS, TEMP_ENV_VARS -from api import create_app +from .temp_env_var import FAUCET_ENABLED_TOKENS, TEMP_ENV_VARS api_prefix = '/api/v1' @@ -53,55 +55,65 @@ def populate_db(self): token.save() def setUp(self): + ''' + Set up to do before running each test + ''' + self.native_tx_counter = 0 + self.erc20_tx_counter = 0 self._mock(TEMP_ENV_VARS) + self.app = create_app() - self.appctx = self.app.app_context() + self.app_ctx = self.app.test_request_context() + self.app_ctx.push() + self.client = self.app.test_client() - self.appctx.push() - with self.appctx: - self._reset_db() - self.native_tx_counter = 0 - self.erc20_tx_counter = 0 + with self.app_ctx: + self._reset_db() def tearDown(self): - self.appctx.pop() - self.client = None - self.app = None - self.appctx = None - + ''' + Cleanup to do after running each test + ''' for p in self.patchers: p.stop() + self.app_ctx.pop() + self.app_ctx = None + class RateLimitIPBaseTest(BaseTest): def setUp(self): + self.native_tx_counter = 0 + self.erc20_tx_counter = 0 env_vars = TEMP_ENV_VARS.copy() env_vars['FAUCET_RATE_LIMIT_STRATEGY'] = Strategy.ip.value self._mock(env_vars) + self.app = create_app() - self.appctx = self.app.app_context() + self.app_ctx = self.app.test_request_context() + self.app_ctx.push() + self.client = self.app.test_client() - self.appctx.push() - with self.appctx: - self._reset_db() - self.native_tx_counter = 0 - self.erc20_tx_counter = 0 + with self.app_ctx: + self._reset_db() class RateLimitIPorAddressBaseTest(BaseTest): def setUp(self): + self.native_tx_counter = 0 + self.erc20_tx_counter = 0 # Set rate limit strategy to IP env_vars = TEMP_ENV_VARS.copy() env_vars['FAUCET_RATE_LIMIT_STRATEGY'] = Strategy.ip_or_address.value self._mock(env_vars) + self.app = create_app() - self.appctx = self.app.app_context() + self.app_ctx = self.app.test_request_context() + self.app_ctx.push() + self.client = self.app.test_client() - self.appctx.push() - with self.appctx: - self._reset_db() - self.native_tx_counter = 0 - self.erc20_tx_counter = 0 + with self.app_ctx: + self._reset_db() diff --git a/api/tests/test_api.py b/api/tests/test_api.py index de552dc..64f74a3 100644 --- a/api/tests/test_api.py +++ b/api/tests/test_api.py @@ -1,13 +1,15 @@ +import unittest + from api.const import ZERO_ADDRESS from api.services.database import Transaction -from conftest import BaseTest, api_prefix + +from .conftest import BaseTest, api_prefix # from mock import patch -from temp_env_var import (CAPTCHA_TEST_RESPONSE_TOKEN, - DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, - DEFAULT_NATIVE_MAX_AMOUNT_PER_DAY, - ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID, - NATIVE_TOKEN_ADDRESS, NATIVE_TRANSFER_TX_HASH, - TOKEN_TRANSFER_TX_HASH) +from .temp_env_var import (CAPTCHA_TEST_RESPONSE_TOKEN, + DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, + DEFAULT_NATIVE_MAX_AMOUNT_PER_DAY, + ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID, + NATIVE_TOKEN_ADDRESS) class TestAPI(BaseTest): @@ -100,9 +102,10 @@ def test_ask_route_native_transaction(self): 'recipient': ZERO_ADDRESS, 'tokenAddress': NATIVE_TOKEN_ADDRESS }) + transaction = Transaction.query.filter_by(recipient=ZERO_ADDRESS).first() self.assertEqual(response.status_code, 200) - self.assertEqual(response.get_json().get('transactionHash'), - NATIVE_TRANSFER_TX_HASH) + self.assertEqual(response.get_json().get('transactionHash'), + transaction.hash) def test_ask_route_token_transaction(self): # not supported token, should return 400 @@ -111,7 +114,7 @@ def test_ask_route_token_transaction(self): 'chainId': FAUCET_CHAIN_ID, 'amount': DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, 'recipient': ZERO_ADDRESS, - 'tokenAddress': '0x' + '1' * 40 + 'tokenAddress': '0x' + '1234' * 10 }) self.assertEqual(response.status_code, 400) @@ -122,8 +125,11 @@ def test_ask_route_token_transaction(self): 'recipient': ZERO_ADDRESS, 'tokenAddress': ERC20_TOKEN_ADDRESS }) + transaction = Transaction.query.filter_by(recipient=ZERO_ADDRESS).first() self.assertEqual(response.status_code, 200) - assert response.get_json().get('transactionHash') == TOKEN_TRANSFER_TX_HASH + self.assertEqual(response.get_json().get('transactionHash'), + transaction.hash) + - transaction = Transaction.get_by_hash(TOKEN_TRANSFER_TX_HASH) - assert transaction.hash == TOKEN_TRANSFER_TX_HASH +if __name__ == '__main__': + unittest.main() diff --git a/api/tests/test_api_claim_rate_limit.py b/api/tests/test_api_claim_rate_limit.py index 4157927..6436013 100644 --- a/api/tests/test_api_claim_rate_limit.py +++ b/api/tests/test_api_claim_rate_limit.py @@ -1,11 +1,14 @@ +import unittest + from api.const import ZERO_ADDRESS from api.services.database import Transaction -from conftest import (RateLimitIPBaseTest, RateLimitIPorAddressBaseTest, - api_prefix) + +from .conftest import (RateLimitIPBaseTest, RateLimitIPorAddressBaseTest, + api_prefix) # from mock import patch -from temp_env_var import (CAPTCHA_TEST_RESPONSE_TOKEN, - DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, - ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID) +from .temp_env_var import (CAPTCHA_TEST_RESPONSE_TOKEN, + DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, + ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID) class TestAPIWithIPLimitStrategy(RateLimitIPBaseTest): @@ -70,6 +73,8 @@ def test_ask_route_limit_by_ip_or_address(self): 'recipient': ZERO_ADDRESS, 'tokenAddress': ERC20_TOKEN_ADDRESS }) - - print("%s: %d" % (response.get_json(), response.status_code)) self.assertEqual(response.status_code, 429) + + +if __name__ == '__main__': + unittest.main() diff --git a/api/tests/test_api_cli.py b/api/tests/test_api_cli.py index 397ef6d..608dccb 100644 --- a/api/tests/test_api_cli.py +++ b/api/tests/test_api_cli.py @@ -1,10 +1,13 @@ +import unittest + from api.const import ZERO_ADDRESS from api.services.database import AccessKey, AccessKeyConfig from api.utils import generate_access_key -from conftest import BaseTest, api_prefix + +from .conftest import BaseTest, api_prefix # from mock import patch -from temp_env_var import (DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, - ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID) +from .temp_env_var import (DEFAULT_ERC20_MAX_AMOUNT_PER_DAY, + ERC20_TOKEN_ADDRESS, FAUCET_CHAIN_ID) class TestAPICli(BaseTest): @@ -56,3 +59,7 @@ def test_ask_route_parameters(self): 'tokenAddress': ERC20_TOKEN_ADDRESS }) self.assertEqual(response.status_code, 429) + + +if __name__ == '__main__': + unittest.main() diff --git a/api/tests/test_database.py b/api/tests/test_database.py index e18ea96..e436ca5 100644 --- a/api/tests/test_database.py +++ b/api/tests/test_database.py @@ -1,11 +1,14 @@ -import pytest +import unittest + +from sqlalchemy.exc import IntegrityError + from api.const import ZERO_ADDRESS from api.services.database import (AccessKey, AccessKeyConfig, Token, Transaction) from api.utils import generate_access_key -from conftest import BaseTest -from sqlalchemy.exc import IntegrityError -from temp_env_var import NATIVE_TOKEN_ADDRESS, NATIVE_TRANSFER_TX_HASH + +from .conftest import BaseTest +from .temp_env_var import NATIVE_TOKEN_ADDRESS, NATIVE_TRANSFER_TX_HASH class TestDatabase(BaseTest): @@ -25,7 +28,7 @@ def test_access_keys(self): assert result[0].enabled is True # Duplicates for secret_access_key are not allowed - with pytest.raises(IntegrityError): + with self.assertRaises(IntegrityError): access_key_id2, _ = generate_access_key() access_key = AccessKey() access_key.access_key_id = access_key_id2 @@ -47,7 +50,7 @@ def test_access_key_config(self): config.save() # Duplicates for (access_key_id, chain_id) are not allowed - with pytest.raises(IntegrityError): + with self.assertRaises(IntegrityError): config = AccessKeyConfig() config.access_key_id = access_key.access_key_id config.chain_id = 10200 @@ -67,7 +70,7 @@ def test_transactions(self): transaction.save() # Duplicates for tx hash are not allowed - with pytest.raises(IntegrityError): + with self.assertRaises(IntegrityError): transaction = Transaction() transaction.hash = NATIVE_TRANSFER_TX_HASH transaction.recipient = ZERO_ADDRESS @@ -75,3 +78,7 @@ def test_transactions(self): transaction.token = token.address transaction.requester_ip = '192.168.0.1' transaction.save() + + +if __name__ == '__main__': + unittest.main()