-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathviews_test.py
909 lines (829 loc) · 36 KB
/
views_test.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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
"""
Test end to end django views.
"""
import json
from unittest.mock import patch, Mock
from urllib.parse import urlencode
import ddt
from django.db.models.signals import post_save
from django.urls import reverse
from django.test import override_settings
from factory import Iterator
from factory.django import mute_signals
from factory.fuzzy import FuzzyText
from rest_framework import status
from rolepermissions.permissions import available_perm_status
from wagtail.images.models import Image
from wagtail.images.tests.utils import get_test_image_file
from cms.factories import (
FacultyFactory,
InfoLinksFactory,
ProgramCourseFactory,
SemesterDateFactory,
)
from cms.models import HomePage, ProgramPage
from cms.serializers import ProgramPageSerializer
from courses.factories import ProgramFactory, CourseFactory
from ecommerce.factories import CouponFactory
from micromasters.serializers import serialize_maybe_user
from micromasters.factories import UserSocialAuthFactory
from profiles.factories import ProfileFactory, SocialProfileFactory
from roles.models import Role
from search.base import MockedESTestCase
from ui.url_utils import DASHBOARD_URL
class ViewsTests(MockedESTestCase):
"""
Test that the views work as expected.
"""
def create_and_login_user(self):
"""
Create and login a user
"""
profile = SocialProfileFactory.create(
agreed_to_terms_of_service=True,
filled_out=True,
)
UserSocialAuthFactory.create(user=profile.user, provider='not_edx')
self.client.force_login(profile.user)
return profile
@ddt.ddt
class TestHomePage(ViewsTests):
"""
Tests for home page
"""
def test_program_liveness(self):
"""Verify only 'live' program visible on homepage"""
program_live_true = ProgramFactory.create(live=True)
program_live_false = ProgramFactory.create(live=False)
response = self.client.get('/')
self.assertContains(
response,
program_live_true.description,
status_code=200
)
self.assertNotContains(
response,
program_live_false.description,
status_code=200
)
def test_program_link(self):
"""Verify that program links are present in home page if ProgramPage is set"""
program = ProgramFactory.create(live=True)
program_page = ProgramPage(program=program, title="Test Program")
homepage = HomePage.objects.first()
homepage.add_child(instance=program_page)
program_page.save_revision().publish()
response = self.client.get('/')
self.assertContains(
response,
program_page.url,
status_code=200
)
def test_program_page(self):
"""Verify that ProgramPage is passed in the context if and only if it's available"""
program_with_page = ProgramFactory.create(live=True)
program_page = ProgramPage(program=program_with_page, title="Test Program")
homepage = HomePage.objects.first()
homepage.add_child(instance=program_page)
program_page.save_revision().publish()
program_without_page = ProgramFactory.create(live=True)
response = self.client.get('/')
assert response.context['programs'] == [
(program_with_page, program_page),
(program_without_page, None),
]
def test_login_button(self):
"""Verify that we see a login button if not logged in"""
response = self.client.get('/')
self.assertContains(response, "Create Account")
def test_sign_out_button(self):
"""Verify that we see a sign out button if logged in"""
self.create_and_login_user()
response = self.client.get('/')
self.assertContains(response, 'Sign Out')
def test_index_context_anonymous(self):
"""
Assert context values when anonymous
"""
ga_tracking_id = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/')
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
assert response.context['authenticated'] is False
assert response.context['username'] == ''
assert response.context['title'] == HomePage.objects.first().title
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
assert response.context['is_staff'] is False
assert response.context['programs'] == []
self.assertContains(response, 'Share this page')
js_settings = json.loads(response.context['js_settings_json'])
assert js_settings['gaTrackingID'] == ga_tracking_id
def test_index_context_logged_in_social_auth(self):
"""
Assert context values when logged in as social auth user
"""
profile = self.create_and_login_user()
user = profile.user
ga_tracking_id = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/')
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
assert response.context['authenticated'] is True
assert response.context['username'] == user.username
assert response.context['title'] == HomePage.objects.first().title
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
assert response.context['is_staff'] is False
assert response.context['programs'] == []
self.assertContains(response, 'Share this page')
js_settings = json.loads(response.context['js_settings_json'])
assert js_settings['gaTrackingID'] == ga_tracking_id
@ddt.data(
*Role.ASSIGNABLE_ROLES
)
def test_index_context_logged_in_staff(self, role):
"""
Assert context values when logged in as staff for a program
"""
program = ProgramFactory.create(live=True)
with mute_signals(post_save):
profile = ProfileFactory.create()
Role.objects.create(
role=role,
program=program,
user=profile.user,
)
self.client.force_login(profile.user)
ga_tracking_id = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
):
response = self.client.get('/')
assert response.context['authenticated'] is True
assert response.context['username'] == profile.user.username
assert response.context['title'] == HomePage.objects.first().title
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
assert response.context['is_staff'] is True
assert response.context['programs'] == [
(program, None),
]
self.assertContains(response, 'Share this page')
js_settings = json.loads(response.context['js_settings_json'])
assert js_settings['gaTrackingID'] == ga_tracking_id
def test_program_order(self):
"""
Assert that programs are output in id order
"""
for i in range(10):
ProgramFactory.create(live=True, title="Program {}".format(i + 1))
response = self.client.get("/")
content = response.content.decode('utf-8')
indexes = [content.find("Program {}".format(i + 1)) for i in range(10)]
assert indexes == sorted(indexes)
class DashboardTests(ViewsTests):
"""
Tests for dashboard views
"""
@override_settings(USE_WEBPACK_DEV_SERVER=False)
def test_dashboard_settings(self):
"""
Assert settings we pass to dashboard
"""
profile = self.create_and_login_user()
user = profile.user
ga_tracking_id = FuzzyText().fuzz()
react_ga_debug = FuzzyText().fuzz()
edx_base_url = FuzzyText().fuzz()
mitxonline_base_url = FuzzyText().fuzz()
mitxonline_url = FuzzyText().fuzz()
host = FuzzyText().fuzz()
email_support = FuzzyText().fuzz()
open_discussions_redirect_url = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
REACT_GA_DEBUG=react_ga_debug,
EDXORG_CALLBACK_URL=edx_base_url,
MITXONLINE_CALLBACK_URL=mitxonline_base_url,
MITXONLINE_URL=mitxonline_url,
WEBPACK_DEV_SERVER_HOST=host,
EMAIL_SUPPORT=email_support,
VERSION='0.0.1',
RAVEN_CONFIG={'dsn': ''},
OPENSEARCH_DEFAULT_PAGE_SIZE=10,
OPEN_DISCUSSIONS_REDIRECT_URL=open_discussions_redirect_url,
), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
resp = self.client.get(DASHBOARD_URL)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'dashboard',
'sentry_client',
'style',
'zendesk_widget',
}
js_settings = json.loads(resp.context['js_settings_json'])
assert js_settings == {
'gaTrackingID': ga_tracking_id,
'reactGaDebug': react_ga_debug,
'user': {
'email': user.email,
'username': user.username,
'first_name': profile.first_name,
'last_name': profile.last_name,
'preferred_name': profile.preferred_name,
"social_auth_providers": ["edxorg", "not_edx"],
},
'host': host,
'edx_base_url': edx_base_url,
"mitxonline_base_url": mitxonline_base_url,
"mitxonline_url": mitxonline_url,
'roles': [],
'search_url': reverse('search_api', kwargs={"opensearch_url": ""}),
'support_email': email_support,
'environment': 'dev',
'release_version': '0.0.1',
'sentry_dsn': "",
'es_page_size': 10,
'public_path': '/static/bundles/',
'FEATURES': {
'PROGRAM_LEARNERS': False,
'DISCUSSIONS_POST_UI': False,
'DISCUSSIONS_CREATE_CHANNEL_UI': False,
'PROGRAM_RECORD_LINK': False,
'ENABLE_PROGRAM_LETTER': False,
},
'open_discussions_redirect_url': open_discussions_redirect_url
}
assert resp.context['is_public'] is False
assert resp.context['has_zendesk_widget'] is True
assert resp.context['support_email'] == email_support
self.assertNotContains(resp, 'Share this page')
def test_roles_setting(self):
"""
Assert SETTINGS when a user has roles assigned to them
"""
profile = self.create_and_login_user()
Role.objects.create(
program=ProgramFactory.create(),
user=profile.user,
role=Role.DEFAULT_ROLE,
)
resp = self.client.get(DASHBOARD_URL)
js_settings = json.loads(resp.context['js_settings_json'])
assert js_settings['roles'] == [
{
'program': role.program.id,
'role': role.role,
'permissions': [key for key, value in available_perm_status(profile.user).items() if value is True],
} for role in profile.user.role_set.all()
]
def test_unauthenticated_user_redirect(self):
"""Verify that an unauthenticated user can't visit '/dashboard'"""
response = self.client.get(DASHBOARD_URL)
self.assertRedirects(
response,
"/signin/?next={}".format(DASHBOARD_URL)
)
def test_authenticated_user_doesnt_redirect(self):
"""Verify that we let an authenticated user through to '/dashboard'"""
self.create_and_login_user()
response = self.client.get(DASHBOARD_URL)
self.assertContains(
response,
"MicroMasters",
status_code=200
)
class HandlerTests(ViewsTests):
"""
Tests for 404 and 500 handlers
"""
def test_404_error_context_logged_in(self):
"""
Assert context values for 404 error page when logged in
"""
with mute_signals(post_save):
profile = self.create_and_login_user()
self.client.force_login(profile.user)
# case with specific page
with override_settings(EMAIL_SUPPORT='support'):
with patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/404/')
assert response.context['authenticated'] is True
assert response.context['name'] == profile.preferred_name
assert response.context['support_email'] == 'support'
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_404_NOT_FOUND)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
# case with a fake page
with self.settings(DEBUG=False), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/gfh0o4n8741387jfmnub134fn348fr38f348f/')
assert response.context['authenticated'] is True
assert response.context['name'] == profile.preferred_name
assert response.context['support_email'] == 'support'
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_404_NOT_FOUND)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
def test_404_error_context_logged_out(self):
"""
Assert context values for 404 error page when logged out
"""
# case with specific page
with patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/404/')
assert response.context['authenticated'] is False
assert response.context['name'] == ""
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_404_NOT_FOUND)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
# case with a fake page
with self.settings(DEBUG=False), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/gfh0o4n8741387jfmnub134fn348fr38f348f/')
assert response.context['authenticated'] is False
assert response.context['name'] == ""
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_404_NOT_FOUND)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
def test_500_error_context_logged_in(self):
"""
Assert context values for 500 error page when logged in
"""
with mute_signals(post_save):
profile = self.create_and_login_user()
self.client.force_login(profile.user)
with override_settings(EMAIL_SUPPORT='support'), patch(
'ui.templatetags.render_bundle._get_bundle'
) as get_bundle:
response = self.client.get('/500/')
assert response.context['authenticated'] is True
assert response.context['name'] == profile.preferred_name
assert response.context['support_email'] == 'support'
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
def test_500_error_context_logged_out(self):
"""
Assert context values for 500 error page when logged out
"""
# case with specific page
with patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
response = self.client.get('/500/')
assert response.context['authenticated'] is False
assert response.context['name'] == ""
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
self.assertContains(response, 'Share this page', status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
@ddt.ddt
class TestProgramPage(ViewsTests):
"""
Test that the ProgramPage view work as expected.
"""
def setUp(self):
super().setUp()
homepage = HomePage.objects.first()
program = ProgramFactory.create(title="Test Program Title", live=True)
self.program_page = ProgramPage(program=program, title="Test Program")
homepage.add_child(instance=self.program_page)
self.program_page.save_revision().publish()
def test_bundles(self):
"""
Assert javascript files rendered on program page
"""
with patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
self.client.get(self.program_page.url)
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'public',
'sentry_client',
'style',
'style_public',
'zendesk_widget',
}
def test_context_anonymous(self):
"""
Assert context values when anonymous which are different than for the logged in user
"""
response = self.client.get(self.program_page.url)
assert response.context['authenticated'] is False
assert response.context['username'] == ''
assert response.context['is_staff'] is False
js_settings = json.loads(response.context['js_settings_json'])
assert js_settings['user'] is None
@ddt.data(
*Role.ASSIGNABLE_ROLES
)
def test_context_has_role(self, role):
"""
Assert context values when staff or instructor which are different than for regular logged in user
"""
with mute_signals(post_save):
profile = ProfileFactory.create()
Role.objects.create(
role=role,
program=self.program_page.program,
user=profile.user,
)
self.client.force_login(profile.user)
response = self.client.get(self.program_page.url)
assert response.context['authenticated'] is True
assert response.context['username'] == profile.user.username
assert response.context['is_staff'] is True
def test_context(self):
"""
Assert context values for logged in user
"""
with mute_signals(post_save):
profile = ProfileFactory.create()
self.client.force_login(profile.user)
# ProgramFaculty and ProgramCourse are asserted via ProgramPageSerializer below
FacultyFactory.create_batch(3, program_page=self.program_page)
courses = self.program_page.program.course_set.all()
ProgramCourseFactory.create_batch(
len(courses), program_page=self.program_page, course=Iterator(courses)
)
ga_tracking_id = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
ENVIRONMENT='environment',
VERSION='version',
WEBPACK_DEV_SERVER_HOST='testserver',
):
response = self.client.get(self.program_page.url)
assert response.context['authenticated'] is True
assert response.context['username'] == profile.user.username
assert response.context['title'] == self.program_page.title
assert response.context['is_public'] is True
assert response.context['has_zendesk_widget'] is True
assert response.context['is_staff'] is False
self.assertContains(response, 'Share this page')
js_settings = json.loads(response.context['js_settings_json'])
assert js_settings == {
'gaTrackingID': ga_tracking_id,
'environment': 'environment',
'sentry_dsn': "",
'host': 'testserver',
'release_version': 'version',
'user': serialize_maybe_user(profile.user),
'program': ProgramPageSerializer(self.program_page).data,
}
def test_info_links(self):
"""
If present, info links should be displayed
"""
info_links = InfoLinksFactory.create_batch(3, program_page=self.program_page)
response = self.client.get(self.program_page.url)
for info_link in info_links:
self.assertContains(response, info_link.title_url)
def test_semester_date(self):
"""
If present, semester data should be displayed
"""
semester_dates = SemesterDateFactory.create_batch(3, program_page=self.program_page)
response = self.client.get(self.program_page.url)
for semester_date in semester_dates:
self.assertContains(response, semester_date.semester_name)
def test_login_button(self):
"""Verify that we see a login button"""
CourseFactory.create(program=self.program_page.program)
response = self.client.get(self.program_page.url)
self.assertContains(response, "Create Account")
def test_program_thumbnail_default(self):
"""Verify that a default thumbnail shows up for a live program"""
self.create_and_login_user()
default_image = 'images/course-thumbnail.png'
# the default image should not show up if no program is live
program = self.program_page.program
program.live = False
program.save()
resp = self.client.get('/')
self.assertNotIn(default_image, resp)
# default image should show up if a program is live and no thumbnail image was set
program.live = True
program.save()
resp = self.client.get('/')
self.assertContains(resp, default_image)
def test_program_thumbnail(self):
"""Verify that a thumbnail shows up if specified for a ProgramPage"""
self.create_and_login_user()
image = Image.objects.create(title='Test image',
file=get_test_image_file())
self.program_page.thumbnail_image = image
self.program_page.save()
resp = self.client.get('/')
self.assertContains(resp, image.get_rendition('fill-690x530').url)
def test_course_listing(self):
"""
Verify that courses are being serialized to JS in the correct order
"""
# Create several courses in the program
courses = [
CourseFactory.create(
program=self.program_page.program,
position_in_program=i,
)
for i in range(5)
]
# render the page
response = self.client.get(self.program_page.url)
js_settings = json.loads(response.context['js_settings_json'])
# check that the courses are in the response
self.assertIn("program", js_settings)
self.assertIn("courses", js_settings["program"])
self.assertEqual(len(js_settings["program"]["courses"]), 5)
# check that they're in the correct order
for course, js_course in zip(courses, js_settings["program"]["courses"]):
self.assertEqual(course.title, js_course["title"])
self.assertEqual(course.description, js_course["description"])
self.assertEqual(course.url, js_course["url"])
def test_no_courses(self):
"""
Verify that no courses result in a different button
"""
program_subscribe_link = "https://fakeurl.com"
self.program_page.program_subscribe_link = program_subscribe_link
self.program_page.save()
response = self.client.get(self.program_page.url)
js_settings = json.loads(response.context['js_settings_json'])
self.assertIn("program", js_settings)
self.assertIn("courses", js_settings["program"])
self.assertEqual(len(js_settings["program"]["courses"]), 0)
self.assertContains(response, "LEARN MORE")
self.assertContains(response, program_subscribe_link)
# pylint: disable=too-many-locals
@override_settings(USE_WEBPACK_DEV_SERVER=False)
class TestUsersPage(ViewsTests):
"""
Tests for user page
"""
def test_users_logged_in(self):
"""
Assert settings we pass to dashboard
"""
profile = self.create_and_login_user()
user = profile.user
username = user.username
ga_tracking_id = FuzzyText().fuzz()
react_ga_debug = FuzzyText().fuzz()
edx_base_url = FuzzyText().fuzz()
mitxonline_base_url = FuzzyText().fuzz()
mitxonline_url = FuzzyText().fuzz()
host = FuzzyText().fuzz()
email_support = FuzzyText().fuzz()
open_discussions_redirect_url = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
REACT_GA_DEBUG=react_ga_debug,
EDXORG_CALLBACK_URL=edx_base_url,
MITXONLINE_CALLBACK_URL=mitxonline_base_url,
MITXONLINE_URL=mitxonline_url,
WEBPACK_DEV_SERVER_HOST=host,
EMAIL_SUPPORT=email_support,
VERSION='0.0.1',
RAVEN_CONFIG={'dsn': ''},
OPENSEARCH_DEFAULT_PAGE_SIZE=10,
OPEN_DISCUSSIONS_REDIRECT_URL=open_discussions_redirect_url
):
# Mock has_permission so we don't worry about testing permissions here
has_permission = Mock(return_value=True)
with patch(
'profiles.permissions.CanSeeIfNotPrivate.has_permission',
has_permission,
), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
resp = self.client.get(reverse('ui-users', kwargs={'user': username}))
assert resp.status_code == 200
assert resp.context['is_public'] is False
assert resp.context['has_zendesk_widget'] is True
self.assertNotContains(resp, 'Share this page')
js_settings = json.loads(resp.context['js_settings_json'])
assert js_settings == {
'gaTrackingID': ga_tracking_id,
'reactGaDebug': react_ga_debug,
'user': {
'email': user.email,
'username': username,
'first_name': profile.first_name,
'last_name': profile.last_name,
'preferred_name': profile.preferred_name,
"social_auth_providers": ["edxorg", "not_edx"],
},
'host': host,
'edx_base_url': edx_base_url,
"mitxonline_base_url": mitxonline_base_url,
"mitxonline_url": mitxonline_url,
'roles': [],
'search_url': reverse('search_api', kwargs={"opensearch_url": ""}),
'support_email': email_support,
'environment': 'dev',
'release_version': '0.0.1',
'sentry_dsn': "",
'es_page_size': 10,
'public_path': '/static/bundles/',
'FEATURES': {
'PROGRAM_LEARNERS': False,
'DISCUSSIONS_POST_UI': False,
'DISCUSSIONS_CREATE_CHANNEL_UI': False,
'PROGRAM_RECORD_LINK': False,
'ENABLE_PROGRAM_LETTER': False,
},
'open_discussions_redirect_url': open_discussions_redirect_url
}
assert has_permission.called
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'dashboard',
'sentry_client',
'style',
'zendesk_widget',
}
def test_users_anonymous(self):
"""
Assert settings we pass to dashboard
"""
profile = self.create_and_login_user()
user = profile.user
self.client.logout()
username = user.username
ga_tracking_id = FuzzyText().fuzz()
react_ga_debug = FuzzyText().fuzz()
edx_base_url = FuzzyText().fuzz()
mitxonline_base_url = FuzzyText().fuzz()
mitxonline_url = FuzzyText().fuzz()
host = FuzzyText().fuzz()
email_support = FuzzyText().fuzz()
open_discussions_redirect_url = FuzzyText().fuzz()
with self.settings(
GA_TRACKING_ID=ga_tracking_id,
REACT_GA_DEBUG=react_ga_debug,
EDXORG_CALLBACK_URL=edx_base_url,
MITXONLINE_CALLBACK_URL=mitxonline_base_url,
MITXONLINE_URL=mitxonline_url,
WEBPACK_DEV_SERVER_HOST=host,
EMAIL_SUPPORT=email_support,
VERSION='0.0.1',
RAVEN_CONFIG={'dsn': ''},
OPENSEARCH_DEFAULT_PAGE_SIZE=10,
OPEN_DISCUSSIONS_REDIRECT_URL=open_discussions_redirect_url
):
# Mock has_permission so we don't worry about testing permissions here
has_permission = Mock(return_value=True)
with patch(
'profiles.permissions.CanSeeIfNotPrivate.has_permission',
has_permission,
), patch('ui.templatetags.render_bundle._get_bundle') as get_bundle:
resp = self.client.get(reverse('ui-users', kwargs={'user': username}))
assert resp.status_code == 200
assert resp.context['is_public'] is False
assert resp.context['has_zendesk_widget'] is True
self.assertNotContains(resp, 'Share this page')
js_settings = json.loads(resp.context['js_settings_json'])
assert js_settings == {
'gaTrackingID': ga_tracking_id,
'reactGaDebug': react_ga_debug,
'user': None,
'host': host,
'edx_base_url': edx_base_url,
"mitxonline_base_url": mitxonline_base_url,
"mitxonline_url": mitxonline_url,
'roles': [],
'search_url': reverse('search_api', kwargs={"opensearch_url": ""}),
'support_email': email_support,
'environment': 'dev',
'release_version': '0.0.1',
'sentry_dsn': "",
'es_page_size': 10,
'public_path': '/static/bundles/',
'FEATURES': {
'PROGRAM_LEARNERS': False,
'DISCUSSIONS_POST_UI': False,
'DISCUSSIONS_CREATE_CHANNEL_UI': False,
'PROGRAM_RECORD_LINK': False,
'ENABLE_PROGRAM_LETTER': False,
},
'open_discussions_redirect_url': open_discussions_redirect_url
}
assert has_permission.called
bundles = [bundle[0][1] for bundle in get_bundle.call_args_list]
assert set(bundles) == {
'dashboard',
'sentry_client',
'style',
'zendesk_widget',
}
def test_users_404(self):
"""
Assert that if we look at a user we don't have permission to see, we get a 404
"""
resp = self.client.get(
reverse('ui-users', kwargs={'user': 'missing'})
)
assert resp.status_code == 404
def test_users_index_logged_in(self):
"""
Assert that a logged in user gets a 200 going to /learner/
"""
self.create_and_login_user()
resp = self.client.get(reverse('ui-users'))
# We don't actually direct here, that happens via react-router
assert resp.status_code == 200
def test_users_index_anonymous(self):
"""
Assert that an anonymous user gets a 404 going to /learner/
"""
resp = self.client.get(reverse('ui-users'))
assert resp.status_code == 404
class TestSignIn(ViewsTests):
""" Tests for the sign in page """
def test_login_program_coupon_redirect(self):
""" Test that the login page redirects if the next url has a coupon for a program """
with self.settings(FEATURES={
"MITXONLINE_LOGIN": True
}):
coupon = CouponFactory.create(program=True)
next_url = f"/dashboard/?coupon={coupon.coupon_code}"
response = self.client.get(f"{reverse('signin')}?{urlencode({'next': next_url})}")
redirect_params = urlencode({
'next': next_url,
'program': coupon.content_object.id
})
assert response.status_code == 302
assert response.url == f"{reverse('signin')}?{redirect_params}"
def test_login_course_coupon_redirect(self):
""" Test that the login page redirects if the next url has a coupon for a course """
with self.settings(FEATURES={
"MITXONLINE_LOGIN": True
}):
coupon = CouponFactory.create(course=True)
next_url = f"/dashboard/?coupon={coupon.coupon_code}"
response = self.client.get(f"{reverse('signin')}?{urlencode({'next': next_url})}")
redirect_params = urlencode({
'next': next_url,
'program': coupon.content_object.program.id
})
assert response.status_code == 302
assert response.url == f"{reverse('signin')}?{redirect_params}"