Skip to content

Commit b0b792e

Browse files
committed
setup tests and first endpoint tests
1 parent 6ff598c commit b0b792e

File tree

13 files changed

+617
-8
lines changed

13 files changed

+617
-8
lines changed

fastapi_2fa/api/endpoints/api_v1/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
)
4141
async def signup(
42-
db: Session = Depends(get_db), user_data: UserCreate = Depends()
42+
user_data: UserCreate, db: Session = Depends(get_db)
4343
) -> Any:
4444
try:
4545
user = await user_crud(transaction=True).create(

fastapi_2fa/core/config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,19 @@ class ProductionConfig(BaseConfig):
8787

8888

8989
class TestingConfig(BaseConfig):
90-
LOGGING_LEVEL = "DEBUG"
90+
# define new attributes with respect to BaseConfig
91+
DATABASE_CONNECT_DICT: dict = {"check_same_thread": False}
92+
LOGGING_LEVEL = 'DEBUG'
93+
SQLALCHEMY_DATABASE_TEST_SYNC_URI: str = "sqlite:///tests/test_db.db"
94+
95+
# celery test config
96+
task_always_eager = True
97+
98+
def __init__(self, *args, **kwargs):
99+
super().__init__(*args, **kwargs)
100+
# override attributes of BaseConfig
101+
# https://fastapi.tiangolo.com/advanced/testing-database/
102+
self.SQLALCHEMY_DATABASE_URI: str = "sqlite+aiosqlite:///tests/test_db.db"
91103

92104

93105
@lru_cache()

fastapi_2fa/core/two_factor_auth.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ def get_decoded_two_factor_auth_key(value: str) -> str:
4040
return _fernet_decode(value=value)
4141

4242

43+
def _get_fake_otp_token(nr_digits: int = settings.TFA_TOKEN_LENGTH):
44+
return ''.join(
45+
str(random.randint(0, 9)) for _ in range(nr_digits)
46+
)
47+
48+
4349
def get_fake_otp_tokens(
4450
nr_digits: int = settings.TFA_TOKEN_LENGTH,
4551
nr_tokens: int = settings.TFA_BACKUP_TOKENS_NR
4652
) -> Iterator[str]:
4753
for _ in range(nr_tokens):
48-
random_otp = ''.join(
49-
str(random.randint(0, 9)) for _ in range(nr_digits)
50-
)
54+
random_otp = _get_fake_otp_token(nr_digits=nr_digits)
5155
yield random_otp
5256

5357

54-
5558
def get_current_totp(user: User) -> pyotp.TOTP:
5659
assert user.tfa_enabled is True, 'User does not have TFA enabled'
5760
assert user.device is not None, 'User has no associated device'

poetry.lock

Lines changed: 126 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fastapi = "^0.87.0"
1212
passlib = {extras = ["bycrypt"], version = "^1.7.4"}
1313
pytest = "^7.2.0"
1414
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
15-
celery = "^5.2.7"
15+
celery = {extras = ["pytest"], version = "^5.2.7"}
1616
pydantic = {extras = ["email"], version = "^1.10.2"}
1717
ipdb = "^0.13.9"
1818
python-dotenv = "^0.21.0"
@@ -31,6 +31,10 @@ qrcode = {extras = ["pil"], version = "^7.3.1"}
3131
pillow = "^9.3.0"
3232
redis = "^4.3.5"
3333
flower = "1.1.0"
34+
pytest-asyncio = "^0.20.2"
35+
aiosqlite = "^0.17.0"
36+
httpx = "^0.23.1"
37+
pytest-celery = "^0.0.0"
3438

3539

3640
[tool.poetry.group.dev.dependencies]

tests/api/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)