Files
meet/src/backend/core/tests/utils.py
T
lebaudantoine 7fa9c3fa4f ♻️(backend) share the user access token test helper
generate_user_access_token was copy-pasted in seven test modules
(user access token authentication, and rooms create, list, retrieve,
update, participants management, rename/toggle and subtitle).

Extract it into core.tests.utils and reuse it everywhere. The shared
version keeps the optional application and claim overrides from the
authentication tests, and reads the token type claim from
USER_ACCESS_TOKEN_TYPE_CLAIM instead of hardcoding it.
2026-10-09 17:37:15 +02:00

42 lines
1.3 KiB
Python

"""Shared helpers for tests in the Meet core application"""
from datetime import datetime, timedelta, timezone
from django.conf import settings
import jwt
from core.factories import ApplicationFactory
from core.models import ApplicationScope
def generate_user_access_token(user, application=None, **overrides):
"""Generate a valid user access JWT signed with the token secret.
Claims can be overridden through keyword arguments; passing None for a
claim removes it from the payload.
"""
now = datetime.now(timezone.utc)
if application is None:
application = ApplicationFactory(scopes=[ApplicationScope.USERS_SESSION])
payload = {
"iss": settings.USER_ACCESS_TOKEN_ISSUER,
"aud": settings.USER_ACCESS_TOKEN_AUDIENCE,
"iat": now,
"exp": now + timedelta(seconds=settings.USER_ACCESS_TOKEN_TTL),
"user_id": str(user.id),
"token_type": settings.USER_ACCESS_TOKEN_TYPE_CLAIM,
"client_id": application.client_id,
"scope": "user:access",
}
payload.update(overrides)
payload = {key: value for key, value in payload.items() if value is not None}
return jwt.encode(
payload,
settings.USER_ACCESS_TOKEN_SECRET_KEY,
algorithm=settings.USER_ACCESS_TOKEN_ALG,
)