You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.5 KiB
37 lines
1.5 KiB
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!")
|