evalutils: add attachment(content, filename, content_type) helper (#27323)
To allow creating mail attachments easily, test contains an example sending a full JSON export of a formdata.
This commit is contained in:
parent
7d05ea914f
commit
735058211a
|
@ -1,3 +1,4 @@
|
|||
import json
|
||||
import datetime
|
||||
import os
|
||||
import pytest
|
||||
|
@ -1257,6 +1258,23 @@ def test_email_attachments(pub, emails):
|
|||
assert emails.emails['foobar']['msg'].get_payload()[2].get_payload() == 'blah'
|
||||
assert len(emails.emails['foobar']['msg'].get_payload()) == 3
|
||||
|
||||
emails.empty()
|
||||
sendmail.attachments = [
|
||||
'utils.attachment(form.formdata.export_to_json(include_files=False))',
|
||||
'utils.attachment(form.formdata.export_to_json(), content_type=\'application/json\')',
|
||||
]
|
||||
sendmail.perform(formdata)
|
||||
get_response().process_after_jobs()
|
||||
assert emails.count() == 1
|
||||
assert emails.emails['foobar']['msg'].is_multipart()
|
||||
assert emails.emails['foobar']['msg'].get_content_subtype() == 'mixed'
|
||||
assert emails.emails['foobar']['msg'].get_payload(0).get_content_type() == 'text/html'
|
||||
assert emails.emails['foobar']['msg'].get_payload(1).get_content_type() == 'application/octet-stream'
|
||||
assert emails.emails['foobar']['msg'].get_payload(2).get_content_type() == 'application/json'
|
||||
payload1 = emails.emails['foobar']['msg'].get_payload(1)
|
||||
payload2 = emails.emails['foobar']['msg'].get_payload(2)
|
||||
assert json.loads(formdata.export_to_json(include_files=False)) == json.loads(payload1.get_payload(decode=True))
|
||||
assert json.loads(formdata.export_to_json()) == json.loads(payload2.get_payload(decode=True))
|
||||
|
||||
|
||||
def test_webservice_call(http_requests, pub):
|
||||
|
|
|
@ -118,6 +118,18 @@ def age_in_seconds(born, today=None):
|
|||
today = make_datetime(today)
|
||||
return datetime_delta(today, born).total_seconds()
|
||||
|
||||
|
||||
def add_days(date, count):
|
||||
'''Add the given number of days to date'''
|
||||
return make_date(date) + datetime.timedelta(days=count)
|
||||
|
||||
|
||||
def attachment(content, filename='', content_type='application/octet-stream'):
|
||||
'''Serialize content as an attachment'''
|
||||
import base64
|
||||
|
||||
return {
|
||||
'filename': filename,
|
||||
'content_type': content_type,
|
||||
'b64_content': base64.b64encode(content),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue