flask_more_smorest.testing
Testing helpers for Flask-More-Smorest.
Provides context managers and utility functions to simplify testing authenticated endpoints and permission-based views.
Functions
|
Context manager to set JWT authentication for an admin user in test requests. |
|
Context manager to set JWT authentication for a user in test requests. |
Clear the custom user registration. |
- flask_more_smorest.testing.as_admin(client, user_id, additional_claims=None, roles=None)[source]
Context manager to set JWT authentication for an admin user in test requests.
This is a convenience wrapper around
as_user()that automatically adds admin role claims to the JWT token.- Parameters:
client (
FlaskClient) – Flask test clientuser_id (
str|UUID) – Admin user ID to authenticate as (string or UUID)additional_claims (
dict[str,Any] |None) – Optional additional JWT claims to include in the tokenroles (
list[str] |None) – List of roles to assign (default: [“admin”]). Use [“superadmin”] for superadmin privileges.
- Yields:
None
Example:
from flask_more_smorest import User from flask_more_smorest.testing import as_admin from flask_more_smorest.perms.models.defaults import UserRole, BaseRoleEnum def test_admin_endpoint(client, db_session): # Create admin user admin = User(email="admin@example.com", password="password123") admin.save() admin.roles.append(UserRole(user=admin, role=BaseRoleEnum.ADMIN)) # Test admin-only endpoint with as_admin(client, admin.id): response = client.get("/api/users/") assert response.status_code == 200
Example with superadmin:
admin.roles.append(UserRole(user=admin, role=BaseRoleEnum.SUPERADMIN)) with as_admin(client, admin.id, roles=["superadmin"]): response = client.delete("/api/users/123/") assert response.status_code == 204
Note
This context manager sets client.environ_base with the authorization header. The Flask test client will include these headers in all requests made within the context.
- flask_more_smorest.testing.as_user(client, user_id, additional_claims=None)[source]
Context manager to set JWT authentication for a user in test requests.
This simplifies testing authenticated endpoints by automatically setting the JWT token in request headers.
- Parameters:
- Yields:
None
Example:
from flask_more_smorest import User from flask_more_smorest.testing import as_user def test_get_my_profile(client, db_session): # Create test user user = User(email="test@example.com", password="password123") user.save() # Test authenticated endpoint with as_user(client, user.id): response = client.get("/api/users/me/") assert response.status_code == 200 assert response.json["email"] == "test@example.com"
Example with additional claims:
with as_user(client, user.id, additional_claims={"custom_claim": "value"}): response = client.get("/api/users/me/") # Token will include custom_claim
Note
This context manager sets client.environ_base with the authorization header. The Flask test client will include these headers in all requests made within the context.
- flask_more_smorest.testing.clear_registration()[source]
Clear the custom user registration.
This is a proxy to
flask_more_smorest.perms.clear_registration()for convenience in test files.Useful for testing to reset to default JWT behavior after registering custom user classes or getters.
Example:
from flask_more_smorest.testing import clear_registration from flask_more_smorest.perms import init_fms def test_with_custom_user(): init_fms(user=MyUser) # ... test ... clear_registration() # Reset for next test
- Return type: