diff --git a/api/api/settings.py b/api/api/settings.py index 9dc7847..23a8187 100644 --- a/api/api/settings.py +++ b/api/api/settings.py @@ -31,7 +31,7 @@ FAUCET_ENABLED_TOKENS=json.loads(os.getenv('FAUCET_ENABLED_TOKENS', default='[]')) FAUCET_ADDRESS: LocalAccount = Account.from_key(FAUCET_PRIVATE_KEY).address FAUCET_RATE_LIMIT_STRATEGY=rate_limit_strategy -FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS=seconds=os.getenv('FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS', 86400) # 86400 = 24h +FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS=seconds=int(os.getenv('FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS', 86400)) # 86400 = 24h CORS_ALLOWED_ORIGINS=os.getenv('CORS_ALLOWED_ORIGINS', '*') diff --git a/api/tests/temp_env_var.py b/api/tests/temp_env_var.py index 468d1aa..a7000a5 100644 --- a/api/tests/temp_env_var.py +++ b/api/tests/temp_env_var.py @@ -1,4 +1,5 @@ from secrets import token_bytes +import json from api.const import NATIVE_TOKEN_ADDRESS @@ -21,7 +22,7 @@ 'FAUCET_CHAIN_ID': '100000', 'FAUCET_PRIVATE_KEY': token_bytes(32).hex(), 'FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS': '1', - 'FAUCET_ENABLED_TOKENS': FAUCET_ENABLED_TOKENS, + 'FAUCET_ENABLED_TOKENS': json.dumps(FAUCET_ENABLED_TOKENS), 'CAPTCHA_SECRET_KEY': CAPTCHA_TEST_SECRET_KEY } diff --git a/api/tests/test_api.py b/api/tests/test_api.py index 1861955..63379e5 100644 --- a/api/tests/test_api.py +++ b/api/tests/test_api.py @@ -1,31 +1,31 @@ from api import create_app -from api.services import RateLimitStrategy, Strategy +from api.services import Strategy import pytest +import os from conftest import api_prefix # from mock import patch from temp_env_var import TEMP_ENV_VARS, NATIVE_TRANSFER_TX_HASH, TOKEN_TRANSFER_TX_HASH, ZERO_ADDRESS, CAPTCHA_TEST_RESPONSE_TOKEN, NATIVE_TOKEN_AMOUNT, NATIVE_TOKEN_ADDRESS, ERC20_TOKEN_ADDRESS class BaseTest: - def _mock(self, mocker): + def _mock(self, mocker, env_variables=None): # Mock values mocker.patch('api.api.claim_native', return_value=NATIVE_TRANSFER_TX_HASH) mocker.patch('api.api.claim_token', return_value=TOKEN_TRANSFER_TX_HASH) + mocker.patch('api.api.print_info', return_value=None) + mocker.patch('api.api.captcha_verify', return_value=True) + if env_variables: + mocker.patch.dict(os.environ, env_variables) return mocker - def _create_app(self, env_vars=None): - # Instantiate app - app = create_app() - if env_vars: - # Override configs - app.config.update(env_vars) - return app + def _create_app(self): + return create_app() @pytest.fixture def app(self, mocker): - mocker = self._mock(mocker) - app = self._create_app(TEMP_ENV_VARS) + mocker = self._mock(mocker, TEMP_ENV_VARS) + app = self._create_app() yield app @pytest.fixture @@ -34,7 +34,7 @@ def client(self, app): class TestAPI(BaseTest): - def test_status_route(self, app, client): + def test_status_route(self, client): response = client.get(api_prefix + '/status') assert response.status_code == 200 assert response.get_json().get('status') == 'ok' @@ -135,18 +135,16 @@ def test_ask_route_token_transaction(self, client): class TestAPIWithIPLimitStrategy(BaseTest): + @pytest.fixture def app(self, mocker): - mocker = self._mock(mocker) - # Set rate limit strategy to IP - rate_limit_strategy = RateLimitStrategy() - rate_limit_strategy.strategy = Strategy.ip.value - env_vars = TEMP_ENV_VARS.copy() - env_vars['FAUCET_RATE_LIMIT_STRATEGY'] = rate_limit_strategy + env_vars['FAUCET_RATE_LIMIT_STRATEGY'] = Strategy.ip.value + + mocker = self._mock(mocker, env_vars) - app = self._create_app(env_vars) + app = self._create_app() yield app def test_ask_route_limit_by_ip(self, client):