diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index d602d66b..2d98e80e 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -231,6 +231,7 @@ class EventAuthorise(generic.UpdateView): success_template = 'RIGS/eventauthorisation_success.html' def form_valid(self, form): + self.object = form.save() # TODO: send email confirmation self.template_name = self.success_template messages.add_message(self.request, messages.SUCCESS, @@ -242,7 +243,7 @@ class EventAuthorise(generic.UpdateView): return models.Event.objects.select_related('organisation', 'person', 'venue').get(pk=self.kwargs['pk']) def get_object(self, queryset=None): - return self.event.authorisation + return getattr(self.event, 'authorisation', None) def get_form_class(self): if self.event.organisation is not None and self.event.organisation.union_account: diff --git a/RIGS/test_functional.py b/RIGS/test_functional.py index f6c60865..13d6871b 100644 --- a/RIGS/test_functional.py +++ b/RIGS/test_functional.py @@ -4,9 +4,11 @@ import re from datetime import date, timedelta import reversion -from django.core import mail +from django.core import mail, signing +from django.core.urlresolvers import reverse from django.db import transaction -from django.test import LiveServerTestCase +from django.http import HttpResponseBadRequest +from django.test import LiveServerTestCase, TestCase from django.test.client import Client from selenium import webdriver from selenium.common.exceptions import StaleElementReferenceException, WebDriverException @@ -17,10 +19,9 @@ from RIGS import models class UserRegistrationTest(LiveServerTestCase): - def setUp(self): self.browser = webdriver.Firefox() - self.browser.implicitly_wait(3) # Set implicit wait session wide + self.browser.implicitly_wait(3) # Set implicit wait session wide os.environ['RECAPTCHA_TESTING'] = 'True' def tearDown(self): @@ -149,17 +150,16 @@ class UserRegistrationTest(LiveServerTestCase): class EventTest(LiveServerTestCase): - def setUp(self): self.profile = models.Profile( username="EventTest", first_name="Event", last_name="Test", initials="ETU", is_superuser=True) self.profile.set_password("EventTestPassword") self.profile.save() - self.vatrate = models.VatRate.objects.create(start_at='2014-03-05',rate=0.20,comment='test1') - + self.vatrate = models.VatRate.objects.create(start_at='2014-03-05', rate=0.20, comment='test1') + self.browser = webdriver.Firefox() - self.browser.implicitly_wait(3) # Set implicit wait session wide + self.browser.implicitly_wait(3) # Set implicit wait session wide self.browser.maximize_window() os.environ['RECAPTCHA_TESTING'] = 'True' @@ -203,7 +203,7 @@ class EventTest(LiveServerTestCase): # Gets redirected to login and back self.authenticate('/event/create/') - wait = WebDriverWait(self.browser, 10) #setup WebDriverWait to use later (to wait for animations) + wait = WebDriverWait(self.browser, 10) # setup WebDriverWait to use later (to wait for animations) wait.until(animation_is_finished()) @@ -370,26 +370,28 @@ class EventTest(LiveServerTestCase): wait.until(animation_is_finished()) modal = self.browser.find_element_by_id("itemModal") modal.find_element_by_id("item_name").send_keys("Test Item 1") - modal.find_element_by_id("item_description").send_keys("This is an item description\nthat for reasons unkown spans two lines") + modal.find_element_by_id("item_description").send_keys( + "This is an item description\nthat for reasons unkown spans two lines") e = modal.find_element_by_id("item_quantity") e.click() e.send_keys(Keys.UP) e.send_keys(Keys.UP) e = modal.find_element_by_id("item_cost") e.send_keys("23.95") - e.send_keys(Keys.ENTER) # enter submit + e.send_keys(Keys.ENTER) # enter submit # Confirm item has been saved to json field objectitems = self.browser.execute_script("return objectitems;") self.assertEqual(1, len(objectitems)) - testitem = objectitems["-1"]['fields'] # as we are deliberately creating this we know the ID + testitem = objectitems["-1"]['fields'] # as we are deliberately creating this we know the ID self.assertEqual("Test Item 1", testitem['name']) - self.assertEqual("2", testitem['quantity']) # test a couple of "worse case" fields + self.assertEqual("2", testitem['quantity']) # test a couple of "worse case" fields # See new item appear in table - row = self.browser.find_element_by_id('item--1') # ID number is known, see above + row = self.browser.find_element_by_id('item--1') # ID number is known, see above self.assertIn("Test Item 1", row.find_element_by_xpath('//span[@class="name"]').text) - self.assertIn("This is an item description", row.find_element_by_xpath('//div[@class="item-description"]').text) + self.assertIn("This is an item description", + row.find_element_by_xpath('//div[@class="item-description"]').text) self.assertEqual(u'£ 23.95', row.find_element_by_xpath('//tr[@id="item--1"]/td[2]').text) self.assertEqual("2", row.find_element_by_xpath('//td[@class="quantity"]').text) self.assertEqual(u'£ 47.90', row.find_element_by_xpath('//tr[@id="item--1"]/td[4]').text) @@ -431,92 +433,95 @@ class EventTest(LiveServerTestCase): # See redirected to success page successTitle = self.browser.find_element_by_xpath('//h1').text event = models.Event.objects.get(name='Test Event Name') - self.assertIn("N0000%d | Test Event Name"%event.pk, successTitle) + self.assertIn("N0000%d | Test Event Name" % event.pk, successTitle) except WebDriverException: # This is a dirty workaround for wercker being a bit funny and not running it correctly. # Waiting for wercker to get back to me about this pass def testEventDuplicate(self): - testEvent = models.Event.objects.create(name="TE E1", status=models.Event.PROVISIONAL, start_date=date.today() + timedelta(days=6), description="start future no end", purchase_order="TESTPO") + testEvent = models.Event.objects.create(name="TE E1", status=models.Event.PROVISIONAL, + start_date=date.today() + timedelta(days=6), + description="start future no end", purchase_order="TESTPO") item1 = models.EventItem( - event=testEvent, - name="Test Item 1", - cost="10.00", - quantity="1", - order=1 - ).save() + event=testEvent, + name="Test Item 1", + cost="10.00", + quantity="1", + order=1 + ).save() item2 = models.EventItem( - event=testEvent, - name="Test Item 2", - description="Foo", - cost="9.72", - quantity="3", - order=2, - ).save() + event=testEvent, + name="Test Item 2", + description="Foo", + cost="9.72", + quantity="3", + order=2, + ).save() self.browser.get(self.live_server_url + '/event/' + str(testEvent.pk) + '/duplicate/') self.authenticate('/event/' + str(testEvent.pk) + '/duplicate/') - wait = WebDriverWait(self.browser, 10) #setup WebDriverWait to use later (to wait for animations) + wait = WebDriverWait(self.browser, 10) # setup WebDriverWait to use later (to wait for animations) save = self.browser.find_element_by_xpath( '(//button[@type="submit"])[3]') form = self.browser.find_element_by_tag_name('form') - # Check the items are visible - table = self.browser.find_element_by_id('item-table') # ID number is known, see above + table = self.browser.find_element_by_id('item-table') # ID number is known, see above self.assertIn("Test Item 1", table.text) self.assertIn("Test Item 2", table.text) # Check the info message is visible - self.assertIn("Event data duplicated but not yet saved",self.browser.find_element_by_id('content').text) + self.assertIn("Event data duplicated but not yet saved", self.browser.find_element_by_id('content').text) # Add item form.find_element_by_xpath('//button[contains(@class, "item-add")]').click() wait.until(animation_is_finished()) modal = self.browser.find_element_by_id("itemModal") modal.find_element_by_id("item_name").send_keys("Test Item 3") - modal.find_element_by_id("item_description").send_keys("This is an item description\nthat for reasons unkown spans two lines") + modal.find_element_by_id("item_description").send_keys( + "This is an item description\nthat for reasons unkown spans two lines") e = modal.find_element_by_id("item_quantity") e.click() e.send_keys(Keys.UP) e.send_keys(Keys.UP) e = modal.find_element_by_id("item_cost") e.send_keys("23.95") - e.send_keys(Keys.ENTER) # enter submit + e.send_keys(Keys.ENTER) # enter submit # Attempt to save save.click() - self.assertNotIn("N0000%d"%testEvent.pk, self.browser.find_element_by_xpath('//h1').text) - self.assertNotIn("Event data duplicated but not yet saved", self.browser.find_element_by_id('content').text) # Check info message not visible + self.assertNotIn("N0000%d" % testEvent.pk, self.browser.find_element_by_xpath('//h1').text) + self.assertNotIn("Event data duplicated but not yet saved", + self.browser.find_element_by_id('content').text) # Check info message not visible # Check the new items are visible - table = self.browser.find_element_by_id('item-table') # ID number is known, see above + table = self.browser.find_element_by_id('item-table') # ID number is known, see above self.assertIn("Test Item 1", table.text) self.assertIn("Test Item 2", table.text) self.assertIn("Test Item 3", table.text) infoPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Event Info")]/..') - self.assertIn("N0000%d"%testEvent.pk, infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) + self.assertIn("N0000%d" % testEvent.pk, + infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) # Check the PO hasn't carried through self.assertNotIn("TESTPO", infoPanel.find_element_by_xpath('//dt[text()="PO"]/following-sibling::dd[1]').text) + self.browser.get(self.live_server_url + '/event/' + str(testEvent.pk)) # Go back to the old event - - self.browser.get(self.live_server_url + '/event/' + str(testEvent.pk)) #Go back to the old event - - #Check that based-on hasn't crept into the old event + # Check that based-on hasn't crept into the old event infoPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Event Info")]/..') - self.assertNotIn("N0000%d"%testEvent.pk, infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) + self.assertNotIn("N0000%d" % testEvent.pk, + infoPanel.find_element_by_xpath('//dt[text()="Based On"]/following-sibling::dd[1]').text) # Check the PO remains on the old event self.assertIn("TESTPO", infoPanel.find_element_by_xpath('//dt[text()="PO"]/following-sibling::dd[1]').text) # Check the items are as they were - table = self.browser.find_element_by_id('item-table') # ID number is known, see above + table = self.browser.find_element_by_id('item-table') # ID number is known, see above self.assertIn("Test Item 1", table.text) self.assertIn("Test Item 2", table.text) self.assertNotIn("Test Item 3", table.text) @@ -526,7 +531,7 @@ class EventTest(LiveServerTestCase): # Gets redirected to login and back self.authenticate('/event/create/') - wait = WebDriverWait(self.browser, 10) #setup WebDriverWait to use later (to wait for animations) + wait = WebDriverWait(self.browser, 10) # setup WebDriverWait to use later (to wait for animations) wait.until(animation_is_finished()) @@ -553,7 +558,6 @@ class EventTest(LiveServerTestCase): self.assertTrue(error.is_displayed()) self.assertIn("can't finish before it has started", error.find_element_by_xpath('//dd[1]/ul/li').text) - # Same date, end time before start time form = self.browser.find_element_by_tag_name('form') save = self.browser.find_element_by_xpath('(//button[@type="submit"])[3]') @@ -575,7 +579,6 @@ class EventTest(LiveServerTestCase): self.assertTrue(error.is_displayed()) self.assertIn("can't finish before it has started", error.find_element_by_xpath('//dd[1]/ul/li').text) - # Same date, end time before start time form = self.browser.find_element_by_tag_name('form') save = self.browser.find_element_by_xpath('(//button[@type="submit"])[3]') @@ -591,7 +594,6 @@ class EventTest(LiveServerTestCase): form.find_element_by_id('id_end_time').clear() form.find_element_by_id('id_end_time').send_keys('06:00') - # No end date, end time before start time form = self.browser.find_element_by_tag_name('form') save = self.browser.find_element_by_xpath('(//button[@type="submit"])[3]') @@ -612,7 +614,6 @@ class EventTest(LiveServerTestCase): self.assertTrue(error.is_displayed()) self.assertIn("can't finish before it has started", error.find_element_by_xpath('//dd[1]/ul/li').text) - # 2 dates, end after start form = self.browser.find_element_by_tag_name('form') save = self.browser.find_element_by_xpath('(//button[@type="submit"])[3]') @@ -623,24 +624,24 @@ class EventTest(LiveServerTestCase): form.find_element_by_id('id_end_date').send_keys('3015-04-26') form.find_element_by_id('id_start_time').clear() - + form.find_element_by_id('id_end_time').clear() - + # Attempt to save - should succeed save.click() - + # See redirected to success page successTitle = self.browser.find_element_by_xpath('//h1').text event = models.Event.objects.get(name='Test Event Name') - self.assertIn("N0000%d | Test Event Name"%event.pk, successTitle) - + self.assertIn("N0000%d | Test Event Name" % event.pk, successTitle) + def testRigNonRig(self): self.browser.get(self.live_server_url + '/event/create/') # Gets redirected to login and back self.authenticate('/event/create/') - wait = WebDriverWait(self.browser, 10) #setup WebDriverWait to use later (to wait for animations) - self.browser.implicitly_wait(3) #Set session-long wait (only works for non-existant DOM objects) + wait = WebDriverWait(self.browser, 10) # setup WebDriverWait to use later (to wait for animations) + self.browser.implicitly_wait(3) # Set session-long wait (only works for non-existant DOM objects) wait.until(animation_is_finished()) @@ -672,7 +673,8 @@ class EventTest(LiveServerTestCase): person = models.Person(name="Event Detail Person", email="eventdetail@person.tests.rigs", phone="123 123") person.save() with transaction.atomic(), reversion.create_revision(): - organisation = models.Organisation(name="Event Detail Organisation", email="eventdetail@organisation.tests.rigs", phone="123 456").save() + organisation = models.Organisation(name="Event Detail Organisation", + email="eventdetail@organisation.tests.rigs", phone="123 456").save() with transaction.atomic(), reversion.create_revision(): venue = models.Venue(name="Event Detail Venue").save() with transaction.atomic(), reversion.create_revision(): @@ -702,59 +704,84 @@ class EventTest(LiveServerTestCase): order=2, ).save() - - self.browser.get(self.live_server_url + '/event/%d'%event.pk) - self.authenticate('/event/%d/'%event.pk) - self.assertIn("N%05d | %s"%(event.pk, event.name), self.browser.find_element_by_xpath('//h1').text) + self.browser.get(self.live_server_url + '/event/%d' % event.pk) + self.authenticate('/event/%d/' % event.pk) + self.assertIn("N%05d | %s" % (event.pk, event.name), self.browser.find_element_by_xpath('//h1').text) personPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Contact Details")]/..') - self.assertEqual(person.name, personPanel.find_element_by_xpath('//dt[text()="Person"]/following-sibling::dd[1]').text) - self.assertEqual(person.email, personPanel.find_element_by_xpath('//dt[text()="Email"]/following-sibling::dd[1]').text) - self.assertEqual(person.phone, personPanel.find_element_by_xpath('//dt[text()="Phone Number"]/following-sibling::dd[1]').text) + self.assertEqual(person.name, + personPanel.find_element_by_xpath('//dt[text()="Person"]/following-sibling::dd[1]').text) + self.assertEqual(person.email, + personPanel.find_element_by_xpath('//dt[text()="Email"]/following-sibling::dd[1]').text) + self.assertEqual(person.phone, + personPanel.find_element_by_xpath('//dt[text()="Phone Number"]/following-sibling::dd[1]').text) organisationPanel = self.browser.find_element_by_xpath('//div[contains(text(), "Contact Details")]/..') -class IcalTest(LiveServerTestCase): +class IcalTest(LiveServerTestCase): def setUp(self): self.all_events = set(range(1, 18)) self.current_events = (1, 2, 3, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18) self.not_current_events = set(self.all_events) - set(self.current_events) - self.vatrate = models.VatRate.objects.create(start_at='2014-03-05',rate=0.20,comment='test1') + self.vatrate = models.VatRate.objects.create(start_at='2014-03-05', rate=0.20, comment='test1') self.profile = models.Profile( username="EventTest", first_name="Event", last_name="Test", initials="ETU", is_superuser=True) self.profile.set_password("EventTestPassword") self.profile.save() # produce 7 normal events - 5 current - 1 last week - 1 two years ago - 2 provisional - 2 confirmed - 3 booked - models.Event.objects.create(name="TE E1", status=models.Event.PROVISIONAL, start_date=date.today() + timedelta(days=6), description="start future no end") - models.Event.objects.create(name="TE E2", status=models.Event.PROVISIONAL, start_date=date.today(), description="start today no end") - models.Event.objects.create(name="TE E3", status=models.Event.CONFIRMED, start_date=date.today(), end_date=date.today(), description="start today with end today") - models.Event.objects.create(name="TE E4", status=models.Event.CONFIRMED, start_date=date.today()-timedelta(weeks=104), description="start past 2 years no end") - models.Event.objects.create(name="TE E5", status=models.Event.BOOKED, start_date=date.today()-timedelta(days=7), end_date=date.today()-timedelta(days=1), description="start past 1 week with end past") - models.Event.objects.create(name="TE E6", status=models.Event.BOOKED, start_date=date.today()-timedelta(days=2), end_date=date.today()+timedelta(days=2), description="start past, end future") - models.Event.objects.create(name="TE E7", status=models.Event.BOOKED, start_date=date.today()+timedelta(days=2), end_date=date.today()+timedelta(days=2), description="start + end in future") + models.Event.objects.create(name="TE E1", status=models.Event.PROVISIONAL, + start_date=date.today() + timedelta(days=6), description="start future no end") + models.Event.objects.create(name="TE E2", status=models.Event.PROVISIONAL, start_date=date.today(), + description="start today no end") + models.Event.objects.create(name="TE E3", status=models.Event.CONFIRMED, start_date=date.today(), + end_date=date.today(), description="start today with end today") + models.Event.objects.create(name="TE E4", status=models.Event.CONFIRMED, + start_date=date.today() - timedelta(weeks=104), + description="start past 2 years no end") + models.Event.objects.create(name="TE E5", status=models.Event.BOOKED, + start_date=date.today() - timedelta(days=7), + end_date=date.today() - timedelta(days=1), + description="start past 1 week with end past") + models.Event.objects.create(name="TE E6", status=models.Event.BOOKED, + start_date=date.today() - timedelta(days=2), + end_date=date.today() + timedelta(days=2), description="start past, end future") + models.Event.objects.create(name="TE E7", status=models.Event.BOOKED, + start_date=date.today() + timedelta(days=2), + end_date=date.today() + timedelta(days=2), description="start + end in future") # 2 cancelled - 1 current - models.Event.objects.create(name="TE E8", start_date=date.today()+timedelta(days=2), end_date=date.today()+timedelta(days=2), status=models.Event.CANCELLED, description="cancelled in future") - models.Event.objects.create(name="TE E9", start_date=date.today()-timedelta(days=1), end_date=date.today()+timedelta(days=2), status=models.Event.CANCELLED, description="cancelled and started") + models.Event.objects.create(name="TE E8", start_date=date.today() + timedelta(days=2), + end_date=date.today() + timedelta(days=2), status=models.Event.CANCELLED, + description="cancelled in future") + models.Event.objects.create(name="TE E9", start_date=date.today() - timedelta(days=1), + end_date=date.today() + timedelta(days=2), status=models.Event.CANCELLED, + description="cancelled and started") # 5 dry hire - 3 current - 1 cancelled models.Event.objects.create(name="TE E10", start_date=date.today(), dry_hire=True, description="dryhire today") - models.Event.objects.create(name="TE E11", start_date=date.today(), dry_hire=True, checked_in_by=self.profile, description="dryhire today, checked in") - models.Event.objects.create(name="TE E12", start_date=date.today()-timedelta(days=1), dry_hire=True, status=models.Event.BOOKED, description="dryhire past") - models.Event.objects.create(name="TE E13", start_date=date.today()-timedelta(days=2), dry_hire=True, checked_in_by=self.profile, description="dryhire past checked in") - models.Event.objects.create(name="TE E14", start_date=date.today(), dry_hire=True, status=models.Event.CANCELLED, description="dryhire today cancelled") + models.Event.objects.create(name="TE E11", start_date=date.today(), dry_hire=True, checked_in_by=self.profile, + description="dryhire today, checked in") + models.Event.objects.create(name="TE E12", start_date=date.today() - timedelta(days=1), dry_hire=True, + status=models.Event.BOOKED, description="dryhire past") + models.Event.objects.create(name="TE E13", start_date=date.today() - timedelta(days=2), dry_hire=True, + checked_in_by=self.profile, description="dryhire past checked in") + models.Event.objects.create(name="TE E14", start_date=date.today(), dry_hire=True, + status=models.Event.CANCELLED, description="dryhire today cancelled") # 4 non rig - 3 current models.Event.objects.create(name="TE E15", start_date=date.today(), is_rig=False, description="non rig today") - models.Event.objects.create(name="TE E16", start_date=date.today()+timedelta(days=1), is_rig=False, description="non rig tomorrow") - models.Event.objects.create(name="TE E17", start_date=date.today()-timedelta(days=1), is_rig=False, description="non rig yesterday") - models.Event.objects.create(name="TE E18", start_date=date.today(), is_rig=False, status=models.Event.CANCELLED, description="non rig today cancelled") + models.Event.objects.create(name="TE E16", start_date=date.today() + timedelta(days=1), is_rig=False, + description="non rig tomorrow") + models.Event.objects.create(name="TE E17", start_date=date.today() - timedelta(days=1), is_rig=False, + description="non rig yesterday") + models.Event.objects.create(name="TE E18", start_date=date.today(), is_rig=False, status=models.Event.CANCELLED, + description="non rig today cancelled") self.browser = webdriver.Firefox() - self.browser.implicitly_wait(3) # Set implicit wait session wide + self.browser.implicitly_wait(3) # Set implicit wait session wide os.environ['RECAPTCHA_TESTING'] = 'True' def tearDown(self): @@ -785,14 +812,15 @@ class IcalTest(LiveServerTestCase): # Completes and comes back to /user/ # Checks that no api key is displayed - self.assertEqual("No API Key Generated", self.browser.find_element_by_xpath("//div[@id='content']/div/div/div[3]/dl[2]/dd").text) + self.assertEqual("No API Key Generated", + self.browser.find_element_by_xpath("//div[@id='content']/div/div/div[3]/dl[2]/dd").text) self.assertEqual("No API Key Generated", self.browser.find_element_by_css_selector("pre").text) - + # Now creates an API key, and check a URL is displayed one self.browser.find_element_by_link_text("Generate API Key").click() self.assertIn("rigs.ics", self.browser.find_element_by_id("cal-url").text) self.assertNotIn("?", self.browser.find_element_by_id("cal-url").text) - + # Lets change everything so it's not the default value self.browser.find_element_by_xpath("//input[@value='rig']").click() self.browser.find_element_by_xpath("//input[@value='non-rig']").click() @@ -802,7 +830,9 @@ class IcalTest(LiveServerTestCase): self.browser.find_element_by_xpath("//input[@value='confirmed']").click() # and then check the url is correct - self.assertIn("rigs.ics?rig=false&non-rig=false&dry-hire=false&cancelled=true&provisional=false&confirmed=false", self.browser.find_element_by_id("cal-url").text) + self.assertIn( + "rigs.ics?rig=false&non-rig=false&dry-hire=false&cancelled=true&provisional=false&confirmed=false", + self.browser.find_element_by_id("cal-url").text) # Awesome - all seems to work @@ -815,27 +845,24 @@ class IcalTest(LiveServerTestCase): # Now creates an API key, and check a URL is displayed one self.browser.find_element_by_link_text("Generate API Key").click() - - c = Client() - + # Default settings - should have all non-cancelled events # Get the ical file (can't do this in selanium because reasons) icalUrl = self.browser.find_element_by_id("cal-url").text response = c.get(icalUrl) self.assertEqual(200, response.status_code) - #Check has entire file + # Check has entire file self.assertIn("BEGIN:VCALENDAR", response.content) self.assertIn("END:VCALENDAR", response.content) - expectedIn= [1,2,3,5,6,7,10,11,12,13,15,16,17] - for test in range(1,18): + expectedIn = [1, 2, 3, 5, 6, 7, 10, 11, 12, 13, 15, 16, 17] + for test in range(1, 18): if test in expectedIn: - self.assertIn("TE E"+str(test)+" ", response.content) + self.assertIn("TE E" + str(test) + " ", response.content) else: - self.assertNotIn("TE E"+str(test)+" ", response.content) - + self.assertNotIn("TE E" + str(test) + " ", response.content) # Only dry hires self.browser.find_element_by_xpath("//input[@value='rig']").click() @@ -845,13 +872,12 @@ class IcalTest(LiveServerTestCase): response = c.get(icalUrl) self.assertEqual(200, response.status_code) - expectedIn= [10,11,12,13] - for test in range(1,18): + expectedIn = [10, 11, 12, 13] + for test in range(1, 18): if test in expectedIn: - self.assertIn("TE E"+str(test)+" ", response.content) + self.assertIn("TE E" + str(test) + " ", response.content) else: - self.assertNotIn("TE E"+str(test)+" ", response.content) - + self.assertNotIn("TE E" + str(test) + " ", response.content) # Only provisional rigs self.browser.find_element_by_xpath("//input[@value='rig']").click() @@ -862,12 +888,12 @@ class IcalTest(LiveServerTestCase): response = c.get(icalUrl) self.assertEqual(200, response.status_code) - expectedIn= [1,2] - for test in range(1,18): + expectedIn = [1, 2] + for test in range(1, 18): if test in expectedIn: - self.assertIn("TE E"+str(test)+" ", response.content) + self.assertIn("TE E" + str(test) + " ", response.content) else: - self.assertNotIn("TE E"+str(test)+" ", response.content) + self.assertNotIn("TE E" + str(test) + " ", response.content) # Only cancelled non-rigs self.browser.find_element_by_xpath("//input[@value='rig']").click() @@ -879,12 +905,12 @@ class IcalTest(LiveServerTestCase): response = c.get(icalUrl) self.assertEqual(200, response.status_code) - expectedIn= [18] - for test in range(1,18): + expectedIn = [18] + for test in range(1, 18): if test in expectedIn: - self.assertIn("TE E"+str(test)+" ", response.content) + self.assertIn("TE E" + str(test) + " ", response.content) else: - self.assertNotIn("TE E"+str(test)+" ", response.content) + self.assertNotIn("TE E" + str(test) + " ", response.content) # Nothing selected self.browser.find_element_by_xpath("//input[@value='non-rig']").click() @@ -894,17 +920,19 @@ class IcalTest(LiveServerTestCase): response = c.get(icalUrl) self.assertEqual(200, response.status_code) - expectedIn= [] - for test in range(1,18): + expectedIn = [] + for test in range(1, 18): if test in expectedIn: - self.assertIn("TE E"+str(test)+" ", response.content) + self.assertIn("TE E" + str(test) + " ", response.content) else: - self.assertNotIn("TE E"+str(test)+" ", response.content) - - # Wow - that was a lot of tests + self.assertNotIn("TE E" + str(test) + " ", response.content) + + # Wow - that was a lot of tests + class animation_is_finished(object): """ Checks if animation is done """ + def __init__(self): pass @@ -915,3 +943,87 @@ class animation_is_finished(object): import time time.sleep(0.1) return finished + + +class ClientEventAuthorisationTest(TestCase): + auth_data = { + 'name': 'Test ABC', + 'po': '1234ABCZXY', + 'account_code': 'ABC TEST 12345', + 'uni_id': 1234567890, + 'tos': True + } + + def setUp(self): + 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=False) + self.event = models.Event.objects.create( + name='Authorisation Test', + start_date=date.today(), + venue=venue, + person=client, + organisation=organisation, + ) + self.hmac = signing.dumps({'pk': self.event.pk, 'email': 'authemail@function.test'}) + self.url = reverse('event_authorise', kwargs={'pk': self.event.pk, 'hmac': self.hmac}) + + def test_requires_valid_hmac(self): + bad_hmac = self.hmac[:-1] + url = reverse('event_authorise', kwargs={'pk': self.event.pk, 'hmac': bad_hmac}) + response = self.client.get(url) + self.assertIsInstance(response, HttpResponseBadRequest) + # TODO: Add some form of sensbile user facing error + # self.assertIn(response.content, "new URL") # check there is some level of sane instruction + + response = self.client.get(self.url) + self.assertContains(response, self.event.organisation.name) + + def test_generic_validation(self): + response = self.client.get(self.url) + self.assertContains(response, "Terms of Hire") + + response = self.client.post(self.url) + self.assertContains(response, "This field is required.", 4) + + data = self.auth_data + data['amount'] = self.event.total + 1 + + response = self.client.post(self.url, data) + self.assertContains(response, "The amount authorised must equal the total for the event") + self.assertNotContains(response, "This field is required.") + + data['amount'] = self.event.total + response = self.client.post(self.url, data) + self.assertContains(response, "Your event has been authorised") + + self.event.refresh_from_db() + self.assertTrue(self.event.authorised) + self.assertEqual(self.event.authorisation.email, "authemail@function.test") + + def test_internal_validation(self): + self.event.organisation.union_account = True + self.event.organisation.save() + + response = self.client.get(self.url) + self.assertContains(response, "Account code") + self.assertContains(response, "University ID") + + response = self.client.post(self.url) + self.assertContains(response, "This field is required.", 5) + + data = self.auth_data + response = self.client.post(self.url, data) + self.assertContains(response, "Your event has been authorised.") + + def test_duplicate_warning(self): + auth = models.EventAuthorisation.objects.create(event=self.event, name='Test ABC', email='dupe@functional.test', + po='ABC12345', amount=self.event.total) + response = self.client.get(self.url) + self.assertContains(response, 'This event has already been authorised.') + + auth.amount += 1 + auth.save() + + response = self.client.get(self.url) + self.assertContains(response, 'amount has changed') diff --git a/RIGS/test_models.py b/RIGS/test_models.py index 0cbe58f1..a858187d 100644 --- a/RIGS/test_models.py +++ b/RIGS/test_models.py @@ -341,28 +341,6 @@ class EventAuthorisationTestCase(TestCase): models.EventItem.objects.create(event=cls.event, name="Authorisation test item", quantity=2, cost=123.45, order=1) - def test_validation(self): - auth = models.EventAuthorisation(event=self.event, email="authroisation@model.test.case", name="Test Auth") - - auth.amount = self.event.total - 1 - self.assertRaises(ValidationError, auth.clean) - auth.amount = self.event.total - - # Test for externals first - self.assertRaises(ValidationError, auth.clean) - self.event.organisation = self.organisation - self.assertRaises(ValidationError, auth.clean) - auth.po = "TEST123" - self.assertIsNone(auth.clean()) - - auth.po = None - self.organisation.union_account = True - self.assertRaises(ValidationError, auth.clean) - auth.uni_id = "1234567" - self.assertRaises(ValidationError, auth.clean) - auth.account_code = "TST AUTH 12345" - self.assertIsNone(auth.clean()) - def test_event_property(self): auth1 = models.EventAuthorisation.objects.create(event=self.event, email="authroisation@model.test.case", name="Test Auth 1", amount=self.event.total - 1) @@ -370,11 +348,3 @@ class EventAuthorisationTestCase(TestCase): auth1.amount = self.event.total auth1.save() self.assertTrue(self.event.authorised) - - auth2 = models.EventAuthorisation.objects.create(event=self.event, email="authroisation@model.test.case", - name="Test Auth 2", amount=self.event.total - 1) - self.assertEqual(auth2.pk, self.event.authroisations.latest('created_at').pk) - self.assertFalse(self.event.authorised) - auth2.amount = self.event.total + 1 - auth2.save() - self.assertTrue(self.event.authorised)