55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import os
|
|
import django
|
|
from django.conf import settings
|
|
import sys
|
|
|
|
# Setup Django environment
|
|
sys.path.append(os.getcwd())
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
|
|
django.setup()
|
|
|
|
from django.test import RequestFactory
|
|
from core.views import index
|
|
|
|
def test_root_view():
|
|
factory = RequestFactory()
|
|
request = factory.get('/')
|
|
|
|
# Simulate logged in user (since index is login_required)
|
|
from django.contrib.auth.models import AnonymousUser, User
|
|
|
|
# Create a dummy user for testing
|
|
if not User.objects.filter(username='testadmin').exists():
|
|
user = User.objects.create_superuser('testadmin', 'admin@example.com', 'pass')
|
|
else:
|
|
user = User.objects.get(username='testadmin')
|
|
|
|
request.user = user # Authenticated
|
|
|
|
try:
|
|
response = index(request)
|
|
print(f"Authenticated Root View Status: {response.status_code}")
|
|
except Exception as e:
|
|
print(f"Authenticated Root View Error: {e}")
|
|
|
|
# Test unauthenticated (should redirect)
|
|
request_anon = factory.get('/')
|
|
request_anon.user = AnonymousUser()
|
|
from django.contrib.auth.decorators import login_required
|
|
# We can't easily run the decorator logic with RequestFactory directly calling the view function
|
|
# unless we use the view wrapped in login_required manually or via client.
|
|
|
|
from django.test import Client
|
|
client = Client()
|
|
response = client.get('/')
|
|
print(f"Client Root Get Status: {response.status_code}")
|
|
if response.status_code == 302:
|
|
print(f"Redirects to: {response.url}")
|
|
|
|
# Check login page
|
|
response_login = client.get('/accounts/login/')
|
|
print(f"Client Login Get Status: {response_login.status_code}")
|
|
|
|
if __name__ == "__main__":
|
|
test_root_view()
|