[WIP] Link hike to places
This commit is contained in:
217
backend/app/tests/api/routes/test_place.py
Normal file
217
backend/app/tests/api/routes/test_place.py
Normal 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"
|
||||
14
backend/app/tests/utils/place.py
Normal file
14
backend/app/tests/utils/place.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from sqlmodel import Session
|
||||
|
||||
from app.models.place import Place, PlaceCreate
|
||||
from app.tests.utils.utils import random_lower_string, random_lower_short_string
|
||||
|
||||
|
||||
def create_random_place(db: Session, name: str = None) -> Place:
|
||||
if not name:
|
||||
name = random_lower_string()
|
||||
|
||||
short_name = random_lower_short_string()
|
||||
|
||||
place_in = PlaceCreate(name=name, short_name=short_name)
|
||||
return Place.create(session=db, create_obj=place_in)
|
||||
@@ -6,7 +6,7 @@ from app.tests.utils.utils import random_lower_string
|
||||
from app.tests.utils.hike import create_random_hike
|
||||
|
||||
|
||||
def create_random_route(db: Session, name: str = None, hike: Hike = None) -> Hike:
|
||||
def create_random_route(db: Session, name: str = None, hike: Hike = None) -> Route:
|
||||
if not name:
|
||||
name = random_lower_string()
|
||||
|
||||
|
||||
@@ -6,15 +6,12 @@ from app.models.event import Event
|
||||
from app.models.team import Team, TeamCreate
|
||||
|
||||
from app.tests.utils.event import create_random_event
|
||||
from app.tests.utils.utils import random_lower_string
|
||||
from app.tests.utils.utils import random_lower_string, random_lower_short_string
|
||||
|
||||
|
||||
def random_short_name() -> str:
|
||||
return str(random.Random().randrange(1, 200))
|
||||
|
||||
def create_random_team(db: Session, event: Event | None = None) -> Team:
|
||||
name = random_lower_string()
|
||||
short_name = random_short_name()
|
||||
short_name = random_lower_short_string()
|
||||
|
||||
if not event:
|
||||
event = create_random_event(db)
|
||||
|
||||
@@ -10,6 +10,10 @@ def random_lower_string() -> str:
|
||||
return "".join(random.choices(string.ascii_lowercase, k=32))
|
||||
|
||||
|
||||
def random_lower_short_string() -> str:
|
||||
return str(random.Random().randrange(1, 8))
|
||||
|
||||
|
||||
def random_email() -> str:
|
||||
return f"{random_lower_string()}@{random_lower_string()}.com"
|
||||
|
||||
@@ -24,3 +28,4 @@ def get_superuser_token_headers(client: TestClient) -> dict[str, str]:
|
||||
a_token = tokens["access_token"]
|
||||
headers = {"Authorization": f"Bearer {a_token}"}
|
||||
return headers
|
||||
|
||||
|
||||
Reference in New Issue
Block a user