mirror of
https://github.com/suitenumerique/meet.git
synced 2026-10-12 07:57:50 +00:00
7fa9c3fa4f
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.
42 lines
1.3 KiB
Python
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,
|
|
)
|