Skip to content

Commit 0fccbde

Browse files
committed
Simplify tests
1 parent 5e2bdf0 commit 0fccbde

File tree

3 files changed

+26
-62
lines changed

3 files changed

+26
-62
lines changed

backend/app/tests/api/routes/test_login_with_hashing.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
pytestmark = pytest.mark.enable_password_hashing
77

88

9-
def test_get_access_token_with_hashing(client: TestClient) -> None:
9+
def test_get_access_token_with_hashing(
10+
client: TestClient,
11+
disable_password_hashing: bool,
12+
) -> None:
13+
assert disable_password_hashing is False
1014
login_data = {
1115
"username": settings.FIRST_SUPERUSER,
1216
"password": settings.FIRST_SUPERUSER_PASSWORD,
@@ -16,12 +20,3 @@ def test_get_access_token_with_hashing(client: TestClient) -> None:
1620
assert r.status_code == 200
1721
assert "access_token" in tokens
1822
assert tokens["access_token"]
19-
20-
21-
def test_get_access_token_incorrect_password(client: TestClient) -> None:
22-
login_data = {
23-
"username": settings.FIRST_SUPERUSER,
24-
"password": "incorrect",
25-
}
26-
r = client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data)
27-
assert r.status_code == 400

backend/app/tests/conftest.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Generator
2-
from contextlib import nullcontext
2+
from unittest.mock import patch
33

44
import pytest
55
from _pytest.fixtures import FixtureRequest
@@ -11,55 +11,43 @@
1111
from app.main import app
1212
from app.models import Item, User
1313
from app.tests.utils.user import authentication_token_from_email
14-
from app.tests.utils.utils import get_superuser_token_headers, patch_password_hashing
14+
from app.tests.utils.utils import get_superuser_token_headers
1515

1616

17-
@pytest.fixture(scope="module", autouse=True)
17+
@pytest.fixture(scope="module")
1818
def disable_password_hashing(request: FixtureRequest) -> Generator[None, None, None]:
19-
"""Fixture disabling password hashing
20-
21-
Password hashing can be enabled on module level by marking the module with `pytest.mark.enable_password_hashing`.
19+
"""
20+
Disable password hashing if no `enable_password_hashing` marker set for module.
2221
"""
2322

24-
with (
25-
patch_password_hashing("app.core.security")
26-
if all(m.name != "enable_password_hashing" for m in request.node.iter_markers())
27-
else nullcontext()
28-
):
29-
yield
23+
module = request.node.getparent(pytest.Module)
24+
if not module.get_closest_marker("enable_password_hashing"):
25+
with (
26+
patch("app.core.security.pwd_context.verify", lambda x, y: x == y),
27+
patch("app.core.security.pwd_context.hash", lambda x: x),
28+
):
29+
yield True
30+
else:
31+
yield False # Don't patch if `enable_password_hashing` marker is set
3032

3133

32-
@pytest.fixture(scope="session")
33-
def db() -> Generator[Session, None, None]:
34-
"""
35-
Module scoped fixture providing a database session initialized with `init_db`.
36-
"""
34+
@pytest.fixture(scope="module")
35+
def db(disable_password_hashing) -> Generator[Session, None, None]: # noqa: ARG001
3736
with Session(engine) as session:
37+
session.execute( # Recreate user for every module, with\without pwd hashing
38+
delete(User)
39+
)
40+
init_db(session)
41+
session.commit()
3842
yield session
3943
# Cleanup test database
4044
session.execute(delete(Item))
4145
session.execute(delete(User))
4246
session.commit()
4347

4448

45-
@pytest.fixture(scope="module", autouse=True)
46-
def init_db_fixture(db: Session) -> None:
47-
# note: deleting all users here is required to enable or disable password hashing per test module.
48-
# If we don't delete all users here, the users created during `init_db` will not be re-created and the password will stay (un)hashed,
49-
# leading to possibly failing tests relying on the created user for authentication.
50-
db.execute(delete(Item))
51-
db.execute(delete(User))
52-
init_db(db)
53-
db.commit()
54-
55-
5649
@pytest.fixture(scope="module")
5750
def client(db: Session) -> Generator[TestClient, None, None]: # noqa: ARG001
58-
"""
59-
Module scoped fixture providing a `TestClient` instance.
60-
61-
NOTE: This fixture uses the `db` fixture WITHOUT hashing passwords!
62-
"""
6351
with TestClient(app) as c:
6452
yield c
6553

backend/app/tests/utils/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import random
22
import string
3-
from collections.abc import Generator
4-
from contextlib import ExitStack, contextmanager
5-
from unittest.mock import patch
63

74
from fastapi.testclient import TestClient
85

@@ -27,19 +24,3 @@ def get_superuser_token_headers(client: TestClient) -> dict[str, str]:
2724
a_token = tokens["access_token"]
2825
headers = {"Authorization": f"Bearer {a_token}"}
2926
return headers
30-
31-
32-
@contextmanager
33-
def patch_password_hashing(*modules: str) -> Generator[None, None, None]:
34-
"""
35-
Contextmanager to patch ``pwd_context`` in the given modules.
36-
:param modules: list of modules to patch.
37-
:return:
38-
"""
39-
with ExitStack() as stack:
40-
for module in modules:
41-
stack.enter_context(
42-
patch(f"{module}.pwd_context.verify", lambda x, y: x == y)
43-
)
44-
stack.enter_context(patch(f"{module}.pwd_context.hash", lambda x: x))
45-
yield

0 commit comments

Comments
 (0)