From b7d466c24260466e8f164c08da4fc0b50963fb8a Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Thu, 30 Apr 2026 08:32:52 -0700 Subject: [PATCH] test(mail): add mail_user and oauth_settings pytest fixtures --- src/paperless_mail/tests/conftest.py | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/paperless_mail/tests/conftest.py b/src/paperless_mail/tests/conftest.py index 75c33ac76..13cf87473 100644 --- a/src/paperless_mail/tests/conftest.py +++ b/src/paperless_mail/tests/conftest.py @@ -1,6 +1,7 @@ from collections.abc import Generator import pytest +from django.test import Client from paperless_mail.mail import MailAccountHandler from paperless_mail.models import MailAccount @@ -27,3 +28,38 @@ def greenmail_mail_account(db: None) -> Generator[MailAccount, None, None]: @pytest.fixture() def mail_account_handler() -> MailAccountHandler: return MailAccountHandler() + + +@pytest.fixture() +def mail_user( + db: None, + django_user_model, + client: Client, +): + """ + Create a user with the `add_mailaccount` permission and log them in via + the test client. Returned so tests can mutate permissions if needed. + """ + from django.contrib.auth.models import Permission + + user = django_user_model.objects.create_user("testuser") + user.user_permissions.add( + *Permission.objects.filter(codename__in=["add_mailaccount"]), + ) + user.save() + client.force_login(user) + return user + + +@pytest.fixture() +def oauth_settings(settings): + """ + Apply the OAuth callback / client-id settings the OAuth flow needs. Uses + pytest-django's `settings` fixture so values are reverted automatically. + """ + settings.OAUTH_CALLBACK_BASE_URL = "http://localhost:8000" + settings.GMAIL_OAUTH_CLIENT_ID = "test_gmail_client_id" + settings.GMAIL_OAUTH_CLIENT_SECRET = "test_gmail_client_secret" + settings.OUTLOOK_OAUTH_CLIENT_ID = "test_outlook_client_id" + settings.OUTLOOK_OAUTH_CLIENT_SECRET = "test_outlook_client_secret" + return settings