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 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)
    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)