a  zeP@sddlZddlZddlmZmZddlmZmZddlm Z m Z ddZ Gddde Z e Gd d d Z Gd d d e e ZGd dde e ZedkredS)N)policymessage_from_string) EmailMessageMIMEPart) TestEmailBase parameterizecCsttdd|dS)NcSs|duSN)xr r P/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_email/test_message.py zfirst..)nextfilter)iterabler r r first src@s"eZdZejZddZddZdS)TestcCsH|d}d|d<|td|d<Wdn1s:0YdS)Nzabc@xyzTozxyz@abc)_str_msg assertRaises ValueErrorselfmr r r +test_error_on_setitem_if_max_count_exceededs  z0Test.test_error_on_setitem_if_max_count_exceededcCsDttdtjd}||dd||dd||tdS)Na> Subject: Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?= From: =?utf-8?q?Pep=C3=A9?= Le Pew To: "Penelope Pussycat" <"penelope@example.com"> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" sample text rZsubjectu Ayons asperges pour le déjeunerfromuPepé Le Pew )rtextwrapdedentrdefault assertEqualZassertIsInstancerrr r r /test_rfc2043_auto_decoded_and_emailmessage_useds   z4Test.test_rfc2043_auto_decoded_and_emailmessage_usedN)__name__ __module__ __qualname__rr rr"r r r r r src@seZdZejZddddedfdddedfddded fddded fd d ded fdddedfdddedfdd dedfdddedfdddedfdddedfdddedfdddedfd d dedfd Zd!d"Zd#d$Z d%d&Z Gd'd(d(Z d)d*Z d+d,Z d-d.Zd/d0Zd1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Zdd?d?Zd@dAZGdBdCdCZdDdEZdFdGZdHdIZdJdKZdLdMZdNS)OTestEmailMessageBase)NNrr r rr'r zV To: foo@example.com simple text body )NNNz To: foo@example.com MIME-Version: 1.0 Content-Type: image/jpg bogus body. )N)r)r(a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="===" preamble --=== Content-Type: text/plain simple body --=== Content-Type: text/html

simple body

--===-- a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" preamble --=== Content-Type: text/plain simple body --=== Content-Type: text/html

simple body

--===-- )NNr))r(a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: text/plain simple body --=== Content-Type: text/html Content-Disposition: attachment

simple body

--===-- )Nr(N)r)a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: text/plain Content-Disposition: AtTaChment simple body --=== Content-Type: text/html

simple body

--===-- a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: text/plain Content-Disposition: InLine simple body --=== Content-Type: text/html Content-Disposition: inline

simple body

--===-- )rr)Na To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/related; boundary="==="; type=text/html --=== Content-Type: text/html

simple body

--=== Content-Type: image/jpg Content-ID: bogus data --===-- )rr(Na To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/related; boundary="==="; type=text/html; start="" --=== Content-Type: text/html Content-ID: useless text --=== Content-Type: text/html Content-ID:

simple body

--===-- )r())r)r,r-a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: multipart/alternative; boundary="+++" --+++ Content-Type: text/plain simple body --+++ Content-Type: multipart/related; boundary="___" --___ Content-Type: text/html

simple body

--___ Content-Type: image/jpg Content-ID: bogus jpg body --___-- --+++-- --=== Content-Type: image/jpg Content-Disposition: attachment bogus jpg body --=== Content-Type: image/jpg Content-Disposition: AttacHmenT another bogus jpg body --===-- )r)r+r*a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: multipart/related; boundary="+++" --+++ Content-Type: multipart/alternative; boundary="___" --___ Content-Type: text/plain simple body --___ Content-Type: text/html

simple body

--___-- --+++ Content-Type: image/jpg Content-ID: bogus jpg body --+++-- --=== Content-Type: image/jpg Content-Disposition: attachment bogus jpg body --=== Content-Type: image/jpg Content-Disposition: attachment another bogus jpg body --===-- )r)NNa To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: multipart/related; boundary="+++" --+++ Content-Type: image/jpg Content-ID: bogus jpg body --+++ Content-Type: multipart/alternative; boundary="___" --___ Content-Type: text/plain simple body --___ Content-Type: text/html

simple body

