1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
Files
AdministrationItDepartmens/backend/app/tests/test_auth.py
2025-01-04 02:19:41 +06:00

83 lines
2.1 KiB
Python

import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from ..main import app
from ..crud import employees
from ..utils.auth import verify_password, get_password_hash
client = TestClient(app)
def test_login_success(test_db: Session):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee = employees.create_employee(
test_db,
{
"first_name": "Test",
"last_name": "User",
"department": "IT",
"office": "101",
"password": "testpass123"
}
)
response = client.post(
"/api/auth/login",
data={
"username": "User",
"password": "testpass123"
}
)
assert response.status_code == 200
assert "access_token" in response.json()
assert response.json()["token_type"] == "bearer"
def test_login_wrong_password(test_db: Session):
response = client.post(
"/api/auth/login",
data={
"username": "User",
"password": "wrongpass"
}
)
assert response.status_code == 401
assert "detail" in response.json()
def test_login_nonexistent_user(test_db: Session):
response = client.post(
"/api/auth/login",
data={
"username": "NonExistent",
"password": "testpass123"
}
)
assert response.status_code == 401
assert "detail" in response.json()
def test_admin_login_success():
response = client.post(
"/api/auth/admin/login",
data={
"username": "admin",
"password": "admin123"
}
)
assert response.status_code == 200
assert "access_token" in response.json()
assert response.json()["token_type"] == "bearer"
def test_admin_login_wrong_password():
response = client.post(
"/api/auth/admin/login",
data={
"username": "admin",
"password": "wrongpass"
}
)
assert response.status_code == 401
assert "detail" in response.json()