mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-24 15:49:42 +02:00
feat(backend): ✨ refactor/fix group management for admins (#838)
* fix(frontend): 🐛 update dialog implementation to simplify state management * test(backend): ✅ refactor test fixtures + admin group tests * chore(backend): 🔨 add launcher.json for python debugging (tests) * fix typing * feat(backend): ✨ refactor/fix group management for admins * feat(frontend): ✨ add/fix admin group management * add LDAP checker Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
parent
0db8a58963
commit
791aa8c610
52 changed files with 881 additions and 331 deletions
|
@ -1,19 +1,13 @@
|
|||
from tests.pre_test import settings # isort:skip
|
||||
|
||||
import json
|
||||
|
||||
import requests
|
||||
from fastapi.testclient import TestClient
|
||||
from pytest import fixture
|
||||
|
||||
from mealie.app import app
|
||||
from mealie.db.db_setup import SessionLocal, generate_session
|
||||
from mealie.db.init_db import main
|
||||
from tests.app_routes import AppRoutes
|
||||
from tests.fixtures import * # noqa: F403 F401
|
||||
from tests.test_config import TEST_DATA
|
||||
from tests.utils.factories import random_email, random_string, user_registration_factory
|
||||
from tests.utils.fixture_schemas import TestUser
|
||||
from tests.utils.recipe_data import get_raw_no_image, get_raw_recipe, get_recipe_test_cases
|
||||
|
||||
main()
|
||||
|
||||
|
@ -39,11 +33,6 @@ def api_client():
|
|||
pass
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def api_routes():
|
||||
return AppRoutes()
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def test_image_jpg():
|
||||
return TEST_DATA.joinpath("images", "test_image.jpg")
|
||||
|
@ -52,132 +41,3 @@ def test_image_jpg():
|
|||
@fixture(scope="session")
|
||||
def test_image_png():
|
||||
return TEST_DATA.joinpath("images", "test_image.png")
|
||||
|
||||
|
||||
def login(form_data, api_client: requests, api_routes: AppRoutes):
|
||||
response = api_client.post(api_routes.auth_token, form_data)
|
||||
assert response.status_code == 200
|
||||
token = json.loads(response.text).get("access_token")
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def admin_token(api_client: requests, api_routes: AppRoutes):
|
||||
form_data = {"username": "changeme@email.com", "password": settings.DEFAULT_PASSWORD}
|
||||
return login(form_data, api_client, api_routes)
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def g2_user(admin_token, api_client: requests, api_routes: AppRoutes):
|
||||
# Create the user
|
||||
create_data = {
|
||||
"fullName": random_string(),
|
||||
"username": random_string(),
|
||||
"email": random_email(),
|
||||
"password": "useruser",
|
||||
"group": "New Group",
|
||||
"admin": False,
|
||||
"tokens": [],
|
||||
}
|
||||
|
||||
response = api_client.post(api_routes.groups, json={"name": "New Group"}, headers=admin_token)
|
||||
response = api_client.post(api_routes.users, json=create_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 201
|
||||
|
||||
# Log in as this user
|
||||
form_data = {"username": create_data["email"], "password": "useruser"}
|
||||
|
||||
token = login(form_data, api_client, api_routes)
|
||||
|
||||
self_response = api_client.get(api_routes.users_self, headers=token)
|
||||
|
||||
assert self_response.status_code == 200
|
||||
|
||||
user_id = json.loads(self_response.text).get("id")
|
||||
group_id = json.loads(self_response.text).get("groupId")
|
||||
|
||||
return TestUser(user_id=user_id, group_id=group_id, token=token, email=create_data["email"])
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def user_token(admin_token, api_client: requests, api_routes: AppRoutes):
|
||||
# Create the user
|
||||
create_data = {
|
||||
"fullName": random_string(),
|
||||
"username": random_string(),
|
||||
"email": random_email(),
|
||||
"password": "useruser",
|
||||
"group": "Home",
|
||||
"admin": False,
|
||||
"tokens": [],
|
||||
}
|
||||
|
||||
response = api_client.post(api_routes.users, json=create_data, headers=admin_token)
|
||||
|
||||
assert response.status_code == 201
|
||||
|
||||
# Log in as this user
|
||||
form_data = {"username": create_data["email"], "password": "useruser"}
|
||||
return login(form_data, api_client, api_routes)
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def raw_recipe():
|
||||
return get_raw_recipe()
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def raw_recipe_no_image():
|
||||
return get_raw_no_image()
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def recipe_store():
|
||||
return get_recipe_test_cases()
|
||||
|
||||
|
||||
@fixture(scope="module")
|
||||
def unique_user(api_client: TestClient, api_routes: AppRoutes):
|
||||
registration = user_registration_factory()
|
||||
|
||||
response = api_client.post("/api/users/register", json=registration.dict(by_alias=True))
|
||||
assert response.status_code == 201
|
||||
|
||||
form_data = {"username": registration.username, "password": registration.password}
|
||||
|
||||
token = login(form_data, api_client, api_routes)
|
||||
|
||||
user_data = api_client.get(api_routes.users_self, headers=token).json()
|
||||
assert token is not None
|
||||
|
||||
try:
|
||||
yield TestUser(
|
||||
group_id=user_data.get("groupId"), user_id=user_data.get("id"), email=user_data.get("email"), token=token
|
||||
)
|
||||
finally:
|
||||
# TODO: Delete User after test
|
||||
pass
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def admin_user(api_client: TestClient, api_routes: AppRoutes):
|
||||
|
||||
form_data = {"username": "changeme@email.com", "password": settings.DEFAULT_PASSWORD}
|
||||
|
||||
token = login(form_data, api_client, api_routes)
|
||||
|
||||
user_data = api_client.get(api_routes.users_self, headers=token).json()
|
||||
assert token is not None
|
||||
|
||||
assert user_data.get("admin") is True
|
||||
assert user_data.get("groupId") is not None
|
||||
assert user_data.get("id") is not None
|
||||
|
||||
try:
|
||||
yield TestUser(
|
||||
group_id=user_data.get("groupId"), user_id=user_data.get("id"), email=user_data.get("email"), token=token
|
||||
)
|
||||
finally:
|
||||
# TODO: Delete User after test
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue