-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base64 encoded attachments to mails (#61)
- Loading branch information
1 parent
94bf0ee
commit 0c58dea
Showing
6 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2023 University of Münster. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Errors and exceptions for mail module.""" | ||
|
||
|
||
class AttachmentOversizeException(Exception): | ||
"""Size of attachment is too big exception.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2015-2018 CERN. | ||
# Copyright (C) 2023 University of Münster. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
@@ -11,12 +12,8 @@ | |
|
||
from __future__ import absolute_import, print_function | ||
|
||
import os | ||
|
||
import pkg_resources | ||
from flask_mail import Attachment | ||
|
||
from invenio_mail.tasks import send_email | ||
from invenio_mail.errors import AttachmentOversizeException | ||
from invenio_mail.tasks import send_email, send_email_with_attachments | ||
|
||
|
||
def test_send_message_outbox(email_task_app): | ||
|
@@ -70,3 +67,58 @@ def test_send_message_with_date(email_task_app): | |
|
||
result_stream = email_task_app.extensions["invenio-mail"].stream | ||
assert result_stream.getvalue().find("Date: Tue, 23 Feb 2016") != -1 | ||
|
||
|
||
def test_send_message_stream_with_attachment(email_task_app): | ||
"""Test sending a message with attachment using Task module.""" | ||
with email_task_app.app_context(): | ||
with email_task_app.extensions["mail"].record_messages() as outbox: | ||
msg = { | ||
"subject": "Test2", | ||
"sender": "[email protected]", | ||
"recipients": ["[email protected]"], | ||
} | ||
attachments = { | ||
"attachments": [ | ||
{ | ||
"base64": "RWluIGVpbmZhY2hlciBTdHJpbmcK", | ||
"disposition": "filename.bin", | ||
}, | ||
] | ||
} | ||
send_email_with_attachments(msg, attachments) | ||
|
||
result_stream = email_task_app.extensions["invenio-mail"].stream | ||
assert ( | ||
result_stream.getvalue().find("Content-Type: application/octet-stream") | ||
!= -1 | ||
) | ||
assert ( | ||
result_stream.getvalue().find("Content-Disposition: filename.bin;") | ||
!= -1 | ||
) | ||
assert result_stream.getvalue().find("RWluIGVpbmZhY2hlciBTdHJpbmcK") != -1 | ||
|
||
|
||
def test_send_message_stream_with_oversize_attachment(email_task_app): | ||
"""Test sending a message with oversize attachment.""" | ||
with email_task_app.app_context(): | ||
with email_task_app.extensions["mail"].record_messages() as outbox: | ||
msg = { | ||
"subject": "Test2", | ||
"sender": "[email protected]", | ||
"recipients": ["[email protected]"], | ||
} | ||
attachments = { | ||
"attachments": [ | ||
{ | ||
"base64": "RGllcyBpc3QgZGFzIEhhdXMgdm9tIE5pa29sYXVzCg==", | ||
"disposition": "filename.bin", | ||
}, | ||
] | ||
} | ||
try: | ||
send_email_with_attachments(msg, attachments) | ||
assert False | ||
except AttachmentOversizeException: | ||
assert True |