annotate contests/tests/view_tests.py @ 679:89b240fe9297

For Django 1.5.2: import json; django.utils.simplejson is deprecated.
author Brian Neal <bgneal@gmail.com>
date Thu, 15 Aug 2013 20:14:33 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@540 1 """
bgneal@540 2 View tests for the contests application.
bgneal@540 3
bgneal@540 4 """
bgneal@540 5 import datetime
bgneal@679 6 import json
bgneal@679 7
bgneal@540 8 from django.test import TestCase
bgneal@540 9 from django.contrib.auth.models import User
bgneal@540 10 from django.core.urlresolvers import reverse
bgneal@540 11
bgneal@540 12 from contests.models import Contest
bgneal@540 13
bgneal@540 14
bgneal@540 15 class NoConstestsTestCase(TestCase):
bgneal@540 16
bgneal@540 17 def test_no_contests(self):
bgneal@540 18 response = self.client.get(reverse('contests-index'))
bgneal@540 19 self.assertEqual(response.status_code, 200)
bgneal@540 20
bgneal@540 21 url = reverse('contests-contest', kwargs={'slug': 'test'})
bgneal@540 22 response = self.client.get(url)
bgneal@540 23 self.assertEqual(response.status_code, 404)
bgneal@540 24
bgneal@540 25
bgneal@540 26 class ConstestsTestCase(TestCase):
bgneal@540 27
bgneal@540 28 def setUp(self):
bgneal@540 29 now = datetime.datetime.now()
bgneal@540 30 start = now - datetime.timedelta(days=7)
bgneal@540 31 end = start - datetime.timedelta(days=3)
bgneal@540 32
bgneal@540 33 user = User.objects.create_user('test_user', '', 'password')
bgneal@540 34 user.save()
bgneal@540 35
bgneal@540 36 c = Contest(title='test',
bgneal@540 37 slug='test',
bgneal@540 38 description='test',
bgneal@540 39 is_public=True,
bgneal@540 40 creation_date=start,
bgneal@540 41 end_date=end,
bgneal@540 42 winner=user,
bgneal@540 43 win_date=end + datetime.timedelta(days=1))
bgneal@540 44 c.save()
bgneal@540 45 self.contest_id = c.id
bgneal@540 46
bgneal@540 47 def test_contests(self):
bgneal@540 48 response = self.client.get(reverse('contests-index'))
bgneal@540 49 self.assertEqual(response.status_code, 200)
bgneal@540 50
bgneal@540 51 url = reverse('contests-contest', kwargs={'slug': 'test'})
bgneal@540 52 response = self.client.get(url)
bgneal@540 53 self.assertEqual(response.status_code, 200)
bgneal@540 54
bgneal@540 55
bgneal@540 56 class ContestEntryTestCase(TestCase):
bgneal@540 57
bgneal@540 58 def setUp(self):
bgneal@540 59 self.username = 'test_user'
bgneal@540 60 self.pw = 'password'
bgneal@540 61 self.user = User.objects.create_user(self.username, '', self.pw)
bgneal@540 62 self.user.save()
bgneal@540 63 self.assertTrue(self.client.login(username=self.username,
bgneal@540 64 password=self.pw))
bgneal@540 65
bgneal@540 66 now = datetime.datetime.now()
bgneal@540 67 start = now - datetime.timedelta(days=7)
bgneal@540 68 end = now + datetime.timedelta(days=3)
bgneal@540 69
bgneal@540 70 c = Contest(title='test',
bgneal@540 71 slug='test',
bgneal@540 72 description='test',
bgneal@540 73 is_public=True,
bgneal@540 74 creation_date=start,
bgneal@540 75 end_date=end)
bgneal@540 76 c.save()
bgneal@540 77 self.contest_id = c.id
bgneal@540 78
bgneal@540 79 def test_entry_toggle(self):
bgneal@540 80 response = self.client.post(reverse('contests-enter'),
bgneal@540 81 {'contest_id': self.contest_id},
bgneal@540 82 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
bgneal@540 83 self.assertEqual(response.status_code, 200)
bgneal@540 84
bgneal@679 85 c = json.loads(response.content)
bgneal@679 86 self.assertTrue(c['entered'])
bgneal@540 87
bgneal@540 88 contest = Contest.objects.get(pk=self.contest_id)
bgneal@540 89 self.assertTrue(self.user in contest.contestants.all())
bgneal@540 90
bgneal@540 91 response = self.client.post(reverse('contests-enter'),
bgneal@540 92 {'contest_id': self.contest_id},
bgneal@540 93 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
bgneal@540 94 self.assertEqual(response.status_code, 200)
bgneal@540 95
bgneal@679 96 c = json.loads(response.content)
bgneal@679 97 self.failIf(c['entered'])
bgneal@540 98
bgneal@540 99 contest = Contest.objects.get(pk=self.contest_id)
bgneal@540 100 self.failIf(self.user in contest.contestants.all())
bgneal@540 101
bgneal@540 102
bgneal@540 103 class NoPublicConstestsTestCase(TestCase):
bgneal@540 104
bgneal@540 105 def setUp(self):
bgneal@540 106 now = datetime.datetime.now()
bgneal@540 107 start = now - datetime.timedelta(days=7)
bgneal@540 108 end = start - datetime.timedelta(days=3)
bgneal@540 109
bgneal@540 110 c = Contest(title='test',
bgneal@540 111 slug='test',
bgneal@540 112 description='test',
bgneal@540 113 is_public=False,
bgneal@540 114 creation_date=start,
bgneal@540 115 end_date=end)
bgneal@540 116 c.save()
bgneal@540 117
bgneal@540 118 def test_contests(self):
bgneal@540 119 response = self.client.get(reverse('contests-index'))
bgneal@540 120 self.assertEqual(response.status_code, 200)
bgneal@540 121
bgneal@540 122 url = reverse('contests-contest', kwargs={'slug': 'test'})
bgneal@540 123 response = self.client.get(url)
bgneal@540 124 self.assertEqual(response.status_code, 404)