def test_staff_masquerading_added_to_group(self):
        """
        Tests that initializer sets masquerading for a staff user in a group.
        """
        # Verify that there is no masquerading group initially
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id)
        )
        # User is staff by default, no content groups filter is set - see all
        self.assertNotIn('content_groups', filter_directory)

        # Install a masquerading group
        request = self._create_mock_json_request(
            self.global_staff,
            body='{"role": "student", "user_partition_id": 0, "group_id": 1}'
        )
        handle_ajax(request, unicode(self.course.id))

        # Call initializer
        LmsSearchInitializer.set_search_enviroment(
            request=request,
            course_id=unicode(self.course.id)
        )

        # Verify that there is masquerading group after masquerade
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id)
        )
        self.assertEqual(filter_directory['content_groups'], [unicode(1)])
    def test_staff_masquerading_added_to_group(self):
        """
        Tests that initializer sets masquerading for a staff user in a group.
        """
        # Verify that there is no masquerading group initially
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id))
        # User is staff by default, no content groups filter is set - see all
        self.assertNotIn('content_groups', filter_directory)

        # Install a masquerading group
        request = self._create_mock_json_request(
            self.global_staff,
            body='{"role": "student", "user_partition_id": 0, "group_id": 1}')
        handle_ajax(request, unicode(self.course.id))

        # Call initializer
        LmsSearchInitializer.set_search_enviroment(request=request,
                                                   course_id=unicode(
                                                       self.course.id))

        # Verify that there is masquerading group after masquerade
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id))
        self.assertEqual(filter_directory['content_groups'], [unicode(1)])
    def _verify_masquerade_for_group(self, group):
        """
        Verify that the masquerade works for the specified group id.
        """
        # Send the request to set the masquerade
        request_json = {
            "role": "student",
        }
        if group and self.user_partition:
            request_json['user_partition_id'] = self.user_partition.id
            request_json['group_id'] = group.id
        request = self._create_mock_json_request(
            self.test_user,
            body=json.dumps(request_json),
            session=self.session
        )
        handle_ajax(request, unicode(self.course.id))

        # Now setup the masquerade for the test user
        setup_masquerade(request, self.test_user, True)
        scheme = self.user_partition.scheme    # pylint: disable=no-member
        self.assertEqual(
            scheme.get_group_for_user(self.course.id, self.test_user, self.user_partition),
            group
        )
示例#4
0
def masquerade_as_group_member(user, course, partition_id, group_id):
    """
    Installs a masquerade for the specified user and course, to enable
    the user to masquerade as belonging to the specific partition/group
    combination.

    Arguments:
        user (User): a user.
        course (CourseDescriptor): a course.
        partition_id (int): the integer partition id, referring to partitions already
           configured in the course.
        group_id (int); the integer group id, within the specified partition.

    Returns: the status code for the AJAX response to update the user's masquerade for
        the specified course.
    """
    request = _create_mock_json_request(user,
                                        data={
                                            "role": "student",
                                            "user_partition_id": partition_id,
                                            "group_id": group_id
                                        })
    response = handle_ajax(request, unicode(course.id))
    setup_masquerade(request, course.id, True)
    return response.status_code
    def _verify_masquerade_for_group(self, group):
        """
        Verify that the masquerade works for the specified group id.
        """
        # Send the request to set the masquerade
        request_json = {
            "role": "student",
            "user_partition_id": self.user_partition.id,
            "group_id": group.id if group is not None else None
        }
        request = self._create_mock_json_request(
            self.test_user,
            data=request_json,
            session=self.session
        )
        response = handle_ajax(request, unicode(self.course.id))
        # pylint has issues analyzing this class (maybe due to circular imports?)
        self.assertEquals(response.status_code, 200)  # pylint: disable=no-member

        # Now setup the masquerade for the test user
        setup_masquerade(request, self.course.id, True)
        scheme = self.user_partition.scheme
        self.assertEqual(
            scheme.get_group_for_user(self.course.id, self.test_user, self.user_partition),
            group
        )
    def _verify_masquerade_for_group(self, group):
        """
        Verify that the masquerade works for the specified group id.
        """
        # Send the request to set the masquerade
        request_json = {
            "role": "student",
            "user_partition_id": self.user_partition.id,
            "group_id": group.id if group is not None else None,
        }
        request = self._create_mock_json_request(self.test_user, body=json.dumps(request_json), session=self.session)
        handle_ajax(request, unicode(self.course.id))

        # Now setup the masquerade for the test user
        setup_masquerade(request, self.test_user, True)
        scheme = self.user_partition.scheme
        self.assertEqual(scheme.get_group_for_user(self.course.id, self.test_user, self.user_partition), group)
示例#7
0
    def test_group_masquerade(self):
        """
        Tests that a staff member can masquerade as being in a particular group.
        """
        # Verify that there is no masquerading group initially
        group_id, user_partition_id = get_masquerading_group_info(self.test_user, self.course.id)
        self.assertIsNone(group_id)
        self.assertIsNone(user_partition_id)

        # Install a masquerading group
        request = self._create_mock_json_request(
            self.test_user, body='{"role": "student", "user_partition_id": 0, "group_id": 1}'
        )
        handle_ajax(request, unicode(self.course.id))
        setup_masquerade(request, self.test_user, True)

        # Verify that the masquerading group is returned
        group_id, user_partition_id = get_masquerading_group_info(self.test_user, self.course.id)
        self.assertEqual(group_id, 1)
        self.assertEqual(user_partition_id, 0)
    def test_staff_masquerading_as_a_staff_user(self):
        """
        Tests that initializer sets masquerading for a staff user as staff.
        """

        # Install a masquerading group
        request = self._create_mock_json_request(self.global_staff,
                                                 body='{"role": "staff"}')
        handle_ajax(request, unicode(self.course.id))

        # Call initializer
        LmsSearchInitializer.set_search_enviroment(request=request,
                                                   course_id=unicode(
                                                       self.course.id))

        # Verify that there is masquerading group after masquerade
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id))
        self.assertNotIn('content_groups', filter_directory)
示例#9
0
    def _verify_masquerade_for_group(self, group):
        """
        Verify that the masquerade works for the specified group id.
        """
        # Send the request to set the masquerade
        request_json = {
            "role": "student",
            "user_partition_id": self.user_partition.id,
            "group_id": group.id if group is not None else None
        }
        request = self._create_mock_json_request(self.test_user,
                                                 body=json.dumps(request_json),
                                                 session=self.session)
        handle_ajax(request, unicode(self.course.id))

        # Now setup the masquerade for the test user
        setup_masquerade(request, self.test_user, True)
        scheme = self.user_partition.scheme  # pylint: disable=no-member
        self.assertEqual(
            scheme.get_group_for_user(self.course.id, self.test_user,
                                      self.user_partition), group)
示例#10
0
    def test_group_masquerade(self):
        """
        Tests that a staff member can masquerade as being in a particular group.
        """
        # Verify that there is no masquerading group initially
        group_id, user_partition_id = get_masquerading_group_info(self.test_user, self.course.id)
        self.assertIsNone(group_id)
        self.assertIsNone(user_partition_id)

        # Install a masquerading group
        request = self._create_mock_json_request(
            self.test_user,
            body='{"role": "student", "user_partition_id": 0, "group_id": 1}'
        )
        handle_ajax(request, unicode(self.course.id))
        setup_masquerade(request, self.test_user, True)

        # Verify that the masquerading group is returned
        group_id, user_partition_id = get_masquerading_group_info(self.test_user, self.course.id)
        self.assertEqual(group_id, 1)
        self.assertEqual(user_partition_id, 0)
    def test_staff_masquerading_as_a_student_user(self):
        """
        Tests that initializer sets masquerading for a staff user as student.
        """

        # Install a masquerading group
        request = self._create_mock_json_request(
            self.global_staff,
            body='{"role": "student"}'
        )
        handle_ajax(request, unicode(self.course.id))

        # Call initializer
        LmsSearchInitializer.set_search_enviroment(
            request=request,
            course_id=unicode(self.course.id)
        )

        # Verify that there is masquerading group after masquerade
        _, filter_directory, _ = LmsSearchFilterGenerator.generate_field_filters(  # pylint: disable=unused-variable
            user=self.global_staff,
            course_id=unicode(self.course.id)
        )
        self.assertEqual(filter_directory['content_groups'], None)
示例#12
0
def masquerade_as_group_member(user, course, partition_id, group_id):
    """
    Installs a masquerade for the specified user and course, to enable
    the user to masquerade as belonging to the specific partition/group
    combination.

    Arguments:
        user (User): a user.
        course (CourseDescriptor): a course.
        partition_id (int): the integer partition id, referring to partitions already
           configured in the course.
        group_id (int); the integer group id, within the specified partition.

    Returns: the status code for the AJAX response to update the user's masquerade for
        the specified course.
    """
    request = _create_mock_json_request(
        user,
        data={"role": "student", "user_partition_id": partition_id, "group_id": group_id}
    )
    response = handle_ajax(request, unicode(course.id))
    setup_masquerade(request, course.id, True)
    return response.status_code
示例#13
0
    def _verify_masquerade_for_group(self, group):
        """
        Verify that the masquerade works for the specified group id.
        """
        # Send the request to set the masquerade
        request_json = {
            "role": "student",
            "user_partition_id": self.user_partition.id,
            "group_id": group.id if group is not None else None
        }
        request = self._create_mock_json_request(self.test_user,
                                                 data=request_json,
                                                 session=self.session)
        response = handle_ajax(request, unicode(self.course.id))
        # pylint has issues analyzing this class (maybe due to circular imports?)
        self.assertEquals(response.status_code, 200)  # pylint: disable=no-member

        # Now setup the masquerade for the test user
        setup_masquerade(request, self.course.id, True)
        scheme = self.user_partition.scheme
        self.assertEqual(
            scheme.get_group_for_user(self.course.id, self.test_user,
                                      self.user_partition), group)