mirror of
https://github.com/Xevion/linkpulse.git
synced 2025-12-06 15:15:34 -06:00
Add test_auth_login
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
from typing import Tuple, Optional
|
from typing import Tuple, Optional
|
||||||
|
|
||||||
|
from fastapi import status
|
||||||
|
from fastapi.responses import ORJSONResponse
|
||||||
from pwdlib import PasswordHash
|
from pwdlib import PasswordHash
|
||||||
from pwdlib.hashers.argon2 import Argon2Hasher
|
from pwdlib.hashers.argon2 import Argon2Hasher
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
@@ -64,7 +66,10 @@ async def login(body: LoginBody):
|
|||||||
if user is None:
|
if user is None:
|
||||||
# Hash regardless of user existence to prevent timing attacks
|
# Hash regardless of user existence to prevent timing attacks
|
||||||
hasher.verify(body.password, dummy_hash)
|
hasher.verify(body.password, dummy_hash)
|
||||||
return LoginError(error="Invalid email or password")
|
return ORJSONResponse(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
content=LoginError(error="Invalid email or password"),
|
||||||
|
)
|
||||||
|
|
||||||
# valid, updated_hash = hasher.verify_and_update(body.password, existing_hash)
|
# valid, updated_hash = hasher.verify_and_update(body.password, existing_hash)
|
||||||
|
|
||||||
|
|||||||
19
backend/linkpulse/tests/test_auth.py
Normal file
19
backend/linkpulse/tests/test_auth.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from fastapi import status
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from linkpulse.app import app
|
||||||
|
from linkpulse.tests.test_user import user
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_login(user):
|
||||||
|
args = {"email": "test@test.com", "password": "test"}
|
||||||
|
|
||||||
|
with TestClient(app) as client:
|
||||||
|
response = client.post("/api/login", json=args)
|
||||||
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
# assert response.json()["token"] is not None
|
||||||
|
|
||||||
|
response = client.post("/api/login", json={**args, "email": "invalid_email"})
|
||||||
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||||
|
|
||||||
|
response = client.post("/api/login", json={**args, "password": "invalid_password"})
|
||||||
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
@@ -9,6 +9,4 @@ logger = structlog.get_logger()
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def user():
|
def user():
|
||||||
return User.create(
|
return User.create(email=random_email(), password_hash=hasher.hash("password"))
|
||||||
email=random_email(), password_hash=hasher.hash(random_string(64))
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user