diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index bb2be51e9640..f5d8b0408950 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -2252,17 +2252,26 @@ def test_modify_access_bad_role(self): }) assert response.status_code == 400 - def test_modify_access_allow(self): - assert CourseEnrollment.is_enrolled(self.other_user, self.course.id) is False - url = reverse('modify_access', kwargs={'course_id': str(self.course.id)}) - response = self.client.post(url, { - 'unique_student_identifier': self.other_user.email, - 'rolename': 'staff', - 'action': 'allow', - }) - assert response.status_code == 200 - # User should be auto enrolled in the course - assert CourseEnrollment.is_enrolled(self.other_user, self.course.id) + def test_modify_access_api(self): + for rolename in ["staff", "limited_staff", "instructor", "data_researcher"]: + assert CourseEnrollment.is_enrolled(self.other_user, self.course.id) is False + url = reverse('modify_access', kwargs={'course_id': str(self.course.id)}) + response = self.client.post(url, { + 'unique_student_identifier': self.other_user.email, + 'rolename': rolename, + 'action': 'allow', + }) + assert response.status_code == 200 + # User should be auto enrolled in the course + assert CourseEnrollment.is_enrolled(self.other_user, self.course.id) + # Test role revoke action + response = self.client.post(url, { + 'unique_student_identifier': self.other_user.email, + 'rolename': rolename, + 'action': 'revoke', + }) + assert response.status_code == 200 + CourseEnrollment.unenroll(self.other_user, self.course.id) def test_modify_access_allow_with_uname(self): url = reverse('modify_access', kwargs={'course_id': str(self.course.id)}) @@ -2273,15 +2282,6 @@ def test_modify_access_allow_with_uname(self): }) assert response.status_code == 200 - def test_modify_access_revoke(self): - url = reverse('modify_access', kwargs={'course_id': str(self.course.id)}) - response = self.client.post(url, { - 'unique_student_identifier': self.other_staff.email, - 'rolename': 'staff', - 'action': 'revoke', - }) - assert response.status_code == 200 - def test_modify_access_revoke_with_username(self): url = reverse('modify_access', kwargs={'course_id': str(self.course.id)}) response = self.client.post(url, { @@ -2443,6 +2443,19 @@ def assert_update_forum_role_membership(self, current_user, identifier, rolename elif action == 'revoke': assert rolename not in user_roles + def test_autoenroll_on_forum_role_add(self): + """ + Test forum role modification auto enrolls user. + """ + seed_permissions_roles(self.course.id) + user = UserFactory() + for rolename in ["Administrator", "Moderator", "Community TA"]: + assert CourseEnrollment.is_enrolled(user, self.course.id) is False + self.assert_update_forum_role_membership(user, user.email, rolename, "allow") + assert CourseEnrollment.is_enrolled(user, self.course.id) + self.assert_update_forum_role_membership(user, user.email, rolename, "revoke") + CourseEnrollment.unenroll(user, self.course.id) + @ddt.ddt class TestInstructorAPILevelsDataDump(SharedModuleStoreTestCase, LoginEnrollmentTestCase): diff --git a/lms/djangoapps/instructor/views/api.py b/lms/djangoapps/instructor/views/api.py index ddcfa7b7aba3..6d7dfe17a9d7 100644 --- a/lms/djangoapps/instructor/views/api.py +++ b/lms/djangoapps/instructor/views/api.py @@ -2812,7 +2812,8 @@ def update_forum_role_membership(request, course_id): )) user = get_student_from_identifier(unique_student_identifier) - + if action == 'allow' and not is_user_enrolled_in_course(user, course_id): + CourseEnrollment.enroll(user, course_id) try: update_forum_role(course_id, user, rolename, action) except Role.DoesNotExist: diff --git a/lms/templates/instructor/instructor_dashboard_2/membership.html b/lms/templates/instructor/instructor_dashboard_2/membership.html index 5a477665868a..9b1f64dfb9d1 100644 --- a/lms/templates/instructor/instructor_dashboard_2/membership.html +++ b/lms/templates/instructor/instructor_dashboard_2/membership.html @@ -229,8 +229,9 @@