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