Files
platform-project/request-module/tests/test_api/test_tickets.py
T
Арсентьев Александр 07dbde2a0e Initial monorepo structure
2026-08-06 11:23:37 +03:00

105 lines
3.6 KiB
Python

"""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)