|
1
|
|
|
from django.contrib.auth.models import User |
|
|
|
|
|
|
2
|
|
|
from django.test import TestCase |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class AccountTestCase(TestCase): |
|
|
|
|
|
|
6
|
|
|
"""Shared methods for account tests.""" |
|
7
|
|
|
def _create_new_account( |
|
8
|
|
|
self, username='test', email='[email protected]', password='test123'): |
|
9
|
|
|
"""Creates a new author account.""" |
|
10
|
|
|
response = self.client.post('/accounts/signup/', { |
|
11
|
|
|
'username': username, |
|
12
|
|
|
'email': email, |
|
13
|
|
|
'password1': password, |
|
14
|
|
|
'password2': password, |
|
15
|
|
|
}) |
|
16
|
|
|
|
|
17
|
|
|
return response |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class SignupTests(AccountTestCase): |
|
|
|
|
|
|
21
|
|
|
"""Tests for the signup page.""" |
|
22
|
|
|
def test_url(self): |
|
23
|
|
|
"""The URL should exist and return a 200.""" |
|
24
|
|
|
response = self.client.get('/accounts/signup/') |
|
25
|
|
|
|
|
26
|
|
|
self.assertEqual(response.status_code, 200) |
|
27
|
|
|
|
|
28
|
|
|
def test_create_user(self): |
|
29
|
|
|
"""Submitting the form should create a user.""" |
|
30
|
|
|
self.client.post('/accounts/signup/', { |
|
31
|
|
|
'username': 'test', |
|
32
|
|
|
'email': '[email protected]', |
|
33
|
|
|
'password1': 'test123', |
|
34
|
|
|
'password2': 'test123', |
|
35
|
|
|
}) |
|
36
|
|
|
|
|
37
|
|
|
self.assertIsNotNone(User.objects.get(username='test')) |
|
38
|
|
|
|
|
39
|
|
|
def test_email_required(self): |
|
40
|
|
|
"""Submitting the form should require an email address.""" |
|
41
|
|
|
response = self.client.post('/accounts/signup/', { |
|
42
|
|
|
'username': 'test', |
|
43
|
|
|
'password1': 'test123', |
|
44
|
|
|
'password2': 'test123', |
|
45
|
|
|
}) |
|
46
|
|
|
|
|
47
|
|
|
self.assertGreater(len(response.context['form'].errors['email']), 0) |
|
48
|
|
|
|
|
49
|
|
|
def test_create_confirm_redirect(self): |
|
50
|
|
|
"""Creating an account should redirect to the dashboard.""" |
|
51
|
|
|
response = self._create_new_account() |
|
52
|
|
|
|
|
53
|
|
|
self.assertRedirects(response, '/dashboard/') |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
class LoginTests(AccountTestCase): |
|
|
|
|
|
|
57
|
|
|
"""Tests for the login view.""" |
|
58
|
|
|
def test_login(self): |
|
59
|
|
|
"""Logging in should redirect to the dashboard.""" |
|
60
|
|
|
self._create_new_account() |
|
61
|
|
|
|
|
62
|
|
|
response = self.client.post('/accounts/login/', { |
|
63
|
|
|
'login': 'test', |
|
64
|
|
|
'password': 'test123', |
|
65
|
|
|
}) |
|
66
|
|
|
|
|
67
|
|
|
self.assertRedirects(response, '/dashboard/') |
|
68
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.