-
Notifications
You must be signed in to change notification settings - Fork 47
/
tests.py
204 lines (168 loc) · 6.76 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import re
from django.contrib.auth.models import User
from django.test import TestCase
from django.utils import translation
from django.core.urlresolvers import reverse
from django.core import mail
from apps.core.testing.mixins import TestModelStringMixin
class AccountsTestCase(TestCase):
fixtures = (
'auth.json',
'accounts.json'
)
class ProfileTests(TestModelStringMixin, AccountsTestCase):
def setUp(self):
translation.activate('en')
self.instances = User.objects.all()
def test_get_profile_update(self):
"""
An authorized GET request to the profile update form returns the form.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_get_profile_update_redirect(self):
"""
An unauthorized GET request to the profile update form gets
redirected to login.
"""
url = reverse('profile_update')
response = self.client.get(url)
self.assertRedirects(response, reverse('account_login') + '?next=' + url)
def test_post_profile_update(self):
"""
An authorized POST request to the profile update form updates the
user and redirects to home.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.post(url, {
'email': 'test@example.com',
'first_name': 'Albert',
'last_name': 'Admin',
'text': 'text',
'textarea': 'textarea',
})
self.assertRedirects(response, reverse('home'), target_status_code=302)
def test_post_profile_update_cancel(self):
"""
An authorized POST request to the profile update form updates with
cancel redirects to home.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.post(url, {
'email': 'test@example.com',
'first_name': 'Albert',
'last_name': 'Admin',
'cancel': 'cancel'
})
self.assertRedirects(response, reverse('home'), target_status_code=302)
def test_post_profile_update_cancel2(self):
"""
An authorized POST request to the profile update form updates with
cancel and the next field redirects to the given url.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.post(url, {
'email': 'test@example.com',
'first_name': 'Albert',
'last_name': 'Admin',
'cancel': 'cancel',
'next': reverse('account_change_password')
})
self.assertRedirects(response, reverse('account_change_password'))
def test_post_profile_update_next(self):
"""
An authorized POST request to the profile update form with next field
updates the user and redirects to the given url.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.post(url, {
'email': 'test@example.com',
'first_name': 'Albert',
'last_name': 'Admin',
'text': 'text',
'textarea': 'textarea',
'next': reverse('account_change_password')
})
self.assertRedirects(response, reverse('account_change_password'))
def test_post_profile_update_next2(self):
"""
An authorized POST request to the profile update form with next
field set to profile_update updates the user and redirects to home.
"""
self.client.login(username='user', password='user')
url = reverse('profile_update')
response = self.client.post(url, {
'email': 'test@example.com',
'first_name': 'Albert',
'last_name': 'Admin',
'text': 'text',
'textarea': 'textarea',
'next': reverse('profile_update')
})
self.assertRedirects(response, reverse('home'), target_status_code=302)
class AdditionalFieldTests(TestModelStringMixin, AccountsTestCase):
def setUp(self):
translation.activate('en')
self.instances = User.objects.all()
class PasswordTests(AccountsTestCase):
def setUp(self):
translation.activate('en')
self.instances = User.objects.all()
def test_password_change_get(self):
"""
An authorized GET request to the password change form returns the form.
"""
self.client.login(username='user', password='user')
url = reverse('account_change_password')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_password_change_post(self):
"""
An authorized POST request to the password change form updates the
password and redirects to home.
"""
self.client.login(username='user', password='user')
url = reverse('account_change_password')
response = self.client.post(url, {
'old_password': 'user',
'new_password1': 'resu',
'new_password2': 'resu',
})
self.assertEqual(response.status_code, 200)
def test_password_reset_get(self):
"""
A GET request to the password reset form returns the form.
"""
url = reverse('account_reset_password')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_password_reset_post_invalid(self):
"""
A POST request to the password reset form with an invalid mail address
sends no mail.
"""
url = reverse('account_reset_password')
response = self.client.post(url, {'email': 'wrong@example.com'})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 0)
def test_password_reset_post_valid(self):
"""
A POST request to the password reset form with an invalid mail address
sends a mail with a correct link.
"""
url = reverse('account_reset_password')
response = self.client.post(url, {'email': 'user@example.com'})
self.assertRedirects(response, reverse('account_reset_password_done'))
self.assertEqual(len(mail.outbox), 1)
# get the link from the mail
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', mail.outbox[0].body)
self.assertEqual(len(urls), 1)
# get the password_reset page
response = self.client.get(urls[0])
self.assertEqual(response.status_code, 200)