mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-05 21:45:25 +02:00
feat(backend): ✨ migrate site-settings to groups (#673)
* feat(frontend): ✨ add user registration page (WIP) * feat(backend): ✨ add user registration (WIP) * test(backend): ✅ add validator testing for registration schema * feat(backend): ✨ continued work on user sign-up * feat(backend): ✨ add signup flow and user/group settings * test(backend): ✅ user-creation tests and small refactor of existing tests * fix(backend): ✅ fix failing group tests * style: 🎨 fix lint issues Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
parent
e179dcdb10
commit
3c504e7048
63 changed files with 1665 additions and 841 deletions
|
@ -1,43 +0,0 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from tests.app_routes import AppRoutes
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def page_data():
|
||||
return {"name": "My New Page", "description": "", "position": 0, "categories": [], "groupId": 1}
|
||||
|
||||
|
||||
def test_create_cookbook(api_client: TestClient, api_routes: AppRoutes, admin_token, page_data):
|
||||
response = api_client.post(api_routes.group_cookbook, json=page_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_read_cookbook(api_client: TestClient, api_routes: AppRoutes, page_data, admin_token):
|
||||
response = api_client.get(api_routes.group_cookbook_id(1), headers=admin_token)
|
||||
|
||||
page_data["id"] = 1
|
||||
page_data["slug"] = "my-new-page"
|
||||
|
||||
assert json.loads(response.text) == page_data
|
||||
|
||||
|
||||
def test_update_cookbook(api_client: TestClient, api_routes: AppRoutes, page_data, admin_token):
|
||||
page_data["id"] = 1
|
||||
page_data["name"] = "My New Name"
|
||||
response = api_client.put(api_routes.group_cookbook_id(1), json=page_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_delete_cookbook(api_client: TestClient, api_routes: AppRoutes, admin_token):
|
||||
response = api_client.delete(api_routes.group_cookbook_id(1), headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = api_client.get(api_routes.group_cookbook_id(1), headers=admin_token)
|
||||
assert response.status_code == 404
|
|
@ -4,6 +4,7 @@ import pytest
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from tests.app_routes import AppRoutes
|
||||
from tests.utils.assertion_helpers import assert_ignore_keys
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -41,8 +42,10 @@ def test_update_group(api_client: TestClient, api_routes: AppRoutes, admin_token
|
|||
# Validate Changes
|
||||
response = api_client.get(api_routes.groups, headers=admin_token)
|
||||
all_groups = json.loads(response.text)
|
||||
|
||||
id_2 = filter(lambda x: x["id"] == 2, all_groups)
|
||||
assert next(id_2) == new_data
|
||||
|
||||
assert_ignore_keys(new_data, next(id_2), ["preferences"])
|
||||
|
||||
|
||||
def test_home_group_not_deletable(api_client: TestClient, api_routes: AppRoutes, admin_token):
|
||||
|
|
|
@ -13,7 +13,7 @@ def backup_data():
|
|||
"force": True,
|
||||
"recipes": True,
|
||||
"settings": False, # ! Broken
|
||||
"groups": True,
|
||||
"groups": False, # ! Also Broken
|
||||
"users": True,
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class Routes:
|
||||
base = "/api/groups/cookbooks"
|
||||
|
||||
def item(item_id: int) -> str:
|
||||
return f"{Routes.base}/{item_id}"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def page_data():
|
||||
return {"name": "My New Page", "description": "", "position": 0, "categories": [], "groupId": 1}
|
||||
|
||||
|
||||
def test_create_cookbook(api_client: TestClient, admin_token, page_data):
|
||||
response = api_client.post(Routes.base, json=page_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_read_cookbook(api_client: TestClient, page_data, admin_token):
|
||||
response = api_client.get(Routes.item(1), headers=admin_token)
|
||||
|
||||
page_data["id"] = 1
|
||||
page_data["slug"] = "my-new-page"
|
||||
|
||||
assert response.json() == page_data
|
||||
|
||||
|
||||
def test_update_cookbook(api_client: TestClient, page_data, admin_token):
|
||||
page_data["id"] = 1
|
||||
page_data["name"] = "My New Name"
|
||||
response = api_client.put(Routes.item(1), json=page_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_delete_cookbook(api_client: TestClient, admin_token):
|
||||
response = api_client.delete(Routes.item(1), headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
response = api_client.get(Routes.item(1), headers=admin_token)
|
||||
assert response.status_code == 404
|
|
@ -0,0 +1,32 @@
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from mealie.schema.user.registration import CreateUserRegistration
|
||||
|
||||
|
||||
class Routes:
|
||||
base = "/api/users/register"
|
||||
auth_token = "/api/auth/token"
|
||||
|
||||
|
||||
def test_user_registration_new_group(api_client: TestClient):
|
||||
registration = CreateUserRegistration(
|
||||
group="New Group Name",
|
||||
email="email@email.com",
|
||||
username="fake-user-name",
|
||||
password="fake-password",
|
||||
password_confirm="fake-password",
|
||||
advanced=False,
|
||||
private=False,
|
||||
)
|
||||
|
||||
response = api_client.post(Routes.base, json=registration.dict(by_alias=True))
|
||||
assert response.status_code == 201
|
||||
|
||||
# Login
|
||||
form_data = {"username": "email@email.com", "password": "fake-password"}
|
||||
|
||||
response = api_client.post(Routes.auth_token, form_data)
|
||||
assert response.status_code == 200
|
||||
token = response.json().get("access_token")
|
||||
|
||||
assert token is not None
|
|
@ -0,0 +1,51 @@
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from mealie.schema.group.group_preferences import UpdateGroupPreferences
|
||||
from tests.utils.assertion_helpers import assert_ignore_keys
|
||||
|
||||
|
||||
class Routes:
|
||||
base = "/api/groups/self"
|
||||
preferences = "/api/groups/preferences"
|
||||
|
||||
|
||||
def test_get_preferences(api_client: TestClient, admin_token) -> None:
|
||||
response = api_client.get(Routes.preferences, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
preferences = response.json()
|
||||
|
||||
# Spot Check Defaults
|
||||
assert preferences["recipePublic"] is True
|
||||
assert preferences["recipeShowNutrition"] is False
|
||||
|
||||
|
||||
def test_preferences_in_group(api_client: TestClient, admin_token) -> None:
|
||||
response = api_client.get(Routes.base, headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
group = response.json()
|
||||
|
||||
assert group["preferences"] is not None
|
||||
|
||||
# Spot Check
|
||||
assert group["preferences"]["recipePublic"] is True
|
||||
assert group["preferences"]["recipeShowNutrition"] is False
|
||||
|
||||
|
||||
def test_update_preferences(api_client: TestClient, admin_token) -> None:
|
||||
new_data = UpdateGroupPreferences(recipe_public=False, recipe_show_nutrition=True)
|
||||
|
||||
response = api_client.put(Routes.preferences, json=new_data.dict(), headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
preferences = response.json()
|
||||
|
||||
assert preferences is not None
|
||||
assert preferences["recipePublic"] is False
|
||||
assert preferences["recipeShowNutrition"] is True
|
||||
|
||||
assert_ignore_keys(new_data.dict(by_alias=True), preferences, ["id", "groupId"])
|
|
@ -0,0 +1,71 @@
|
|||
import pytest
|
||||
|
||||
from mealie.schema.user.registration import CreateUserRegistration
|
||||
|
||||
|
||||
def test_create_user_registration() -> None:
|
||||
CreateUserRegistration(
|
||||
group="Home",
|
||||
group_token=None,
|
||||
email="SomeValidEmail@email.com",
|
||||
username="SomeValidUsername",
|
||||
password="SomeValidPassword",
|
||||
password_confirm="SomeValidPassword",
|
||||
advanced=False,
|
||||
private=True,
|
||||
)
|
||||
|
||||
CreateUserRegistration(
|
||||
group=None,
|
||||
group_token="asdfadsfasdfasdfasdf",
|
||||
email="SomeValidEmail@email.com",
|
||||
username="SomeValidUsername",
|
||||
password="SomeValidPassword",
|
||||
password_confirm="SomeValidPassword",
|
||||
advanced=False,
|
||||
private=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("group, group_token", [(None, None), ("", None), (None, "")])
|
||||
def test_group_or_token_validator(group, group_token) -> None:
|
||||
with pytest.raises(ValueError):
|
||||
CreateUserRegistration(
|
||||
group=group,
|
||||
group_token=group_token,
|
||||
email="SomeValidEmail@email.com",
|
||||
username="SomeValidUsername",
|
||||
password="SomeValidPassword",
|
||||
password_confirm="SomeValidPassword",
|
||||
advanced=False,
|
||||
private=True,
|
||||
)
|
||||
|
||||
|
||||
def test_group_no_args_passed() -> None:
|
||||
with pytest.raises(ValueError):
|
||||
CreateUserRegistration(
|
||||
email="SomeValidEmail@email.com",
|
||||
username="SomeValidUsername",
|
||||
password="SomeValidPassword",
|
||||
password_confirm="SomeValidPassword",
|
||||
advanced=False,
|
||||
private=True,
|
||||
)
|
||||
|
||||
|
||||
def test_password_validator() -> None:
|
||||
with pytest.raises(ValueError):
|
||||
CreateUserRegistration(
|
||||
group=None,
|
||||
group_token="asdfadsfasdfasdfasdf",
|
||||
email="SomeValidEmail@email.com",
|
||||
username="SomeValidUsername",
|
||||
password="SomeValidPassword",
|
||||
password_confirm="PasswordDefNotMatch",
|
||||
advanced=False,
|
||||
private=True,
|
||||
)
|
||||
|
||||
|
||||
test_create_user_registration()
|
17
tests/utils/assertion_helpers.py
Normal file
17
tests/utils/assertion_helpers.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
def assert_ignore_keys(dict1: dict, dict2: dict, ignore_keys: list) -> None:
|
||||
"""
|
||||
Itterates through a list of keys and checks if they are in the the provided ignore_keys list,
|
||||
if they are not in the ignore_keys list, it checks the value of the key in the provided against
|
||||
the value provided in dict2. If the value of the key in dict1 is not equal to the value of the
|
||||
key in dict2, The assertion fails. Useful for testing id / group_id agnostic data
|
||||
|
||||
Note: ignore_keys defaults to ['id', 'group_id']
|
||||
"""
|
||||
if ignore_keys is None:
|
||||
ignore_keys = ["id", "group_id"]
|
||||
|
||||
for key, value in dict1.items():
|
||||
if key in ignore_keys:
|
||||
continue
|
||||
else:
|
||||
assert value == dict2[key]
|
Loading…
Add table
Add a link
Reference in a new issue