mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Fix database
This commit is contained in:
@@ -12,6 +12,7 @@ from app.main import app
|
||||
from app.dependencies import get_db
|
||||
from app.models.employee import Employee
|
||||
from app.utils.security import get_password_hash
|
||||
from app.utils.jwt import create_and_save_token
|
||||
|
||||
# Mock Telegram notifications
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -93,4 +94,24 @@ def test_admin(db_session):
|
||||
db_session.add(admin)
|
||||
db_session.commit()
|
||||
db_session.refresh(admin)
|
||||
return admin
|
||||
return admin
|
||||
|
||||
@pytest.fixture
|
||||
def employee_token(test_employee, db_session):
|
||||
"""Create employee token"""
|
||||
return create_and_save_token(test_employee.id, db_session)
|
||||
|
||||
@pytest.fixture
|
||||
def admin_token(test_admin, db_session):
|
||||
"""Create admin token"""
|
||||
return create_and_save_token(test_admin.id, db_session)
|
||||
|
||||
@pytest.fixture
|
||||
def employee_headers(employee_token):
|
||||
"""Get employee headers"""
|
||||
return {"Authorization": f"Bearer {employee_token}"}
|
||||
|
||||
@pytest.fixture
|
||||
def admin_headers(admin_token):
|
||||
"""Get admin headers"""
|
||||
return {"Authorization": f"Bearer {admin_token}"}
|
@@ -1,19 +1,18 @@
|
||||
"""Employee tests"""
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
def test_create_employee(client: TestClient, admin_token: str):
|
||||
"""Test employee creation"""
|
||||
def test_create_employee(client: TestClient, admin_headers):
|
||||
"""Test create employee"""
|
||||
response = client.post(
|
||||
"/api/employees",
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
"/api/employees/",
|
||||
headers=admin_headers,
|
||||
json={
|
||||
"first_name": "New",
|
||||
"last_name": "Employee",
|
||||
"department": "IT",
|
||||
"office": "103",
|
||||
"office": "Main",
|
||||
"password": "newpass123",
|
||||
"is_active": True,
|
||||
"is_admin": False
|
||||
}
|
||||
)
|
||||
@@ -21,111 +20,62 @@ def test_create_employee(client: TestClient, admin_token: str):
|
||||
data = response.json()
|
||||
assert data["first_name"] == "New"
|
||||
assert data["last_name"] == "Employee"
|
||||
assert data["department"] == "IT"
|
||||
assert data["office"] == "103"
|
||||
assert data["is_admin"] == False
|
||||
assert "hashed_password" not in data
|
||||
|
||||
def test_create_employee_unauthorized(client: TestClient):
|
||||
"""Test employee creation without authorization"""
|
||||
def test_create_employee_not_admin(client: TestClient, employee_headers):
|
||||
"""Test create employee without admin rights"""
|
||||
response = client.post(
|
||||
"/api/employees",
|
||||
"/api/employees/",
|
||||
headers=employee_headers,
|
||||
json={
|
||||
"first_name": "New",
|
||||
"last_name": "Employee",
|
||||
"department": "IT",
|
||||
"office": "103",
|
||||
"password": "newpass123",
|
||||
"is_admin": False
|
||||
}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_create_employee_not_admin(client: TestClient, employee_token: str):
|
||||
"""Test employee creation by non-admin user"""
|
||||
response = client.post(
|
||||
"/api/employees",
|
||||
headers={"Authorization": f"Bearer {employee_token}"},
|
||||
json={
|
||||
"first_name": "New",
|
||||
"last_name": "Employee",
|
||||
"department": "IT",
|
||||
"office": "103",
|
||||
"office": "Main",
|
||||
"password": "newpass123",
|
||||
"is_active": True,
|
||||
"is_admin": False
|
||||
}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"] == "Not enough permissions"
|
||||
|
||||
def test_get_employees(client: TestClient, admin_token: str):
|
||||
"""Test getting all employees"""
|
||||
response = client.get(
|
||||
"/api/employees",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
def test_get_employees(client: TestClient, admin_headers):
|
||||
"""Test get all employees"""
|
||||
response = client.get("/api/employees/", headers=admin_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
assert len(data) > 0
|
||||
|
||||
def test_get_employees_unauthorized(client: TestClient):
|
||||
"""Test getting employees without authorization"""
|
||||
response = client.get("/api/employees")
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_get_employees_not_admin(client: TestClient, employee_token: str):
|
||||
"""Test getting employees by non-admin user"""
|
||||
response = client.get(
|
||||
"/api/employees",
|
||||
headers={"Authorization": f"Bearer {employee_token}"}
|
||||
)
|
||||
def test_get_employees_not_admin(client: TestClient, employee_headers):
|
||||
"""Test get all employees without admin rights"""
|
||||
response = client.get("/api/employees/", headers=employee_headers)
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"] == "Not enough permissions"
|
||||
|
||||
def test_get_me(client: TestClient, employee_token: str, test_employee: dict):
|
||||
"""Test getting current employee"""
|
||||
response = client.get(
|
||||
"/api/employees/me",
|
||||
headers={"Authorization": f"Bearer {employee_token}"}
|
||||
)
|
||||
def test_get_me(client: TestClient, employee_headers, test_employee):
|
||||
"""Test get current employee"""
|
||||
response = client.get("/api/employees/me", headers=employee_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == test_employee.id
|
||||
assert data["first_name"] == test_employee.first_name
|
||||
assert data["last_name"] == test_employee.last_name
|
||||
assert data["department"] == test_employee.department
|
||||
assert data["office"] == test_employee.office
|
||||
|
||||
def test_get_me_unauthorized(client: TestClient):
|
||||
"""Test getting current employee without authorization"""
|
||||
response = client.get("/api/employees/me")
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_update_me(client: TestClient, employee_token: str):
|
||||
"""Test updating current employee"""
|
||||
def test_update_me(client: TestClient, employee_headers, test_employee):
|
||||
"""Test update current employee"""
|
||||
response = client.put(
|
||||
"/api/employees/me",
|
||||
headers={"Authorization": f"Bearer {employee_token}"},
|
||||
headers=employee_headers,
|
||||
json={
|
||||
"first_name": "Updated",
|
||||
"last_name": "User",
|
||||
"department": "HR",
|
||||
"office": "104"
|
||||
"office": "Branch"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["first_name"] == "Updated"
|
||||
assert data["last_name"] == "User"
|
||||
assert data["department"] == "HR"
|
||||
assert data["office"] == "104"
|
||||
|
||||
def test_update_me_unauthorized(client: TestClient):
|
||||
"""Test updating current employee without authorization"""
|
||||
response = client.put(
|
||||
"/api/employees/me",
|
||||
json={
|
||||
"department": "HR",
|
||||
"office": "104"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
assert data["office"] == "Branch"
|
@@ -1,119 +1,72 @@
|
||||
"""Request tests"""
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
def test_create_request(client: TestClient, employee_token: str):
|
||||
"""Test request creation"""
|
||||
def test_create_request(client: TestClient, employee_headers):
|
||||
"""Test create request"""
|
||||
response = client.post(
|
||||
"/api/requests",
|
||||
headers={"Authorization": f"Bearer {employee_token}"},
|
||||
"/api/requests/",
|
||||
headers=employee_headers,
|
||||
json={
|
||||
"request_type": "equipment",
|
||||
"description": "Need a new laptop",
|
||||
"priority": "medium"
|
||||
"request_type": "HARDWARE",
|
||||
"description": "Need new laptop",
|
||||
"priority": "HIGH"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["request_type"] == "equipment"
|
||||
assert data["description"] == "Need a new laptop"
|
||||
assert data["priority"] == "medium"
|
||||
assert data["status"] == "new"
|
||||
assert data["request_type"] == "HARDWARE"
|
||||
assert data["description"] == "Need new laptop"
|
||||
assert data["priority"] == "HIGH"
|
||||
assert data["status"] == "NEW"
|
||||
|
||||
def test_create_request_unauthorized(client: TestClient):
|
||||
"""Test request creation without authorization"""
|
||||
response = client.post(
|
||||
"/api/requests",
|
||||
json={
|
||||
"request_type": "equipment",
|
||||
"description": "Need a new laptop",
|
||||
"priority": "medium"
|
||||
}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_get_my_requests(client: TestClient, employee_token: str):
|
||||
"""Test getting employee's requests"""
|
||||
response = client.get(
|
||||
"/api/requests/my",
|
||||
headers={"Authorization": f"Bearer {employee_token}"}
|
||||
)
|
||||
def test_get_my_requests(client: TestClient, employee_headers):
|
||||
"""Test get my requests"""
|
||||
response = client.get("/api/requests/my", headers=employee_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_get_my_requests_unauthorized(client: TestClient):
|
||||
"""Test getting employee's requests without authorization"""
|
||||
response = client.get("/api/requests/my")
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_get_all_requests_admin(client: TestClient, admin_token: str):
|
||||
"""Test getting all requests by admin"""
|
||||
response = client.get(
|
||||
"/api/requests/admin",
|
||||
headers={"Authorization": f"Bearer {admin_token}"}
|
||||
)
|
||||
def test_get_all_requests_admin(client: TestClient, admin_headers):
|
||||
"""Test get all requests as admin"""
|
||||
response = client.get("/api/requests/", headers=admin_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_get_all_requests_unauthorized(client: TestClient):
|
||||
"""Test getting all requests without authorization"""
|
||||
response = client.get("/api/requests/admin")
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
|
||||
def test_get_all_requests_not_admin(client: TestClient, employee_token: str):
|
||||
"""Test getting all requests by non-admin user"""
|
||||
response = client.get(
|
||||
"/api/requests/admin",
|
||||
headers={"Authorization": f"Bearer {employee_token}"}
|
||||
)
|
||||
def test_get_all_requests_not_admin(client: TestClient, employee_headers):
|
||||
"""Test get all requests without admin rights"""
|
||||
response = client.get("/api/requests/", headers=employee_headers)
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"] == "Not enough permissions"
|
||||
|
||||
def test_update_request_status_admin(client: TestClient, admin_token: str):
|
||||
"""Test updating request status by admin"""
|
||||
def test_update_request_status_admin(client: TestClient, admin_headers):
|
||||
"""Test update request status as admin"""
|
||||
# Сначала создаем запрос
|
||||
response = client.post(
|
||||
"/api/requests",
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
create_response = client.post(
|
||||
"/api/requests/",
|
||||
headers=admin_headers,
|
||||
json={
|
||||
"request_type": "equipment",
|
||||
"description": "Need a new laptop",
|
||||
"priority": "medium"
|
||||
"request_type": "SOFTWARE",
|
||||
"description": "Need new IDE",
|
||||
"priority": "MEDIUM"
|
||||
}
|
||||
)
|
||||
request_id = response.json()["id"]
|
||||
|
||||
# Обновляем статус
|
||||
response = client.patch(
|
||||
request_id = create_response.json()["id"]
|
||||
|
||||
# Затем обновляем его статус
|
||||
response = client.put(
|
||||
f"/api/requests/{request_id}/status",
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
json={"status": "in_progress"}
|
||||
headers=admin_headers,
|
||||
json={"status": "IN_PROGRESS"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["status"] == "in_progress"
|
||||
assert data["status"] == "IN_PROGRESS"
|
||||
|
||||
def test_update_request_status_not_admin(client: TestClient, employee_token: str):
|
||||
"""Test updating request status by non-admin user"""
|
||||
response = client.patch(
|
||||
def test_update_request_status_not_admin(client: TestClient, employee_headers):
|
||||
"""Test update request status without admin rights"""
|
||||
response = client.put(
|
||||
"/api/requests/1/status",
|
||||
headers={"Authorization": f"Bearer {employee_token}"},
|
||||
json={"status": "in_progress"}
|
||||
headers=employee_headers,
|
||||
json={"status": "IN_PROGRESS"}
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"] == "Not enough permissions"
|
||||
|
||||
def test_update_request_status_unauthorized(client: TestClient):
|
||||
"""Test updating request status without authorization"""
|
||||
response = client.patch(
|
||||
"/api/requests/1/status",
|
||||
json={"status": "in_progress"}
|
||||
)
|
||||
assert response.status_code == 401
|
||||
assert response.json()["detail"] == "Not authenticated"
|
||||
assert response.status_code == 403
|
Reference in New Issue
Block a user