--___-- --+++-- --=== Content-Type: image/jpg Content-Disposition: attachment bogus jpg body --=== Content-Type: image/jpg Content-Disposition: attachment another bogus jpg body --===-- z To: foo@example.com MIME-Version: 1.0 Content-Type: message/rfc822 To: bar@example.com From: robot@examp.com this is a message body. a To: foo@example.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===" --=== Content-Type: text/plain Your message has bounced, sir. --=== Content-Type: message/rfc822 To: bar@example.com From: robot@examp.com this is a message body. --===-- )Z empty_messageZnon_mime_plainZ mime_non_textZplain_html_alternativeZplain_html_mixedZplain_html_attachment_mixedZhtml_text_attachment_mixedZ!html_text_attachment_inline_mixedrelatedZrelated_with_startZmixed_alternative_plain_relatedZ$mixed_related_alternative_plain_htmlZ0mixed_related_alternative_plain_html_wrong_orderZmessage_rfc822Zmixed_text_message_rfc822c s||}t|fdd|D}d}d}d} ||t|||jddt|||jddt|||d||jd dt|||| g||jd dt||| d||jd gd||||jd gd||||jd gd|| ||jddt|| |dd||jddt|| ||g||jddt||dd||jddt|ddd||jddt|||| ||gdS)Ncs g|]}|durdn|qSrr .0nZallpartsr r r z.rr)r()r.htmlplain)Zpreferencelist)r.r4)r.r5)r4r5r.r4r5)r5r4)r5r.)r4r.)r5r4r.)r4r5r.)rlistwalkr!get_bodyr) r body_parts attachmentspartsmsgrZexpectedr.r4r5r r2r message_as_get_bodysP   z(TestEmailMessageBase.message_as_get_bodycs@||}t|fdd|D}|t||dS)Ncsg|] }|qSr r r/r2r r r3r zCTestEmailMessageBase.message_as_iter_attachment..)rr7r8r!iter_attachmentsrr:r;r<r=rr r2r message_as_iter_attachments  z/TestEmailMessageBase.message_as_iter_attachmentcsXdd}||}t|fdd|D}||rDt|ng}|||dS)NcSsd|vS)NzContent-Type: multipartr )r=r r r _is_multipart_msgszETestEmailMessageBase.message_as_iter_parts.._is_multipart_msgcsg|] }|qSr r r/r2r r r3r z>TestEmailMessageBase.message_as_iter_parts..)rr7r8 iter_partsr!)rr:r;r<r=rBrrCr r2r message_as_iter_partss   z*TestEmailMessageBase.message_as_iter_partsc@seZdZddZddZdS)z(TestEmailMessageBase._TestContentManagercOs |||fSrr rr=argskwr r r get_contentsz4TestEmailMessageBase._TestContentManager.get_contentcOs||_||_||_dSr)r=rFrGrEr r r set_contentsz4TestEmailMessageBase._TestContentManager.set_contentN)r#r$r%rHrIr r r r _TestContentManagersrJcCst|d}|}||j|d|dif|jd|ddd\}}}|||||d||tddd dS Nrcontent_managerr foor)r()rMbarkrNrOrP)rrJr!rHdict)rrcmr=rFrGr r r test_get_content_with_cms   z-TestEmailMessageBase.test_get_content_with_cmcCs|tjj|d}|jd|d}|||dif|jdddd\}}}|||||d ||tddddS NrLrrr rNr)r(rRrQ)rr clonerJrr!rHrS)rprr=rFrGr r r -test_get_content_default_cm_comes_from_policys  zBTestEmailMessageBase.test_get_content_default_cm_comes_from_policycCs|d}|}|j|d||j|||jd||ji|jd|ddd||j|||jd||jtddd dSrK)rrJrIr!r=rFrGrSrrrTr r r test_set_content_with_cm s  z-TestEmailMessageBase.test_set_content_with_cmcCs|}tjj|d}|jd|d}|||j|||jd||j i|jdddd||j|||jd ||j t ddddSrV) rJrr rWrrIr!r=rFrGrS)rrTrXrr r r -test_set_content_default_cm_comes_from_policyszBTestEmailMessageBase.test_set_content_default_cm_comes_from_policy))r. no_contentsucceeds)r.noner^)r.r5r^)r.r.r)r. alternativeraises)r.mixedra)r`r]r^)r`r_r^)r`r5r^)r`r.r^)r`r`r)r`rbra)rbr]r^)rbr_r^)rbr5r^)rbr.r^)rbr`r^)rbrbrcCs|}d}ddg}|dkr|d|dkrHd}|d|dn|dkrfg}|dd |f|d |||D]\}}|||<q~|||fS) N)rz foo@bar.com)ZFromz bar@foo.comr])zX-Random-HeaderZCorwintextr) Content-Type text/plainrd multipart/)zX-TrumpRandom)messageappend set_payload)rsubtyperpayload msg_headersnamevaluer r r _make_subtype_test_messageAs&       z/TestEmailMessageBase._make_subtype_test_messagecCs\|t}t||Wdn1s,0Yt|j}||||||dSr)rrgetattrstr exceptionZassertIn)rrZ method_namerkmethodarexc_textr r r _check_disallowed_subtype_raisesXs  *  z5TestEmailMessageBase._check_disallowed_subtype_raisescCsd}|D].\}}|ds|||||d7}q|t||dt|}d}|D].\}}|dr`|||||d7}q`|t|||||dS)Nrcontent-r))lower startswithr!lenrrC get_payload)rrrmrlcountrnropartr r r _check_make_multipart_s     z*TestEmailMessageBase._check_make_multipartcCs||\}}}d|}|dvr4|||||dSt||||d||||dkr|t|d|||dd|fgn"|t|d| |||dS) Nmake_rra multipartr]rrdrfr)) rprwrqr!get_content_maintypeget_content_subtyper{r|itemsr)rrtrkoutcomerrmrlZ make_methodr r r subtype_as_makeos"  z$TestEmailMessageBase.subtype_as_makecCs|}|dvrZd||d<|t }t|d|Wdn1sL0YdS|dkrld|d<n|dkrd||d<t|d|dd ||||ddS) Nrrfrdrr5rer]abc)boundary)rhrrrq assertTrueZ is_multipartr!Z get_boundary)rrtrkrrrTr r r subtype_as_make_with_boundarys  .  z2TestEmailMessageBase.subtype_as_make_with_boundarycCsNdD]D}|j|jjddd}d|d<t||||djjdqdS)N)Z make_relatedZmake_alternativeZ make_mixedrNrLrrerdr)rhrrWrqr!r|rM)rrtrr r r 3test_policy_on_part_made_by_make_comes_from_messages  zHTestEmailMessageBase.test_policy_on_part_made_by_make_comes_from_messagec@seZdZddZdS)z+TestEmailMessageBase._TestSetContentManagercOsd|d<||dS)Nrerd)rjrr=ZcontentrFrGr r r rIsz7TestEmailMessageBase._TestSetContentManager.set_contentNr#r$r%rIr r r r _TestSetContentManagersrc Csb||\}}}|}|dkr$dnd|}|dkrH|||||dSt||d|d||d|||||ks|dkr|t|d |D]\} } ||| | q|d } n.|t|d | ||||d } || d || d|dkr4|| d dn*|dkrP|| d dn| | d dS)Nrbadd_attachmentZadd_ratestrLrr]r)rr(reContent-Disposition attachmentr.inline) rprrwrqr!rrr{r|rZget_content_type assertIsNone) rrtrkrrrmrlrTZ add_methodrnror~r r r subtype_as_adds0    z#TestEmailMessageBase.subtype_as_addc@seZdZddZdS)z2TestEmailMessageBase._TestSetRaisingContentManagercOs tddS)Nr) Exceptionrr r r rIsz>TestEmailMessageBase._TestSetRaisingContentManager.set_contentNrr r r r _TestSetRaisingContentManagersrc Csv|}|j|jj|dd}dD]N}|t}t||dWdn1sT0Y|t|j dq"dS)NrLr) add_relatedZadd_alternativerrr) rrhrrWrrrqr!rrrs)rrTrrtrur r r 6test_default_content_manager_for_add_comes_from_policys  ,zKTestEmailMessageBase.test_default_content_manager_for_add_comes_from_policycCs\||}||t|d|t|g|||t|gdS)Nr) rclearr!r{r7rrr|rCr@r r r message_as_clears  z%TestEmailMessageBase.message_as_clearcCs^||}dd|D}||t|||||t|gdS)NcSsg|]}|ds|qS)rx)ryrz)r0hr r r r3szATestEmailMessageBase.message_as_clear_content..)rkeysZ clear_contentr!r7rr|rC)rr:r;r<r=rZexpected_headersr r r message_as_clear_contents  z-TestEmailMessageBase.message_as_clear_contentcCs|}||d|d<|||dd|||dd|||ddd||dS)NrrrZ AtTachMentfilenamezabc.png) _make_messageZ assertFalseZ is_attachmentZreplace_headerrZ set_paramrr r r test_is_attachments  z'TestEmailMessageBase.test_is_attachmentcCs^|}|d|d|d|}|tt|d|||dS)Nzarbitrary text as main partzmore text as a related partz&yet more text as a second "attachment"r() rrIrr|copyr!r{r7r?)rrZorigr r r test_iter_attachments_mutations    z3TestEmailMessageBase.test_iter_attachments_mutationN)r#r$r%rr rrZmessage_paramsr>rArDrJrUrYr[r\Zsubtype_paramsrprwrrrrrrrrrrrrr r r r r&&s  563       r&c@seZdZeZddZGdddZddZddZd d Z d d Z d dZ ddZ ddZ ddZddZddZddZddZddZdd Zd!d"Zd#S)$TestEmailMessagecCs>|d}|}|d||j|d||dddSNr MIME-VersionrL1.0)rrJ assertNotInrIr!rZr r r "test_set_content_adds_MIME_Versions    z3TestEmailMessage.test_set_content_adds_MIME_Versionc@seZdZddZdS)z(TestEmailMessage._MIME_Version_adding_CMcOs d|d<dS)Nrrr rEr r r rIsz4TestEmailMessage._MIME_Version_adding_CM.set_contentNrr r r r _MIME_Version_adding_CMsrcCs>|d}|}|d||j|d||dddSr)rrrrIr!rZr r r 0test_set_content_does_not_duplicate_MIME_Versions    zATestEmailMessage.test_set_content_does_not_duplicate_MIME_VersioncCs*|d}|t|ddSNSubject: long line ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab r*rr!r{ as_stringstrip splitlinesrr r r 0test_as_string_uses_max_header_length_by_defaults zATestEmailMessage.test_as_string_uses_max_header_length_by_defaultcCsN|d}|t|jddd|t|jddddS)Nrr)Z maxheaderlenr)"r,rrr r r "test_as_string_allows_maxheaderlens z3TestEmailMessage.test_as_string_allows_maxheaderlencCs@|d}|d||jddd||jddddS)Nrz%From foo@bar Thu Jan 1 00:00:00 1970T)Zunixfromz+From foo@bar Thu Jan 1 00:00:00 1970 testFz test)rZ set_unixfromr!rrr r r test_as_string_unixforms   z(TestEmailMessage.test_as_string_unixformcCs*|d}|tt|ddSr)rr!r{rrrrrr r r +test_str_defaults_to_policy_max_line_lengths z Date: Wed, 08 Nov 2017 08:50:22 +0700 From: Foo Bar MIME-Version: 1.0 To: email@email.com Subject: Python Email Content-Type: multipart/mixed; boundary="------------879045806563892972123996" X-Global-filter:Messagescannedforspamandviruses:passedalltests This is a multi-part message in MIME format. --------------879045806563892972123996 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Your message is ready to be sent with the following file or link attachments: XU89 - 08.11.2017 )rrrr9)rr=rr r r test_get_body_malformeds  z(TestEmailMessage.test_get_body_malformedN)r#r$r%rrhrrrrrrrrrrrrrrrrrr r r r rs$rc@s eZdZeZddZddZdS) TestMIMEPartcCs:|d}|}|d||j|d|d|dS)NrrrL)rrJrrIrZr r r *test_set_content_does_not_add_MIME_Versions    z7TestMIMEPart.test_set_content_does_not_add_MIME_VersioncCs0ttdtjd}|}|t|gdS)NzT Content-Type: multipart/mixed; charset="utf-8" sample text r)rrrrr r?r!r7)rr=r;r r r /test_string_payload_with_multipart_content_types  zs$LQ