-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_views.py
315 lines (264 loc) · 11.6 KB
/
test_views.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from datetime import date, datetime
from unittest.mock import patch
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from django.urls import reverse
from rest_framework import status
from hackathon_site.tests import SetupUserMixin
from registration.forms import ApplicationForm
from registration.models import Application, Team, User
from registration.views import SignUpView
class SignUpViewTestCase(SetupUserMixin, TestCase):
"""
Tests for the sign up view
As with other templates, ideally this test would be performed with
Selenium. Instead, for simplicity, tests are limited to making sure
the templates render correctly.
"""
def setUp(self):
super().setUp()
self.view = reverse("registration:signup")
def test_signup_get(self):
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_displays_errors(self):
"""
Test that the template displays field errors
All field errors are rendered the same, so using the field
missing error is the simplest
"""
response = self.client.post(self.view, {})
self.assertContains(response, "This field is required", count=6)
def test_valid_submit_redirect(self):
data = {
"email": "[email protected]",
"first_name": "Foo",
"last_name": "Bar",
"password1": "abcdef456",
"password2": "abcdef456",
"g-recaptcha-response": "PASSED",
}
response = self.client.post(self.view, data)
self.assertRedirects(response, reverse("registration:signup_complete"))
redirected_response = response.client.get(response.url)
self.assertContains(redirected_response, "Activate your account")
def test_lowercases_username(self):
data = {
"email": "[email protected]",
"first_name": "Foo",
"last_name": "Bar",
"password1": "abcdef456",
"password2": "abcdef456",
"g-recaptcha-response": "PASSED",
}
self.client.post(self.view, data)
self.assertTrue(
User.objects.filter(
username="[email protected]", email="[email protected]"
).exists()
)
def test_redirects_user_to_dashboard_if_authenticated(self):
self._login()
response = self.client.get(self.view)
self.assertRedirects(response, reverse("event:dashboard"))
class SignUpClosedViewTestCase(TestCase):
def setUp(self):
self.view = reverse("registration:signup_closed")
@override_settings(
REGISTRATION_OPEN_DATE=datetime(2020, 1, 2, tzinfo=settings.TZ_INFO)
)
@override_settings(
REGISTRATION_CLOSE_DATE=datetime(2020, 1, 3, tzinfo=settings.TZ_INFO)
)
@patch("registration.views._now")
def test_not_open_yet(self, mock_now):
mock_now.return_value = datetime(2020, 1, 1, tzinfo=settings.TZ_INFO)
response = self.client.get(self.view)
self.assertContains(response, "Applications have not opened yet")
self.assertContains(
response, settings.REGISTRATION_OPEN_DATE.strftime("%B %-d, %Y")
)
@override_settings(
REGISTRATION_OPEN_DATE=datetime(2020, 1, 1, tzinfo=settings.TZ_INFO)
)
@override_settings(
REGISTRATION_CLOSE_DATE=datetime(2020, 1, 3, tzinfo=settings.TZ_INFO)
)
@patch("registration.views._now")
def test_registration_open(self, mock_now):
mock_now.return_value = datetime(2020, 1, 2, tzinfo=settings.TZ_INFO)
response = self.client.get(self.view)
self.assertContains(response, "Applications are open!")
self.assertContains(response, reverse("registration:signup"))
@override_settings(
REGISTRATION_OPEN_DATE=datetime(2020, 1, 1, tzinfo=settings.TZ_INFO)
)
@override_settings(
REGISTRATION_CLOSE_DATE=datetime(2020, 1, 2, tzinfo=settings.TZ_INFO)
)
@patch("registration.views._now")
def test_closed(self, mock_now):
mock_now.return_value = datetime(2020, 1, 3, tzinfo=settings.TZ_INFO)
response = self.client.get(self.view)
self.assertContains(response, "Applications have closed")
self.assertContains(
response, settings.REGISTRATION_CLOSE_DATE.strftime("%B %-d, %Y")
)
class ActivationViewTestCase(SetupUserMixin, TestCase):
"""
Test the activation view
"""
def setUp(self):
super().setUp()
self.view_name = "registration:activate"
self.activation_key = SignUpView().get_activation_key(self.user)
def _build_view(self, activation_key):
return reverse(self.view_name, kwargs={"activation_key": activation_key})
def test_invalid_key(self):
response = self.client.get(self._build_view("i-am-fake"))
self.assertContains(response, "Activation link is invalid")
self.assertContains(response, settings.CONTACT_EMAIL)
def test_account_already_activated(self):
self.user.is_active = True
self.user.save()
response = self.client.get(self._build_view(self.activation_key))
self.assertContains(response, "Account already activated")
self.assertContains(response, reverse("event:login"))
self.assertNotContains(response, settings.CONTACT_EMAIL)
@override_settings(ACCOUNT_ACTIVATION_DAYS=0)
def test_activation_link_expired(self):
response = self.client.get(self._build_view(self.activation_key))
self.assertContains(response, "Activation link has expired")
self.assertContains(response, settings.CONTACT_EMAIL)
def test_successful_activation(self):
self.user.is_active = False
self.user.save()
activation_key = SignUpView().get_activation_key(self.user)
response = self.client.get(self._build_view(activation_key))
self.assertRedirects(response, reverse("event:login"))
class ApplicationViewTestCase(SetupUserMixin, TestCase):
def setUp(self):
super().setUp()
self.view = reverse("registration:application")
self.data = {
"country": "Canada",
"tshirt_size": "L",
"birthday": date(2000, 7, 7),
"gender": "no-answer",
"ethnicity": "no-answer",
"phone_number": "1234567890",
"school": "UofT",
"study_level": "other",
"graduation_year": 2020,
"program": "Engineering",
"how_many_hackathons": "1",
"what_hackathon_experience": "there",
"why_participate": "foo",
"what_technical_experience": "yellow",
"referral_source": "my friend",
"conduct_agree": True,
"logistics_agree": True,
"email_agree": True,
"resume_sharing": True,
"hardware_preference": "pickup",
"resume": "uploads/resumes/my_resume.pdf",
}
self.team = Team.objects.create()
self.post_data = self.data.copy()
self.post_data["birthday"] = "2000-01-01" # The format used by the widget
self.post_data["resume"] = SimpleUploadedFile(
"my_resume.pdf", b"some content", content_type="application/pdf"
)
def test_requires_login(self):
response = self.client.get(self.view)
self.assertRedirects(response, f"{reverse('event:login')}?next={self.view}")
def test_displays_errors(self):
"""
Test that the template displays errors. All fields are rendered the same.
"""
self._login()
form = ApplicationForm(user=self.user)
num_required_fields = len(
[field for field in form.fields.values() if field.required]
)
response = self.client.post(self.view, {})
self.assertContains(response, "This field is required", num_required_fields)
def test_creates_application(self):
self._login()
response = self.client.post(self.view, data=self.post_data)
self.assertRedirects(response, reverse("event:dashboard"))
self.assertEqual(Application.objects.count(), 1)
self.assertEqual(Application.objects.first().user, self.user)
def test_redirects_if_has_application(self):
Application.objects.create(user=self.user, team=self.team, **self.data)
self._login()
response = self.client.get(self.view)
self.assertRedirects(response, reverse("event:dashboard"))
response = self.client.post(self.view, data=self.post_data)
self.assertRedirects(response, reverse("event:dashboard"))
@patch("registration.views.is_registration_open")
def test_redirects_if_registration_closed(self, mock_is_registration_open):
mock_is_registration_open.return_value = False
self._login()
response = self.client.get(self.view)
self.assertRedirects(response, reverse("event:dashboard"))
response = self.client.post(self.view, data=self.post_data)
self.assertRedirects(response, reverse("event:dashboard"))
class MiscRegistrationViewsTestCase(TestCase):
"""
Tests for the straggler registration views, that are just
defined in the urlconf with TemplateViews.
"""
def test_signup_complete(self):
response = self.client.get(reverse("registration:signup_complete"))
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Test that the default from email was passed in as a context variable
self.assertContains(response, settings.DEFAULT_FROM_EMAIL)
class LeaveTeamViewTestCase(SetupUserMixin, TestCase):
def setUp(self):
super().setUp()
self.view = reverse("registration:leave-team")
def test_requires_login(self):
response = self.client.get(self.view)
self.assertRedirects(response, f"{reverse('event:login')}?next={self.view}")
def test_bad_response_for_no_application(self):
self._login()
response = self.client.get(self.view)
self.assertContains(
response,
"You have not submitted an application.",
status_code=status.HTTP_400_BAD_REQUEST,
)
def test_leaves_and_deletes_empty_team(self):
self._login()
self._apply()
initial_team_id = self.user.application.team.id
response = self.client.get(self.view)
self.assertRedirects(response, reverse("event:dashboard"))
self.user.application.refresh_from_db()
self.assertNotEqual(self.user.application.team.id, initial_team_id)
self.assertEqual(Team.objects.count(), 1)
def test_leaves_and_does_not_delete_nonempty_team(self):
self._login()
application = self._apply()
new_user = User.objects.create_user(
username="[email protected]", password="hithere987"
)
self._apply_as_user(new_user, team=application.team)
initial_team_id = self.user.application.team.id
response = self.client.get(self.view)
self.assertRedirects(response, reverse("event:dashboard"))
self.user.application.refresh_from_db()
self.assertNotEqual(self.user.application.team.id, initial_team_id)
self.assertEqual(Team.objects.count(), 2)
@patch("registration.views.is_registration_open")
def test_registration_has_closed(self, mock_is_registration_open):
mock_is_registration_open.return_value = False
self._login()
response = self.client.get(self.view)
self.assertContains(
response,
"You cannot change teams after registration has closed.",
status_code=status.HTTP_400_BAD_REQUEST,
)