Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Licari committed Mar 9, 2024
1 parent 41689be commit 9f4e368
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 58 deletions.
3 changes: 2 additions & 1 deletion api/api/services/database.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
5 changes: 4 additions & 1 deletion api/api/services/validator.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
64 changes: 38 additions & 26 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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'

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

Expand All @@ -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()
19 changes: 12 additions & 7 deletions api/tests/test_api_claim_rate_limit.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
13 changes: 10 additions & 3 deletions api/tests/test_api_cli.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
21 changes: 14 additions & 7 deletions api/tests/test_database.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -67,11 +70,15 @@ 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
transaction.amount = 1
transaction.token = token.address
transaction.requester_ip = '192.168.0.1'
transaction.save()


if __name__ == '__main__':
unittest.main()

0 comments on commit 9f4e368

Please sign in to comment.