bgneal@540: """ bgneal@540: View tests for the contests application. bgneal@540: bgneal@540: """ bgneal@540: import datetime bgneal@679: import json bgneal@679: bgneal@540: from django.test import TestCase bgneal@540: from django.contrib.auth.models import User bgneal@540: from django.core.urlresolvers import reverse bgneal@540: bgneal@540: from contests.models import Contest bgneal@540: bgneal@540: bgneal@540: class NoConstestsTestCase(TestCase): bgneal@540: bgneal@540: def test_no_contests(self): bgneal@540: response = self.client.get(reverse('contests-index')) bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@540: url = reverse('contests-contest', kwargs={'slug': 'test'}) bgneal@540: response = self.client.get(url) bgneal@540: self.assertEqual(response.status_code, 404) bgneal@540: bgneal@540: bgneal@540: class ConstestsTestCase(TestCase): bgneal@540: bgneal@540: def setUp(self): bgneal@540: now = datetime.datetime.now() bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@540: user = User.objects.create_user('test_user', '', 'password') bgneal@540: user.save() bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end, bgneal@540: winner=user, bgneal@540: win_date=end + datetime.timedelta(days=1)) bgneal@540: c.save() bgneal@540: self.contest_id = c.id bgneal@540: bgneal@540: def test_contests(self): bgneal@540: response = self.client.get(reverse('contests-index')) bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@540: url = reverse('contests-contest', kwargs={'slug': 'test'}) bgneal@540: response = self.client.get(url) bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@540: bgneal@540: class ContestEntryTestCase(TestCase): bgneal@540: bgneal@540: def setUp(self): bgneal@540: self.username = 'test_user' bgneal@540: self.pw = 'password' bgneal@540: self.user = User.objects.create_user(self.username, '', self.pw) bgneal@540: self.user.save() bgneal@540: self.assertTrue(self.client.login(username=self.username, bgneal@540: password=self.pw)) bgneal@540: bgneal@540: now = datetime.datetime.now() bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = now + datetime.timedelta(days=3) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: c.save() bgneal@540: self.contest_id = c.id bgneal@540: bgneal@540: def test_entry_toggle(self): bgneal@540: response = self.client.post(reverse('contests-enter'), bgneal@540: {'contest_id': self.contest_id}, bgneal@540: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@679: c = json.loads(response.content) bgneal@679: self.assertTrue(c['entered']) bgneal@540: bgneal@540: contest = Contest.objects.get(pk=self.contest_id) bgneal@540: self.assertTrue(self.user in contest.contestants.all()) bgneal@540: bgneal@540: response = self.client.post(reverse('contests-enter'), bgneal@540: {'contest_id': self.contest_id}, bgneal@540: HTTP_X_REQUESTED_WITH='XMLHttpRequest') bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@679: c = json.loads(response.content) bgneal@744: self.assertFalse(c['entered']) bgneal@540: bgneal@540: contest = Contest.objects.get(pk=self.contest_id) bgneal@744: self.assertFalse(self.user in contest.contestants.all()) bgneal@540: bgneal@540: bgneal@540: class NoPublicConstestsTestCase(TestCase): bgneal@540: bgneal@540: def setUp(self): bgneal@540: now = datetime.datetime.now() bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=False, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: c.save() bgneal@540: bgneal@540: def test_contests(self): bgneal@540: response = self.client.get(reverse('contests-index')) bgneal@540: self.assertEqual(response.status_code, 200) bgneal@540: bgneal@540: url = reverse('contests-contest', kwargs={'slug': 'test'}) bgneal@540: response = self.client.get(url) bgneal@540: self.assertEqual(response.status_code, 404)