2021-09-09 08:51:29 -08:00
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
from tests.utils.factories import random_string
|
|
|
|
from tests.utils.fixture_schemas import TestUser
|
|
|
|
|
|
|
|
|
|
|
|
class Routes:
|
|
|
|
base = "/api/recipes"
|
|
|
|
user = "/api/users/self"
|
|
|
|
|
|
|
|
|
2021-12-04 14:18:46 -09:00
|
|
|
def test_ownership_on_new_with_admin(api_client: TestClient, admin_user: TestUser):
|
2021-09-09 08:51:29 -08:00
|
|
|
recipe_name = random_string()
|
2021-12-04 14:18:46 -09:00
|
|
|
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=admin_user.token)
|
2021-09-09 08:51:29 -08:00
|
|
|
assert response.status_code == 201
|
|
|
|
|
2021-12-04 14:18:46 -09:00
|
|
|
recipe = api_client.get(Routes.base + f"/{recipe_name}", headers=admin_user.token).json()
|
2021-09-09 08:51:29 -08:00
|
|
|
|
2021-12-04 14:18:46 -09:00
|
|
|
assert recipe["userId"] == admin_user.user_id
|
|
|
|
assert recipe["groupId"] == admin_user.group_id
|
2021-09-09 08:51:29 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_ownership_on_new_with_user(api_client: TestClient, g2_user: TestUser):
|
|
|
|
recipe_name = random_string()
|
|
|
|
|
|
|
|
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=g2_user.token)
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
response = api_client.get(Routes.base + f"/{recipe_name}", headers=g2_user.token)
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
recipe = response.json()
|
|
|
|
|
|
|
|
assert recipe["userId"] == g2_user.user_id
|
|
|
|
assert recipe["groupId"] == g2_user.group_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_only_includes_group_recipes(api_client: TestClient, unique_user: TestUser):
|
|
|
|
for _ in range(5):
|
|
|
|
recipe_name = random_string()
|
|
|
|
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=unique_user.token)
|
|
|
|
|
|
|
|
response = api_client.get(Routes.base, headers=unique_user.token)
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
recipes = response.json()
|
|
|
|
|
|
|
|
assert len(recipes) == 5
|
|
|
|
|
|
|
|
for recipe in recipes:
|
|
|
|
assert recipe["groupId"] == unique_user.group_id
|
|
|
|
assert recipe["userId"] == unique_user.user_id
|
|
|
|
|
|
|
|
|
|
|
|
def test_unique_slug_by_group(api_client: TestClient, unique_user: TestUser, g2_user: TestUser) -> None:
|
|
|
|
create_data = {"name": random_string()}
|
|
|
|
|
|
|
|
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
response = api_client.post(Routes.base, json=create_data, headers=g2_user.token)
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
# Try to create a recipe again with the same name
|
|
|
|
response = api_client.post(Routes.base, json=create_data, headers=g2_user.token)
|
|
|
|
assert response.status_code == 400
|