|
| 1 | +""" |
| 2 | +Test the enable/disable discussions for all units API endpoint. |
| 3 | +""" |
| 4 | +import json |
| 5 | + |
| 6 | +from django.urls import reverse |
| 7 | +from opaque_keys.edx.keys import CourseKey |
| 8 | +from xmodule.modulestore import ModuleStoreEnum |
| 9 | +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase |
| 10 | +from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory |
| 11 | + |
| 12 | +from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient |
| 13 | +from common.djangoapps.student.tests.factories import UserFactory |
| 14 | + |
| 15 | + |
| 16 | +class BulkEnableDisableDiscussionsTestCase(ModuleStoreTestCase): |
| 17 | + """ |
| 18 | + Test the enable/disable discussions for all units API endpoint. |
| 19 | + """ |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + super().setUp() |
| 23 | + self.user = UserFactory(is_staff=True, is_superuser=True) |
| 24 | + self.user.set_password(self.user_password) |
| 25 | + self.user.save() |
| 26 | + |
| 27 | + self.course_key = CourseKey.from_string("course-v1:edx+TestX+2025") |
| 28 | + |
| 29 | + self.url = reverse('bulk_enable_disable_discussions', args=[str(self.course_key)]) |
| 30 | + self.client = AjaxEnabledTestClient() |
| 31 | + self.client.login(username=self.user.username, password=self.user_password) |
| 32 | + |
| 33 | + # Create a test course |
| 34 | + self.course = CourseFactory.create( |
| 35 | + org=self.course_key.org, |
| 36 | + course=self.course_key.course, |
| 37 | + run=self.course_key.run, |
| 38 | + default_store=ModuleStoreEnum.Type.split, |
| 39 | + display_name="EnableDisableDiscussionsTestCase Course", |
| 40 | + ) |
| 41 | + with self.store.bulk_operations(self.course_key): |
| 42 | + section = BlockFactory.create( |
| 43 | + parent=self.course, |
| 44 | + category='chapter', |
| 45 | + display_name="Generated Section", |
| 46 | + ) |
| 47 | + sequence = BlockFactory.create( |
| 48 | + parent=section, |
| 49 | + category='sequential', |
| 50 | + display_name="Generated Sequence", |
| 51 | + ) |
| 52 | + unit1 = BlockFactory.create( |
| 53 | + parent=sequence, |
| 54 | + category='vertical', |
| 55 | + display_name="Unit in Section1", |
| 56 | + discussion_enabled=True, |
| 57 | + ) |
| 58 | + unit2 = BlockFactory.create( |
| 59 | + parent=sequence, |
| 60 | + category='vertical', |
| 61 | + display_name="Unit in Section2", |
| 62 | + discussion_enabled=True, |
| 63 | + ) |
| 64 | + |
| 65 | + def test_disable_discussions_for_all_units(self): |
| 66 | + """ |
| 67 | + Test that the API successfully disables discussions for all units. |
| 68 | + """ |
| 69 | + self.enable_disable_discussions_for_all_units(False) |
| 70 | + |
| 71 | + def test_enable_discussions_for_all_units(self): |
| 72 | + """ |
| 73 | + Test that the API successfully enables discussions for all units. |
| 74 | + """ |
| 75 | + self.enable_disable_discussions_for_all_units(True) |
| 76 | + |
| 77 | + def enable_disable_discussions_for_all_units(self, is_enabled): |
| 78 | + """ |
| 79 | + Test that the API successfully enables/disables discussions for all units. |
| 80 | + """ |
| 81 | + data = { |
| 82 | + "discussion_enabled": is_enabled |
| 83 | + } |
| 84 | + response = self.client.put(self.url, data=json.dumps(data), content_type='application/json') |
| 85 | + self.assertEqual(response.status_code, 200) |
| 86 | + response_data = response.json() |
| 87 | + print(response_data) |
| 88 | + self.assertEqual(response_data['updated_and_republished'], 0 if is_enabled else 2) |
| 89 | + |
| 90 | + # Check that all verticals now have discussion_enabled set to the expected value |
| 91 | + with self.store.bulk_operations(self.course_key): |
| 92 | + verticals = self.store.get_items(self.course_key, qualifiers={'block_type': 'vertical'}) |
| 93 | + for vertical in verticals: |
| 94 | + self.assertEqual(vertical.discussion_enabled, is_enabled) |
| 95 | + |
| 96 | + def test_permission_denied_for_non_staff(self): |
| 97 | + """ |
| 98 | + Test that non-staff users are denied access to the API. |
| 99 | + """ |
| 100 | + # Create a non-staff user |
| 101 | + non_staff_user = UserFactory(is_staff=False, is_superuser=False) |
| 102 | + non_staff_user.set_password(self.user_password) |
| 103 | + non_staff_user.save() |
| 104 | + |
| 105 | + # Create a new client for the non-staff user |
| 106 | + non_staff_client = AjaxEnabledTestClient() |
| 107 | + non_staff_client.login(username=non_staff_user.username, password=self.user_password) |
| 108 | + |
| 109 | + response = non_staff_client.put(self.url, content_type='application/json') |
| 110 | + self.assertEqual(response.status_code, 403) |
| 111 | + |
| 112 | + def test_badrequest_for_empty_request_body(self): |
| 113 | + """ |
| 114 | + Test that the API returns a 400 for an empty request body. |
| 115 | + """ |
| 116 | + response = self.client.put(self.url, data=json.dumps({}), content_type='application/json') |
| 117 | + self.assertEqual(response.status_code, 400) |
0 commit comments