bgneal@447: """ bgneal@447: Tests for the views in the polls application. bgneal@447: bgneal@447: """ bgneal@447: import datetime bgneal@447: bgneal@447: from django.contrib.auth.models import User bgneal@447: from django.test import TestCase bgneal@447: from django.core.urlresolvers import reverse bgneal@447: from polls.models import Poll, Choice bgneal@447: bgneal@447: bgneal@447: class PollHomeTest(TestCase): bgneal@447: bgneal@447: def setUp(self): bgneal@447: now = datetime.datetime.now() bgneal@447: Poll.objects.all().delete() bgneal@447: bgneal@447: past = now - datetime.timedelta(7) bgneal@447: future = now + datetime.timedelta(7) bgneal@447: bgneal@447: Poll.objects.create(start_date=past, bgneal@447: end_date=past, bgneal@447: is_enabled=True, bgneal@447: question='Old Poll') bgneal@447: bgneal@447: active = Poll.objects.create(start_date=past, bgneal@447: end_date=future, bgneal@447: is_enabled=True, bgneal@447: question='Active Poll') bgneal@447: bgneal@447: Poll.objects.create(start_date=future, bgneal@447: end_date=future, bgneal@447: is_enabled=True, bgneal@447: question='Future Poll') bgneal@447: bgneal@447: Poll.objects.create(start_date=past, bgneal@447: end_date=future, bgneal@447: is_enabled=False, bgneal@447: question='Disabled Poll') bgneal@447: bgneal@447: choice1 = Choice.objects.create(poll=active, choice="Yes") bgneal@447: choice2 = Choice.objects.create(poll=active, choice="Yes") bgneal@447: bgneal@447: self.username = 'test_user' bgneal@447: self.pw = 'password' bgneal@447: self.user = User.objects.create_user(self.username, '', self.pw) bgneal@447: self.user.save() bgneal@447: self.assertTrue(self.client.login(username=self.username, bgneal@447: password=self.pw)) bgneal@447: bgneal@447: def tearDown(self): bgneal@447: self.client.logout() bgneal@447: bgneal@447: def testHome(self): bgneal@447: response = self.client.get(reverse('polls-main')) bgneal@447: bgneal@447: old = response.context['old_polls'] bgneal@447: self.assertEqual(len(old), 1) bgneal@447: if old: bgneal@447: self.assertEqual(old[0].question, 'Old Poll') bgneal@447: bgneal@447: current = response.context['current_polls'] bgneal@447: self.assertEqual(len(current), 1) bgneal@447: if current: bgneal@447: self.assertEqual(current[0].question, 'Active Poll') bgneal@447: bgneal@447: self.assertNotContains(response, 'Future Poll') bgneal@447: self.assertNotContains(response, 'Disabled Poll') bgneal@447: bgneal@447: def testFuture(self): bgneal@447: poll = Poll.objects.get(question='Future Poll') bgneal@447: response = self.client.get(reverse('polls-detail', kwargs={'poll_id': bgneal@447: poll.id})) bgneal@447: self.assertEqual(response.status_code, 404) bgneal@447: bgneal@447: def testDisabled(self): bgneal@447: poll = Poll.objects.get(question='Disabled Poll') bgneal@447: response = self.client.get(reverse('polls-detail', kwargs={'poll_id': bgneal@447: poll.id})) bgneal@447: self.assertEqual(response.status_code, 404) bgneal@447: bgneal@447: def testOld(self): bgneal@447: poll = Poll.objects.get(question='Old Poll') bgneal@447: response = self.client.get(reverse('polls-detail', kwargs={'poll_id': bgneal@447: poll.id})) bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: def testActive(self): bgneal@447: poll = Poll.objects.get(question='Active Poll') bgneal@447: response = self.client.get(reverse('polls-detail', kwargs={'poll_id': bgneal@447: poll.id})) bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: def testVoteOld(self): bgneal@447: poll = Poll.objects.get(question='Old Poll') bgneal@447: response = self.client.get(reverse('polls-vote', kwargs={'poll_id': bgneal@447: poll.id}), follow=True) bgneal@447: bgneal@447: self.assertEqual(len(response.redirect_chain), 1) bgneal@447: if response.redirect_chain: bgneal@447: self.assertEqual(response.redirect_chain[0][0], bgneal@447: 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) bgneal@447: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@447: bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: def testVoteActive(self): bgneal@447: poll = Poll.objects.get(question='Active Poll') bgneal@447: response = self.client.get(reverse('polls-vote', kwargs={'poll_id': bgneal@447: poll.id}), follow=True) bgneal@447: bgneal@447: self.assertEqual(len(response.redirect_chain), 0) bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: def testVoteFuture(self): bgneal@447: poll = Poll.objects.get(question='Future Poll') bgneal@447: response = self.client.get(reverse('polls-vote', kwargs={'poll_id': bgneal@447: poll.id}), follow=True) bgneal@447: bgneal@447: self.assertEqual(len(response.redirect_chain), 1) bgneal@447: if response.redirect_chain: bgneal@447: self.assertEqual(response.redirect_chain[0][0], bgneal@447: 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) bgneal@447: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@447: bgneal@447: self.assertEqual(response.status_code, 404) bgneal@447: bgneal@447: def testVoteDisabled(self): bgneal@447: poll = Poll.objects.get(question='Disabled Poll') bgneal@447: response = self.client.get(reverse('polls-vote', kwargs={'poll_id': bgneal@447: poll.id}), follow=True) bgneal@447: bgneal@447: self.assertEqual(response.status_code, 404) bgneal@447: bgneal@447: def testVoteActivePost(self): bgneal@447: self._voteTest() bgneal@447: bgneal@447: def testDeleteVote(self): bgneal@447: poll = self._voteTest() bgneal@447: bgneal@447: response = self.client.post( bgneal@447: reverse('polls-delete_vote'), bgneal@447: {'poll_id': poll.id}, bgneal@447: follow=True) bgneal@447: bgneal@447: self.assertEqual(len(response.redirect_chain), 1) bgneal@447: if response.redirect_chain: bgneal@447: self.assertEqual(response.redirect_chain[0][0], bgneal@447: 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) bgneal@447: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@447: bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: choice = Choice.objects.get(pk=1) bgneal@447: self.assertEqual(choice.votes, 0) bgneal@447: self.assertTrue(self.user not in choice.voters.all()) bgneal@447: bgneal@447: def _voteTest(self): bgneal@447: poll = Poll.objects.get(question='Active Poll') bgneal@447: response = self.client.post( bgneal@447: reverse('polls-vote', kwargs={'poll_id': poll.id}), bgneal@447: {'choices': 1}, bgneal@447: follow=True) bgneal@447: bgneal@447: self.assertEqual(len(response.redirect_chain), 1) bgneal@447: if response.redirect_chain: bgneal@447: self.assertEqual(response.redirect_chain[0][0], bgneal@447: 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) bgneal@447: self.assertEqual(response.redirect_chain[0][1], 302) bgneal@447: bgneal@447: self.assertEqual(response.status_code, 200) bgneal@447: bgneal@447: choice = Choice.objects.get(pk=1) bgneal@447: self.assertEqual(choice.votes, 1) bgneal@447: self.assertTrue(self.user in choice.voters.all()) bgneal@447: bgneal@447: return poll