mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 13:32:15 +00:00
More optimisation and cleanup (#420)
This commit is contained in:
@@ -8,54 +8,12 @@ from django.http import HttpResponseBadRequest
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
import PyRIGS.tests.base
|
||||
from RIGS import models
|
||||
from pytest_django.asserts import assertContains, assertNotContains
|
||||
|
||||
|
||||
class BaseCase(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.vatrate = models.VatRate.objects.create(start_at='2014-03-05', rate=0.20, comment='test1')
|
||||
cls.profile = models.Profile.objects.get_or_create(
|
||||
first_name='Test',
|
||||
last_name='TEC User',
|
||||
username='eventauthtest',
|
||||
email='teccie@functional.test',
|
||||
is_superuser=True # lazily grant all permissions
|
||||
)[0]
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.profile.set_password('testuser')
|
||||
self.profile.save()
|
||||
self.assertTrue(self.client.login(username=self.profile.username, password='testuser'))
|
||||
venue = models.Venue.objects.create(name='Authorisation Test Venue')
|
||||
client = models.Person.objects.create(name='Authorisation Test Person', email='authorisation@functional.test')
|
||||
organisation = models.Organisation.objects.create(name='Authorisation Test Organisation', union_account=True)
|
||||
self.event = models.Event.objects.create(
|
||||
name='Authorisation Test',
|
||||
start_date=date.today(),
|
||||
venue=venue,
|
||||
person=client,
|
||||
organisation=organisation,
|
||||
)
|
||||
|
||||
|
||||
class TestEventValidation(BaseCase):
|
||||
def test_create(self):
|
||||
url = reverse('event_create')
|
||||
# end time before start access after start
|
||||
response = self.client.post(url, {'start_date': datetime.date(2020, 1, 1), 'start_time': datetime.time(10, 00),
|
||||
'end_time': datetime.time(9, 00),
|
||||
'access_at': datetime.datetime(2020, 1, 5, 10)})
|
||||
self.assertFormError(response, 'form', 'end_time',
|
||||
"Unless you've invented time travel, the event can't finish before it has started.")
|
||||
self.assertFormError(response, 'form', 'access_at',
|
||||
"Regardless of what some clients might think, access time cannot be after the event has started.")
|
||||
from pytest_django.asserts import assertContains, assertNotContains, assertFormError
|
||||
|
||||
|
||||
def setup_event():
|
||||
models.VatRate.objects.create(start_at='2014-03-05', rate=0.20, comment='test1')
|
||||
venue = models.Venue.objects.create(name='Authorisation Test Venue')
|
||||
client = models.Person.objects.create(name='Authorisation Test Person', email='authorisation@functional.test')
|
||||
organisation = models.Organisation.objects.create(name='Authorisation Test Organisation', union_account=True)
|
||||
@@ -84,6 +42,18 @@ def setup_mail(event, profile):
|
||||
return auth_data, hmac, url
|
||||
|
||||
|
||||
def test_create(admin_client):
|
||||
url = reverse('event_create')
|
||||
# end time before start access after start
|
||||
response = admin_client.post(url, {'start_date': datetime.date(2020, 1, 1), 'start_time': datetime.time(10, 00),
|
||||
'end_time': datetime.time(9, 00),
|
||||
'access_at': datetime.datetime(2020, 1, 5, 10)})
|
||||
assertFormError(response, 'form', 'end_time',
|
||||
"Unless you've invented time travel, the event can't finish before it has started.")
|
||||
assertFormError(response, 'form', 'access_at',
|
||||
"Regardless of what some clients might think, access time cannot be after the event has started.")
|
||||
|
||||
|
||||
def test_requires_valid_hmac(client, admin_user):
|
||||
event = setup_event()
|
||||
auth_data, hmac, url = setup_mail(event, admin_user)
|
||||
@@ -138,7 +108,7 @@ def test_duplicate_warning(client, admin_user):
|
||||
assertContains(response, 'amount has changed')
|
||||
|
||||
|
||||
@pytest.mark.django_db(transaction=True)
|
||||
@pytest.mark.django_db
|
||||
def test_email_sent(admin_client, admin_user, mailoutbox):
|
||||
event = setup_event()
|
||||
auth_data, hmac, url = setup_mail(event, admin_user)
|
||||
@@ -152,36 +122,36 @@ def test_email_sent(admin_client, admin_user, mailoutbox):
|
||||
assert mailoutbox[1].to == [settings.AUTHORISATION_NOTIFICATION_ADDRESS]
|
||||
|
||||
|
||||
class TECEventAuthorisationTest(BaseCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.url = reverse('event_authorise_request', kwargs={'pk': self.event.pk})
|
||||
def test_email_check(admin_client, admin_user):
|
||||
event = setup_event()
|
||||
url = reverse('event_authorise_request', kwargs={'pk': event.pk})
|
||||
admin_user.email = 'teccie@someotherdomain.com'
|
||||
admin_user.save()
|
||||
|
||||
def test_email_check(self):
|
||||
self.profile.email = 'teccie@someotherdomain.com'
|
||||
self.profile.save()
|
||||
response = admin_client.post(url)
|
||||
|
||||
response = self.client.post(self.url)
|
||||
assertContains(response, 'must have an @nottinghamtec.co.uk email address')
|
||||
|
||||
self.assertContains(response, 'must have an @nottinghamtec.co.uk email address')
|
||||
|
||||
def test_request_send(self):
|
||||
self.profile.email = 'teccie@nottinghamtec.co.uk'
|
||||
self.profile.save()
|
||||
response = self.client.post(self.url)
|
||||
self.assertContains(response, 'This field is required.')
|
||||
def test_request_send(admin_client, admin_user):
|
||||
event = setup_event()
|
||||
url = reverse('event_authorise_request', kwargs={'pk': event.pk})
|
||||
admin_user.email = 'teccie@nottinghamtec.co.uk'
|
||||
admin_user.save()
|
||||
response = admin_client.post(url)
|
||||
assertContains(response, 'This field is required.')
|
||||
|
||||
mail.outbox = []
|
||||
mail.outbox = []
|
||||
|
||||
response = self.client.post(self.url, {'email': 'client@functional.test'})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
email = mail.outbox[0]
|
||||
self.assertIn('client@functional.test', email.to)
|
||||
self.assertIn('/event/%d/' % (self.event.pk), email.body)
|
||||
response = admin_client.post(url, {'email': 'client@functional.test'})
|
||||
assert response.status_code == 302
|
||||
assert len(mail.outbox) == 1
|
||||
email = mail.outbox[0]
|
||||
assert 'client@functional.test' in email.to
|
||||
assert '/event/%d/' % event.pk in email.body
|
||||
|
||||
# Check sent by details are populated
|
||||
self.event.refresh_from_db()
|
||||
self.assertEqual(self.event.auth_request_by, self.profile)
|
||||
self.assertEqual(self.event.auth_request_to, 'client@functional.test')
|
||||
self.assertIsNotNone(self.event.auth_request_at)
|
||||
# Check sent by details are populated
|
||||
event.refresh_from_db()
|
||||
assert event.auth_request_by == admin_user
|
||||
assert event.auth_request_to == 'client@functional.test'
|
||||
assert event.auth_request_at is not None
|
||||
|
||||
Reference in New Issue
Block a user