-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathtest_group_configurations.py
More file actions
1539 lines (1364 loc) · 60 KB
/
test_group_configurations.py
File metadata and controls
1539 lines (1364 loc) · 60 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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Group Configuration Tests.
"""
import json
from operator import itemgetter
from unittest.mock import patch
import ddt
from django.test import Client
from openedx_authz.constants.roles import COURSE_DATA_RESEARCHER, COURSE_STAFF
from rest_framework import status
from cms.djangoapps.contentstore.api.tests.base import BaseCourseViewTest
from cms.djangoapps.contentstore.course_group_config import (
ENROLLMENT_SCHEME,
GroupConfiguration,
)
from cms.djangoapps.contentstore.tests.utils import CourseTestCase
from cms.djangoapps.contentstore.utils import reverse_course_url, reverse_usage_url
from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.djangoapps.authz.tests.mixins import CourseAuthzTestMixin
from openedx.features.content_type_gating.helpers import CONTENT_GATING_PARTITION_ID
from openedx.features.content_type_gating.partitions import CONTENT_TYPE_GATING_SCHEME
from xmodule.modulestore import ModuleStoreEnum # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE
from xmodule.modulestore.tests.factories import ( # lint-amnesty, pylint: disable=wrong-import-order
BlockFactory,
CourseFactory,
)
from xmodule.partitions.partitions import ( # lint-amnesty, pylint: disable=wrong-import-order
ENROLLMENT_TRACK_PARTITION_ID,
Group,
UserPartition,
)
from xmodule.validation import ( # lint-amnesty, pylint: disable=wrong-import-order
StudioValidation,
StudioValidationMessage,
)
GROUP_CONFIGURATION_JSON = {
'name': 'Test name',
'scheme': 'random',
'description': 'Test description',
'version': UserPartition.VERSION,
'groups': [
{
'name': 'Group A',
'version': 1,
}, {
'name': 'Group B',
'version': 1,
},
],
}
# pylint: disable=no-member
class HelperMethods:
"""
Mixin that provides useful methods for Group Configuration tests.
"""
def _create_content_experiment(self, cid=-1, group_id=None, cid_for_problem=None,
name_suffix='', special_characters=''):
"""
Create content experiment.
Assign Group Configuration to the experiment if cid is provided.
Assigns a problem to the first group in the split test if group_id and cid_for_problem is provided.
"""
sequential = BlockFactory.create(
category='sequential',
parent_location=self.course.location,
display_name=f'Test Subsection {name_suffix}'
)
vertical = BlockFactory.create(
category='vertical',
parent_location=sequential.location,
display_name=f'Test Unit {name_suffix}'
)
c0_url = self.course.id.make_usage_key("vertical", f"split_test_cond0_{name_suffix}")
c1_url = self.course.id.make_usage_key("vertical", f"split_test_cond1_{name_suffix}")
c2_url = self.course.id.make_usage_key("vertical", f"split_test_cond2_{name_suffix}")
split_test = BlockFactory.create(
category='split_test',
parent_location=vertical.location,
user_partition_id=cid,
display_name=f"Test Content Experiment {name_suffix}{special_characters}",
group_id_to_child={"0": c0_url, "1": c1_url, "2": c2_url}
)
BlockFactory.create(
parent_location=split_test.location,
category="vertical",
display_name="Condition 0 vertical",
location=c0_url,
)
c1_vertical = BlockFactory.create(
parent_location=split_test.location,
category="vertical",
display_name="Condition 1 vertical",
location=c1_url,
)
BlockFactory.create(
parent_location=split_test.location,
category="vertical",
display_name="Condition 2 vertical",
location=c2_url,
)
problem = None
if group_id and cid_for_problem:
problem = BlockFactory.create(
category='problem',
parent_location=c1_vertical.location,
display_name="Test Problem"
)
self.client.ajax_post(
reverse_usage_url("xblock_handler", problem.location),
data={'metadata': {'group_access': {cid_for_problem: [group_id]}}}
)
c1_vertical.children.append(problem.location)
partitions_json = [p.to_json() for p in self.course.user_partitions]
self.client.ajax_post(
reverse_usage_url("xblock_handler", split_test.location),
data={'metadata': {'user_partitions': partitions_json}}
)
self.save_course()
return vertical, split_test, problem
def _create_problem_with_content_group(self, cid, group_id, name_suffix='', special_characters='', orphan=False):
"""
Create a problem
Assign content group to the problem.
"""
vertical_parent_location = self.course.location
if not orphan:
subsection = BlockFactory.create(
category='sequential',
parent_location=self.course.location,
display_name=f"Test Subsection {name_suffix}"
)
vertical_parent_location = subsection.location
vertical = BlockFactory.create(
category='vertical',
parent_location=vertical_parent_location,
display_name=f"Test Unit {name_suffix}"
)
problem = BlockFactory.create(
category='problem',
parent_location=vertical.location,
display_name=f"Test Problem {name_suffix}{special_characters}"
)
group_access_content = {'group_access': {cid: [group_id]}}
self.client.ajax_post(
reverse_usage_url("xblock_handler", problem.location),
data={'metadata': group_access_content}
)
if not orphan:
self.course.children.append(subsection.location)
self.save_course()
return vertical, problem
def _add_user_partitions(self, count=1, scheme_id="random"):
"""
Create user partitions for the course.
"""
partitions = [
UserPartition(
i, 'Name ' + str(i), 'Description ' + str(i),
[Group(0, 'Group A'), Group(1, 'Group B'), Group(2, 'Group C')],
scheme=None, scheme_id=scheme_id
) for i in range(count)
]
self.course.user_partitions = partitions
self.save_course()
# pylint: disable=no-member
class GroupConfigurationsBaseTestCase:
"""
Mixin with base test cases for the group configurations.
"""
def _remove_ids(self, content):
"""
Remove ids from the response. We cannot predict IDs, because they're
generated randomly.
We use this method to clean up response when creating new group configurations.
Returns a tuple that contains removed group configuration ID and group IDs.
"""
configuration_id = content.pop("id")
group_ids = [group.pop("id") for group in content["groups"]]
return (configuration_id, group_ids)
def test_required_fields_are_absent(self):
"""
Test required fields are absent.
"""
bad_jsons = [
# must have name of the configuration
{
'description': 'Test description',
'groups': [
{'name': 'Group A'},
{'name': 'Group B'},
],
},
# must have at least one group
{
'name': 'Test name',
'description': 'Test description',
'groups': [],
},
# an empty json
{},
]
for bad_json in bad_jsons:
response = self.client.post(
self._url(),
data=json.dumps(bad_json),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 400) # noqa: PT009
self.assertNotIn("Location", response) # noqa: PT009
content = json.loads(response.content.decode('utf-8'))
self.assertIn("error", content) # noqa: PT009
def test_invalid_json(self):
"""
Test invalid json handling.
"""
# No property name.
invalid_json = "{'name': 'Test Name', []}"
response = self.client.post(
self._url(),
data=invalid_json,
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 400) # noqa: PT009
self.assertNotIn("Location", response) # noqa: PT009
content = json.loads(response.content.decode('utf-8'))
self.assertIn("error", content) # noqa: PT009
@ddt.ddt
class GroupConfigurationsListHandlerTestCase(CourseTestCase, GroupConfigurationsBaseTestCase, HelperMethods):
"""
Test cases for group_configurations_list_handler.
"""
def _url(self):
"""
Return url for the handler.
"""
return reverse_course_url('group_configurations_list_handler', self.course.id)
def test_view_index_ok(self):
"""
Basic check that the groups configuration page redirects to the MFE.
"""
response = self.client.get(self._url())
self.assertEqual(response.status_code, 302) # noqa: PT009
def test_unsupported_http_accept_header(self):
"""
Test if not allowed header present in request.
"""
response = self.client.get(
self._url(),
HTTP_ACCEPT="text/plain",
)
self.assertEqual(response.status_code, 406) # noqa: PT009
def test_can_create_group_configuration(self):
"""
Test that you can create a group configuration.
"""
expected = {
'description': 'Test description',
'name': 'Test name',
'scheme': 'random',
'version': UserPartition.VERSION,
'groups': [
{'name': 'Group A', 'version': 1},
{'name': 'Group B', 'version': 1},
],
'parameters': {},
'active': True
}
response = self.client.ajax_post(
self._url(),
data=GROUP_CONFIGURATION_JSON
)
self.assertEqual(response.status_code, 201) # noqa: PT009
self.assertIn("Location", response) # noqa: PT009
content = json.loads(response.content.decode('utf-8'))
configuration_id, group_ids = self._remove_ids(content) # pylint: disable=unused-variable
self.assertEqual(content, expected) # noqa: PT009
# IDs are unique
self.assertEqual(len(group_ids), len(set(group_ids))) # noqa: PT009
self.assertEqual(len(group_ids), 2) # noqa: PT009
self.reload_course()
# Verify that user_partitions in the course contains the new group configuration.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'Test name') # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 2) # noqa: PT009
self.assertEqual(user_partititons[0].groups[0].name, 'Group A') # noqa: PT009
self.assertEqual(user_partititons[0].groups[1].name, 'Group B') # noqa: PT009
self.assertEqual(user_partititons[0].parameters, {}) # noqa: PT009
def test_lazily_creates_cohort_configuration(self):
"""
Test that a cohort schemed user partition is NOT created by
default for the user.
"""
self.assertEqual(len(self.course.user_partitions), 0) # noqa: PT009
self.client.get(self._url())
self.reload_course()
self.assertEqual(len(self.course.user_partitions), 0) # noqa: PT009
@ddt.data('content_type_gate', 'enrollment_track')
def test_cannot_create_restricted_group_configuration(self, scheme_id):
"""
Test that you cannot create a restricted group configuration.
"""
group_config = dict(GROUP_CONFIGURATION_JSON)
group_config['scheme'] = scheme_id
group_config.setdefault('parameters', {})['course_id'] = str(self.course.id)
response = self.client.ajax_post(
self._url(),
data=group_config
)
self.assertEqual(response.status_code, 400) # noqa: PT009
@ddt.ddt
class GroupConfigurationsDetailHandlerTestCase(CourseTestCase, GroupConfigurationsBaseTestCase, HelperMethods):
"""
Test cases for group_configurations_detail_handler.
"""
ID = 0
def _url(self, cid=-1):
"""
Return url for the handler.
"""
cid = cid if cid > 0 else self.ID
return reverse_course_url(
'group_configurations_detail_handler',
self.course.id,
kwargs={'group_configuration_id': cid},
)
def test_can_create_new_content_group_if_it_does_not_exist(self):
"""
PUT new content group.
"""
expected = {
'id': 666,
'name': 'Test name',
'scheme': 'cohort',
'description': 'Test description',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1, 'usage': []},
{'id': 1, 'name': 'Group B', 'version': 1, 'usage': []},
],
'parameters': {},
'active': True,
}
response = self.client.put(
self._url(cid=666),
data=json.dumps(expected),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
content = json.loads(response.content.decode('utf-8'))
self.assertEqual(content, expected) # noqa: PT009
self.reload_course()
# Verify that user_partitions in the course contains the new group configuration.
user_partitions = self.course.user_partitions
self.assertEqual(len(user_partitions), 1) # noqa: PT009
self.assertEqual(user_partitions[0].name, 'Test name') # noqa: PT009
self.assertEqual(len(user_partitions[0].groups), 2) # noqa: PT009
self.assertEqual(user_partitions[0].groups[0].name, 'Group A') # noqa: PT009
self.assertEqual(user_partitions[0].groups[1].name, 'Group B') # noqa: PT009
self.assertEqual(user_partitions[0].parameters, {}) # noqa: PT009
def test_can_edit_content_group(self):
"""
Edit content group and check its id and modified fields.
"""
self._add_user_partitions(scheme_id='cohort')
self.save_course()
expected = {
'id': self.ID,
'name': 'New Test name',
'scheme': 'cohort',
'description': 'New Test description',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'New Group Name', 'version': 1, 'usage': []},
{'id': 2, 'name': 'Group C', 'version': 1, 'usage': []},
],
'parameters': {},
'active': True,
}
response = self.client.put(
self._url(),
data=json.dumps(expected),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
content = json.loads(response.content.decode('utf-8'))
self.assertEqual(content, expected) # noqa: PT009
self.reload_course()
# Verify that user_partitions is properly updated in the course.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'New Test name') # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 2) # noqa: PT009
self.assertEqual(user_partititons[0].groups[0].name, 'New Group Name') # noqa: PT009
self.assertEqual(user_partititons[0].groups[1].name, 'Group C') # noqa: PT009
self.assertEqual(user_partititons[0].parameters, {}) # noqa: PT009
def test_can_delete_content_group(self):
"""
Delete content group and check user partitions.
"""
self._add_user_partitions(count=1, scheme_id='cohort')
self.save_course()
details_url_with_group_id = self._url(cid=0) + '/1'
response = self.client.delete(
details_url_with_group_id,
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 204) # noqa: PT009
self.reload_course()
# Verify that group and partition is properly updated in the course.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'Name 0') # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 2) # noqa: PT009
self.assertEqual(user_partititons[0].groups[1].name, 'Group C') # noqa: PT009
def test_cannot_delete_used_content_group(self):
"""
Cannot delete content group if it is in use.
"""
self._add_user_partitions(count=1, scheme_id='cohort')
self._create_problem_with_content_group(cid=0, group_id=1)
details_url_with_group_id = self._url(cid=0) + '/1'
response = self.client.delete(
details_url_with_group_id,
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 400) # noqa: PT009
content = json.loads(response.content.decode('utf-8'))
self.assertTrue(content['error']) # noqa: PT009
self.reload_course()
# Verify that user_partitions and groups are still the same.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 3) # noqa: PT009
self.assertEqual(user_partititons[0].groups[1].name, 'Group B') # noqa: PT009
def test_cannot_delete_non_existent_content_group(self):
"""
Cannot delete content group if it is doesn't exist.
"""
self._add_user_partitions(count=1, scheme_id='cohort')
details_url_with_group_id = self._url(cid=0) + '/90'
response = self.client.delete(
details_url_with_group_id,
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 404) # noqa: PT009
# Verify that user_partitions is still the same.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 3) # noqa: PT009
def test_can_create_new_group_configuration_if_it_does_not_exist(self):
"""
PUT new group configuration when no configurations exist in the course.
"""
expected = {
'id': 999,
'name': 'Test name',
'scheme': 'random',
'description': 'Test description',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1},
{'id': 1, 'name': 'Group B', 'version': 1},
],
'usage': [],
'parameters': {},
'active': True,
}
response = self.client.put(
self._url(cid=999),
data=json.dumps(expected),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
content = json.loads(response.content.decode('utf-8'))
self.assertEqual(content, expected) # noqa: PT009
self.reload_course()
# Verify that user_partitions in the course contains the new group configuration.
user_partitions = self.course.user_partitions
self.assertEqual(len(user_partitions), 1) # noqa: PT009
self.assertEqual(user_partitions[0].name, 'Test name') # noqa: PT009
self.assertEqual(len(user_partitions[0].groups), 2) # noqa: PT009
self.assertEqual(user_partitions[0].groups[0].name, 'Group A') # noqa: PT009
self.assertEqual(user_partitions[0].groups[1].name, 'Group B') # noqa: PT009
self.assertEqual(user_partitions[0].parameters, {}) # noqa: PT009
def test_can_edit_group_configuration(self):
"""
Edit group configuration and check its id and modified fields.
"""
self._add_user_partitions()
self.save_course()
expected = {
'id': self.ID,
'name': 'New Test name',
'scheme': 'random',
'description': 'New Test description',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'New Group Name', 'version': 1},
{'id': 2, 'name': 'Group C', 'version': 1},
],
'usage': [],
'parameters': {},
'active': True,
}
response = self.client.put(
self._url(),
data=json.dumps(expected),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
content = json.loads(response.content.decode('utf-8'))
self.assertEqual(content, expected) # noqa: PT009
self.reload_course()
# Verify that user_partitions is properly updated in the course.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'New Test name') # noqa: PT009
self.assertEqual(len(user_partititons[0].groups), 2) # noqa: PT009
self.assertEqual(user_partititons[0].groups[0].name, 'New Group Name') # noqa: PT009
self.assertEqual(user_partititons[0].groups[1].name, 'Group C') # noqa: PT009
self.assertEqual(user_partititons[0].parameters, {}) # noqa: PT009
def test_can_delete_group_configuration(self):
"""
Delete group configuration and check user partitions.
"""
self._add_user_partitions(count=2)
self.save_course()
response = self.client.delete(
self._url(cid=0),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 204) # noqa: PT009
self.reload_course()
# Verify that user_partitions is properly updated in the course.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 1) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'Name 1') # noqa: PT009
def test_cannot_delete_used_group_configuration(self):
"""
Cannot delete group configuration if it is in use.
"""
self._add_user_partitions(count=2)
self._create_content_experiment(cid=0)
response = self.client.delete(
self._url(cid=0),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 400) # noqa: PT009
content = json.loads(response.content.decode('utf-8'))
self.assertTrue(content['error']) # noqa: PT009
self.reload_course()
# Verify that user_partitions is still the same.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 2) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'Name 0') # noqa: PT009
def test_cannot_delete_non_existent_group_configuration(self):
"""
Cannot delete group configuration if it is doesn't exist.
"""
self._add_user_partitions(count=2)
response = self.client.delete(
self._url(cid=999),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 404) # noqa: PT009
# Verify that user_partitions is still the same.
user_partititons = self.course.user_partitions
self.assertEqual(len(user_partititons), 2) # noqa: PT009
self.assertEqual(user_partititons[0].name, 'Name 0') # noqa: PT009
@ddt.data(CONTENT_TYPE_GATING_SCHEME, ENROLLMENT_SCHEME)
def test_cannot_create_restricted_group_configuration(self, scheme_id):
"""
Test that you cannot create a restricted group configuration.
"""
group_config = dict(GROUP_CONFIGURATION_JSON)
group_config['scheme'] = scheme_id
group_config.setdefault('parameters', {})['course_id'] = str(self.course.id)
response = self.client.ajax_post(
self._url(),
data=group_config
)
self.assertEqual(response.status_code, 400) # noqa: PT009
@ddt.data(
(CONTENT_TYPE_GATING_SCHEME, CONTENT_GATING_PARTITION_ID),
(ENROLLMENT_SCHEME, ENROLLMENT_TRACK_PARTITION_ID),
)
@ddt.unpack
def test_cannot_edit_restricted_group_configuration(self, scheme_id, partition_id):
"""
Test that you cannot edit a restricted group configuration.
"""
group_config = dict(GROUP_CONFIGURATION_JSON)
group_config['scheme'] = scheme_id
group_config.setdefault('parameters', {})['course_id'] = str(self.course.id)
response = self.client.put(
self._url(cid=partition_id),
data=json.dumps(group_config),
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 400) # noqa: PT009
@ddt.ddt
class GroupConfigurationsUsageInfoTestCase(CourseTestCase, HelperMethods):
"""
Tests for usage information of configurations and content groups.
"""
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE
def _get_user_partition(self, scheme):
"""
Returns the first user partition with the specified scheme.
"""
for group in GroupConfiguration.get_all_user_partition_details(self.store, self.course):
if group['scheme'] == scheme:
return group
return None
def _get_expected_content_group(self, usage_for_group):
"""
Returns the expected configuration with particular usage.
"""
return {
'id': 0,
'name': 'Name 0',
'scheme': 'cohort',
'description': 'Description 0',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1, 'usage': []},
{'id': 1, 'name': 'Group B', 'version': 1, 'usage': usage_for_group},
{'id': 2, 'name': 'Group C', 'version': 1, 'usage': []},
],
'parameters': {},
'active': True,
}
def test_content_group_not_used(self):
"""
Test that right data structure will be created if content group is not used.
"""
self._add_user_partitions(scheme_id='cohort')
actual = self._get_user_partition('cohort')
expected = self._get_expected_content_group(usage_for_group=[])
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_correct_usage_info_when_special_characters_are_in_content(self):
"""
Test if content group json updated successfully with usage information.
"""
self._add_user_partitions(count=1, scheme_id='cohort')
vertical, __ = self._create_problem_with_content_group(
cid=0, group_id=1, name_suffix='0', special_characters="JOSÉ ANDRÉS"
)
actual = self._get_user_partition('cohort')
expected = self._get_expected_content_group(
usage_for_group=[
{
'url': f"/container/{vertical.location}",
'label': "Test Unit 0 / Test Problem 0JOSÉ ANDRÉS"
}
]
)
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_correct_usage_info_for_content_groups(self):
"""
Test if content group json updated successfully with usage information.
"""
self._add_user_partitions(count=1, scheme_id='cohort')
vertical, __ = self._create_problem_with_content_group(cid=0, group_id=1, name_suffix='0')
actual = self._get_user_partition('cohort')
expected = self._get_expected_content_group(usage_for_group=[
{
'url': f'/container/{vertical.location}',
'label': 'Test Unit 0 / Test Problem 0'
}
])
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_correct_usage_info_with_orphan(self):
"""
Test if content group json updated successfully with usage information
even if there is an orphan in content group.
"""
self.course = CourseFactory.create()
self._add_user_partitions(count=1, scheme_id='cohort')
vertical, __ = self._create_problem_with_content_group(cid=0, group_id=1, name_suffix='0', orphan=True)
# Assert that there is an orphan in the course, and that it's the vertical
self.assertEqual(len(self.store.get_orphans(self.course.id)), 1) # noqa: PT009
self.assertIn(vertical.location, self.store.get_orphans(self.course.id)) # noqa: PT009
# Get the expected content group information.
expected = self._get_expected_content_group(usage_for_group=[])
# Get the actual content group information
actual = self._get_user_partition('cohort')
# Assert that actual content group information is same as expected one.
self.assertEqual(actual, expected) # noqa: PT009
def test_can_use_one_content_group_in_multiple_problems(self):
"""
Test if multiple problems are present in usage info when they use same
content group.
"""
self._add_user_partitions(scheme_id='cohort')
vertical1, __ = self._create_problem_with_content_group(cid=0, group_id=1, name_suffix='1')
vertical, __ = self._create_problem_with_content_group(cid=0, group_id=1, name_suffix='0')
actual = self._get_user_partition('cohort')
expected = self._get_expected_content_group(usage_for_group=[
{
'url': f'/container/{vertical1.location}',
'label': 'Test Unit 1 / Test Problem 1'
},
{
'url': f'/container/{vertical.location}',
'label': 'Test Unit 0 / Test Problem 0'
}
])
self.assertEqual(actual, expected) # noqa: PT009
def test_group_configuration_not_used(self):
"""
Test that right data structure will be created if group configuration is not used.
"""
self._add_user_partitions()
actual = GroupConfiguration.get_split_test_partitions_with_usage(self.store, self.course)
expected = [{
'id': 0,
'name': 'Name 0',
'scheme': 'random',
'description': 'Description 0',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1},
{'id': 1, 'name': 'Group B', 'version': 1},
{'id': 2, 'name': 'Group C', 'version': 1},
],
'usage': [],
'parameters': {},
'active': True,
}]
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_correct_usage_info_for_split_test(self):
"""
When a split test is created and content group access is set for a problem within a group,
the usage info should return a url to the split test, not to the group.
"""
# Create user partition for groups in the split test,
# and another partition to set group access for the problem within the split test.
self._add_user_partitions(count=1)
self.course.user_partitions += [
UserPartition(
id=1,
name='Cohort User Partition',
scheme=UserPartition.get_scheme('cohort'),
description='Cohort User Partition',
groups=[
Group(id=3, name="Problem Group")
],
),
]
self.store.update_item(self.course, ModuleStoreEnum.UserID.test)
self.reload_course()
__, split_test, problem = self._create_content_experiment(cid=0, name_suffix='0', group_id=3, cid_for_problem=1) # lint-amnesty, pylint: disable=unused-variable
expected = {
'id': 1,
'name': 'Cohort User Partition',
'scheme': 'cohort',
'description': 'Cohort User Partition',
'version': UserPartition.VERSION,
'groups': [
{'id': 3, 'name': 'Problem Group', 'version': 1, 'usage': [
{
'url': f'/container/{split_test.location}',
'label': 'Condition 1 vertical / Test Problem'
}
]},
],
'parameters': {},
'active': True,
}
actual = self._get_user_partition('cohort')
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_correct_usage_info_for_unit(self):
"""
When group access is set on the unit level, the usage info should return a url to the unit, not
the sequential parent of the unit.
"""
self.course.user_partitions = [
UserPartition(
id=0,
name='User Partition',
scheme=UserPartition.get_scheme('cohort'),
description='User Partition',
groups=[
Group(id=0, name="Group")
],
),
]
vertical, __ = self._create_problem_with_content_group(
cid=0, group_id=0, name_suffix='0'
)
self.client.ajax_post(
reverse_usage_url("xblock_handler", vertical.location),
data={'metadata': {'group_access': {0: [0]}}}
)
actual = self._get_user_partition('cohort')
# order of usage list is arbitrary, sort for reliable comparison
actual['groups'][0]['usage'].sort(key=itemgetter('label'))
expected = {
'id': 0,
'name': 'User Partition',
'scheme': 'cohort',
'description': 'User Partition',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group', 'version': 1, 'usage': [
{
'url': f"/container/{vertical.location}",
'label': "Test Subsection 0 / Test Unit 0"
},
{
'url': f"/container/{vertical.location}",
'label': "Test Unit 0 / Test Problem 0"
}
]},
],
'parameters': {},
'active': True,
}
self.maxDiff = None
assert actual == expected
def test_can_get_correct_usage_info(self):
"""
Test if group configurations json updated successfully with usage information.
"""
self._add_user_partitions(count=2)
__, split_test, __ = self._create_content_experiment(cid=0, name_suffix='0')
self._create_content_experiment(name_suffix='1')
actual = GroupConfiguration.get_split_test_partitions_with_usage(self.store, self.course)
expected = [{
'id': 0,
'name': 'Name 0',
'scheme': 'random',
'description': 'Description 0',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1},
{'id': 1, 'name': 'Group B', 'version': 1},
{'id': 2, 'name': 'Group C', 'version': 1},
],
'usage': [{
'url': f'/container/{split_test.location}',
'label': 'Test Unit 0 / Test Content Experiment 0',
'validation': None,
}],
'parameters': {},
'active': True,
}, {
'id': 1,
'name': 'Name 1',
'scheme': 'random',
'description': 'Description 1',
'version': UserPartition.VERSION,
'groups': [
{'id': 0, 'name': 'Group A', 'version': 1},
{'id': 1, 'name': 'Group B', 'version': 1},
{'id': 2, 'name': 'Group C', 'version': 1},
],
'usage': [],
'parameters': {},
'active': True,
}]
self.assertEqual(actual, expected) # noqa: PT009
def test_can_get_usage_info_when_special_characters_are_used(self):
"""
Test if group configurations json updated successfully when special
characters are being used in content experiment
"""
self._add_user_partitions(count=1)
__, split_test, __ = self._create_content_experiment(cid=0, name_suffix='0', special_characters="JOSÉ ANDRÉS")
actual = GroupConfiguration.get_split_test_partitions_with_usage(self.store, self.course, )
expected = [{
'id': 0,