Skip to content

Commit 471685b

Browse files
committed
Revert removal of patch_password_hashing utility
1 parent 96d0296 commit 471685b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

backend/app/tests/conftest.py

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

43
import pytest
54
from _pytest.fixtures import FixtureRequest
@@ -11,7 +10,7 @@
1110
from app.main import app
1211
from app.models import Item, User
1312
from app.tests.utils.user import authentication_token_from_email
14-
from app.tests.utils.utils import get_superuser_token_headers
13+
from app.tests.utils.utils import get_superuser_token_headers, patch_password_hashing
1514

1615

1716
@pytest.fixture(scope="module")
@@ -22,10 +21,7 @@ def disable_password_hashing(request: FixtureRequest) -> Generator[bool, None, N
2221

2322
module = request.node.getparent(pytest.Module)
2423
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-
):
24+
with patch_password_hashing("app.core.security"):
2925
yield True
3026
else:
3127
yield False # Don't patch if `enable_password_hashing` marker is set

backend/app/tests/utils/utils.py

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

47
from fastapi.testclient import TestClient
58

@@ -24,3 +27,19 @@ def get_superuser_token_headers(client: TestClient) -> dict[str, str]:
2427
a_token = tokens["access_token"]
2528
headers = {"Authorization": f"Bearer {a_token}"}
2629
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)