Skip to content

Commit

Permalink
API: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Licari committed Nov 30, 2023
1 parent 369cd7e commit 8b44690
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '*')

Expand Down
3 changes: 2 additions & 1 deletion api/tests/temp_env_var.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from secrets import token_bytes
import json

from api.const import NATIVE_TOKEN_ADDRESS

Expand All @@ -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
}

Expand Down
36 changes: 17 additions & 19 deletions api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 8b44690

Please sign in to comment.