from django.core.management.base import BaseCommand from rest_framework.test import APIClient from apps.account.models import User # Your user model import json class Command(BaseCommand): def handle(self, *args, **options): client = APIClient() # Step 1: Create guest token print("\nšŸ“ Step 1: Creating guest token...") response = client.post('/api/account/web/guest/', data=json.dumps({"timezone": "UTC", "user_agent": "test"}), content_type='application/json' ) print(f"Status: {response.status_code}") if response.status_code == 200: print(f"Response: {response.json()}") token = response.json()['token'] else: print(f"Error Response: {response.content.decode('utf-8')}") print("āŒ Failed to create token!") return print(f"āœ… Token created: {token[:20]}...") # Step 2: Test authentication with token print("\nšŸ” Step 2: Testing token authentication...") client.credentials(HTTP_AUTHORIZATION=f'Token {token}') response = client.get('/api/library/books/') print(f"Status: {response.status_code}") if response.status_code == 200: print(f"Response: {response.json()}") print("āœ… Token authentication works!") else: print(f"Error Response: {response.content.decode('utf-8')}") print("āŒ Token authentication failed!")