Skip to content

Commit 3865552

Browse files
committed
Add login endpoint with email verification check
1 parent e4387f4 commit 3865552

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

backend/app/api/api_v1/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from app.api.api_v1.endpoints import login
2+
api_router.include_router(login.router, prefix="/auth", tags=["auth"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi import APIRouter, HTTPException
2+
from pydantic import BaseModel
3+
4+
router = APIRouter()
5+
6+
class LoginRequest(BaseModel):
7+
email: str
8+
password: str
9+
is_verified: bool = False
10+
11+
@router.post("/login")
12+
def login(request: LoginRequest):
13+
if not request.is_verified:
14+
raise HTTPException(status_code=403, detail="Email not verified")
15+
return {"message": f"Welcome {request.email}"}

backend/app/tests/test_login.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fastapi.testclient import TestClient
2+
from app.main import app
3+
4+
client = TestClient(app)
5+
6+
def test_login_verified_user():
7+
response = client.post("/auth/login", json={
8+
"email": "[email protected]",
9+
"password": "secure",
10+
"is_verified": True
11+
})
12+
assert response.status_code == 200
13+
assert "Welcome" in response.json()["message"]
14+
15+
def test_login_unverified_user():
16+
response = client.post("/auth/login", json={
17+
"email": "[email protected]",
18+
"password": "secure",
19+
"is_verified": False
20+
})
21+
assert response.status_code == 403
22+
assert response.json()["detail"] == "Email not verified"

0 commit comments

Comments
 (0)