2
2
3
3
import pytest
4
4
5
- from fastapi_mail import ConnectionConfig , FastMail , MessageSchema , MessageType
5
+ from fastapi_mail import (
6
+ ConnectionConfig ,
7
+ FastMail ,
8
+ MessageSchema ,
9
+ MessageType ,
10
+ MultipartSubtypeEnum ,
11
+ )
6
12
7
13
CONTENT = "This file contents some information."
8
14
@@ -81,10 +87,7 @@ async def test_attachement_message(mail_config):
81
87
82
88
assert len (outbox ) == 1
83
89
assert mail ._payload [1 ].get_content_maintype () == "application"
84
- assert (
85
- mail ._payload [1 ].__dict__ .get ("_headers" )[0 ][1 ]
86
- == "application/octet-stream"
87
- )
90
+ assert mail ._payload [1 ].__dict__ .get ("_headers" )[0 ][1 ] == "application/octet-stream"
88
91
89
92
90
93
@pytest .mark .asyncio
@@ -146,20 +149,16 @@ async def test_attachement_message_with_headers(mail_config):
146
149
147
150
assert len (outbox ) == 1
148
151
mail = outbox [0 ]
149
- assert mail ._payload [1 ].get_content_maintype () == msg .attachments [0 ][1 ].get (
150
- "mime_type"
151
- )
152
- assert mail ._payload [1 ].get_content_subtype () == msg .attachments [0 ][1 ].get (
153
- "mime_subtype"
154
- )
152
+ assert mail ._payload [1 ].get_content_maintype () == msg .attachments [0 ][1 ].get ("mime_type" )
153
+ assert mail ._payload [1 ].get_content_subtype () == msg .attachments [0 ][1 ].get ("mime_subtype" )
155
154
156
155
assert mail ._payload [1 ].__dict__ .get ("_headers" )[0 ][1 ] == "image/png"
157
- assert mail ._payload [1 ].__dict__ .get ("_headers" )[3 ][1 ] == msg .attachments [0 ][
158
- 1
159
- ]. get ( "headers" ).get ("Content-ID" )
160
- assert mail ._payload [1 ].__dict__ .get ("_headers" )[4 ][1 ] == msg .attachments [0 ][
161
- 1
162
- ]. get ( "headers" ).get ("Content-Disposition" )
156
+ assert mail ._payload [1 ].__dict__ .get ("_headers" )[3 ][1 ] == msg .attachments [0 ][1 ]. get (
157
+ "headers"
158
+ ).get ("Content-ID" )
159
+ assert mail ._payload [1 ].__dict__ .get ("_headers" )[4 ][1 ] == msg .attachments [0 ][1 ]. get (
160
+ "headers"
161
+ ).get ("Content-Disposition" )
163
162
164
163
assert (
165
164
mail ._payload [2 ].__dict__ .get ("_headers" )[3 ][1 ] == "attachment; "
@@ -183,9 +182,7 @@ async def test_jinja_message_list(mail_config):
183
182
fm = FastMail (conf )
184
183
185
184
with fm .record_messages () as outbox :
186
- await fm .send_message (
187
- message = msg , template_name = "array_iteration_jinja_template.html"
188
- )
185
+ await fm .send_message (message = msg , template_name = "array_iteration_jinja_template.html" )
189
186
190
187
assert len (outbox ) == 1
191
188
mail = outbox [0 ]
@@ -284,10 +281,84 @@ async def test_jinja_message_with_html(mail_config):
284
281
)
285
282
conf = ConnectionConfig (** mail_config )
286
283
fm = FastMail (conf )
287
- await fm .send_message (
288
- message = msg , template_name = "array_iteration_jinja_template.html"
289
- )
284
+ await fm .send_message (message = msg , template_name = "array_iteration_jinja_template.html" )
290
285
291
286
assert msg .template_body == ("\n \n \n Andrej\n \n \n " )
292
287
293
288
assert not msg .body
289
+
290
+
291
+ @pytest .mark .asyncio
292
+ async def test_send_msg_with_alternative_body (mail_config ):
293
+ msg = MessageSchema (
294
+ subject = "testing" ,
295
+
296
+ body = "<p Test data </p>" ,
297
+ subtype = MessageType .html ,
298
+ alternative_body = "Test data" ,
299
+ multipart_subtype = MultipartSubtypeEnum .alternative ,
300
+ )
301
+
302
+ sender = f"{ mail_config ['MAIL_FROM_NAME' ]} <{ mail_config ['MAIL_FROM' ]} >"
303
+ conf = ConnectionConfig (** mail_config )
304
+ fm = FastMail (conf )
305
+ fm .config .SUPPRESS_SEND = 1
306
+ with fm .record_messages () as outbox :
307
+ await fm .send_message (message = msg )
308
+
309
+ mail = outbox [0 ]
310
+ assert len (outbox ) == 1
311
+ body = mail ._payload [0 ]
312
+ assert len (body ._payload ) == 2
313
+ assert body ._headers [1 ][1 ] == 'multipart/alternative; charset="utf-8"'
314
+ assert mail ["subject" ] == "testing"
315
+ assert mail ["from" ] == sender
316
+ assert mail [
"To" ]
== "[email protected] "
317
+
318
+ assert body ._payload [0 ]._headers [0 ][1 ] == 'text/html; charset="utf-8"'
319
+ assert body ._payload [1 ]._headers [0 ][1 ] == 'text/plain; charset="utf-8"'
320
+ assert msg .alternative_body == "Test data"
321
+ assert msg .multipart_subtype == MultipartSubtypeEnum .alternative
322
+
323
+
324
+ @pytest .mark .asyncio
325
+ async def test_send_msg_with_alternative_body_and_attachements (mail_config ):
326
+ directory = os .getcwd ()
327
+ text_file = directory + "/tests/txt_files/plain.txt"
328
+
329
+ with open (text_file , "w" ) as file :
330
+ file .write (CONTENT )
331
+
332
+ subject = "testing"
333
+
334
+ msg = MessageSchema (
335
+ subject = subject ,
336
+ recipients = [to ],
337
+ body = "html test" ,
338
+ subtype = "html" ,
339
+ attachments = [text_file ],
340
+ alternative_body = "plain test" ,
341
+ multipart_subtype = "alternative" ,
342
+ )
343
+ sender = f"{ mail_config ['MAIL_FROM_NAME' ]} <{ mail_config ['MAIL_FROM' ]} >"
344
+ conf = ConnectionConfig (** mail_config )
345
+ fm = FastMail (conf )
346
+ fm .config .SUPPRESS_SEND = 1
347
+ with fm .record_messages () as outbox :
348
+ await fm .send_message (message = msg )
349
+
350
+ mail = outbox [0 ]
351
+
352
+ assert len (outbox ) == 1
353
+ body = mail ._payload [0 ]
354
+ assert len (body ._payload ) == 2
355
+ assert body ._headers [1 ][1 ] == 'multipart/alternative; charset="utf-8"'
356
+ assert mail ["subject" ] == "testing"
357
+ assert mail ["from" ] == sender
358
+ assert mail [
"To" ]
== "[email protected] "
359
+
360
+ assert body ._payload [0 ]._headers [0 ][1 ] == 'text/html; charset="utf-8"'
361
+ assert body ._payload [1 ]._headers [0 ][1 ] == 'text/plain; charset="utf-8"'
362
+
363
+ assert mail ._payload [1 ].get_content_maintype () == "application"
364
+ assert mail ._payload [1 ].__dict__ .get ("_headers" )[0 ][1 ] == "application/octet-stream"
0 commit comments