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@796
|
42 win_date=end + datetime.timedelta(days=1),
|
bgneal@796
|
43 num_winners=1)
|
bgneal@540
|
44 c.save()
|
bgneal@796
|
45 c.contestants.add(user)
|
bgneal@796
|
46 c.winners.add(user)
|
bgneal@540
|
47 self.contest_id = c.id
|
bgneal@540
|
48
|
bgneal@540
|
49 def test_contests(self):
|
bgneal@540
|
50 response = self.client.get(reverse('contests-index'))
|
bgneal@540
|
51 self.assertEqual(response.status_code, 200)
|
bgneal@540
|
52
|
bgneal@540
|
53 url = reverse('contests-contest', kwargs={'slug': 'test'})
|
bgneal@540
|
54 response = self.client.get(url)
|
bgneal@540
|
55 self.assertEqual(response.status_code, 200)
|
bgneal@540
|
56
|
bgneal@540
|
57
|
bgneal@540
|
58 class ContestEntryTestCase(TestCase):
|
bgneal@540
|
59
|
bgneal@540
|
60 def setUp(self):
|
bgneal@540
|
61 self.username = 'test_user'
|
bgneal@540
|
62 self.pw = 'password'
|
bgneal@540
|
63 self.user = User.objects.create_user(self.username, '', self.pw)
|
bgneal@540
|
64 self.user.save()
|
bgneal@540
|
65 self.assertTrue(self.client.login(username=self.username,
|
bgneal@540
|
66 password=self.pw))
|
bgneal@540
|
67
|
bgneal@540
|
68 now = datetime.datetime.now()
|
bgneal@540
|
69 start = now - datetime.timedelta(days=7)
|
bgneal@540
|
70 end = now + datetime.timedelta(days=3)
|
bgneal@540
|
71
|
bgneal@540
|
72 c = Contest(title='test',
|
bgneal@540
|
73 slug='test',
|
bgneal@540
|
74 description='test',
|
bgneal@540
|
75 is_public=True,
|
bgneal@540
|
76 creation_date=start,
|
bgneal@796
|
77 end_date=end,
|
bgneal@796
|
78 num_winners=1)
|
bgneal@540
|
79 c.save()
|
bgneal@540
|
80 self.contest_id = c.id
|
bgneal@540
|
81
|
bgneal@540
|
82 def test_entry_toggle(self):
|
bgneal@540
|
83 response = self.client.post(reverse('contests-enter'),
|
bgneal@540
|
84 {'contest_id': self.contest_id},
|
bgneal@540
|
85 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
bgneal@540
|
86 self.assertEqual(response.status_code, 200)
|
bgneal@540
|
87
|
bgneal@679
|
88 c = json.loads(response.content)
|
bgneal@679
|
89 self.assertTrue(c['entered'])
|
bgneal@540
|
90
|
bgneal@540
|
91 contest = Contest.objects.get(pk=self.contest_id)
|
bgneal@540
|
92 self.assertTrue(self.user in contest.contestants.all())
|
bgneal@540
|
93
|
bgneal@540
|
94 response = self.client.post(reverse('contests-enter'),
|
bgneal@540
|
95 {'contest_id': self.contest_id},
|
bgneal@540
|
96 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
bgneal@540
|
97 self.assertEqual(response.status_code, 200)
|
bgneal@540
|
98
|
bgneal@679
|
99 c = json.loads(response.content)
|
bgneal@744
|
100 self.assertFalse(c['entered'])
|
bgneal@540
|
101
|
bgneal@540
|
102 contest = Contest.objects.get(pk=self.contest_id)
|
bgneal@744
|
103 self.assertFalse(self.user in contest.contestants.all())
|
bgneal@540
|
104
|
bgneal@540
|
105
|
bgneal@540
|
106 class NoPublicConstestsTestCase(TestCase):
|
bgneal@540
|
107
|
bgneal@540
|
108 def setUp(self):
|
bgneal@540
|
109 now = datetime.datetime.now()
|
bgneal@540
|
110 start = now - datetime.timedelta(days=7)
|
bgneal@540
|
111 end = start - datetime.timedelta(days=3)
|
bgneal@540
|
112
|
bgneal@540
|
113 c = Contest(title='test',
|
bgneal@540
|
114 slug='test',
|
bgneal@540
|
115 description='test',
|
bgneal@540
|
116 is_public=False,
|
bgneal@540
|
117 creation_date=start,
|
bgneal@540
|
118 end_date=end)
|
bgneal@540
|
119 c.save()
|
bgneal@540
|
120
|
bgneal@540
|
121 def test_contests(self):
|
bgneal@540
|
122 response = self.client.get(reverse('contests-index'))
|
bgneal@540
|
123 self.assertEqual(response.status_code, 200)
|
bgneal@540
|
124
|
bgneal@540
|
125 url = reverse('contests-contest', kwargs={'slug': 'test'})
|
bgneal@540
|
126 response = self.client.get(url)
|
bgneal@540
|
127 self.assertEqual(response.status_code, 404)
|