[WIP] Link hike to places

This commit is contained in:
Sebastiaan
2025-11-01 01:03:34 +01:00
parent 84d75e21ca
commit 0d6ef073df
15 changed files with 875 additions and 10 deletions

View File

@@ -0,0 +1,217 @@
import uuid
from fastapi import status
from fastapi.testclient import TestClient
from sqlmodel import Session
from app.core.config import settings
from app.tests.utils.place import create_random_place
from app.models.place import PlaceType, VisitedCountType
def test_create_place(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
data = {
"place_type": PlaceType.PLACE,
"name": "Post 1",
"short_name": "1",
"contact": "Sebas",
"description": "Post met renspel",
"question_file": None,
"answer_file": None,
"place_time": "PT10M",
"latitude": None,
"longitude": None,
"radius": None,
"max_points": None,
"visited_points": 10,
"visited_count_type": VisitedCountType.ONE_VISIT,
"skipped_penalty_points": 50,
}
response = client.post(
f"{settings.API_V1_STR}/places/",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["name"] == data["name"]
assert content["place_type"] == data["place_type"]
assert content["name"] == data["name"]
assert content["short_name"] == data["short_name"]
assert content["contact"] == data["contact"]
assert content["description"] == data["description"]
assert content["question_file"] == data["question_file"]
assert content["answer_file"] == data["answer_file"]
assert content["latitude"] == data["latitude"]
assert content["longitude"] == data["longitude"]
assert content["radius"] == data["radius"]
assert content["max_points"] == data["max_points"]
assert content["visited_points"] == data["visited_points"]
assert content["visited_count_type"] == data["visited_count_type"]
assert content["skipped_penalty_points"] == data["skipped_penalty_points"]
assert content["place_time"] == data["place_time"]
assert "id" in content
def test_create_place_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
data = {
"place_type": PlaceType.PLACE,
"name": "No permissions",
"short_name": "No perm",
"visited_count_type": VisitedCountType.ONE_VISIT,
}
response = client.post(
f"{settings.API_V1_STR}/places/",
headers=normal_user_token_headers,
json=data,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_read_place(
client: TestClient, superuser_token_headers: dict[str, str], db: Session
) -> None:
place = create_random_place(db)
response = client.get(
f"{settings.API_V1_STR}/places/{place.id}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["id"] == str(place.id)
assert content["name"] == place.name
assert content["contact"] == place.contact
def test_read_place_not_found(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
response = client.get(
f"{settings.API_V1_STR}/places/{uuid.uuid4()}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Place not found"
def test_read_place_no_permission(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
place = create_random_place(db)
response = client.get(
f"{settings.API_V1_STR}/places/{place.id}",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_read_places(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
create_random_place(db)
create_random_place(db)
response = client.get(
f"{settings.API_V1_STR}/places/",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert "count" in content
assert content["count"] >= 2
assert "data" in content
assert isinstance(content["data"], list)
assert len(content["data"]) <= content["count"]
def test_read_places_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
create_random_place(db)
create_random_place(db)
response = client.get(
f"{settings.API_V1_STR}/places/",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert "count" in content
assert content["count"] == 0
assert "data" in content
assert isinstance(content["data"], list)
assert len(content["data"]) == 0
def test_update_place(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
place = create_random_place(db)
data = {
"name": "Updated name",
"short_name": "4", # TODO: Fix Shortname
}
response = client.put(
f"{settings.API_V1_STR}/places/{place.id}",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["id"] == str(place.id)
assert content["name"] == data["name"]
assert content["short_name"] == data["short_name"]
def test_update_place_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
data = {
"name": "Not found",
"short_name": "5", # TODO: Fix Shortname
}
response = client.put(
f"{settings.API_V1_STR}/places/{uuid.uuid4()}",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Place not found"
def test_update_place_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
place = create_random_place(db)
data = {
"name": "No permissions",
"short_name": "6", # TODO: Fix Shortname
}
response = client.put(
f"{settings.API_V1_STR}/places/{place.id}",
headers=normal_user_token_headers,
json=data,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_delete_place(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
place = create_random_place(db)
response = client.delete(
f"{settings.API_V1_STR}/places/{place.id}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["message"] == "Place deleted successfully"
def test_delete_place_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
response = client.delete(
f"{settings.API_V1_STR}/places/{uuid.uuid4()}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Place not found"
def test_delete_place_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
place = create_random_place(db)
response = client.delete(
f"{settings.API_V1_STR}/places/{place.id}",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"