Example #1
0
    def test_apply_groups_admin(self):
        """Test application of groups by role, relating to an admin user."""
        handler = AllauthSignalListener()

        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # Apply groups based on admin role being present on Discord
        handler._apply_groups(self.discord_admin, self.social_admin)
        self.assertTrue(self.admin_group in self.django_admin.groups.all())

        # Remove groups based on the user apparently leaving the server
        handler._apply_groups(self.discord_admin, self.social_admin, True)
        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # Apply the admin role again
        handler._apply_groups(self.discord_admin, self.social_admin)

        # Remove all of the roles from the user
        self.discord_admin.roles.clear()

        # Remove groups based on the user no longer having the admin role on Discord
        handler._apply_groups(self.discord_admin, self.social_admin)
        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        self.discord_admin.roles.append(self.admin_role.id)
        self.discord_admin.save()
Example #2
0
    def test_apply_groups_moderator(self):
        """Test application of groups by role, relating to a non-`is_staff` moderator user."""
        handler = AllauthSignalListener()

        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # Apply groups based on moderator role being present on Discord
        handler._apply_groups(self.discord_moderator, self.social_moderator)
        self.assertTrue(self.moderator_group in self.django_moderator.groups.all())

        # Remove groups based on the user apparently leaving the server
        handler._apply_groups(self.discord_moderator, self.social_moderator, True)
        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # Apply the moderator role again
        handler._apply_groups(self.discord_moderator, self.social_moderator)

        # Remove all of the roles from the user
        self.discord_moderator.roles.clear()

        # Remove groups based on the user no longer having the moderator role on Discord
        handler._apply_groups(self.discord_moderator, self.social_moderator)
        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        self.discord_moderator.roles.append(self.moderator_role.id)
        self.discord_moderator.save()
Example #3
0
    def test_model_save(self):
        """Test signal handling for when Discord user model objects are saved to DB."""
        mock_obj = mock.Mock()

        with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
            AllauthSignalListener()

            post_save.send(
                DiscordUser,
                instance=self.discord_user,
                raw=True,
                created=None,        # Not realistic, but we don't use it
                using=None,          # Again, we don't use it
                update_fields=False  # Always false during integration testing
            )

            mock_obj.assert_not_called()

            post_save.send(
                DiscordUser,
                instance=self.discord_user,
                raw=False,
                created=None,        # Not realistic, but we don't use it
                using=None,          # Again, we don't use it
                update_fields=False  # Always false during integration testing
            )

            mock_obj.assert_called_with(self.discord_user, self.social_user)
Example #4
0
    def test_apply_groups_other(self):
        """Test application of groups by role, relating to non-standard cases."""
        handler = AllauthSignalListener()

        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # No groups should be applied when there's no user account yet
        handler._apply_groups(self.discord_unmapped, self.social_unmapped)
        self.assertEqual(self.django_user_discordless.groups.all().count(), 0)

        # No groups should be applied when there are only unmapped roles to match
        handler._apply_groups(self.discord_unmapped, self.social_user)
        self.assertEqual(self.django_user.groups.all().count(), 0)

        # No groups should be applied when the user isn't in the guild
        handler._apply_groups(self.discord_not_in_guild, self.social_user)
        self.assertEqual(self.django_user.groups.all().count(), 0)
Example #5
0
    def test_logged_in(self):
        """Test the user-logged-in Allauth signal handling."""
        mock_obj = mock.Mock()

        with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
            AllauthSignalListener()

            # Don't attempt to apply groups if the user doesn't have a linked Discord account
            user_logged_in.send(DjangoUser, user=self.django_user_discordless)
            mock_obj.assert_not_called()

            # Don't attempt to apply groups if the user hasn't joined the Discord server
            user_logged_in.send(DjangoUser, user=self.django_user_never_joined)
            mock_obj.assert_not_called()

            # Attempt to apply groups if everything checks out
            user_logged_in.send(DjangoUser, user=self.django_user)
            mock_obj.assert_called_with(self.discord_user, self.social_user)
Example #6
0
    def test_social_removed(self):
        """Test the social-account-removed Allauth signal handling."""
        mock_obj = mock.Mock()

        with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
            AllauthSignalListener()

            # Don't attempt to remove groups if the user doesn't have a linked Discord account
            social_account_removed.send(SocialLogin, socialaccount=self.social_user_github)
            mock_obj.assert_not_called()

            # Don't attempt to remove groups if the social account doesn't map to a Django user
            social_account_removed.send(SocialLogin, socialaccount=self.social_unmapped)
            mock_obj.assert_not_called()

            # Attempt to remove groups if everything checks out
            social_account_removed.send(SocialLogin, socialaccount=self.social_user)
            mock_obj.assert_called_with(self.discord_user, self.social_user, deletion=True)
Example #7
0
    def test_social_updated(self):
        """Test the social-account-updated Allauth signal handling."""
        mock_obj = mock.Mock()

        discord_login = SocialLogin(self.django_user, self.social_user)
        github_login = SocialLogin(self.django_user, self.social_user_github)
        unmapped_login = SocialLogin(self.django_user, self.social_unmapped)

        with mock.patch.object(AllauthSignalListener, "_apply_groups", mock_obj):
            AllauthSignalListener()

            # Don't attempt to apply groups if the user doesn't have a linked Discord account
            social_account_updated.send(SocialLogin, sociallogin=github_login)
            mock_obj.assert_not_called()

            # Don't attempt to apply groups if the user hasn't joined the Discord server
            social_account_updated.send(SocialLogin, sociallogin=unmapped_login)
            mock_obj.assert_not_called()

            # Attempt to apply groups if everything checks out
            social_account_updated.send(SocialLogin, sociallogin=discord_login)
            mock_obj.assert_called_with(self.discord_user, self.social_user)
Example #8
0
    def ready(self) -> None:
        """Run when the app has been loaded and is ready to serve requests."""
        from pydis_site.apps.home.signals import AllauthSignalListener

        self.signal_listener = AllauthSignalListener()
        self.patch_allauth()