From 4ab7546dc30fb6393d8cab606e869018e4c970be Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Tue, 6 May 2025 18:57:16 +0200 Subject: [PATCH] More tests --- modoboa/webmail/lib/imaputils.py | 11 ++--------- modoboa/webmail/lib/sendmail.py | 9 +++++++-- modoboa/webmail/tests/data.py | 8 ++++++++ modoboa/webmail/tests/test_viewsets.py | 7 +++++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/modoboa/webmail/lib/imaputils.py b/modoboa/webmail/lib/imaputils.py index f26d494d5..db51b6dde 100644 --- a/modoboa/webmail/lib/imaputils.py +++ b/modoboa/webmail/lib/imaputils.py @@ -1,6 +1,5 @@ """Extra IMAPv4 utilities.""" -import base64 import email from functools import wraps import imaplib @@ -13,6 +12,7 @@ from django.conf import settings from django.utils.translation import gettext as _ from modoboa.lib import imap_utf7 # noqa +from modoboa.lib import oauth2 from modoboa.lib.exceptions import InternalError from modoboa.parameters import tools as param_tools @@ -255,14 +255,7 @@ class IMAPconnector: raise ImapError(_(f"Connection to IMAP server failed: {error}")) from None if not settings.WEBMAIL_DEV_MODE: - token = ( - b"n,a=" - + user.encode("utf-8") - + b",\001auth=Bearer " - + rawtoken.encode("utf-8") - + b"\001\001" - ) - token = base64.b64encode(token) + token = oauth2.build_oauthbearer_string(user, rawtoken) data = self._cmd("AUTHENTICATE", b"OAUTHBEARER", token) else: user = bytes(settings.WEBMAIL_DEV_USERNAME, "utf-8") diff --git a/modoboa/webmail/lib/sendmail.py b/modoboa/webmail/lib/sendmail.py index a419478af..2b19a8065 100644 --- a/modoboa/webmail/lib/sendmail.py +++ b/modoboa/webmail/lib/sendmail.py @@ -155,8 +155,13 @@ def send_mail( options.update({"use_tls": True}) if conf["smtp_authentication"]: if not settings.WEBMAIL_DEV_MODE: - # TODO: oauth2 auth - pass + options.update( + { + "backend": "modoboa.lib.smtp_backend.OAuthBearerEmailBackend", + "username": request.user.email, + "password": request.auth, + } + ) else: options.update( { diff --git a/modoboa/webmail/tests/data.py b/modoboa/webmail/tests/data.py index 9cf9a3c1a..09a84d5a0 100644 --- a/modoboa/webmail/tests/data.py +++ b/modoboa/webmail/tests/data.py @@ -125,6 +125,14 @@ BODYSTRUCTURE_EMPTY_MAIL_WITH_HEADERS = [ EMPTY_BODY = [(b"33 (UID 33 BODY[1] {0}", b""), b")"] +BODYSTRUCTURE_WITH_PDF = [ + ( + b'1000 (UID 3444 BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7BIT" 123 4 NIL NIL NIL)("application" "pdf" ("name" "file.pdf") NIL NIL "base64" 789 NIL ("attachment" ("filename" "file.pdf")) NIL NIL) "mixed" ("boundary" "--boundary_32281_b52cf564-2a50-4f96-aeb0-ef5f83b05463") NIL NIL NIL) BODY[2] {752}', + b"JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAyIDAgUgo+PgplbmRvYmoKMiAwIG9iago8PC9UeXBlIC9QYWdlcwovS2lkcyBbMyAwIFJdCi9Db3VudCAxCj4+CmVuZG9iagozIDAgb2JqCjw8L1R5cGUgL1BhZ2UKL1BhcmVudCAyIDAgUgovUmVzb3VyY2VzIDw8L0ZvbnQgPDwKL0YxIDQgMCBSCj4+Cj4+Ci9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKNSAwIG9iago8PC9MZW5ndGggNzYKc3RyZWFtCkJUIApIZWxsbyBXb3JsZCENCkVUCmVuZHN0cmVhbQplbmRvYmoKNCAwIG9iago8PC9UeXBlIC9Gb250Ci9TdWJ0eXBlIC9UcnVlVHlwZQovTmFtZSAvRjEKL0Jhc2VGb250IC9IZWx2ZXRpY2ENCj4+CmVuZG9iagp4cmVmCjAgNgowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDEgMDAwMDAgbiAKMDAwMDAwMDA2MCAwMDAwMCBuIAowMDAwMDAwMTIyIDAwMDAwIG4gCjAwMDAwMDAyMzAgMDAwMDAgbiAKMDAwMDAwMDM0MCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDYKL1Jvb3QgMSAwIFIKL0luZm8gNiAwIFIKPj4Kc3RhcnR4cmVmCjQ1MwolJUVPRgo=", + ), + b")", +] + COMPLETE_MAIL = [ ( b"109 (UID 133872 BODY[] {2504}", diff --git a/modoboa/webmail/tests/test_viewsets.py b/modoboa/webmail/tests/test_viewsets.py index 030da4354..744215dcf 100644 --- a/modoboa/webmail/tests/test_viewsets.py +++ b/modoboa/webmail/tests/test_viewsets.py @@ -93,6 +93,8 @@ class IMAP4Mock: data = tests_data.BODYSTRUCTURE_EMPTY_MAIL_WITH_HEADERS else: data = tests_data.EMPTY_BODY + elif uid == 3444: + data = tests_data.BODYSTRUCTURE_WITH_PDF elif uid == 133872: data = tests_data.COMPLETE_MAIL return "OK", data @@ -296,6 +298,11 @@ class UserEmailViewSetTestCase(WebmailTestCase): url = reverse("v2:webmail-email-attachment") response = self.client.get(url) self.assertEqual(response.status_code, 404) + response = self.client.get(f"{url}?mailbox=INBOX&mailid=3444&partnum=2") + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.headers["Content-Disposition"], "attachment; filename=attachment" + ) class ComposeSessionViewSetTestCase(WebmailTestCase):