Initial monorepo structure
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
"""Pytest configuration."""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Create test client."""
|
||||
from app.main import app
|
||||
from app.core.database import engine, Base, SessionLocal
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
yield TestClient(app)
|
||||
|
||||
# Drop tables
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db():
|
||||
"""Create test database session."""
|
||||
from app.core.database import SessionLocal
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
yield db
|
||||
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_user(db):
|
||||
"""Create sample user for testing."""
|
||||
from app.models.user import User
|
||||
from app.core.security import get_password_hash
|
||||
|
||||
user = User(
|
||||
email="test@example.com",
|
||||
password_hash=get_password_hash("password123"),
|
||||
first_name="Test",
|
||||
last_name="User",
|
||||
is_active=True
|
||||
)
|
||||
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def auth_headers(client, sample_user):
|
||||
"""Get auth headers for testing."""
|
||||
from app.core.security import create_access_token
|
||||
|
||||
token = create_access_token(data={"sub": str(sample_user.id)})
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_ticket(db, sample_user):
|
||||
"""Create sample ticket for testing."""
|
||||
from app.models.ticket import Ticket
|
||||
from app.models.priority import Priority
|
||||
from app.models.status import Status
|
||||
from app.models.category import Category
|
||||
|
||||
# Create sample data
|
||||
category = Category(name="Hardware")
|
||||
priority = Priority(name="Medium", level=2)
|
||||
status = Status(name="Open", is_final=False)
|
||||
|
||||
db.add_all([category, priority, status])
|
||||
db.commit()
|
||||
db.refresh(category)
|
||||
db.refresh(priority)
|
||||
db.refresh(status)
|
||||
|
||||
ticket = Ticket(
|
||||
subject="Test Ticket",
|
||||
description="This is a test ticket",
|
||||
category_id=category.id,
|
||||
priority_id=priority.id,
|
||||
status_id=status.id,
|
||||
source="web",
|
||||
opened_by=sample_user.id
|
||||
)
|
||||
|
||||
db.add(ticket)
|
||||
db.commit()
|
||||
db.refresh(ticket)
|
||||
|
||||
return ticket
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_admin_user(db):
|
||||
"""Create sample admin user for testing."""
|
||||
from app.models.user import User
|
||||
from app.models.role import Role
|
||||
from app.models.extra import UserRole
|
||||
from app.core.security import get_password_hash
|
||||
|
||||
# Create admin role
|
||||
admin_role = Role(name="admin")
|
||||
db.add(admin_role)
|
||||
db.commit()
|
||||
db.refresh(admin_role)
|
||||
|
||||
user = User(
|
||||
email="admin@example.com",
|
||||
password_hash=get_password_hash("admin123"),
|
||||
first_name="Admin",
|
||||
last_name="User",
|
||||
is_active=True
|
||||
)
|
||||
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
# Assign admin role
|
||||
user_role = UserRole(user_id=user.id, role_id=admin_role.id)
|
||||
db.add(user_role)
|
||||
db.commit()
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_role(db):
|
||||
"""Create sample role for testing."""
|
||||
from app.models.role import Role
|
||||
|
||||
role = Role(name="test_role", description="Test role description")
|
||||
db.add(role)
|
||||
db.commit()
|
||||
db.refresh(role)
|
||||
|
||||
return role
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_status(db):
|
||||
"""Create sample status for testing."""
|
||||
from app.models.status import Status
|
||||
|
||||
status = Status(name="Open", is_final=False)
|
||||
db.add(status)
|
||||
db.commit()
|
||||
db.refresh(status)
|
||||
|
||||
return status
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_priority(db):
|
||||
"""Create sample priority for testing."""
|
||||
from app.models.priority import Priority
|
||||
|
||||
priority = Priority(name="Medium", level=2, default_response_time_minutes=60)
|
||||
db.add(priority)
|
||||
db.commit()
|
||||
db.refresh(priority)
|
||||
|
||||
return priority
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_category(db):
|
||||
"""Create sample category for testing."""
|
||||
from app.models.category import Category
|
||||
|
||||
category = Category(name="Hardware")
|
||||
db.add(category)
|
||||
db.commit()
|
||||
db.refresh(category)
|
||||
|
||||
return category
|
||||
@@ -0,0 +1 @@
|
||||
"""Test API package."""
|
||||
@@ -0,0 +1,95 @@
|
||||
"""Test auth endpoints."""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
|
||||
class TestAuth:
|
||||
"""Test authentication endpoints."""
|
||||
|
||||
def test_register_user(self, client: TestClient):
|
||||
"""Test user registration."""
|
||||
response = client.post(
|
||||
"/api/v1/auth/register",
|
||||
json={
|
||||
"email": "newuser@example.com",
|
||||
"password": "password123",
|
||||
"first_name": "John",
|
||||
"last_name": "Doe"
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["email"] == "newuser@example.com"
|
||||
assert data["first_name"] == "John"
|
||||
|
||||
def test_register_duplicate_user(self, client: TestClient, sample_user):
|
||||
"""Test registering with existing email."""
|
||||
response = client.post(
|
||||
"/api/v1/auth/register",
|
||||
json={
|
||||
"email": sample_user.email,
|
||||
"password": "password123",
|
||||
"first_name": "Jane",
|
||||
"last_name": "Smith"
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_login_success(self, client: TestClient, sample_user):
|
||||
"""Test successful login."""
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={
|
||||
"email": sample_user.email,
|
||||
"password": "password123"
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "access_token" in data
|
||||
assert data["token_type"] == "bearer"
|
||||
assert data["user"]["email"] == sample_user.email
|
||||
|
||||
def test_login_invalid_credentials(self, client: TestClient):
|
||||
"""Test login with invalid credentials."""
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={
|
||||
"email": "nonexistent@example.com",
|
||||
"password": "wrongpassword"
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
def test_login_wrong_password(self, client: TestClient, sample_user):
|
||||
"""Test login with wrong password."""
|
||||
response = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={
|
||||
"email": sample_user.email,
|
||||
"password": "wrongpassword"
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
def test_get_current_user(self, client: TestClient, sample_user, auth_headers):
|
||||
"""Test getting current user."""
|
||||
response = client.get("/api/v1/auth/me", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == sample_user.id
|
||||
assert data["email"] == sample_user.email
|
||||
|
||||
def test_get_current_user_unauthorized(self, client: TestClient):
|
||||
"""Test getting current user without auth."""
|
||||
response = client.get("/api/v1/auth/me")
|
||||
|
||||
assert response.status_code == 403
|
||||
@@ -0,0 +1,281 @@
|
||||
"""Test reference data endpoints."""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
|
||||
class TestRoles:
|
||||
"""Test role endpoints."""
|
||||
|
||||
def test_get_roles(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting roles list."""
|
||||
response = client.get("/api/v1/reference/roles", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_create_role(self, client: TestClient, auth_headers: dict):
|
||||
"""Test creating a new role."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/roles",
|
||||
json={
|
||||
"name": "test_role",
|
||||
"description": "Test role description"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["name"] == "test_role"
|
||||
assert data["description"] == "Test role description"
|
||||
|
||||
def test_create_duplicate_role(self, client: TestClient, auth_headers: dict, sample_role):
|
||||
"""Test creating role with existing name."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/roles",
|
||||
json={
|
||||
"name": sample_role.name,
|
||||
"description": "Duplicate role"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_update_role(self, client: TestClient, auth_headers: dict, sample_role):
|
||||
"""Test updating a role."""
|
||||
response = client.put(
|
||||
f"/api/v1/reference/roles/{sample_role.id}",
|
||||
json={
|
||||
"name": "updated_role",
|
||||
"description": "Updated description"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == "updated_role"
|
||||
|
||||
def test_delete_role(self, client: TestClient, auth_headers: dict, sample_role):
|
||||
"""Test deleting a role."""
|
||||
response = client.delete(
|
||||
f"/api/v1/reference/roles/{sample_role.id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
|
||||
class TestStatuses:
|
||||
"""Test status endpoints."""
|
||||
|
||||
def test_get_statuses(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting statuses list."""
|
||||
response = client.get("/api/v1/reference/statuses", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_create_status(self, client: TestClient, auth_headers: dict):
|
||||
"""Test creating a new status."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/statuses",
|
||||
json={
|
||||
"name": "New Status",
|
||||
"is_final": False
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["name"] == "New Status"
|
||||
assert data["is_final"] == False
|
||||
|
||||
def test_create_duplicate_status(self, client: TestClient, auth_headers: dict, sample_status):
|
||||
"""Test creating status with existing name."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/statuses",
|
||||
json={
|
||||
"name": sample_status.name,
|
||||
"is_final": False
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_update_status(self, client: TestClient, auth_headers: dict, sample_status):
|
||||
"""Test updating a status."""
|
||||
response = client.put(
|
||||
f"/api/v1/reference/statuses/{sample_status.id}",
|
||||
json={
|
||||
"name": "Updated Status",
|
||||
"is_final": True
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == "Updated Status"
|
||||
assert data["is_final"] == True
|
||||
|
||||
def test_delete_status(self, client: TestClient, auth_headers: dict, sample_status):
|
||||
"""Test deleting a status."""
|
||||
response = client.delete(
|
||||
f"/api/v1/reference/statuses/{sample_status.id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
|
||||
class TestPriorities:
|
||||
"""Test priority endpoints."""
|
||||
|
||||
def test_get_priorities(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting priorities list."""
|
||||
response = client.get("/api/v1/reference/priorities", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_create_priority(self, client: TestClient, auth_headers: dict):
|
||||
"""Test creating a new priority."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/priorities",
|
||||
json={
|
||||
"name": "Critical",
|
||||
"level": 4,
|
||||
"default_response_time_minutes": 30
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["name"] == "Critical"
|
||||
assert data["level"] == 4
|
||||
|
||||
def test_create_duplicate_priority(self, client: TestClient, auth_headers: dict, sample_priority):
|
||||
"""Test creating priority with existing name."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/priorities",
|
||||
json={
|
||||
"name": sample_priority.name,
|
||||
"level": 5
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_update_priority(self, client: TestClient, auth_headers: dict, sample_priority):
|
||||
"""Test updating a priority."""
|
||||
response = client.put(
|
||||
f"/api/v1/reference/priorities/{sample_priority.id}",
|
||||
json={
|
||||
"name": "High",
|
||||
"level": 3,
|
||||
"default_response_time_minutes": 45
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == "High"
|
||||
assert data["level"] == 3
|
||||
|
||||
def test_delete_priority(self, client: TestClient, auth_headers: dict, sample_priority):
|
||||
"""Test deleting a priority."""
|
||||
response = client.delete(
|
||||
f"/api/v1/reference/priorities/{sample_priority.id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
|
||||
class TestCategories:
|
||||
"""Test category endpoints."""
|
||||
|
||||
def test_get_categories(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting categories list."""
|
||||
response = client.get("/api/v1/reference/categories", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_create_category(self, client: TestClient, auth_headers: dict):
|
||||
"""Test creating a new category."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/categories",
|
||||
json={
|
||||
"name": "Software"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["name"] == "Software"
|
||||
|
||||
def test_create_subcategory(self, client: TestClient, auth_headers: dict, sample_category):
|
||||
"""Test creating a subcategory."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/categories",
|
||||
json={
|
||||
"name": "Laptops",
|
||||
"parent_id": sample_category.id
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["name"] == "Laptops"
|
||||
assert data["parent_id"] == sample_category.id
|
||||
|
||||
def test_create_duplicate_category(self, client: TestClient, auth_headers: dict, sample_category):
|
||||
"""Test creating category with existing name."""
|
||||
response = client.post(
|
||||
"/api/v1/reference/categories",
|
||||
json={
|
||||
"name": sample_category.name
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
|
||||
def test_update_category(self, client: TestClient, auth_headers: dict, sample_category):
|
||||
"""Test updating a category."""
|
||||
response = client.put(
|
||||
f"/api/v1/reference/categories/{sample_category.id}",
|
||||
json={
|
||||
"name": "Updated Category"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["name"] == "Updated Category"
|
||||
|
||||
def test_delete_category(self, client: TestClient, auth_headers: dict, sample_category):
|
||||
"""Test deleting a category."""
|
||||
response = client.delete(
|
||||
f"/api/v1/reference/categories/{sample_category.id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
@@ -0,0 +1,104 @@
|
||||
"""Test ticket endpoints."""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
|
||||
class TestTickets:
|
||||
"""Test ticket endpoints."""
|
||||
|
||||
def test_create_ticket(self, client: TestClient, auth_headers: dict, sample_user, sample_category, sample_priority, sample_status):
|
||||
"""Test creating a new ticket."""
|
||||
response = client.post(
|
||||
"/api/v1/tickets",
|
||||
json={
|
||||
"subject": "Test Ticket",
|
||||
"description": "This is a test ticket",
|
||||
"category_id": sample_category.id,
|
||||
"priority_id": sample_priority.id,
|
||||
"status_id": sample_status.id
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["subject"] == "Test Ticket"
|
||||
assert data["opened_by"] == sample_user.id
|
||||
|
||||
def test_get_tickets(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting tickets list."""
|
||||
response = client.get("/api/v1/tickets", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "tickets" in data
|
||||
assert "total" in data
|
||||
assert "page" in data
|
||||
|
||||
def test_get_ticket_by_id(self, client: TestClient, auth_headers: dict, sample_ticket):
|
||||
"""Test getting ticket by ID."""
|
||||
response = client.get(f"/api/v1/tickets/{sample_ticket.id}", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == sample_ticket.id
|
||||
assert data["subject"] == sample_ticket.subject
|
||||
|
||||
def test_update_ticket(self, client: TestClient, auth_headers: dict, sample_ticket):
|
||||
"""Test updating a ticket."""
|
||||
response = client.put(
|
||||
f"/api/v1/tickets/{sample_ticket.id}",
|
||||
json={
|
||||
"subject": "Updated Ticket",
|
||||
"description": "Updated description"
|
||||
},
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["subject"] == "Updated Ticket"
|
||||
|
||||
def test_delete_ticket(self, client: TestClient, auth_headers: dict, sample_ticket):
|
||||
"""Test deleting a ticket."""
|
||||
response = client.delete(
|
||||
f"/api/v1/tickets/{sample_ticket.id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 204
|
||||
|
||||
# Verify ticket is deleted
|
||||
get_response = client.get(f"/api/v1/tickets/{sample_ticket.id}", headers=auth_headers)
|
||||
assert get_response.status_code == 404
|
||||
|
||||
def test_create_ticket_unauthorized(self, client: TestClient):
|
||||
"""Test creating ticket without auth."""
|
||||
response = client.post(
|
||||
"/api/v1/tickets",
|
||||
json={
|
||||
"subject": "Test Ticket",
|
||||
"description": "This is a test ticket",
|
||||
"category_id": 1,
|
||||
"priority_id": 1,
|
||||
"status_id": 1
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
def test_get_ticket_not_found(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting non-existent ticket."""
|
||||
response = client.get("/api/v1/tickets/99999", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_get_ticket_comments(self, client: TestClient, auth_headers: dict, sample_ticket):
|
||||
"""Test getting ticket comments."""
|
||||
response = client.get(f"/api/v1/tickets/{sample_ticket.id}/comments", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert isinstance(data, list)
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Test user endpoints."""
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
|
||||
class TestUsers:
|
||||
"""Test user endpoints."""
|
||||
|
||||
def test_get_users(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting users list."""
|
||||
response = client.get("/api/v1/users/", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "users" in data
|
||||
assert "total" in data
|
||||
|
||||
def test_get_me(self, client: TestClient, auth_headers: dict):
|
||||
"""Test getting current user."""
|
||||
response = client.get("/api/v1/users/me", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "id" in data
|
||||
assert "email" in data
|
||||
Reference in New Issue
Block a user