tests for temp_password

This commit is contained in:
2026-09-28 09:56:59 +03:00
parent 54c670ddec
commit 6a6a95e863
2 changed files with 32 additions and 0 deletions
+4
View File
@@ -1,5 +1,6 @@
import asyncio
from src.database.auth.refresh_tokens import JwtCrudActions
from src.database.users.crud import UsersCrudActions
from src.errors.http_errors.errors import Errors
from src.models.pydantic_models.model import UserCreate, UserOut, UserUpdate
@@ -12,6 +13,7 @@ class CrudService:
self.errors=Errors()
self.crud_db_actions=UsersCrudActions()
self.hash_service=HashService()
self.jwt_db_actions=JwtCrudActions()
async def _plain_to_hash(self, user_data:dict)->dict:
@@ -78,6 +80,8 @@ class CrudService:
async def reset_password(self, email:str)->str:
new_password=await self.hash_service.generate_temp_password()
await self.update_user(email, UserUpdate(plain_password=new_password)) # type: ignore[call-arg]
return new_password
+28
View File
@@ -6,6 +6,7 @@ from fastapi import HTTPException
from jose import jwt
from src.models.configs_read.env import env_settings
from src.models.pydantic_models.model import validate_password
from src.service.auth.jwt import HashService, JwtService
@@ -146,3 +147,30 @@ class TestJwt:
async def test_token_to_hash_different_input(self, hash_service:HashService)->None:
assert hash_service.token_to_hash("abc")!=hash_service.token_to_hash("xyz")
@pytest.mark.parametrize("complexity",[
pytest.param(8,id="low_edge"),
pytest.param(9,id="low_edge+1"),
pytest.param(16,id="default"),
pytest.param(72,id="high_edge"),
pytest.param(71,id="high_edge-1")
])
async def test_generate_temp_password_positive(self, hash_service:HashService, complexity:int)->None:
for _ in range(10000):
password=await hash_service.generate_temp_password(complexity)
try:
validate_password(password)
except ValueError as e:
pytest.fail(f"{password!r}: {e}")
@pytest.mark.parametrize("complexity", [
pytest.param(73,id="too_long"),
pytest.param(7,id="too_short")
])
async def test_generate_temp_password_negative(self, hash_service:HashService, complexity:int)->None:
password = await hash_service.generate_temp_password(complexity)
with pytest.raises(ValueError, match="8-72"):
validate_password(password)