import uuid from fastapi.testclient import TestClient from sqlmodel import Session from app.core.config import settings from tests.utils.location import create_random_location def test_create_location( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: data = {"title": "Test Location", "description": "A test location"} response = client.post( f"{settings.API_V1_STR}/locations/", headers=superuser_token_headers, json=data, ) assert response.status_code == 200 content = response.json() assert content["title"] == data["title"] assert content["description"] == data["description"] assert "id" in content assert "owner_id" in content def test_read_location( client: TestClient, superuser_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) response = client.get( f"{settings.API_V1_STR}/locations/{location.id}", headers=superuser_token_headers, ) assert response.status_code == 200 content = response.json() assert content["title"] == location.title assert content["description"] == location.description assert content["id"] == str(location.id) assert content["owner_id"] == str(location.owner_id) def test_read_location_not_found( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: response = client.get( f"{settings.API_V1_STR}/locations/{uuid.uuid4()}", headers=superuser_token_headers, ) assert response.status_code == 404 content = response.json() assert content["detail"] == "Location not found" def test_read_location_not_enough_permissions( client: TestClient, normal_user_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) response = client.get( f"{settings.API_V1_STR}/locations/{location.id}", headers=normal_user_token_headers, ) assert response.status_code == 403 content = response.json() assert content["detail"] == "Not enough permissions" def test_read_locations( client: TestClient, superuser_token_headers: dict[str, str], db: Session ) -> None: create_random_location(db) create_random_location(db) response = client.get( f"{settings.API_V1_STR}/locations/", headers=superuser_token_headers, ) assert response.status_code == 200 content = response.json() assert len(content["data"]) >= 2 def test_update_location( client: TestClient, superuser_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) data = {"title": "Updated title", "description": "Updated description"} response = client.put( f"{settings.API_V1_STR}/locations/{location.id}", headers=superuser_token_headers, json=data, ) assert response.status_code == 200 content = response.json() assert content["title"] == data["title"] assert content["description"] == data["description"] assert content["id"] == str(location.id) assert content["owner_id"] == str(location.owner_id) def test_update_location_not_found( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: data = {"title": "Updated title", "description": "Updated description"} response = client.put( f"{settings.API_V1_STR}/locations/{uuid.uuid4()}", headers=superuser_token_headers, json=data, ) assert response.status_code == 404 content = response.json() assert content["detail"] == "Location not found" def test_update_location_not_enough_permissions( client: TestClient, normal_user_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) data = {"title": "Updated title", "description": "Updated description"} response = client.put( f"{settings.API_V1_STR}/locations/{location.id}", headers=normal_user_token_headers, json=data, ) assert response.status_code == 403 content = response.json() assert content["detail"] == "Not enough permissions" def test_delete_location( client: TestClient, superuser_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) response = client.delete( f"{settings.API_V1_STR}/locations/{location.id}", headers=superuser_token_headers, ) assert response.status_code == 200 content = response.json() assert content["message"] == "Location deleted successfully" def test_delete_location_not_found( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: response = client.delete( f"{settings.API_V1_STR}/locations/{uuid.uuid4()}", headers=superuser_token_headers, ) assert response.status_code == 404 content = response.json() assert content["detail"] == "Location not found" def test_delete_location_not_enough_permissions( client: TestClient, normal_user_token_headers: dict[str, str], db: Session ) -> None: location = create_random_location(db) response = client.delete( f"{settings.API_V1_STR}/locations/{location.id}", headers=normal_user_token_headers, ) assert response.status_code == 403 content = response.json() assert content["detail"] == "Not enough permissions"