class ViewTestCases(TestCase): def setUp(self): self.rf = MockRequest() def test_complete_200(self): """The complete page renders with 200 response""" request = self.rf.get(reverse("cccontact:complete")) response = complete(request) self.assertEqual(200, response.status_code) def test_contact_200__get(self): """The contact view gives a 200 status""" request = self.rf.get(reverse("cccontact:contact")) response = contact(request) self.assertEqual(200, response.status_code) def test_contact_200_post(self): """The contact view gives a 200 status""" request = self.rf.post(reverse("cccontact:contact"), {}) response = contact(request) self.assertEqual(200, response.status_code) def test_contact_honeypot(self): """if data is entered into the honeypot then abort, but do so silently and return a 200""" self.assertEqual(0, Message.objects.count()) # send data including honeypot data = { "name": "Tester", "subject": "Oh HAI", "email": "*****@*****.**", "message": "buy viagra", "phone": "9789789", "m": "buy viagra", } request = self.rf.post(reverse("cccontact:contact"), data) response = contact(request) # 200 response self.assertEqual(200, response.status_code) # no messages self.assertEqual(0, Message.objects.count()) def test_send(self): """if the correcy data is sent, then a message is sent""" self.assertEqual(0, Message.objects.count()) # send data including honeypot data = { "name": "Tester", "subject": "I want to buy", "email": "*****@*****.**", "phone": "9789789", "m": "I want to buy all the things", } request = self.rf.post(reverse("cccontact:contact"), data) response = contact(request) # 200 response self.assertEqual(302, response.status_code) # no messages self.assertEqual(1, Message.objects.count())
class ListenerTestCases(TestCase): def setUp(self): # set up request factory self.rf = MockRequest() def test_badheadererror_subject(self): """BadHeaderError are handled gracefully""" # none self.assertEqual(0, Message.objects.count()) # set up data = { 'name': 'Tester', 'subject': 'I want to buy\n\n\ncc:[email protected]', 'email': '*****@*****.**', 'phone': '9789789', 'm': 'I want to buy all the things'} # post it request = self.rf.post( reverse('cccontact:contact'), data) # get response response = contact(request) # now have one self.assertEqual(1, Message.objects.count())
class SignalTestCases(TestCase): def setUp(self): # set up test dict global testdict testdict = {'signal_sent': False} # set up request factory self.rf = MockRequest() # set up the data data = { 'name': 'Tester', 'subject': 'I want to buy', 'email': '*****@*****.**', 'phone': '9789789', 'm': 'I want to buy all the things'} # make the request self.request = self.rf.post( reverse('cccontact:contact'), data) def tearDown(self): # disconnect signals contact_form_is_valid.disconnect( dispatch_uid='test_contact_form_is_valid_sends') contact_form_is_valid.disconnect( dispatch_uid='test_sends_correct_arguments') def test_sends_correct_arguments(self): """"test that the view passes the signal the correct args""" global testdict # the listener def test_listener(sender, request, form, **kwargs): global testdict testdict['request'] = request testdict['form'] = form # connect it contact_form_is_valid.connect( test_listener, dispatch_uid='test_sends_correct_arguments') # now get the response response = contact(self.request) # test dict has been updated self.assertTrue(testdict['request'], True) self.assertTrue(testdict['form'], True) def test_contact_form_is_valid_sends(self): """when the contact form is valid the valid signal is sent""" # our test dict confirms that the signal not sent global testdict self.assertEqual(testdict['signal_sent'], False) # test listener def test_listener(**kwargs): global testdict testdict['signal_sent'] = True # connect it contact_form_is_valid.connect( test_listener, dispatch_uid='test_contact_form_is_valid_sends') # now get the response response = contact(self.request) # test dict has been updated self.assertEqual(testdict['signal_sent'], True)