|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
from polls.models import Vote, Poll, Option
|
|
|
|
|
|
|
|
|
|
|
|
class PollDetailsTests(
|
|
|
|
TestCase,
|
|
|
|
):
|
|
|
|
def fresh_user_logged_in(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
test_user = User.objects.create_user(
|
|
|
|
"test",
|
|
|
|
password="test",
|
|
|
|
)
|
|
|
|
self.client.force_login(
|
|
|
|
test_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
return test_user
|
|
|
|
|
|
|
|
def create_poll_with_questions(
|
|
|
|
self,
|
|
|
|
questions=3,
|
|
|
|
):
|
|
|
|
poll = Poll.objects.create(
|
|
|
|
question="Testing Question",
|
|
|
|
)
|
|
|
|
for i in range(questions):
|
|
|
|
Option.objects.create(
|
|
|
|
poll=poll,
|
|
|
|
text=f"Option #{i}",
|
|
|
|
)
|
|
|
|
return poll
|
|
|
|
|
|
|
|
def test_shows_options_if_not_voted(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
If test user has not yet voted, options should be shown
|
|
|
|
"""
|
|
|
|
self.fresh_user_logged_in()
|
|
|
|
poll = self.create_poll_with_questions()
|
|
|
|
|
|
|
|
response = self.client.get(
|
|
|
|
reverse(
|
|
|
|
"poll-details",
|
|
|
|
kwargs={
|
|
|
|
"poll_id": poll.pk,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
response.status_code,
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
"Select from one of the following options:",
|
|
|
|
)
|
|
|
|
options = Option.objects.filter(
|
|
|
|
poll=poll,
|
|
|
|
)
|
|
|
|
self.assertQuerySetEqual(
|
|
|
|
response.context["options"],
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_shows_results_if_voted(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
If test user has already voted, results should be shown
|
|
|
|
"""
|
|
|
|
test_user = self.fresh_user_logged_in()
|
|
|
|
poll = self.create_poll_with_questions()
|
|
|
|
options = Option.objects.filter(
|
|
|
|
poll=poll,
|
|
|
|
)
|
|
|
|
|
|
|
|
Vote.objects.create(
|
|
|
|
poll=poll,
|
|
|
|
option=options[0],
|
|
|
|
user=test_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.get(
|
|
|
|
reverse(
|
|
|
|
"poll-details",
|
|
|
|
kwargs={
|
|
|
|
"poll_id": poll.pk,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
response.status_code,
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
self.assertNotContains(
|
|
|
|
response,
|
|
|
|
"Select from one of the following options:",
|
|
|
|
)
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
|
|
|
"Review the poll results below",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_re_voting_doesnt_change_votes(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
If test user has already voted, voting again should not change anything
|
|
|
|
"""
|
|
|
|
test_user = self.fresh_user_logged_in()
|
|
|
|
poll = self.create_poll_with_questions()
|
|
|
|
options = Option.objects.filter(
|
|
|
|
poll=poll,
|
|
|
|
)
|
|
|
|
|
|
|
|
Vote.objects.create(
|
|
|
|
poll=poll,
|
|
|
|
option=options[0],
|
|
|
|
user=test_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
Vote.objects.count(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
response = self.client.post(
|
|
|
|
reverse(
|
|
|
|
"cast-vote",
|
|
|
|
kwargs={
|
|
|
|
"poll_id": poll.pk,
|
|
|
|
"option_id": options[1].pk,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
response.status_code,
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Vote.objects.count(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unauthenticated_user_cant_vote(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
If an unauthenticated user tries to vote it should fail
|
|
|
|
"""
|
|
|
|
poll = self.create_poll_with_questions()
|
|
|
|
options = Option.objects.filter(
|
|
|
|
poll=poll,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
Vote.objects.count(),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
reverse(
|
|
|
|
"cast-vote",
|
|
|
|
kwargs={
|
|
|
|
"poll_id": poll.pk,
|
|
|
|
"option_id": options[0].pk,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
response.status_code,
|
|
|
|
200,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Vote.objects.count(),
|
|
|
|
0,
|
|
|
|
)
|