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

Add basic tests for EventAdmin

parent 2fff7a72
......@@ -15,21 +15,27 @@ from utils.translation import TranslatedModelAdmin
from . import forms, models
class DoNextModelAdmin(TranslatedModelAdmin):
def _do_next(self, request, response):
def _do_next(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):
"""
This class adds processing of a `next` parameter in the urls
of the add and change admin forms. If it is set and safe this
override will redirect the user to the provided url.
"""
def response_add(self, request, obj):
res = super().response_add(request, obj)
return self._do_next(request, res)
return _do_next(request, res)
def response_change(self, request, obj):
res = super().response_change(request, obj)
return self._do_next(request, res)
return _do_next(request, res)
class RegistrationInformationFieldInline(admin.StackedInline):
......
......@@ -3,11 +3,16 @@ from unittest import mock
from django.contrib.admin import AdminSite
from django.http import HttpResponseRedirect
from django.test import TestCase, RequestFactory
from django.test import TestCase, RequestFactory, override_settings
from django.utils import timezone
from freezegun import freeze_time
from activemembers.models import Committee, CommitteeMembership
from events.admin import DoNextModelAdmin, RegistrationInformationFieldInline
from events.admin import (
DoNextModelAdmin,
RegistrationInformationFieldInline,
EventAdmin
)
from events.models import Event, RegistrationInformationField
from members.models import Member
......@@ -25,20 +30,22 @@ class DoNextModelAdminTest(TestCase):
request = self.rf.get('/admin/events/event/1')
response = self.admin.response_add(request, None)
self.assertIsNone(response, HttpResponseRedirect)
self.assertIsNone(response, "Should return the original response")
request = self.rf.get('/admin/events/event/1', data={
'next': 'http://example.org',
})
response = self.admin.response_add(request, None)
self.assertIsNone(response, HttpResponseRedirect)
self.assertNotIsInstance(response, HttpResponseRedirect,
"Should not redirect")
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)
self.assertEqual('/test', response.url,
"Should return the url in the next parameter.")
@mock.patch('utils.translation.TranslatedModelAdmin.response_change')
def test_response_change(self, super_mock):
......@@ -46,22 +53,25 @@ class DoNextModelAdminTest(TestCase):
request = self.rf.get('/admin/events/event/1')
response = self.admin.response_change(request, None)
self.assertIsNone(response, HttpResponseRedirect)
self.assertIsNone(response, "Should return the original response")
request = self.rf.get('/admin/events/event/1', data={
'next': 'http://example.org',
})
response = self.admin.response_change(request, None)
self.assertIsNone(response, HttpResponseRedirect)
self.assertNotIsInstance(response, HttpResponseRedirect,
"Should not redirect")
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)
self.assertEqual('/test', response.url,
"Should return the url in the next parameter.")
@freeze_time('2017-01-01')
class RegistrationInformationFieldInlineTest(TestCase):
fixtures = ['members.json', 'committees.json']
......@@ -82,7 +92,8 @@ class RegistrationInformationFieldInlineTest(TestCase):
location_nl='test locatie',
map_location='test map location',
price=0.00,
fine=0.00)
fine=0.00
)
cls.member = Member.objects.filter(last_name="Wiggers").first()
cls.member.is_superuser = True
cls.member.save()
......@@ -135,7 +146,81 @@ class RegistrationInformationFieldInlineTest(TestCase):
self.assertEqual(3, formset.form.declared_fields['order'].initial)
@freeze_time('2017-01-01')
class EventAdminTest(TestCase):
fixtures = ['members.json', 'committees.json']
@classmethod
def setUpTestData(cls):
cls.committee = Committee.objects.get(pk=1)
cls.event = Event.objects.create(
pk=1,
organiser=cls.committee,
title_nl='testevenement',
title_en='testevent',
description_en='desc',
description_nl='besch',
published=True,
start=(timezone.now() + datetime.timedelta(hours=1)),
end=(timezone.now() + datetime.timedelta(hours=2)),
location_en='test location',
location_nl='test locatie',
map_location='test map location',
price=0.00,
fine=0.00
)
cls.member = Member.objects.filter(last_name="Wiggers").first()
def setUp(self):
pass
self.admin_site = AdminSite()
self.admin = EventAdmin(Event, self.admin_site)
self.rf = RequestFactory()
def test_overview_link(self):
self.assertEqual(self.admin.overview_link(self.event),
'<a href="/events/admin/1/">testevenement</a>')
@mock.patch('events.admin.DoNextModelAdmin.has_change_permission')
@mock.patch('events.services.is_organiser')
def test_has_change_permission(self, organiser_mock, permission_mock):
permission_mock.return_value = None
organiser_mock.return_value = True
request = self.rf.get('/admin/events/event/1/change/')
request.member = self.member
res = self.admin.has_change_permission(request, None)
self.assertEqual(res, None)
res = self.admin.has_change_permission(request, self.event)
self.assertEqual(res, None)
organiser_mock.return_value = False
res = self.admin.has_change_permission(request, self.event)
self.assertEqual(res, False)
@override_settings(LANGUAGE_CODE='en')
def test_event_date(self):
self.assertEqual(self.admin.event_date(self.event),
'Sunday 01 jan 2017, 2:00')
@override_settings(LANGUAGE_CODE='en')
def test_registration_date(self):
self.assertEqual(self.admin.registration_date(self.event),
'')
self.event.registration_start = timezone.now()
self.assertEqual(self.admin.registration_date(self.event),
'Sunday 01 jan 2017, 1:00')
@override_settings(LANGUAGE_CODE='en')
def test_edit_link(self):
self.assertEqual(self.admin.edit_link(None), 'Edit')
@override_settings(LANGUAGE_CODE='en')
def test_num_participants(self):
self.assertEqual(self.admin.num_participants(self.event),
'0/∞')
self.event.max_participants = 2
self.assertEqual(self.admin.num_participants(self.event),
'0/2')
......@@ -4,6 +4,7 @@ from unittest import mock
from django.contrib.auth.models import AnonymousUser, Permission
from django.test import TestCase
from django.utils import timezone
from freezegun import freeze_time
from activemembers.models import Committee, CommitteeMembership
from events import services
......@@ -12,6 +13,7 @@ from events.models import Event, Registration, RegistrationInformationField
from members.models import Member
@freeze_time('2017-01-01')
class ServicesTest(TestCase):
fixtures = ['members.json', 'committees.json']
......
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