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

Все подряд

This commit is contained in:
MoonTestUse1
2024-12-31 02:37:57 +06:00
parent 8e53bb6cb2
commit d5780b2eab
3258 changed files with 1087440 additions and 268 deletions

View File

@@ -0,0 +1 @@
"""Test package initialization"""

72
backend/tests/conftest.py Normal file
View File

@@ -0,0 +1,72 @@
"""Test configuration and fixtures"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
import logging
from app.database import Base, get_db
from app.main import app
# Configure logging for tests
logging.basicConfig(level=logging.INFO)
# Create test database
SQLALCHEMY_TEST_DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(
SQLALCHEMY_TEST_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
"""Override database dependency"""
db = TestingSessionLocal()
try:
yield db
finally:
db.close()
@pytest.fixture(scope="function")
def test_db():
"""Create test database"""
Base.metadata.create_all(bind=engine)
db = TestingSessionLocal()
try:
yield db
finally:
db.close()
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def client(test_db):
"""Create test client"""
app.dependency_overrides[get_db] = override_get_db
with TestClient(app) as test_client:
yield test_client
app.dependency_overrides.clear()
@pytest.fixture
def test_employee():
"""Test employee data"""
return {
"first_name": "Test",
"last_name": "User",
"department": "general",
"office": "101",
"password": "testpass123"
}
@pytest.fixture
def test_request():
"""Test request data"""
return {
"employee_id": 1,
"department": "general",
"request_type": "hardware",
"priority": "medium",
"description": "Test request"
}

View File

@@ -0,0 +1,47 @@
"""Authentication endpoint tests"""
import pytest
from app.crud import employees
from app.models.employee import EmployeeCreate
def test_login_success(client, test_db, test_employee):
"""Test successful login"""
# Create test employee
employee_data = EmployeeCreate(**test_employee)
employees.create_employee(test_db, employee_data)
# Attempt login
response = client.post(
"/api/auth/login",
json={
"lastName": test_employee["last_name"],
"password": test_employee["password"]
}
)
assert response.status_code == 200
data = response.json()
assert data["lastName"] == test_employee["last_name"]
assert "password" not in data
def test_login_invalid_credentials(client):
"""Test login with invalid credentials"""
response = client.post(
"/api/auth/login",
json={
"lastName": "NonExistent",
"password": "wrongpass"
}
)
assert response.status_code == 401
assert response.json()["detail"] == "Неверные учетные данные"
def test_login_missing_fields(client):
"""Test login with missing fields"""
response = client.post(
"/api/auth/login",
json={"lastName": "Test"}
)
assert response.status_code == 400
assert "Необходимо указать" in response.json()["detail"]

View File

@@ -0,0 +1,68 @@
"""Employee management endpoint tests"""
import pytest
from app.models.employee import EmployeeCreate
def test_create_employee(client, test_employee):
"""Test employee creation"""
response = client.post(
"/api/employees",
json={
"first_name": test_employee["first_name"],
"last_name": test_employee["last_name"],
"department": test_employee["department"],
"office": test_employee["office"],
"password": test_employee["password"]
}
)
assert response.status_code == 200
data = response.json()
assert data["firstName"] == test_employee["first_name"]
assert data["lastName"] == test_employee["last_name"]
assert "password" not in data
def test_create_employee_duplicate(client, test_employee):
"""Test creating duplicate employee"""
# Create first employee
client.post(
"/api/employees",
json={
"first_name": test_employee["first_name"],
"last_name": test_employee["last_name"],
"department": test_employee["department"],
"office": test_employee["office"],
"password": test_employee["password"]
}
)
# Try to create duplicate
response = client.post(
"/api/employees",
json={
"first_name": test_employee["first_name"],
"last_name": test_employee["last_name"],
"department": test_employee["department"],
"office": test_employee["office"],
"password": test_employee["password"]
}
)
assert response.status_code == 400
assert "уже существует" in response.json()["detail"]
def test_create_employee_invalid_data(client):
"""Test creating employee with invalid data"""
invalid_employee = {
"first_name": "", # Empty name
"last_name": "Test",
"department": "invalid", # Invalid department
"office": "101",
"password": "test"
}
response = client.post(
"/api/employees",
json=invalid_employee
)
assert response.status_code == 422

View File

@@ -0,0 +1,75 @@
"""Request management endpoint tests"""
import pytest
from unittest.mock import patch
def test_create_request(client, test_db, test_employee, test_request):
"""Test request creation"""
# Create test employee first
employee_response = client.post(
"/api/employees",
json={
"first_name": test_employee["first_name"],
"last_name": test_employee["last_name"],
"department": test_employee["department"],
"office": test_employee["office"],
"password": test_employee["password"]
}
)
assert employee_response.status_code == 200
employee_data = employee_response.json()
test_request["employee_id"] = employee_data["id"]
# Create request
with patch('app.bot.notifications.send_notification'): # Mock notification
response = client.post(
"/api/requests",
json=test_request
)
assert response.status_code == 200
data = response.json()
assert data["employee_id"] == test_request["employee_id"]
assert data["status"] == "new"
def test_create_request_invalid_employee(client, test_request):
"""Test creating request with invalid employee ID"""
test_request["employee_id"] = 999 # Non-existent ID
response = client.post(
"/api/requests",
json=test_request
)
assert response.status_code == 404
assert "не найден" in response.json()["detail"]
def test_create_request_invalid_priority(client, test_db, test_employee):
"""Test creating request with invalid priority"""
# Create test employee first
employee_response = client.post(
"/api/employees",
json={
"first_name": test_employee["first_name"],
"last_name": test_employee["last_name"],
"department": test_employee["department"],
"office": test_employee["office"],
"password": test_employee["password"]
}
)
assert employee_response.status_code == 200
employee_data = employee_response.json()
invalid_request = {
"employee_id": employee_data["id"],
"department": "general",
"request_type": "hardware",
"priority": "invalid", # Invalid priority
"description": "Test request"
}
response = client.post(
"/api/requests",
json=invalid_request
)
assert response.status_code == 422