Ejemplo n.º 1
0
    def test_get_readable_forums_super_user(self):
        """
        Super user has access to all the forum filtered on LTIContext and
        archived ones but don't need to have access in reading.
        """
        super_user = UserFactory(is_superuser=True)
        basic_user = UserFactory()
        lti_context_a = LTIContextFactory(lti_consumer=super_user.lti_consumer)
        lti_context_b = LTIContextFactory(lti_consumer=super_user.lti_consumer)

        # Create 2 forums for context A
        forum_a1 = ForumFactory(name="Forum A1")
        forum_a1.lti_contexts.add(lti_context_a)
        forum_a2_archived = ForumFactory(name="Forum A2")
        forum_a2_archived.lti_contexts.add(lti_context_a)
        forum_a2_archived.archived = True
        forum_a2_archived.save()
        # Create 1 forum for context B
        forum_b1 = ForumFactory(name="Forum B1")
        forum_b1.lti_contexts.add(lti_context_b)

        # Instantiate the permission Handler
        permission_handler = PermissionHandler()

        forums_qs = Forum.objects.all()
        # With no lti_context super_user can see all forums
        readable_forums = permission_handler.get_readable_forums(forums_qs, super_user)
        self.assertCountEqual(readable_forums, [forum_a1, forum_b1])
        # standard user can't see any
        readable_forums = permission_handler.get_readable_forums(forums_qs, basic_user)
        self.assertCountEqual(readable_forums, [])

        # Inject a LTI context into the permission handler and ensure that
        # the results are filtered according to it
        permission_handler.current_lti_context_id = lti_context_a.id
        readable_forums = permission_handler.get_readable_forums(forums_qs, super_user)
        # even if super user has no can_read_forum permission is still can see all
        # unarchived forums of his lti_context
        self.assertCountEqual(readable_forums, [forum_a1])

        # standard user can't see any
        readable_forums = permission_handler.get_readable_forums(forums_qs, basic_user)
        self.assertCountEqual(readable_forums, [])
Ejemplo n.º 2
0
    def test_forum_moderation_list_forums_with_deleted_form(self):
        """
        Create three forums in the same LTIContext and archive one. Load the form to choose
        to which forum the user wants to move the topic. Control that the forum deleted is
        not part of the choice.
        """
        user = UserFactory()

        lti_context = LTIContextFactory(lti_consumer=user.lti_consumer)
        forum1 = ForumFactory(name="Forum1")
        forum2 = ForumFactory(name="Forum2")
        forum3 = ForumFactory(name="Forum3")

        lti_context.sync_user_groups(user, ["instructor"])

        # Assign permission to the group for this forum
        self._init_forum(forum1, lti_context)
        self._init_forum(forum2, lti_context)
        self._init_forum(forum3, lti_context)
        # Create a post for a topic part of lti_context
        topicForum1 = TopicFactory(forum=forum1)
        PostFactory(topic=topicForum1, )
        topicForum2 = TopicFactory(forum=forum2)
        PostFactory(topic=topicForum2, )
        topicForum3 = TopicFactory(forum=forum3)
        PostFactory(topic=topicForum3, )

        # Create the session and logged in lti_context
        self.client.force_login(user, "ashley.auth.backend.LTIBackend")
        session = self.client.session
        session[SESSION_LTI_CONTEXT_ID] = lti_context.id
        session.save()

        form = TopicMoveForm(user=user,
                             lti_context=lti_context,
                             topic=topicForum1)

        # Check that only the forum that is allowed is proposed as choice
        self.assertEqual(
            form.fields["forum"].choices,
            [
                (forum1.id, {
                    "label": " Forum1",
                    "disabled": True
                }),
                (
                    forum2.id,
                    "{} {}".format("-" * forum2.margin_level, forum2.name),
                ),
                (
                    forum3.id,
                    "{} {}".format("-" * forum3.margin_level, forum3.name),
                ),
            ],
        )

        # Archive the forum2
        forum2.archived = True
        forum2.save()

        form = TopicMoveForm(user=user,
                             lti_context=lti_context,
                             topic=topicForum1)

        # Check that forum2 is not proposed as choice anymore as it has been archived
        self.assertEqual(
            form.fields["forum"].choices,
            [
                (forum1.id, {
                    "label": " Forum1",
                    "disabled": True
                }),
                (
                    forum3.id,
                    "{} {}".format("-" * forum3.margin_level, forum3.name),
                ),
            ],
        )

        # We try to move topic from forum1 to forum2 even if forum2 has been archived
        response = self.client.post(
            f"/forum/moderation/topic/{topicForum1.slug}-{topicForum1.id}/move/",
            data={"forum": {forum2.id}},
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        # Control that we get an error and the move is not executed
        self.assertContains(
            response,
            f"Select a valid choice. {forum2.id} is not one of the available choices.",
            html=True,
        )
Ejemplo n.º 3
0
    def test_forum_search_archived_form_search(self):
        """
        Create different forums in the same lti_context, make sure user can't
        select archived forums in the advanced search.
        """
        user = UserFactory()

        lti_context = LTIContextFactory(lti_consumer=user.lti_consumer)
        forum = ForumFactory()
        forum2 = ForumFactory()
        forum3 = ForumFactory()
        forum.lti_contexts.add(lti_context)
        forum2.lti_contexts.add(lti_context)
        forum3.lti_contexts.add(lti_context)

        PostFactory(topic=TopicFactory(forum=forum), )

        PostFactory(topic=TopicFactory(forum=forum2), )
        PostFactory(topic=TopicFactory(forum=forum3), )
        # Creates the session
        self.client.force_login(user, "ashley.auth.backend.LTIBackend")
        session = self.client.session
        session[SESSION_LTI_CONTEXT_ID] = lti_context.id
        session.save()

        assign_perm("can_read_forum", user, forum)
        assign_perm("can_read_forum", user, forum2)
        assign_perm("can_read_forum", user, forum3)

        # Load the advanced search form
        form = SearchForm(user=user, lti_context=lti_context)

        # Check that only forums that are allowed are proposed as choice
        self.assertEqual(
            form.fields["search_forums"].choices,
            [
                (forum.id, "{} {}".format("-" * forum.margin_level,
                                          forum.name)),
                (forum2.id, "{} {}".format("-" * forum.margin_level,
                                           forum2.name)),
                (forum3.id, "{} {}".format("-" * forum.margin_level,
                                           forum3.name)),
            ],
        )

        # Archive the forum2
        forum2.archived = True
        forum2.save()

        form = SearchForm(user=user, lti_context=lti_context)

        # Check that forum2 is not proposed as choice anymore as it has been archived
        self.assertEqual(
            form.fields["search_forums"].choices,
            [
                (forum.id, "{} {}".format("-" * forum.margin_level,
                                          forum.name)),
                (forum3.id, "{} {}".format("-" * forum.margin_level,
                                           forum3.name)),
            ],
        )

        # Despite that, we force the request on the forum that is not allowed
        response = self.client.get(
            f"/forum/search/?q=world&search_forums={forum2.id}")
        self.assertEqual(response.status_code, 200)
        # Control that we get an error and the search is not executed
        self.assertContains(
            response,
            f"Select a valid choice. {forum2.id} is not one of the available choices.",
            html=True,
        )