Esempio n. 1
0
    def test_get_user_roles(self):
        """get_user_roles should return the list of the roles names
        without the specific patterns cg:{context.id}:role: only the label
        of the roles
        """
        lti_consumer = LTIConsumerFactory()
        context = LTIContextFactory(lti_consumer=lti_consumer)

        # Create two users
        user = UserFactory(lti_consumer=lti_consumer)
        user2 = UserFactory(lti_consumer=lti_consumer)

        # Sync user groups in context with multiple roles
        context.sync_user_groups(user, ["role1", "role2"])
        # check that the list of roles is returned
        self.assertCountEqual(
            ["role1", "role2"],
            context.get_user_roles(user),
        )
        # add an extra group
        instructor_group = Group.objects.create(
            name=f"{context.base_group_name}:role:instructor")
        user.groups.add(instructor_group)
        # check that the list of roles is returned
        self.assertCountEqual(
            ["role1", "role2", "instructor"],
            context.get_user_roles(user),
        )
        # check that group that are not roles get ignored
        other_group = Group.objects.create(
            name=f"{context.base_group_name}:other)")
        user2.groups.add(other_group)
        self.assertCountEqual(
            [],
            context.get_user_roles(user2),
        )
Esempio n. 2
0
    def test_get_user_roles_other_context(self):
        """get_user_roles should return the list of the roles names without the specific
        patterns cg:{context.id}:role: only the label of the roles and only for the
        current context"""
        lti_consumer = LTIConsumerFactory()
        context = LTIContextFactory(lti_consumer=lti_consumer)
        context2 = LTIContextFactory(lti_consumer=lti_consumer)
        # Create user
        user = UserFactory(lti_consumer=lti_consumer)

        # Sync user groups in context with multiple roles
        context.sync_user_groups(user, ["role1", "role2"])
        # Sync user groups in context2 with other roles)
        context2.sync_user_groups(user, ["newgroup"])

        # check that the list of roles is returned
        self.assertCountEqual(
            ["role1", "role2"],
            context.get_user_roles(user),
        )
        self.assertCountEqual(
            ["newgroup"],
            context2.get_user_roles(user),
        )
Esempio n. 3
0
    def test_sync_user_with_internal_moderators_groups(self):
        """
        Test that internal group moderator doesn't get removed using sync_user_groups
        """
        lti_consumer = LTIConsumerFactory()
        context = LTIContextFactory(lti_consumer=lti_consumer)
        # Initialize a user with no group
        user = UserFactory(lti_consumer=lti_consumer)
        self.assertEqual(0, user.groups.count())

        # Sync user groups in context with role "student"
        context.sync_user_groups(user, ["student"])
        self.assertCountEqual(
            [
                context.base_group_name,
                f"{context.base_group_name}:role:student"
            ],
            list(user.groups.values_list("name", flat=True)),
        )
        # Add the user to moderator group
        group_moderator_name = context.get_group_role_name(
            _FORUM_ROLE_MODERATOR)
        group_moderator = Group.objects.create(name=group_moderator_name)
        user.groups.add(group_moderator)
        user.save()
        # confirm group has been added
        self.assertCountEqual(
            [
                context.base_group_name,
                f"{context.base_group_name}:role:student",
                f"{context.base_group_name}:role:moderator",
            ],
            list(user.groups.values_list("name", flat=True)),
        )

        # Sync user groups in context with funnyrole group
        context.sync_user_groups(user, ["funnyrole"])
        self.assertCountEqual(
            ["funnyrole", "moderator"],
            context.get_user_roles(user),
        )
        # User should still have the moderator group
        self.assertCountEqual(
            [
                context.base_group_name,
                f"{context.base_group_name}:role:funnyrole",
                f"{context.base_group_name}:role:moderator",
            ],
            list(user.groups.values_list("name", flat=True)),
        )

        # creates a new context
        context2 = LTIContextFactory(lti_consumer=lti_consumer)
        context2.sync_user_groups(user, ["instructor"])
        # moderator group should not exist in this context
        self.assertCountEqual(
            [
                context.base_group_name,
                f"{context.base_group_name}:role:funnyrole",
                f"{context.base_group_name}:role:moderator",
                context2.base_group_name,
                f"{context2.base_group_name}:role:instructor",
            ],
            list(user.groups.values_list("name", flat=True)),
        )