mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-05 13:35:23 +02:00
Feature/user photo storage (#877)
* add default assets for user profile * add recipe avatar * change user_id to UUID * add profile image upload * setup image cache keys * cleanup tests and add image tests * purge user data on delete * new user repository tests * add user_id validator for int -> UUID conversion * delete depreciated route * force set content type * refactor tests to use temp directory * validate parent exists before createing * set user_id to correct type * update instruction id * reset primary key on migration
This commit is contained in:
parent
a2f8f27193
commit
ea7c4771ee
64 changed files with 433 additions and 181 deletions
|
@ -6,8 +6,8 @@ from tests.utils.app_routes import AppRoutes
|
|||
from tests.utils.fixture_schemas import TestUser
|
||||
|
||||
|
||||
def test_init_superuser(api_client: TestClient, api_routes: AppRoutes, admin_token, admin_user: TestUser):
|
||||
response = api_client.get(api_routes.users_id(1), headers=admin_token)
|
||||
def test_init_superuser(api_client: TestClient, api_routes: AppRoutes, admin_user: TestUser):
|
||||
response = api_client.get(api_routes.users_id(admin_user.user_id), headers=admin_user.token)
|
||||
assert response.status_code == 200
|
||||
|
||||
admin_data = response.json()
|
||||
|
@ -56,36 +56,56 @@ def test_create_user_as_non_admin(api_client: TestClient, api_routes: AppRoutes,
|
|||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_update_user(api_client: TestClient, api_routes: AppRoutes, admin_token):
|
||||
update_data = {"id": 1, "fullName": "Updated Name", "email": "changeme@email.com", "group": "Home", "admin": True}
|
||||
response = api_client.put(api_routes.users_id(1), headers=admin_token, json=update_data)
|
||||
def test_update_user(api_client: TestClient, api_routes: AppRoutes, admin_user: TestUser):
|
||||
update_data = {
|
||||
"id": admin_user.user_id,
|
||||
"fullName": "Updated Name",
|
||||
"email": "changeme@email.com",
|
||||
"group": "Home",
|
||||
"admin": True,
|
||||
}
|
||||
response = api_client.put(api_routes.users_id(admin_user.user_id), headers=admin_user.token, json=update_data)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text).get("access_token")
|
||||
|
||||
|
||||
def test_update_other_user_as_not_admin(api_client: TestClient, api_routes: AppRoutes, unique_user: TestUser):
|
||||
update_data = {"id": 1, "fullName": "Updated Name", "email": "changeme@email.com", "group": "Home", "admin": True}
|
||||
response = api_client.put(api_routes.users_id(1), headers=unique_user.token, json=update_data)
|
||||
def test_update_other_user_as_not_admin(
|
||||
api_client: TestClient, api_routes: AppRoutes, unique_user: TestUser, g2_user: TestUser
|
||||
):
|
||||
update_data = {
|
||||
"id": unique_user.user_id,
|
||||
"fullName": "Updated Name",
|
||||
"email": "changeme@email.com",
|
||||
"group": "Home",
|
||||
"admin": True,
|
||||
}
|
||||
response = api_client.put(api_routes.users_id(g2_user.user_id), headers=unique_user.token, json=update_data)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_self_demote_admin(api_client: TestClient, api_routes: AppRoutes, admin_token):
|
||||
def test_self_demote_admin(api_client: TestClient, api_routes: AppRoutes, admin_user: TestUser):
|
||||
update_data = {"fullName": "Updated Name", "email": "changeme@email.com", "group": "Home", "admin": False}
|
||||
response = api_client.put(api_routes.users_id(1), headers=admin_token, json=update_data)
|
||||
response = api_client.put(api_routes.users_id(admin_user.user_id), headers=admin_user.token, json=update_data)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_self_promote_admin(api_client: TestClient, api_routes: AppRoutes, user_token):
|
||||
update_data = {"id": 3, "fullName": "Updated Name", "email": "user@email.com", "group": "Home", "admin": True}
|
||||
response = api_client.put(api_routes.users_id(2), headers=user_token, json=update_data)
|
||||
def test_self_promote_admin(api_client: TestClient, api_routes: AppRoutes, unique_user: TestUser):
|
||||
update_data = {
|
||||
"id": unique_user.user_id,
|
||||
"fullName": "Updated Name",
|
||||
"email": "user@email.com",
|
||||
"group": "Home",
|
||||
"admin": True,
|
||||
}
|
||||
response = api_client.put(api_routes.users_id(unique_user.user_id), headers=unique_user.token, json=update_data)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_delete_user(api_client: TestClient, api_routes: AppRoutes, admin_token):
|
||||
response = api_client.delete(api_routes.users_id(2), headers=admin_token)
|
||||
def test_delete_user(api_client: TestClient, api_routes: AppRoutes, admin_token, unique_user: TestUser):
|
||||
response = api_client.delete(api_routes.users_id(unique_user.user_id), headers=admin_token)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from tests.utils.app_routes import AppRoutes
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def backup_data():
|
||||
return {
|
||||
"name": "test_backup_2021-Apr-27.zip",
|
||||
"force": True,
|
||||
"recipes": True,
|
||||
"settings": False, # ! Broken
|
||||
"groups": False, # ! Also Broken
|
||||
"users": False,
|
||||
}
|
||||
|
||||
|
||||
def test_import(api_client: TestClient, api_routes: AppRoutes, backup_data, admin_token):
|
||||
import_route = api_routes.backups_file_name_import("test_backup_2021-Apr-27.zip")
|
||||
response = api_client.post(import_route, json=backup_data, headers=admin_token)
|
||||
assert response.status_code == 200
|
||||
for _, value in json.loads(response.content).items():
|
||||
for v in value:
|
||||
assert v["status"] is True
|
|
@ -1,30 +1,33 @@
|
|||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from mealie.core.config import get_app_dirs
|
||||
|
||||
app_dirs = get_app_dirs()
|
||||
from tests.utils.app_routes import AppRoutes
|
||||
from tests import data as test_data
|
||||
from tests.utils.fixture_schemas import TestUser
|
||||
|
||||
|
||||
def test_update_user_image(
|
||||
api_client: TestClient, api_routes: AppRoutes, test_image_jpg: Path, test_image_png: Path, admin_token
|
||||
):
|
||||
response = api_client.post(
|
||||
api_routes.users_id_image(2), files={"profile_image": test_image_jpg.open("rb")}, headers=admin_token
|
||||
)
|
||||
class Routes:
|
||||
def get_user_image(user_id: str, file_name: str = "profile.webp") -> str:
|
||||
return f"/api/media/users/{user_id}/{file_name}"
|
||||
|
||||
def user_image(user_id: str) -> str:
|
||||
return f"/api/users/{user_id}/image"
|
||||
|
||||
|
||||
def test_user_get_image(api_client: TestClient, unique_user: TestUser):
|
||||
# Get the user's image
|
||||
response = api_client.get(Routes.get_user_image(str(unique_user.user_id)))
|
||||
assert response.status_code == 200
|
||||
|
||||
response = api_client.post(
|
||||
api_routes.users_id_image(2), files={"profile_image": test_image_png.open("rb")}, headers=admin_token
|
||||
)
|
||||
# Ensure that the returned value is a valid image
|
||||
assert response.headers["Content-Type"] == "image/webp"
|
||||
|
||||
|
||||
def test_user_update_image(api_client: TestClient, unique_user: TestUser):
|
||||
image = {"profile": test_data.images_test_image_1.read_bytes()}
|
||||
|
||||
# Update the user's image
|
||||
response = api_client.post(Routes.user_image(str(unique_user.user_id)), files=image, headers=unique_user.token)
|
||||
assert response.status_code == 200
|
||||
|
||||
directory = app_dirs.USER_DIR.joinpath("2")
|
||||
assert directory.joinpath("profile_image.png").is_file()
|
||||
|
||||
# Old profile images are removed
|
||||
assert 1 == len([file for file in directory.glob("profile_image.*") if file.is_file()])
|
||||
# Request the image again
|
||||
response = api_client.get(Routes.get_user_image(str(unique_user.user_id)))
|
||||
assert response.status_code == 200
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue