Files
eventspace/backend/tests/api/routes/events/test_guests.py
Felipe Cardoso 3c196b1e91
All checks were successful
Build and Push Docker Images / changes (push) Successful in 4s
Build and Push Docker Images / build-backend (push) Successful in 50s
Build and Push Docker Images / build-frontend (push) Has been skipped
Add invitation code validation and auto-generation for guests
Validate uniqueness of invitation codes during guest creation to prevent duplicates. Automatically generate an 8-character code if none is provided, ensuring consistent data handling. Updated tests and schemas to support these changes.
2025-03-15 20:34:38 +01:00

132 lines
5.0 KiB
Python

import uuid
import pytest
from app.api.routes.events.guests import router as guests_router
from app.models import GuestStatus
@pytest.fixture
def guest_data():
return {
"event_id": str(uuid.uuid4()), # Correctly generates actual UUID objects
"invited_by": str(uuid.uuid4()),
"full_name": "John Doe",
"email": "john.doe@example.com",
"invitation_code": "INVITE12345",
"can_bring_guests": False
}
class TestGuestsRouter:
@pytest.fixture(autouse=True)
def setup_method(self, create_test_client, db_session, mock_user, guest_fixture):
self.client = create_test_client(
router=guests_router,
prefix="/guests",
db_session=db_session,
user=mock_user
)
self.db_session = db_session
self.mock_user = mock_user
self.endpoint = "/guests"
self.guest = guest_fixture
def test_create_guest_success(self, guest_data):
response = self.client.post(self.endpoint, json=guest_data)
assert response.status_code == 200
data = response.json()
assert data["full_name"] == guest_data["full_name"]
assert data["email"] == guest_data["email"]
def test_create_guest_fails_on_duplicate_invitation_code(self, guest_data):
# First create the guest successfully
response_initial = self.client.post(self.endpoint, json=guest_data)
assert response_initial.status_code == 200
# Attempt to create another guest with the same invitation code
new_guest_data = guest_data.copy()
new_guest_data["email"] = "new.email@example.com"
response_duplicate = self.client.post(self.endpoint, json=new_guest_data)
assert response_duplicate.status_code == 400
assert response_duplicate.json()["detail"] == "Guest with this invitation code already exists"
def test_create_guest_generates_invitation_code_if_not_provided(self, guest_data):
# Remove invitation_code to test auto-generation
guest_data_without_code = guest_data.copy()
guest_data_without_code.pop("invitation_code", None)
response = self.client.post(self.endpoint, json=guest_data_without_code)
assert response.status_code == 200
data = response.json()
print(data)
assert "invitation_code" in data
assert len(data["invitation_code"]) == 8
def test_create_guest_missing_required_fields_fails(self):
incomplete_payload = {
"email": "john.doe@example.com"
}
response = self.client.post(self.endpoint, json=incomplete_payload)
assert response.status_code == 422
def test_read_guest_success(self, guest_fixture):
response = self.client.get(f"{self.endpoint}/{guest_fixture.id}")
assert response.status_code == 200
data = response.json()
assert data["id"] == str(guest_fixture.id)
assert data["full_name"] == guest_fixture.full_name
def test_read_guest_not_found(self):
random_uuid = str(uuid.uuid4())
response = self.client.get(f"{self.endpoint}/{random_uuid}")
assert response.status_code == 404
assert response.json()["detail"] == "Guest not found"
def test_update_guest_success(self):
update_data = {"full_name": "Jane Doe"}
response = self.client.put(f"{self.endpoint}/{self.guest.id}", json=update_data)
assert response.status_code == 200
assert response.json()["full_name"] == "Jane Doe"
def test_update_guest_not_found_fails(self):
fake_uuid = str(uuid.uuid4())
update_data = {"full_name": "Nobody"}
response = self.client.put(f"{self.endpoint}/{fake_uuid}", json=update_data)
assert response.status_code == 404
def test_delete_guest_success(self):
response = self.client.delete(f"{self.endpoint}/{self.guest.id}")
assert response.status_code == 200
assert response.json()["id"] == str(self.guest.id)
def test_delete_guest_not_found_fails(self):
fake_uuid = str(uuid.uuid4())
response = self.client.delete(f"{self.endpoint}/{fake_uuid}")
assert response.status_code == 404
assert response.json()["detail"] == "Guest not found"
def test_list_guests_success(self):
response = self.client.get(self.endpoint)
assert response.status_code == 200
assert isinstance(response.json(), list)
assert any(g["id"] == str(self.guest.id) for g in response.json())
def test_guest_update_status_success(self):
response = self.client.patch(
f"{self.endpoint}/{self.guest.id}/status",
params={"status": "confirmed"}
)
assert response.status_code == 200
assert response.json()["status"] == GuestStatus.CONFIRMED.value
def test_guest_status_update_invalid_status_fails(self):
response = self.client.patch(
f"{self.endpoint}/{self.guest.id}/status",
params={"status": "invalid_status"}
)
assert response.status_code == 422