11import asyncio
2- from typing import List
3- from unittest . case import TestCase
2+ from typing import List , Optional
3+ from unittest import TestCase
44from unittest .mock import patch
55
6- from asserts import assert_equal , assert_greater_equal , fail
6+ from asserts import assert_equal , assert_greater_equal , fail , \
7+ assert_is_not_none , assert_datetime_about_now_utc
78
8- from fakesmtpd .connection import handle_connection
9+ from fakesmtpd .connection import ConnectionHandler
910from fakesmtpd .smtp import SMTPStatus
11+ from fakesmtpd .state import State
1012
1113FAKE_HOST = "mail.example.com"
1214
@@ -78,15 +80,19 @@ def setUp(self):
7880 patch ("fakesmtpd.commands.getfqdn" , lambda : FAKE_HOST )
7981 self ._getfqdn_patch1 .start ()
8082 self ._getfqdn_patch2 .start ()
83+ self .printed_state : Optional [State ] = None
8184
8285 def tearDown (self ):
8386 self ._getfqdn_patch1 .stop ()
8487 self ._getfqdn_patch2 .stop ()
8588
8689 def _handle (self ):
8790 loop = asyncio .get_event_loop ()
88- c = handle_connection (self .reader , self .writer )
89- loop .run_until_complete (c )
91+ handler = ConnectionHandler (self .reader , self .writer , self ._print_mail )
92+ loop .run_until_complete (handler .handle ())
93+
94+ def _print_mail (self , state : State ) -> None :
95+ self .printed_state = state
9096
9197 def test_greeting (self ):
9298 self ._handle ()
@@ -311,7 +317,7 @@ def test_complete_mail(self):
311317 "From: foo@example.com" ,
312318 "To: bar@example.com" ,
313319 "Subject: Foobar" ,
314- ""
320+ "" ,
315321 "Line 1" ,
316322 "Line 2" ,
317323 "." ,
@@ -328,7 +334,7 @@ def test_two_transactions(self):
328334 "From: foo@example.com" ,
329335 "To: bar@example.com" ,
330336 "Subject: Foobar" ,
331- ""
337+ "" ,
332338 "." ,
333339 "MAIL FROM:<foo@example.com>" ,
334340 ]
@@ -376,3 +382,33 @@ def test_vrfy(self):
376382 self ._handle ()
377383 self .writer .assert_last_reply (
378384 SMTPStatus .CANNOT_VRFY , "Verify not allowed" )
385+
386+ def test_mail_printed (self ):
387+ self .reader .lines = [
388+ "EHLO client.example.com" ,
389+ "MAIL FROM:<foo@example.com>" ,
390+ "RCPT TO:<bar1@example.com>" ,
391+ "RCPT TO:<bar2@example.com>" ,
392+ "DATA" ,
393+ "From: foo@example.com" ,
394+ "To: bar@example.com" ,
395+ "Subject: Foobar" ,
396+ "" ,
397+ "Line 1 " ,
398+ "Line 2" ,
399+ "." ,
400+ ]
401+ self ._handle ()
402+ assert_is_not_none (self .printed_state )
403+ assert_datetime_about_now_utc (self .printed_state .date )
404+ assert_equal ("foo@example.com" , self .printed_state .reverse_path )
405+ assert_equal (["bar1@example.com" , "bar2@example.com" ],
406+ self .printed_state .forward_path )
407+ assert_equal (
408+ "From: foo@example.com\r \n "
409+ "To: bar@example.com\r \n "
410+ "Subject: Foobar\r \n "
411+ "\r \n "
412+ "Line 1 \r \n "
413+ "Line 2\r \n " ,
414+ self .printed_state .mail_data )
0 commit comments