Files
poona-ams/backend/tests/test_task4_passwords.py

166 lines
5.6 KiB
Python

import pytest
from fastapi.testclient import TestClient
from app.core.security import validate_password
from app.models.masters import User
from app.seed.seed import seed_database
@pytest.fixture(scope="module", autouse=True)
def seeded_task4_db(db, tmp_path_factory):
upload_dir = tmp_path_factory.mktemp("task4_test_uploads")
seed_database(db, upload_dir=upload_dir)
return upload_dir
def _auth_headers(client, email: str = "admin@poona.demo") -> dict[str, str]:
res = client.post("/api/auth/login", json={"email": email, "password": "Demo@123"})
assert res.status_code == 200, res.text
return {"Authorization": f"Bearer {res.json()['access_token']}"}
def test_validate_password_unit():
# 1. Short password (< 8)
with pytest.raises(Exception) as exc1:
validate_password("Short1", email="user@hospital.com")
assert "at least 8 characters" in str(exc1.value)
# 2. No digits
with pytest.raises(Exception) as exc2:
validate_password("NoDigitsHere", email="user@hospital.com")
assert "at least one letter and at least one digit" in str(exc2.value)
# 3. No letters
with pytest.raises(Exception) as exc3:
validate_password("1234567890", email="user@hospital.com")
assert "at least one letter and at least one digit" in str(exc3.value)
# 4. Leading / trailing space
with pytest.raises(Exception) as exc4a:
validate_password(" Secret123", email="user@hospital.com")
assert "space" in str(exc4a.value)
with pytest.raises(Exception) as exc4b:
validate_password("Secret123 ", email="user@hospital.com")
assert "space" in str(exc4b.value)
# 5. Blocklisted passwords
for bad in ("12345678", "password", "password1", "qwerty123", "admin123", "welcome1"):
with pytest.raises(Exception) as exc5:
validate_password(bad, email="user@hospital.com")
assert "easily guessable" in str(exc5.value) or "at least 8" in str(exc5.value) or "at least one" in str(exc5.value)
# 6. Demo password MUST be allowed
validate_password("demo@123", email="admin@poona.demo")
validate_password("Demo@123", email="admin@poona.demo")
# 7. Contains email local part (>= 4 chars)
with pytest.raises(Exception) as exc7:
validate_password("Secureadmin123", email="admin@poona.demo")
assert "username" in str(exc7.value).lower()
# Short local part (< 4 chars) does NOT trigger username check
validate_password("Securebio123", email="bio@poona.demo")
def test_api_create_user_password_validation(client, db):
headers = _auth_headers(client, "admin@poona.demo")
# Rejected: no digit
res_bad = client.post(
"/api/users",
headers=headers,
json={
"name": "Test User",
"email": "test_pw_rule@poona.demo",
"password": "NoDigitsPassword",
"role": "biomedical",
},
)
assert res_bad.status_code == 422
assert "letter and at least one digit" in res_bad.json()["detail"]
# Rejected: contains email username "tester123"
res_username = client.post(
"/api/users",
headers=headers,
json={
"name": "Test User 2",
"email": "tester123@poona.demo",
"password": "tester123Password1",
"role": "biomedical",
},
)
assert res_username.status_code == 422
assert "username" in res_username.json()["detail"].lower()
# Accepted: valid password with letters and numbers
res_ok = client.post(
"/api/users",
headers=headers,
json={
"name": "Test User 3",
"email": "validuser@poona.demo",
"password": "ValidPassword123",
"role": "biomedical",
},
)
assert res_ok.status_code == 201
user_id = res_ok.json()["id"]
# Reset password with leading space rejected
res_reset_bad = client.post(
f"/api/users/{user_id}/reset-password",
headers=headers,
json={"new_password": " LeadingSpace123"},
)
assert res_reset_bad.status_code == 422
assert "space" in res_reset_bad.json()["detail"]
# Reset password with valid password succeeds
res_reset_ok = client.post(
f"/api/users/{user_id}/reset-password",
headers=headers,
json={"new_password": "NewValidSecret456"},
)
assert res_reset_ok.status_code == 200
def test_api_change_password_validation(client, db):
# Log in as bio user
bio_headers = _auth_headers(client, "bio@poona.demo")
# Reject new == current
res_same = client.post(
"/api/auth/change-password",
headers=bio_headers,
json={"current_password": "Demo@123", "new_password": "Demo@123"},
)
assert res_same.status_code == 422
assert res_same.json()["detail"] == "New password must be different."
# Reject trailing space
res_space = client.post(
"/api/auth/change-password",
headers=bio_headers,
json={"current_password": "Demo@123", "new_password": "NewSecretPass1 "},
)
assert res_space.status_code == 422
assert "space" in res_space.json()["detail"]
# Reject blocklisted password
res_block = client.post(
"/api/auth/change-password",
headers=bio_headers,
json={"current_password": "Demo@123", "new_password": "password1"},
)
assert res_block.status_code == 422
# Accept valid change
res_ok = client.post(
"/api/auth/change-password",
headers=bio_headers,
json={"current_password": "Demo@123", "new_password": "BrandNewSecret789"},
)
assert res_ok.status_code == 200
assert res_ok.json()["message"] == "Password changed successfully."