-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_views.py
More file actions
667 lines (576 loc) · 27.4 KB
/
Copy pathtest_views.py
File metadata and controls
667 lines (576 loc) · 27.4 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import json
from unittest.mock import patch, Mock
from django.conf import settings
from django.test import TestCase, Client as RequestClient, override_settings
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from thunderbird_accounts.authentication.models import User
from thunderbird_accounts.legal.models import LegalDocument, LegalDocumentResponse
from thunderbird_accounts.mail.models import Account
class HomeViewRedirectTestCase(TestCase):
"""Test redirect behavior for authenticated and unauthenticated users."""
def setUp(self):
self.client = RequestClient()
self.user = User.objects.create(username=f'test@{settings.PRIMARY_EMAIL_DOMAIN}', oidc_id='1234')
self.account = Account.objects.create(name=f'test@{settings.PRIMARY_EMAIL_DOMAIN}', user=self.user)
def test_unauthenticated_user_redirected_to_login_for_home(self):
"""Test that unauthenticated users are redirected to login when accessing home."""
response = self.client.get('/')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('login'))
def test_unauthenticated_user_redirected_to_login_for_non_public_routes(self):
"""Test that unauthenticated users are redirected to login for non-public routes."""
non_public_paths = ['/dashboard', '/mail', '/some-other-path']
for path in non_public_paths:
with self.subTest(path=path):
response = self.client.get(path)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('login'))
def test_unauthenticated_user_can_access_privacy_page(self):
"""Test that unauthenticated users can access the /privacy public route."""
response = self.client.get('/privacy')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'index.html')
def test_unauthenticated_user_can_access_terms_page(self):
"""Test that unauthenticated users can access the /terms public route."""
response = self.client.get('/terms')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'index.html')
def test_authenticated_user_can_access_home(self):
"""Test that authenticated users can access home without redirect."""
self.client.force_login(self.user)
# Mock OIDC session data to prevent SessionRefresh middleware from redirecting
session = self.client.session
session['oidc_id_token_expiration'] = 9999999999 # Far future timestamp
session.save()
with patch('thunderbird_accounts.mail.views.MailClient') as mock_mail_client:
mock_instance = Mock()
mock_instance.get_account.return_value = {
'description': 'Test User',
'secrets': [],
'emails': [f'test@{settings.PRIMARY_EMAIL_DOMAIN}'],
}
mock_mail_client.return_value = mock_instance
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'index.html')
def test_authenticated_user_can_access_any_path(self):
"""Test that authenticated users can access any path without redirect."""
self.client.force_login(self.user)
# Mock OIDC session data to prevent SessionRefresh middleware from redirecting
session = self.client.session
session['oidc_id_token_expiration'] = 9999999999 # Far future timestamp
session.save()
paths = ['/dashboard', '/mail', '/privacy', '/terms']
for path in paths:
with self.subTest(path=path):
with patch('thunderbird_accounts.mail.views.MailClient') as mock_mail_client:
mock_instance = Mock()
mock_instance.get_account.return_value = {
'description': 'Test User',
'secrets': [],
'emails': [f'test@{settings.PRIMARY_EMAIL_DOMAIN}'],
}
mock_mail_client.return_value = mock_instance
response = self.client.get(path)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'index.html')
class ZendeskContactFieldsTestCase(TestCase):
def setUp(self):
self.client = RequestClient()
@patch('thunderbird_accounts.core.views.ZendeskClient')
def test_contact_fields_success_filters_and_transforms(self, mock_client_cls):
instance = Mock()
mock_client_cls.return_value = instance
instance.get_ticket_fields.return_value = {
'success': True,
'data': {
'ticket_form': {'id': 123, 'name': 'Support'},
'ticket_fields': [
{
'id': 1,
'title': 'Subject',
'description': 'Subject field',
'required': True,
'type': 'subject',
'active': True,
'visible_in_portal': True,
'editable_in_portal': True,
},
{
'id': 2,
'title': 'Category',
'description': 'Choose a category',
'required': False,
'type': 'tagger',
'active': True,
'visible_in_portal': True,
'editable_in_portal': True,
'custom_field_options': [
{'id': 21, 'name': 'General', 'value': 'general', 'extra': 'ignored'},
{'id': 22, 'name': 'Billing', 'value': 'billing'},
],
},
{
# Should be filtered out (not editable in portal)
'id': 3,
'title': 'Internal',
'description': 'Internal only',
'required': False,
'type': 'text',
'active': True,
'visible_in_portal': True,
'editable_in_portal': False,
},
],
},
}
url = reverse('contact_fields')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
payload = json.loads(response.content.decode())
self.assertTrue(payload['success'])
self.assertEqual(payload['ticket_form'], {'id': 123})
self.assertIn('ticket_fields', payload)
# Only two fields should pass the filter
fields = payload['ticket_fields']
self.assertEqual(len(fields), 2)
# Field 1 minimal keys + values
f1 = next(f for f in fields if f['id'] == 1)
self.assertEqual(
{k: f1[k] for k in ['id', 'title', 'description', 'required', 'type']},
{'id': 1, 'title': 'Subject', 'description': 'Subject field', 'required': True, 'type': 'subject'},
)
self.assertNotIn('custom_field_options', f1)
# Field 2 options trimmed to id/name/value
f2 = next(f for f in fields if f['id'] == 2)
self.assertEqual(f2['title'], 'Category')
self.assertIn('custom_field_options', f2)
self.assertEqual(
f2['custom_field_options'],
[
{'id': 21, 'name': 'General', 'value': 'general'},
{'id': 22, 'name': 'Billing', 'value': 'billing'},
],
)
# Ensure client was called once
instance.get_ticket_fields.assert_called_once()
@patch('thunderbird_accounts.core.views.ZendeskClient')
def test_contact_fields_error_from_backend(self, mock_client_cls):
instance = Mock()
mock_client_cls.return_value = instance
instance.get_ticket_fields.return_value = {'success': False, 'error': 'Boom'}
url = reverse('contact_fields')
response = self.client.get(url)
self.assertEqual(response.status_code, 500)
payload = json.loads(response.content.decode())
self.assertEqual(payload, {'success': False, 'error': 'Boom'})
def test_contact_fields_method_not_allowed(self):
url = reverse('contact_fields')
response = self.client.post(url, data={})
self.assertEqual(response.status_code, 405)
class ZendeskContactSubmitTestCase(TestCase):
def setUp(self):
self.client = RequestClient()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@patch('thunderbird_accounts.core.utils.parse_user_agent_info')
@override_settings(
ZENDESK_FORM_ID='42',
ZENDESK_FORM_BROWSER_FIELD_ID='1001',
ZENDESK_FORM_OS_FIELD_ID='1002',
)
def test_contact_submit_success_with_attachments(self, mock_parse_ua, mock_client_cls):
mock_parse_ua.return_value = ('Firefox 120', 'macOS 14')
instance = Mock()
mock_client_cls.return_value = instance
instance.upload_file.return_value = {'success': True, 'upload_token': 'tok123', 'filename': 'test.txt'}
create_resp = Mock()
create_resp.ok = True
create_resp.json.return_value = {'request': {'id': 555}}
instance.create_ticket.return_value = create_resp
update_resp = Mock()
update_resp.ok = True
instance.update_ticket.return_value = update_resp
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
{'id': 13, 'title': 'Category', 'type': 'tagger', 'value': 'general', 'required': False},
],
}
uploaded = SimpleUploadedFile('test.txt', b'hi', content_type='text/plain')
response = self.client.post(
url,
data={'data': json.dumps(payload), 'attachments': uploaded},
HTTP_USER_AGENT='Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) Firefox/120.0',
)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode()), {'success': True})
# upload called for the file
instance.upload_file.assert_called_once()
# create called with expected payload
args, _kwargs = instance.create_ticket.call_args
sent_fields = args[0]
self.assertEqual(sent_fields['ticket_form_id'], 42)
self.assertEqual(sent_fields['name'], 'user@example.org') # Defaults to email when name not provided
self.assertEqual(sent_fields['email'], 'user@example.org')
self.assertEqual(sent_fields['subject'], 'Hello')
self.assertEqual(sent_fields['description'], 'Body')
self.assertEqual(sent_fields['attachments'], [{'token': 'tok123', 'filename': 'test.txt'}])
self.assertEqual(sent_fields['custom_fields'], [{'id': 13, 'value': 'general'}])
# update called with browser/os hidden fields
instance.update_ticket.assert_called_once()
# Ensure the IDs are ints and values are what we mocked from UA
update_payload = instance.update_ticket.call_args.args[1]
self.assertEqual(
update_payload,
{
'custom_fields': [
{'id': 1001, 'value': 'Firefox 120'},
{'id': 1002, 'value': 'macOS 14'},
]
},
)
@patch('thunderbird_accounts.core.views.ZendeskClient')
def test_contact_submit_validation_error_for_required_field(self, mock_client_cls):
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': '', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
response = self.client.post(url, data={'data': json.dumps(payload)})
self.assertEqual(response.status_code, 400)
body = json.loads(response.content.decode())
self.assertFalse(body['success'])
self.assertIn('Subject is required', body['error'])
# Ensure no calls were made to the client
mock_client_cls.assert_not_called()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@override_settings(ZENDESK_FORM_ID='42')
def test_contact_submit_upload_failure(self, mock_client_cls):
instance = Mock()
mock_client_cls.return_value = instance
instance.upload_file.return_value = {'success': False, 'error': 'Zendesk upload failed'}
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
uploaded = SimpleUploadedFile('test.txt', b'hi', content_type='text/plain')
response = self.client.post(url, data={'data': json.dumps(payload), 'attachments': uploaded})
self.assertEqual(response.status_code, 500)
body = json.loads(response.content.decode())
self.assertFalse(body['success'])
self.assertIn('Failed to upload file test.txt:', body['error'])
# create_ticket should not be called
instance.create_ticket.assert_not_called()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@override_settings(ZENDESK_FORM_ID='42')
def test_contact_submit_upload_exception(self, mock_client_cls):
instance = Mock()
mock_client_cls.return_value = instance
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
uploaded = SimpleUploadedFile('test.txt', b'hi', content_type='text/plain')
response = self.client.post(url, data={'data': json.dumps(payload), 'attachments': uploaded})
self.assertEqual(response.status_code, 500)
body = json.loads(response.content.decode())
self.assertFalse(body['success'])
self.assertIn('Failed to upload file test.txt', body['error'])
instance.create_ticket.assert_not_called()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@override_settings(ZENDESK_FORM_ID='42')
def test_contact_submit_create_ticket_failure(self, mock_client_cls):
instance = Mock()
mock_client_cls.return_value = instance
instance.upload_file.return_value = {'success': True, 'upload_token': 'tok123', 'filename': 'test.txt'}
create_resp = Mock()
create_resp.ok = False
instance.create_ticket.return_value = create_resp
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
uploaded = SimpleUploadedFile('test.txt', b'hi', content_type='text/plain')
response = self.client.post(url, data={'data': json.dumps(payload), 'attachments': uploaded})
self.assertEqual(response.status_code, 500)
self.assertEqual(json.loads(response.content.decode()), {'success': False})
instance.update_ticket.assert_not_called()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@patch('thunderbird_accounts.core.utils.parse_user_agent_info')
@override_settings(
ZENDESK_FORM_ID='42',
ZENDESK_FORM_BROWSER_FIELD_ID='1001',
ZENDESK_FORM_OS_FIELD_ID='1002',
)
def test_contact_submit_update_ticket_failure(self, mock_parse_ua, mock_client_cls):
mock_parse_ua.return_value = ('Firefox 120', 'macOS 14')
instance = Mock()
mock_client_cls.return_value = instance
instance.upload_file.return_value = {'success': True, 'upload_token': 'tok123', 'filename': 'test.txt'}
create_resp = Mock()
create_resp.ok = True
create_resp.json.return_value = {'request': {'id': 555}}
instance.create_ticket.return_value = create_resp
update_resp = Mock()
update_resp.ok = False
instance.update_ticket.return_value = update_resp
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
uploaded = SimpleUploadedFile('test.txt', b'hi', content_type='text/plain')
response = self.client.post(url, data={'data': json.dumps(payload), 'attachments': uploaded})
# Even though the update failed, at this point the ticket was created successfully
# So we were just unable to update the hidden fields, so we still return success to the user
instance.update_ticket.assert_called_once()
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content.decode()), {'success': True})
@patch('thunderbird_accounts.core.views.ZendeskClient')
@patch('thunderbird_accounts.core.utils.parse_user_agent_info')
@override_settings(
ZENDESK_FORM_ID='42',
ZENDESK_FORM_BROWSER_FIELD_ID='1001',
ZENDESK_FORM_OS_FIELD_ID='1002',
)
def test_contact_submit_name_defaults_to_email_when_not_provided(self, mock_parse_ua, mock_client_cls):
"""Test that when name is not provided in the payload, it defaults to the email address."""
mock_parse_ua.return_value = ('Firefox 120', 'macOS 14')
instance = Mock()
mock_client_cls.return_value = instance
create_resp = Mock()
create_resp.ok = True
create_resp.json.return_value = {'request': {'id': 555}}
instance.create_ticket.return_value = create_resp
update_resp = Mock()
update_resp.ok = True
instance.update_ticket.return_value = update_resp
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
response = self.client.post(
url,
data={'data': json.dumps(payload)},
HTTP_USER_AGENT='Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) Firefox/120.0',
)
self.assertEqual(response.status_code, 200)
# Verify the name field defaults to email when not provided in payload
instance.create_ticket.assert_called_once_with(
{
'ticket_form_id': 42,
'name': 'user@example.org',
'email': 'user@example.org',
'subject': 'Hello',
'description': 'Body',
'attachments': [],
'custom_fields': [],
}
)
instance.update_ticket.assert_called_once()
@patch('thunderbird_accounts.core.views.ZendeskClient')
@patch('thunderbird_accounts.core.utils.parse_user_agent_info')
@override_settings(
ZENDESK_FORM_ID='42',
ZENDESK_FORM_BROWSER_FIELD_ID='1001',
ZENDESK_FORM_OS_FIELD_ID='1002',
)
def test_contact_submit_uses_name_from_payload_when_provided(self, mock_parse_ua, mock_client_cls):
"""Test that when name is provided in the payload, it uses that name instead of defaulting to email."""
mock_parse_ua.return_value = ('Firefox 120', 'macOS 14')
instance = Mock()
mock_client_cls.return_value = instance
create_resp = Mock()
create_resp.ok = True
create_resp.json.return_value = {'request': {'id': 555}}
instance.create_ticket.return_value = create_resp
update_resp = Mock()
update_resp.ok = True
instance.update_ticket.return_value = update_resp
url = reverse('contact_submit')
payload = {
'email': 'user@example.org',
'name': 'John Doe',
'fields': [
{'id': 11, 'title': 'Subject', 'type': 'subject', 'value': 'Hello', 'required': True},
{'id': 12, 'title': 'Description', 'type': 'description', 'value': 'Body', 'required': True},
],
}
response = self.client.post(
url,
data={'data': json.dumps(payload)},
HTTP_USER_AGENT='Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) Firefox/120.0',
)
self.assertEqual(response.status_code, 200)
# Verify the name field uses the provided name from payload
args, _kwargs = instance.create_ticket.call_args
sent_fields = args[0]
self.assertEqual(sent_fields['name'], 'John Doe')
self.assertEqual(sent_fields['email'], 'user@example.org')
instance.update_ticket.assert_called_once()
class HomeViewNeedsTosAcceptanceTestCase(TestCase):
def setUp(self):
self.client = RequestClient()
self.user = User.objects.create(username=f'tostest@{settings.PRIMARY_EMAIL_DOMAIN}', oidc_id='tos-1')
self.account = Account.objects.create(name=f'tostest@{settings.PRIMARY_EMAIL_DOMAIN}', user=self.user)
# Delete all existing legal documents so that we can test the absence of documents as well
LegalDocument.objects.all().delete()
def _login_and_get_home(self):
self.client.force_login(self.user)
session = self.client.session
session['oidc_id_token_expiration'] = 9999999999
session.save()
with patch('thunderbird_accounts.mail.views.MailClient') as mock_mail_client:
mock_instance = Mock()
mock_instance.get_account.return_value = {
'description': 'Test User',
'secrets': [],
'emails': [f'tostest@{settings.PRIMARY_EMAIL_DOMAIN}'],
}
mock_mail_client.return_value = mock_instance
return self.client.get('/')
def test_needs_tos_acceptance_false_when_no_current_docs(self):
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_true_when_docs_not_accepted(self):
LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='2.0',
is_current=True,
content_path='tos/v2.0',
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertTrue(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_false_when_all_docs_accepted(self):
tos = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='2.0',
is_current=True,
content_path='tos/v2.0',
)
privacy = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.PRIVACY,
version='2.0',
is_current=True,
content_path='privacy/v2.0',
)
LegalDocumentResponse.objects.create(
user=self.user,
document=tos,
action=LegalDocumentResponse.Action.ACCEPTED,
)
LegalDocumentResponse.objects.create(
user=self.user,
document=privacy,
action=LegalDocumentResponse.Action.ACCEPTED,
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_true_when_partially_accepted(self):
tos = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='2.0',
is_current=True,
content_path='tos/v2.0',
)
LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.PRIVACY,
version='2.0',
is_current=True,
content_path='privacy/v2.0',
)
LegalDocumentResponse.objects.create(
user=self.user,
document=tos,
action=LegalDocumentResponse.Action.ACCEPTED,
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertTrue(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_true_when_only_declined(self):
tos = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='2.0',
is_current=True,
content_path='tos/v2.0',
)
LegalDocumentResponse.objects.create(
user=self.user,
document=tos,
action=LegalDocumentResponse.Action.DECLINED,
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertTrue(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_false_with_duplicate_acceptances(self):
"""Duplicate acceptance responses should not cause the check to fail."""
tos = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='2.0',
is_current=True,
content_path='tos/v2.0',
)
privacy = LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.PRIVACY,
version='2.0',
is_current=True,
content_path='privacy/v2.0',
)
# Force duplicate responses
LegalDocumentResponse.objects.create(
user=self.user,
document=tos,
action=LegalDocumentResponse.Action.ACCEPTED,
)
LegalDocumentResponse.objects.create(
user=self.user,
document=tos,
action=LegalDocumentResponse.Action.ACCEPTED,
)
LegalDocumentResponse.objects.create(
user=self.user,
document=privacy,
action=LegalDocumentResponse.Action.ACCEPTED,
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context['needs_tos_acceptance'])
def test_needs_tos_acceptance_ignores_non_current_docs(self):
LegalDocument.objects.create(
document_type=LegalDocument.DocumentType.TOS,
version='0.9',
is_current=False,
content_path='tos/v0.9',
)
response = self._login_and_get_home()
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context['needs_tos_acceptance'])