|
| 1 | +from django.contrib.auth import authenticate |
| 2 | +from django.contrib.auth.models import User |
1 | 3 | from django.test import TestCase |
2 | | -import datetime |
3 | | - |
4 | 4 | from django.utils import timezone |
5 | | -from django.urls import reverse |
6 | | - |
7 | | -from .models import Question |
8 | | - |
9 | | - |
10 | | -class QuestionModelTest(TestCase): |
11 | | - def test_was_published_recently_with_future_question(self): |
12 | | - time = timezone.now() + datetime.timedelta(days=30) |
13 | | - future_question = Question(pub_date=time) |
14 | | - self.assertIs(future_question.was_published_recently(), False) |
15 | 5 |
|
16 | | - """ |
17 | | - was_published_recently() returns False for questions whose pub_date |
18 | | - is older than 1 day. |
19 | | - """ |
| 6 | +from .models import Poll, Choice, Vote |
20 | 7 |
|
21 | 8 |
|
22 | | -def test_was_published_recently_with_old_question(self): |
23 | | - """ |
24 | | - was_published_recently() returns False for questions whose pub_date |
25 | | - is older than 1 day. |
26 | | - """ |
27 | | - time = timezone.now() - datetime.timedelta(days=1, seconds=1) |
28 | | - old_question = Question(pub_date=time) |
29 | | - self.assertIs(old_question.was_published_recently(), False) |
| 9 | +class PollModelTest(TestCase): |
| 10 | + def test_user_can_vote(self): |
| 11 | + user = User.objects.create_user('john') |
| 12 | + poll = Poll.objects.create(owner=user) |
| 13 | + self.assertTrue(poll.user_can_vote(user)) |
30 | 14 |
|
| 15 | + choice = poll.choice_set.create(choice_text='pizza') |
| 16 | + Vote.objects.create(user=user, poll=poll, choice=choice) |
| 17 | + self.assertFalse(poll.user_can_vote(user)) |
31 | 18 |
|
32 | | -def test_was_published_recently_with_recent_question(self): |
33 | | - """ |
34 | | - was_published_recently() returns True for questions whose pub_date |
35 | | - is within the last day. |
36 | | - """ |
37 | | - time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) |
38 | | - recent_question = Question(pub_date=time) |
39 | | - self.assertIs(recent_question.was_published_recently(), True) |
40 | 19 |
|
| 20 | +class PollViewTest(TestCase): |
| 21 | + def test_home(self): |
| 22 | + response = self.client.get('/') |
| 23 | + self.assertEqual(response.status_code, 200) |
41 | 24 |
|
| 25 | + def test_login(self): |
| 26 | + User.objects.create_user(username='john', password='rambo') |
| 27 | + response = self.client.post('/accounts/login/', {'username': 'john', 'password': 'rambo'}) |
| 28 | + self.assertRedirects(response, '/') |
42 | 29 |
|
43 | | -# Create your tests here. |
| 30 | + def test_register(self): |
| 31 | + # print(response.context['messages']) |
| 32 | + response = self.client.post('/accounts/register/', {'username': 'johny', |
| 33 | + 'password1': 'rambo', |
| 34 | + 'password2': 'rambo', |
| 35 | + 'email': 'johny.rambo@usarmy.gov' |
| 36 | + }) |
| 37 | + self.assertRedirects(response, '/accounts/login/') |
| 38 | + # assert that user got actually created in the backend |
| 39 | + self.assertIsNotNone(authenticate(username='johny', password='rambo')) |
0 commit comments