Skip to content
Snippets Groups Projects
Unverified Commit 83b3c1a6 authored by Marco Ciotola's avatar Marco Ciotola
Browse files

Improvements to tests

parent 48e87d03
Branches
No related tags found
No related merge requests found
...@@ -348,6 +348,13 @@ class LegacyElectionBlackboxTests(DataFormatBlackboxTests, TestCase): ...@@ -348,6 +348,13 @@ class LegacyElectionBlackboxTests(DataFormatBlackboxTests, TestCase):
# EXPECTED_BALLOTS_FILE = 'helios/fixtures/v3.1-ballots-expected.json' # EXPECTED_BALLOTS_FILE = 'helios/fixtures/v3.1-ballots-expected.json'
class WebTest(django_webtest.WebTest): 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): def assertRedirects(self, response, url):
""" """
reimplement this in case it's a WebOp response reimplement this in case it's a WebOp response
...@@ -355,35 +362,20 @@ class WebTest(django_webtest.WebTest): ...@@ -355,35 +362,20 @@ class WebTest(django_webtest.WebTest):
thus the localhost exception thus the localhost exception
""" """
if hasattr(response, 'location'): if hasattr(response, 'location'):
assert url in response.location assert url in response.location, response.location
else:
assert url in response._headers['location'][1]
if hasattr(response, 'status_code'):
assert response.status_code == 302
else: else:
assert response.status_int == 302 assert url in response['location'], response['location']
self.assertStatusCode(response, 302)
#self.assertEqual(response.status_code, 302)
#return super(django_webtest.WebTest, self).assertRedirects(response, url) #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) #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
def assertContains(self, response, text):
self.assertStatusCode(response, 200)
if hasattr(response, "testbody"): if hasattr(response, "testbody"):
assert text in response.testbody, "missing text %s" % text assert text in response.testbody, "missing text %s" % text
else: elif hasattr(response, "body"):
if hasattr(response, "body"):
assert text in response.body, "missing text %s" % text assert text in response.body, "missing text %s" % text
else: 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): ...@@ -400,22 +392,6 @@ class ElectionBlackboxTests(WebTest):
self.election = models.Election.objects.all()[0] self.election = models.Election.objects.all()[0]
self.user = auth_models.User.objects.get(user_id='ben@adida.net', user_type='google') 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): def setup_login(self):
# set up the session # set up the session
session = self.client.session session = self.client.session
...@@ -438,11 +414,11 @@ class ElectionBlackboxTests(WebTest): ...@@ -438,11 +414,11 @@ class ElectionBlackboxTests(WebTest):
def test_election_404(self): def test_election_404(self):
response = self.client.get("/helios/elections/foobar") response = self.client.get("/helios/elections/foobar")
self.assertEquals(response.status_code, 404) self.assertStatusCode(response, 404)
def test_election_bad_trustee(self): def test_election_bad_trustee(self):
response = self.client.get("/helios/t/%s/foobar@bar.com/badsecret" % self.election.short_name) 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): def test_get_election_shortcut(self):
response = self.client.get("/helios/e/%s" % self.election.short_name, follow=True) response = self.client.get("/helios/e/%s" % self.election.short_name, follow=True)
...@@ -510,7 +486,7 @@ class ElectionBlackboxTests(WebTest): ...@@ -510,7 +486,7 @@ class ElectionBlackboxTests(WebTest):
new_election = models.Election.objects.get(uuid = self.election.uuid) new_election = models.Election.objects.get(uuid = self.election.uuid)
self.assertEquals(new_election.short_name, self.election.short_name + "-2") 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" "do the setup part of a whole election"
# a bogus call to set up the session # a bogus call to set up the session
...@@ -532,12 +508,14 @@ class ElectionBlackboxTests(WebTest): ...@@ -532,12 +508,14 @@ class ElectionBlackboxTests(WebTest):
} }
# override with the given # 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) 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 # 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 # helios is automatically added as a trustee
...@@ -577,7 +555,7 @@ class ElectionBlackboxTests(WebTest): ...@@ -577,7 +555,7 @@ class ElectionBlackboxTests(WebTest):
self.assertContains(response, '"uuid": "%s"' % single_voter.uuid) self.assertContains(response, '"uuid": "%s"' % single_voter.uuid)
response = self.client.get("/helios/elections/%s/voters/foobar" % election_id) response = self.client.get("/helios/elections/%s/voters/foobar" % election_id)
self.assertEquals(response.status_code, 404) self.assertStatusCode(response, 404)
# add questions # add questions
response = self.client.post("/helios/elections/%s/save_questions" % election_id, { response = self.client.post("/helios/elections/%s/save_questions" % election_id, {
...@@ -670,7 +648,7 @@ class ElectionBlackboxTests(WebTest): ...@@ -670,7 +648,7 @@ class ElectionBlackboxTests(WebTest):
# at this point an email should have gone out to the user # 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 # at position num_messages after, since that was the len() before we cast this ballot
email_message = mail.outbox[len(mail.outbox) - 1] 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 # check that we can get at that URL
if not need_login: if not need_login:
...@@ -690,7 +668,7 @@ class ElectionBlackboxTests(WebTest): ...@@ -690,7 +668,7 @@ class ElectionBlackboxTests(WebTest):
login_form['password'] = ' ' + password + ' ' login_form['password'] = ' ' + password + ' '
login_form.submit() login_form.submit()
response = self.app.get(url) response = self.app.get(url, auto_follow=True)
self.assertContains(response, ballot.hash) self.assertContains(response, ballot.hash)
self.assertContains(response, html_escape(encrypted_vote)) self.assertContains(response, html_escape(encrypted_vote))
...@@ -723,7 +701,7 @@ class ElectionBlackboxTests(WebTest): ...@@ -723,7 +701,7 @@ class ElectionBlackboxTests(WebTest):
# check that we can't get the tally yet # check that we can't get the tally yet
response = self.client.get("/helios/elections/%s/result" % election_id) response = self.client.get("/helios/elections/%s/result" % election_id)
self.assertEquals(response.status_code, 403) self.assertStatusCode(response, 403)
# release # release
response = self.client.post("/helios/elections/%s/release_result" % election_id, { response = self.client.post("/helios/elections/%s/release_result" % election_id, {
...@@ -788,7 +766,9 @@ class ElectionBlackboxTests(WebTest): ...@@ -788,7 +766,9 @@ class ElectionBlackboxTests(WebTest):
"private_p" : "False", "private_p" : "False",
'csrf_token': self.client.session['csrf_token']}) '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 # update eligiblity
response = self.client.post("/helios/elections/%s/voters/eligibility" % election_id, { response = self.client.post("/helios/elections/%s/voters/eligibility" % election_id, {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment