Files
MathApp/accounts/testing.py
Zareian 8835e01595 Remove guest login: every user registers with a phone number
POST /api/auth/session is gone (404). Existing is_guest rows keep refresh and
upgrade-by-phone until the demo/guest rows are purged; the code that serves
them is listed in docs/security-before-production.md §10. Tests that used the
endpoint as a fixture now use accounts/testing.guest_session.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 17:38:18 +03:30

31 lines
1.5 KiB
Python

"""
ابزارِ تست: یک حسابِ مهمانِ قدیمی با نشست می‌سازد.
ورودِ مهمان (`POST /api/auth/session`) حذف شد، اما ردیف‌های مهمانی که پیش‌تر ساخته شده‌اند
(حساب‌های دمو و کاربرانِ قبلی) هنوز در دیتابیس‌اند و رفتارِ ارتقایشان به حسابِ شماره‌دار
(ورود با شماره، حفظ پیشرفت، حذف حساب) باید تست شود. این تابع همان ردیفی را می‌سازد که آن اندپوینت می‌ساخت.
"""
import secrets
from django.conf import settings
from django.utils import timezone
from .authentication import issue_access_token
from .models import Session, User, hash_refresh_token
def guest_session(device_id):
"""مثلِ پاسخِ قدیمیِ اندپوینت: userId, token, refreshToken, isNewUser, isGuest."""
user, is_new = User.objects.get_or_create(device_id=device_id, is_guest=True, defaults={'name': ''})
user.last_activity_at = timezone.now()
user.save(update_fields=['last_activity_at'])
refresh = secrets.token_hex(32)
Session.objects.create(
user=user, refresh_token=hash_refresh_token(refresh), device_id=device_id,
expires_at=timezone.now() + timezone.timedelta(days=settings.REFRESH_TOKEN_TTL_DAYS),
)
return {
'userId': str(user.id), 'token': issue_access_token(user.id),
'refreshToken': refresh, 'isNewUser': is_new, 'isGuest': True,
}