1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 05:25:26 +02:00

chore: file generation cleanup (#1736)

This PR does too many things :( 

1. Major refactoring of the dev/scripts and dev/code-generation folders. 

Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 

2. Re-Implement using the generated api routes for testing

This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 

3. Minor Perf improvements for the All Recipes endpoint

  A. Removed redundant loops
  B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default 
       jsonable_encoder.

4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR.

See this issue https://github.com/phillipdupuis/pydantic-to-typescript/issues/28

Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
This commit is contained in:
Hayden 2022-10-18 14:49:41 -08:00 committed by GitHub
parent a8f0fb14a7
commit 9ecef4c25f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
107 changed files with 2520 additions and 1948 deletions

View file

@ -3,29 +3,29 @@ import json
from fastapi.testclient import TestClient
from pytest import fixture
from tests.utils.app_routes import AppRoutes
from tests.utils import api_routes
@fixture
def long_live_token(api_client: TestClient, api_routes: AppRoutes, admin_token):
def long_live_token(api_client: TestClient, admin_token):
response = api_client.post(api_routes.users_api_tokens, json={"name": "Test Fixture Token"}, headers=admin_token)
assert response.status_code == 201
return {"Authorization": f"Bearer {json.loads(response.text).get('token')}"}
def test_api_token_creation(api_client: TestClient, api_routes: AppRoutes, admin_token):
def test_api_token_creation(api_client: TestClient, admin_token):
response = api_client.post(api_routes.users_api_tokens, json={"name": "Test API Token"}, headers=admin_token)
assert response.status_code == 201
def test_use_token(api_client: TestClient, api_routes: AppRoutes, long_live_token):
def test_use_token(api_client: TestClient, long_live_token):
response = api_client.get(api_routes.users, headers=long_live_token)
assert response.status_code == 200
def test_delete_token(api_client: TestClient, api_routes: AppRoutes, admin_token):
def test_delete_token(api_client: TestClient, admin_token):
response = api_client.delete(api_routes.users_api_tokens_token_id(1), headers=admin_token)
assert response.status_code == 200

View file

@ -1,20 +1,13 @@
from fastapi.testclient import TestClient
from tests import data as test_data
from tests.utils import api_routes
from tests.utils.fixture_schemas import TestUser
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)))
response = api_client.get(api_routes.media_users_user_id_file_name(str(unique_user.user_id), "profile.webp"))
assert response.status_code == 200
# Ensure that the returned value is a valid image
@ -25,9 +18,11 @@ 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)
response = api_client.post(
api_routes.users_id_image(str(unique_user.user_id)), files=image, headers=unique_user.token
)
assert response.status_code == 200
# Request the image again
response = api_client.get(Routes.get_user_image(str(unique_user.user_id)))
response = api_client.get(api_routes.media_users_user_id_file_name(str(unique_user.user_id), "profile.webp"))
assert response.status_code == 200

View file

@ -5,11 +5,11 @@ from fastapi.testclient import TestClient
from mealie.core.config import get_app_settings
from mealie.repos.repository_factory import AllRepositories
from mealie.services.user_services.user_service import UserService
from tests.utils.app_routes import AppRoutes
from tests.utils import api_routes
from tests.utils.fixture_schemas import TestUser
def test_failed_login(api_client: TestClient, api_routes: AppRoutes):
def test_failed_login(api_client: TestClient):
settings = get_app_settings()
form_data = {"username": settings.DEFAULT_EMAIL, "password": "WRONG_PASSWORD"}
@ -18,7 +18,7 @@ def test_failed_login(api_client: TestClient, api_routes: AppRoutes):
assert response.status_code == 401
def test_superuser_login(api_client: TestClient, api_routes: AppRoutes, admin_token):
def test_superuser_login(api_client: TestClient, admin_token):
settings = get_app_settings()
form_data = {"username": settings.DEFAULT_EMAIL, "password": settings.DEFAULT_PASSWORD}
@ -33,7 +33,7 @@ def test_superuser_login(api_client: TestClient, api_routes: AppRoutes, admin_to
return {"Authorization": f"Bearer {new_token}"}
def test_user_token_refresh(api_client: TestClient, api_routes: AppRoutes, admin_user: TestUser):
def test_user_token_refresh(api_client: TestClient, admin_user: TestUser):
response = api_client.post(api_routes.auth_refresh, headers=admin_user.token)
response = api_client.get(api_routes.users_self, headers=admin_user.token)
assert response.status_code == 200
@ -44,17 +44,16 @@ def test_user_lockout_after_bad_attemps(api_client: TestClient, unique_user: Tes
if the user has more than 5 bad login attempts the user will be locked out for 4 hours
This only applies if there is a user in the database with the same username
"""
routes = AppRoutes()
settings = get_app_settings()
for _ in range(settings.SECURITY_MAX_LOGIN_ATTEMPTS):
form_data = {"username": unique_user.email, "password": "bad_password"}
response = api_client.post(routes.auth_token, form_data)
response = api_client.post(api_routes.auth_token, form_data)
assert response.status_code == 401
valid_data = {"username": unique_user.email, "password": unique_user.password}
response = api_client.post(routes.auth_token, valid_data)
response = api_client.post(api_routes.auth_token, valid_data)
assert response.status_code == 423
# Cleanup

View file

@ -5,17 +5,11 @@ from fastapi.testclient import TestClient
from mealie.db.db_setup import session_context
from mealie.services.user_services.password_reset_service import PasswordResetService
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/users/reset-password"
login = "/api/auth/token"
self = "/api/users/self"
@pytest.mark.parametrize("casing", ["lower", "upper", "mixed"])
def test_password_reset(api_client: TestClient, unique_user: TestUser, casing: str):
cased_email = ""
@ -46,19 +40,19 @@ def test_password_reset(api_client: TestClient, unique_user: TestUser, casing: s
}
# Test successful password reset
response = api_client.post(Routes.base, json=payload)
response = api_client.post(api_routes.users_reset_password, json=payload)
assert response.status_code == 200
# Test Login
form_data = {"username": unique_user.email, "password": new_password}
response = api_client.post(Routes.login, form_data)
response = api_client.post(api_routes.auth_token, form_data)
assert response.status_code == 200
# Test Token
new_token = json.loads(response.text).get("access_token")
response = api_client.get(Routes.self, headers={"Authorization": f"Bearer {new_token}"})
response = api_client.get(api_routes.users_self, headers={"Authorization": f"Bearer {new_token}"})
assert response.status_code == 200
# Test successful password reset
response = api_client.post(Routes.base, json=payload)
response = api_client.post(api_routes.users_reset_password, json=payload)
assert response.status_code == 400