diff --git a/helios/tests.py b/helios/tests.py index 3bd573197b062287d6dd34896d03c8f904156f66..99806990f51c36e049266e59d14181c4e21efbf5 100644 --- a/helios/tests.py +++ b/helios/tests.py @@ -348,6 +348,13 @@ class LegacyElectionBlackboxTests(DataFormatBlackboxTests, TestCase): # EXPECTED_BALLOTS_FILE = 'helios/fixtures/v3.1-ballots-expected.json' class WebTest(django_webtest.WebTest): + def assertStatusCode(self, response, status_code): + if hasattr(response, 'status_code'): + assert response.status_code == status_code, response.status_code + else: + assert response.status_int == status_code, response.status_int + + def assertRedirects(self, response, url): """ reimplement this in case it's a WebOp response @@ -355,38 +362,23 @@ class WebTest(django_webtest.WebTest): thus the localhost exception """ if hasattr(response, 'location'): - assert url in response.location - else: - assert url in response._headers['location'][1] - - if hasattr(response, 'status_code'): - assert response.status_code == 302 + assert url in response.location, response.location else: - assert response.status_int == 302 - - #self.assertEqual(response.status_code, 302) - + assert url in response['location'], response['location'] + self.assertStatusCode(response, 302) #return super(django_webtest.WebTest, self).assertRedirects(response, url) - #if hasattr(response, 'status_code') and hasattr(response, 'location'): - - #assert url in response.location, "redirected to %s instead of %s" % (response.location, url) + def assertContains(self, response, text): - if hasattr(response, 'status_code'): - assert response.status_code == 200 -# return super(django_webtest.WebTest, self).assertContains(response, text) - else: - assert response.status_int == 200 + self.assertStatusCode(response, 200) - if hasattr(response, "testbody"): assert text in response.testbody, "missing text %s" % text + elif hasattr(response, "body"): + assert text in response.body, "missing text %s" % text else: - if hasattr(response, "body"): - assert text in response.body, "missing text %s" % text - else: - assert text in response.content, "missing text %s" % text + assert text in response.content, "missing text %s" % text ## @@ -400,22 +392,6 @@ class ElectionBlackboxTests(WebTest): self.election = models.Election.objects.all()[0] self.user = auth_models.User.objects.get(user_id='ben@adida.net', user_type='google') - def assertContains(self, response, text): - if hasattr(response, 'status_code'): - assert response.status_code == 200 -# return super(django_webtest.WebTest, self).assertContains(response, text) - else: - assert response.status_int == 200 - - - if hasattr(response, "testbody"): - assert text in response.testbody, "missing text %s" % text - else: - if hasattr(response, "body"): - assert text in response.body, "missing text %s" % text - else: - assert text in response.content, "missing text %s" % text - def setup_login(self): # set up the session session = self.client.session @@ -438,11 +414,11 @@ class ElectionBlackboxTests(WebTest): def test_election_404(self): response = self.client.get("/helios/elections/foobar") - self.assertEquals(response.status_code, 404) + self.assertStatusCode(response, 404) def test_election_bad_trustee(self): response = self.client.get("/helios/t/%s/foobar@bar.com/badsecret" % self.election.short_name) - self.assertEquals(response.status_code, 404) + self.assertStatusCode(response, 404) def test_get_election_shortcut(self): response = self.client.get("/helios/e/%s" % self.election.short_name, follow=True) @@ -510,7 +486,7 @@ class ElectionBlackboxTests(WebTest): new_election = models.Election.objects.get(uuid = self.election.uuid) self.assertEquals(new_election.short_name, self.election.short_name + "-2") - def _setup_complete_election(self, election_params={}): + def _setup_complete_election(self, election_params=None): "do the setup part of a whole election" # a bogus call to set up the session @@ -532,12 +508,14 @@ class ElectionBlackboxTests(WebTest): } # override with the given - full_election_params.update(election_params) + full_election_params.update(election_params or {}) response = self.client.post("/helios/elections/new", full_election_params) # we are redirected to the election, let's extract the ID out of the URL - election_id = re.search('/elections/([^/]+)/', str(response['Location'])).group(1) + election_id = re.search('/elections/([^/]+)/', str(response['Location'])) + self.assertIsNotNone(election_id, "Election id not found in redirect: %s" % str(response['Location'])) + election_id = election_id.group(1) # helios is automatically added as a trustee @@ -577,7 +555,7 @@ class ElectionBlackboxTests(WebTest): self.assertContains(response, '"uuid": "%s"' % single_voter.uuid) response = self.client.get("/helios/elections/%s/voters/foobar" % election_id) - self.assertEquals(response.status_code, 404) + self.assertStatusCode(response, 404) # add questions response = self.client.post("/helios/elections/%s/save_questions" % election_id, { @@ -670,7 +648,7 @@ class ElectionBlackboxTests(WebTest): # at this point an email should have gone out to the user # at position num_messages after, since that was the len() before we cast this ballot email_message = mail.outbox[len(mail.outbox) - 1] - url = re.search('http://[^/]+(/[^ \n]*)', email_message.body).group(1) + url = re.search('https?://[^/]+(/[^ \n]*)', email_message.body).group(1) # check that we can get at that URL if not need_login: @@ -690,7 +668,7 @@ class ElectionBlackboxTests(WebTest): login_form['password'] = ' ' + password + ' ' login_form.submit() - response = self.app.get(url) + response = self.app.get(url, auto_follow=True) self.assertContains(response, ballot.hash) self.assertContains(response, html_escape(encrypted_vote)) @@ -723,7 +701,7 @@ class ElectionBlackboxTests(WebTest): # check that we can't get the tally yet response = self.client.get("/helios/elections/%s/result" % election_id) - self.assertEquals(response.status_code, 403) + self.assertStatusCode(response, 403) # release response = self.client.post("/helios/elections/%s/release_result" % election_id, { @@ -788,7 +766,9 @@ class ElectionBlackboxTests(WebTest): "private_p" : "False", 'csrf_token': self.client.session['csrf_token']}) - election_id = re.match("(.*)/elections/(.*)/view", response['Location']).group(2) + election_id = re.match("(.*)/elections/(.*)/view", str(response['Location'])) + self.assertIsNotNone(election_id, "Election id not found in redirect: %s" % str(response['Location'])) + election_id = election_id.group(2) # update eligiblity response = self.client.post("/helios/elections/%s/voters/eligibility" % election_id, {