| 1 |
""" |
| 2 |
Tests for Django-RQ admin integration. |
| 3 |
|
| 4 |
These tests verify that Django-RQ views are automatically accessible via Django admin |
| 5 |
URLs without requiring manual URL configuration in urls.py. |
| 6 |
""" |
| 7 |
|
| 8 |
from django.contrib.auth.models import User |
| 9 |
from django.test import TestCase, override_settings |
| 10 |
from django.test.client import Client |
| 11 |
from django.urls import reverse |
| 12 |
|
| 13 |
from django_rq import get_queue |
| 14 |
|
| 15 |
from .fixtures import say_hello |
| 16 |
from .redis_config import REDIS_CONFIG_1 |
| 17 |
from .utils import get_queue_index |
| 18 |
|
| 19 |
|
| 20 |
@override_settings( |
| 21 |
RQ={'AUTOCOMMIT': True}, |
| 22 |
RQ_QUEUES={ |
| 23 |
'default': { |
| 24 |
'HOST': REDIS_CONFIG_1.host, |
| 25 |
'PORT': REDIS_CONFIG_1.port, |
| 26 |
'DB': REDIS_CONFIG_1.db, |
| 27 |
'DEFAULT_TIMEOUT': 500, |
| 28 |
} |
| 29 |
}, |
| 30 |
) |
| 31 |
class AuthenticatedAdminURLTest(TestCase): |
| 32 |
"""Test Django-RQ views accessible via Django admin URLs with authenticated user""" |
| 33 |
|
| 34 |
def setUp(self): |
| 35 |
self.client = Client() |
| 36 |
# Create superuser for admin access |
| 37 |
self.admin_user = User.objects.create_superuser( |
| 38 |
username='admin', email='[email protected]', password='admin123' |
| 39 |
) |
| 40 |
self.client.login(username='admin', password='admin123') |
| 41 |
|
| 42 |
def test_dashboard_url(self): |
| 43 |
"""Verify dashboard accessible via admin URLs""" |
| 44 |
# Use admin changelist URL which proxies to stats view |
| 45 |
response = self.client.get(reverse('admin:django_rq_dashboard_changelist')) |
| 46 |
self.assertEqual(response.status_code, 200) |
| 47 |
# Should render the stats template |
| 48 |
self.assertTemplateUsed(response, 'django_rq/stats.html') |
| 49 |
|
| 50 |
def test_stats_json_url(self): |
| 51 |
"""Verify stats JSON endpoint accessible via admin URLs""" |
| 52 |
response = self.client.get(reverse('admin:django_rq_home_json')) |
| 53 |
self.assertEqual(response.status_code, 200) |
| 54 |
self.assertEqual(response['Content-Type'], 'application/json') |
| 55 |
|
| 56 |
def test_queue_jobs_url(self): |
| 57 |
"""Verify queue jobs view accessible via admin URLs""" |
| 58 |
queue_index = get_queue_index('default') |
| 59 |
response = self.client.get(reverse('admin:django_rq_jobs', args=[queue_index])) |
| 60 |
self.assertEqual(response.status_code, 200) |
| 61 |
self.assertTemplateUsed(response, 'django_rq/jobs.html') |
| 62 |
|
| 63 |
def test_workers_url(self): |
| 64 |
"""Verify workers view accessible via admin URLs""" |
| 65 |
queue_index = get_queue_index('default') |
| 66 |
response = self.client.get(reverse('admin:django_rq_workers', args=[queue_index])) |
| 67 |
self.assertEqual(response.status_code, 200) |
| 68 |
|
| 69 |
def test_job_detail_url(self): |
| 70 |
"""Verify job detail view accessible via admin URLs""" |
| 71 |
queue = get_queue('default') |
| 72 |
job = queue.enqueue(say_hello) |
| 73 |
queue_index = get_queue_index('default') |
| 74 |
|
| 75 |
response = self.client.get(reverse('admin:django_rq_job_detail', args=[queue_index, job.id])) |
| 76 |
self.assertEqual(response.status_code, 200) |
| 77 |
self.assertTemplateUsed(response, 'django_rq/job_detail.html') |
| 78 |
|
| 79 |
def test_failed_jobs_url(self): |
| 80 |
"""Verify failed jobs registry accessible via admin URLs""" |
| 81 |
queue_index = get_queue_index('default') |
| 82 |
response = self.client.get(reverse('admin:django_rq_failed_jobs', args=[queue_index])) |
| 83 |
self.assertEqual(response.status_code, 200) |
| 84 |
|
| 85 |
def test_finished_jobs_url(self): |
| 86 |
"""Verify finished jobs registry accessible via admin URLs""" |
| 87 |
queue_index = get_queue_index('default') |
| 88 |
response = self.client.get(reverse('admin:django_rq_finished_jobs', args=[queue_index])) |
| 89 |
self.assertEqual(response.status_code, 200) |
| 90 |
|
| 91 |
def test_scheduled_jobs_url(self): |
| 92 |
"""Verify scheduled jobs registry accessible via admin URLs""" |
| 93 |
queue_index = get_queue_index('default') |
| 94 |
response = self.client.get(reverse('admin:django_rq_scheduled_jobs', args=[queue_index])) |
| 95 |
self.assertEqual(response.status_code, 200) |
| 96 |
|
| 97 |
@override_settings(ROOT_URLCONF='tests.default_with_custom_mount_urls') |
| 98 |
def test_url_names_still_work(self): |
| 99 |
"""Verify reverse() with URL names still resolves correctly""" |
| 100 |
# Test that the admin integration names can be resolved |
| 101 |
url = reverse('admin:django_rq_home') |
| 102 |
self.assertTrue(url.startswith('/admin')) |
| 103 |
|
| 104 |
# Test that a custom mount can be resolved |
| 105 |
url = reverse('django_rq:home') |
| 106 |
self.assertEqual(url, '/django-rq') |
| 107 |
|
| 108 |
|
| 109 |
@override_settings( |
| 110 |
RQ={'AUTOCOMMIT': True}, |
| 111 |
RQ_QUEUES={ |
| 112 |
'default': { |
| 113 |
'HOST': REDIS_CONFIG_1.host, |
| 114 |
'PORT': REDIS_CONFIG_1.port, |
| 115 |
'DB': REDIS_CONFIG_1.db, |
| 116 |
'DEFAULT_TIMEOUT': 500, |
| 117 |
} |
| 118 |
}, |
| 119 |
) |
| 120 |
class UnauthenticatedAdminURLTest(TestCase): |
| 121 |
"""Test Django-RQ views accessible via Django admin URLs without authentication""" |
| 122 |
|
| 123 |
def setUp(self): |
| 124 |
self.client = Client() |
| 125 |
|
| 126 |
def test_requires_staff_authentication(self): |
| 127 |
"""Verify admin views require staff user authentication""" |
| 128 |
url = reverse('admin:django_rq_dashboard_changelist') |
| 129 |
|
| 130 |
# Test 1: Unauthenticated access should redirect to login |
| 131 |
response = self.client.get(url) |
| 132 |
self.assertEqual(response.status_code, 302) |
| 133 |
self.assertIn('/admin/login/', response.url) |
| 134 |
|
| 135 |
# Test 2: Non-staff user should be denied access |
| 136 |
regular_user = User.objects.create_user(username='regular', password='pass') |
| 137 |
regular_user.is_active = True |
| 138 |
regular_user.save() |
| 139 |
|
| 140 |
self.client.login(username='regular', password='pass') |
| 141 |
response = self.client.get(url) |
| 142 |
# Should redirect to login page or show permission error |
| 143 |
self.assertIn(response.status_code, [302, 403]) |
| 144 |
|
| 145 |
@override_settings(RQ_API_TOKEN='12345abcde') |
| 146 |
def test_api_token_authentication(self): |
| 147 |
"""Verify API token authentication works through admin URLs""" |
| 148 |
token = '12345abcde' |
| 149 |
# Test 1: Valid token should succeed |
| 150 |
response = self.client.get(reverse('admin:django_rq_home_json'), HTTP_AUTHORIZATION=f'Bearer {token}') |
| 151 |
self.assertEqual(response.status_code, 200) |
| 152 |
self.assertEqual(response['Content-Type'], 'application/json') |
| 153 |
data = response.json() |
| 154 |
self.assertIn('queues', data) |
| 155 |
|
| 156 |
# Test 2: Missing token should fail |
| 157 |
response = self.client.get(reverse('admin:django_rq_home_json')) |
| 158 |
self.assertEqual(response.status_code, 401) |
| 159 |
|
| 160 |
# Test 3: Invalid token should fail |
| 161 |
response = self.client.get(reverse('admin:django_rq_home_json'), HTTP_AUTHORIZATION='Bearer wrong_token') |
| 162 |
self.assertEqual(response.status_code, 401) |