1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Создание чата9testt

This commit is contained in:
MoonTestUse1
2025-01-05 06:32:34 +06:00
parent 9ba671bdaa
commit 7f7838a0d3
28 changed files with 653 additions and 721 deletions

View File

@@ -5,94 +5,87 @@ from app.main import app
from app.crud import employees
from app.utils.auth import verify_password, get_password_hash
from app.schemas.employee import EmployeeCreate
from app.core.auth import create_access_token
from app.core.redis import redis_client
client = TestClient(app)
def test_login_success(test_db: Session):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee_data = EmployeeCreate(
first_name="Test",
last_name="User",
department="IT",
office="101",
password="testpass123"
)
employee = employees.create_employee(test_db, employee_data, hashed_password)
# Переопределяем redis_client для тестов
def pytest_configure(config):
from app.core import redis
redis.redis_client = config.getoption("--redis", default=None)
pytestmark = pytest.mark.asyncio
@pytest.mark.asyncio
async def test_login_success(client: TestClient, test_user: dict, redis):
"""Test successful login"""
response = client.post(
"/api/auth/login",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"username": "User",
"password": "testpass123"
"username": test_user["email"],
"password": test_user["password"]
}
)
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):
# Создаем тестового сотрудника
hashed_password = get_password_hash("testpass123")
employee_data = EmployeeCreate(
first_name="Test",
last_name="User",
department="IT",
office="101",
password="testpass123"
)
employees.create_employee(test_db, employee_data, hashed_password)
@pytest.mark.asyncio
async def test_login_wrong_password(client: TestClient, test_user: dict, redis):
"""Test login with wrong password"""
response = client.post(
"/api/auth/login",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"username": "User",
"password": "wrongpass"
"username": test_user["email"],
"password": "wrongpassword"
}
)
assert response.status_code == 401
assert "detail" in response.json()
assert response.json()["detail"] == "Incorrect email or password"
def test_login_nonexistent_user(test_db: Session):
@pytest.mark.asyncio
async def test_login_wrong_email(client: TestClient, redis):
"""Test login with wrong email"""
response = client.post(
"/api/auth/login",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"username": "NonExistent",
"password": "testpass123"
"username": "wrong@example.com",
"password": "test123"
}
)
assert response.status_code == 401
assert "detail" in response.json()
assert response.json()["detail"] == "Incorrect email or password"
def test_admin_login_success():
response = client.post(
"/api/auth/admin/login",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"username": "admin",
"password": "admin123"
}
@pytest.mark.asyncio
async def test_get_current_user(client: TestClient, test_user: dict, redis):
"""Test getting current user info"""
access_token = create_access_token(
data={"sub": test_user["email"], "is_admin": False}
)
response = client.get(
"/api/auth/me",
headers={"Authorization": f"Bearer {access_token}"}
)
assert response.status_code == 200
assert "access_token" in response.json()
assert response.json()["token_type"] == "bearer"
assert response.json()["email"] == test_user["email"]
assert response.json()["full_name"] == test_user["full_name"]
assert not response.json()["is_admin"]
def test_admin_login_wrong_password():
response = client.post(
"/api/auth/admin/login",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={
"username": "admin",
"password": "wrongpass"
}
)
@pytest.mark.asyncio
async def test_get_current_user_no_token(client: TestClient, redis):
"""Test getting current user without token"""
response = client.get("/api/auth/me")
assert response.status_code == 401
assert "detail" in response.json()
assert response.json()["detail"] == "Not authenticated"
@pytest.mark.asyncio
async def test_get_current_user_invalid_token(client: TestClient, redis):
"""Test getting current user with invalid token"""
response = client.get(
"/api/auth/me",
headers={"Authorization": "Bearer invalid_token"}
)
assert response.status_code == 401
assert response.json()["detail"] == "Could not validate credentials"