Verified Commit e51fe10d authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add tests for DoNextModelAdmin

parent 25d2ec3d
......@@ -15,22 +15,21 @@ from utils.translation import TranslatedModelAdmin
from . import forms, models
def _do_next(request, response):
class DoNextModelAdmin(TranslatedModelAdmin):
def _do_next(self, request, response):
if 'next' in request.GET and is_safe_url(request.GET['next']):
return HttpResponseRedirect(request.GET['next'])
else:
return response
class DoNextModelAdmin(TranslatedModelAdmin):
def response_add(self, request, obj):
res = super().response_add(request, obj)
return _do_next(request, res)
return self._do_next(request, res)
def response_change(self, request, obj):
res = super().response_change(request, obj)
return _do_next(request, res)
return self._do_next(request, res)
class RegistrationInformationFieldInline(admin.StackedInline):
......
from unittest import mock
from django.contrib.admin import AdminSite
from django.http import HttpResponseRedirect
from django.test import TestCase, RequestFactory
from events.admin import DoNextModelAdmin
from events.models import Event
class DoNextModelAdminTest(TestCase):
def setUp(self):
self.site = AdminSite()
self.admin = DoNextModelAdmin(Event, admin_site=self.site)
self.rf = RequestFactory()
@mock.patch('utils.translation.TranslatedModelAdmin.response_add')
def test_response_add(self, super_mock):
super_mock.return_value = None
request = self.rf.get('/admin/events/event/1')
response = self.admin.response_add(request, None)
self.assertIsNone(response, HttpResponseRedirect)
request = self.rf.get('/admin/events/event/1', data={
'next': 'http://example.org',
})
response = self.admin.response_add(request, None)
self.assertIsNone(response, HttpResponseRedirect)
request = self.rf.get('/admin/events/event/1', data={
'next': '/test',
})
response = self.admin.response_add(request, None)
self.assertIsInstance(response, HttpResponseRedirect)
self.assertEqual('/test', response.url)
@mock.patch('utils.translation.TranslatedModelAdmin.response_change')
def test_response_change(self, super_mock):
super_mock.return_value = None
request = self.rf.get('/admin/events/event/1')
response = self.admin.response_change(request, None)
self.assertIsNone(response, HttpResponseRedirect)
request = self.rf.get('/admin/events/event/1', data={
'next': 'http://example.org',
})
response = self.admin.response_change(request, None)
self.assertIsNone(response, HttpResponseRedirect)
request = self.rf.get('/admin/events/event/1', data={
'next': '/test',
})
response = self.admin.response_change(request, None)
self.assertIsInstance(response, HttpResponseRedirect)
self.assertEqual('/test', response.url)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment