B
    v9aƒ  ã               @   s„   d dl Z d dlmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZ eG dd„ deƒƒZeG dd	„ d	eƒƒZed
kr€e  ¡  dS )é    N)ÚTestEmailBaseÚparameterize)Úpolicy)ÚEmailMessage)ÚContentManagerÚraw_data_managerc               @   sÌ   e Zd ZejZeZddddœZdd„ Zdd„ Z	d	d
„ Z
G dd„ deƒZe d d ZG dd„ deƒZe d d ZdefdefdddefdefdddefddddœZdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)S )*ÚTestContentManager)é   z
text/plain)é   Útext)é   Ú )Z	full_typeZmaintype_onlyÚnull_keyc             C   sN   d
dd„}t ƒ }| ||¡ |  ¡ }d|d< d|d< |  |j|ddd	¡ d S )Nc             S   s   | d }||fS )NzX-Bar-Header© )ÚmsgÚfooÚbarr   r   ú)/usr/lib/python3.7/test_contentmanager.pyÚ
foo_getter   s    zATestContentManager.get_key_as_get_content_key.<locals>.foo_getterz
text/plainzContent-Typer   zX-Bar-Headerr   )r   )r   r   )N)r   Úadd_get_handlerÚ_make_messageÚassertEqualÚget_content)ÚselfÚorderÚkeyr   ÚcmÚmr   r   r   Úget_key_as_get_content_key   s    
z-TestContentManager.get_key_as_get_content_keyc             C   s„   dd„ }dd„ }t ƒ }| ||¡ x*| j ¡ D ]\}}||kr.| ||¡ q.W |  ¡ }d|d< d|d< d	|d
< |  | |¡d	¡ d S )Nc             S   s   | d S )NzX-Bar-Headerr   )r   r   r   r   Ú
bar_getter!   s    zGTestContentManager.get_key_as_get_content_key_order.<locals>.bar_getterc             S   s   | d S )NzX-Foo-Headerr   )r   r   r   r   r   #   s    zGTestContentManager.get_key_as_get_content_key_order.<locals>.foo_getterz
text/plainzContent-Typer   zX-Bar-Headerr   zX-Foo-Header)r   r   Úget_key_paramsÚvaluesr   r   r   )r   r   r   r   r   r   Ú
precedencer   r   r   r   Ú get_key_as_get_content_key_order    s    z3TestContentManager.get_key_as_get_content_key_orderc          	   C   s<   t ƒ }|  ¡ }d|d< |  td¡ | |¡ W d Q R X d S )Nz
text/plainzContent-Type)r   r   ÚassertRaisesRegexÚKeyErrorr   )r   r   r   r   r   r   Ú:test_get_content_raises_if_unknown_mimetype_and_no_default0   s
    zMTestContentManager.test_get_content_raises_if_unknown_mimetype_and_no_defaultc               @   s   e Zd ZdS )zTestContentManager.BaseThingN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   Ú	BaseThing7   s   r*   Ú.zTestContentManager.BaseThingc               @   s   e Zd ZdS )zTestContentManager.ThingN)r'   r(   r)   r   r   r   r   ÚThing:   s   r,   zTestContentManager.Thingr   r	   )r
   zTestContentManager.Thing)r   r,   é   é   )é   zTestContentManager.BaseThing)é   r*   é   )é	   zbuiltins.str)é
   Ústr)é   N)ÚtypeÚ	full_pathÚqualnameÚnameZ	base_typeZbase_full_pathZbase_qualnameZ	base_nameZstr_typeZstr_full_pathZstr_namer   c             C   s`   ddd„}t ƒ }| ||¡ |  ¡ }|  ¡ }|j||dd |  |d d¡ |  | ¡ |¡ d S )Nc             S   s   || d< |   |¡ d S )NzX-Foo-Header)Úset_payload)r   Úobjr   r   r   r   Ú
foo_setterN   s    zATestContentManager.set_key_as_set_content_key.<locals>.foo_setterr   )r   zX-Foo-Header)N)r   Úadd_set_handlerr   r,   Úset_contentr   Úget_payload)r   r   r   r<   r   r   Úmsg_objr   r   r   Úset_key_as_set_content_keyM   s    
z-TestContentManager.set_key_as_set_content_keyc       	      C   sŽ   dd„ }dd„ }t ƒ }| ||¡ x*| j ¡ D ]\}}||kr.| ||¡ q.W |  ¡ }|  ¡ }| ||¡ |  |d d¡ |  | ¡ |¡ d S )Nc             S   s   d| d< |   |¡ d S )Nr   zX-FooBar-Header)r:   )r   r;   r   r   r   r<   Z   s    zGTestContentManager.set_key_as_set_content_key_order.<locals>.foo_setterc             S   s   d| d< d S )Nr   zX-FooBar-Headerr   )r   r;   r   r   r   Ú
bar_setter]   s    zGTestContentManager.set_key_as_set_content_key_order.<locals>.bar_setterzX-FooBar-Headerr   )	r   r=   r    r!   r   r,   r>   r   r?   )	r   r   r   r<   rB   r   r"   r   r@   r   r   r   Ú set_key_as_set_content_key_orderY   s    z3TestContentManager.set_key_as_set_content_key_orderc          	   C   s@   t ƒ }|  ¡ }|  ¡ }|  t| j¡ | ||¡ W d Q R X d S )N)r   r   r,   r$   r%   Útestobject_full_pathr>   )r   r   r   r@   r   r   r   Ú6test_set_content_raises_if_unknown_type_and_no_defaultj   s
    zITestContentManager.test_set_content_raises_if_unknown_type_and_no_defaultc          	   C   s<   t ƒ }|  ¡ }d|d< |  t¡ | |d¡ W d Q R X d S )Nzmultipart/foozContent-TypeÚtest)r   r   ÚassertRaisesÚ	TypeErrorr>   )r   r   r   r   r   r   Ú.test_set_content_raises_if_called_on_multipartq   s
    zATestContentManager.test_set_content_raises_if_called_on_multipartc             C   sŒ   |   ¡ }d|d< d|d< d|d< | d¡ tƒ }| tdd	„ ¡ |jd
|d |  |d ¡ |  |d ¡ |  |d d¡ |  | ¡ ¡ d S )Nr   zContent-Fooz	text/htmlzContent-TyperF   ÚToÚabcc              _   s   d S )Nr   )ÚargsÚkwr   r   r   Ú<lambda>   ó    zITestContentManager.test_set_content_calls_clear_content.<locals>.<lambda>Úxyz)Úcontent_manager)	r   r:   r   r=   r4   r>   ZassertIsNoner   r?   )r   r   r   r   r   r   Ú$test_set_content_calls_clear_contentx   s    
z7TestContentManager.test_set_content_calls_clear_contentN)r'   r(   r)   r   Údefaultr   Úmessager    r   r#   r&   r4   r*   Zbaseobject_full_pathr,   rD   Zset_key_paramsrA   rC   rE   rI   rR   r   r   r   r   r   	   s:   r   c               @   sÖ  e Zd ZejjdedZeZdd„ Z	dd„ Z
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Z d3d4„ Z!d5d6„ Z"d7d8„ Z#d9d:„ Z$d;d<„ Z%d=d>„ Z&d?d@„ Z'dAdB„ Z(dCdD„ Z)dEdF„ Z*dGdH„ Z+dIdJ„ Z,dKdL„ Z-dMdN„ Z.dOdP„ Z/dQdR„ Z0dSdT„ Z1dUdV„ Z2dWdX„ Z3dYdZ„ Z4d[d\„ Z5d]d^„ Z6d_d`dadbeƒ dcfeƒ ddfdeœZ7dfdg„ Z8dhdi„ Z9djdk„ Z:dldm„ Z;dndo„ Z<dpS )qÚTestRawDataManageré<   )Úmax_line_lengthrQ   c             C   s&   |   t d¡¡}|  t |¡d¡ d S )NzJ            Content-Type: text/plain

            Basic text.
            zBasic text.
)Ú_str_msgÚtextwrapÚdedentr   r   r   )r   r   r   r   r   Útest_get_text_plain”   s    z&TestRawDataManager.test_get_text_plainc             C   s&   |   t d¡¡}|  t |¡d¡ d S )NzP            Content-Type: text/html

            <p>Basic text.</p>
            z<p>Basic text.</p>
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Útest_get_text_htmlœ   s    z%TestRawDataManager.test_get_text_htmlc             C   s,   |   t d¡ d¡¡}|  t |¡d¡ d S )Nu\               Content-Type: text/plain; charset=latin1

            BasÃ¬c tÃ«xt.
            Úlatin1u   BasÃ¬c tÃ«xt.
)Z
_bytes_msgrY   rZ   Úencoder   r   r   )r   r   r   r   r   Útest_get_text_plain_latin1¥   s    z-TestRawDataManager.test_get_text_plain_latin1c             C   s&   |   t d¡¡}|  t |¡d¡ d S )Nz™            Content-Type: text/plain; charset="latin-1"
            Content-Transfer-Encoding: quoted-printable

            Bas=ECc t=EBxt.
            u   BasÃ¬c tÃ«xt.
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Ú+test_get_text_plain_latin1_quoted_printable­   s    z>TestRawDataManager.test_get_text_plain_latin1_quoted_printablec             C   s&   |   t d¡¡}|  t |¡d¡ d S )Nz‘            Content-Type: text/plain; charset="utf8"
            Content-Transfer-Encoding: base64

            QmFzw6xjIHTDq3h0Lgo=
            u   BasÃ¬c tÃ«xt.
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Útest_get_text_plain_utf8_base64¶   s    z2TestRawDataManager.test_get_text_plain_utf8_base64c             C   s&   |   t d¡¡}|  t |¡d¡ d S )NzŸ            Content-Type: text/plain; charset="utf8"
            Content-Transfer-Encoding: quoted-printable

            Bas=c3=acc t=c3=abxt=fd.
            u   BasÃ¬c tÃ«xtï¿½.
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Ú-test_get_text_plain_bad_utf8_quoted_printable¿   s    z@TestRawDataManager.test_get_text_plain_bad_utf8_quoted_printablec             C   s*   |   t d¡¡}|  tj|ddd¡ d S )NzŸ            Content-Type: text/plain; charset="utf8"
            Content-Transfer-Encoding: quoted-printable

            Bas=c3=acc t=c3=abxt=fd.
            Úignore)Úerrorsu   BasÃ¬c tÃ«xt.
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Ú;test_get_text_plain_bad_utf8_quoted_printable_ignore_errorsÈ   s    zNTestRawDataManager.test_get_text_plain_bad_utf8_quoted_printable_ignore_errorsc             C   s*   |   t d¡¡}|  tj|ddd¡ d S )Nu“               Content-Type: text/plain; charset="utf8"
            Content-Transfer-Encoding: base64

            QmFzw6xjIHTDq3h0LgoÃ¿=
            rc   )rd   u   BasÃ¬c tÃ«xt.
)rX   rY   rZ   r   r   r   )r   r   r   r   r   Ú8test_get_text_plain_utf8_base64_recoverable_bad_CTE_dataÒ   s    zKTestRawDataManager.test_get_text_plain_utf8_base64_recoverable_bad_CTE_datac          	   C   s8   |   t d¡¡}|  t¡ tj|dd W d Q R X d S )NzJ            Content-Type: text/plain

            Basic text.
            rc   )r   )rX   rY   rZ   rG   rH   r   r   )r   r   r   r   r   Útest_get_text_invalid_keywordÜ   s    z0TestRawDataManager.test_get_text_invalid_keywordc          
   C   s^   t  d¡}xNd ¡ D ]B}| j|d, |  | |d ¡¡}|  t |¡d¡ W d Q R X qW d S )Nzu            Content-Type: {}
            Content-Transfer-Encoding: base64

            Ym9ndXMgZGF0YQ==
            zaudio image video application)Úmaintypez/foos
   bogus data)	rY   rZ   ÚsplitÚsubTestrX   Úformatr   r   r   )r   Útemplaterh   r   r   r   r   Útest_get_non_textå   s    z$TestRawDataManager.test_get_non_textc          	   C   s8   |   t d¡¡}|  t¡ tj|dd W d Q R X d S )Nz|            Content-Type: image/jpg
            Content-Transfer-Encoding: base64

            Ym9ndXMgZGF0YQ==
            rc   )rd   )rX   rY   rZ   rG   rH   r   r   )r   r   r   r   r   Ú!test_get_non_text_invalid_keywordñ   s    z4TestRawDataManager.test_get_non_text_invalid_keywordc          	   C   s4   |   t d¡¡}|  t¡ t |¡ W d Q R X d S )Nzm            Content-Type: multipart/mixed; boundary="==="

            --===
            --===--
            )rX   rY   rZ   rG   r%   r   r   )r   r   r   r   r   Útest_get_raises_on_multipartû   s    z/TestRawDataManager.test_get_raises_on_multipartc          
   C   sš   t  d¡}xŠd ¡ D ]~}| j|dh |  | |¡¡}t |¡}|  || j	¡ |  
t |¡d¡ |  
|d d¡ |  
|d jd jd	¡ W d Q R X qW d S )
Nz±            Content-Type: message/{}

            To: foo@example.com
            From: bar@example.com
            Subject: example

            an example message
            zrfc822 external-body)Úsubtypezan example message
Útozfoo@example.comÚfromr   r   )rY   rZ   ri   rj   rX   rk   r   r   ÚassertIsInstancerT   r   Z	addressesZusername)r   rl   rp   r   Zsub_msgr   r   r   Ú)test_get_message_rfc822_and_external_body  s    
z<TestRawDataManager.test_get_message_rfc822_and_external_bodyc             C   s.   |   t d¡¡}|  t |¡d d… d¡ d S )NzÈ            Content-Type: message/partial

            To: foo@example.com
            From: bar@example.com
            Subject: example

            The real body is in another message.
            r3   s
   To: foo@ex)rX   rY   rZ   r   r   r   )r   r   r   r   r   Ú9test_get_message_non_rfc822_or_external_body_yields_bytes  s    zLTestRawDataManager.test_get_message_non_rfc822_or_external_body_yields_bytesc             C   s\   |   ¡ }d}t ||¡ |  t|ƒt d¡¡ |  |jdd d¡|¡ |  | 	¡ |¡ d S )NzSimple message.
z‹            Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 7bit

            Simple message.
            T)Údecodezutf-8)
r   r   r>   r   r4   rY   rZ   r?   rv   r   )r   r   Úcontentr   r   r   Útest_set_text_plain%  s    z&TestRawDataManager.test_set_text_plainc             C   s`   |   ¡ }d}tj||dd |  t|ƒt d¡¡ |  |jdd d¡|¡ |  | 	¡ |¡ d S )Nz<p>Simple message.</p>
Úhtml)rp   z‘            Content-Type: text/html; charset="utf-8"
            Content-Transfer-Encoding: 7bit

            <p>Simple message.</p>
            T)rv   zutf-8)
r   r   r>   r   r4   rY   rZ   r?   rv   r   )r   r   rw   r   r   r   Útest_set_text_html2  s    z%TestRawDataManager.test_set_text_htmlc             C   s`   |   ¡ }d}tj||dd |  t|ƒt d¡¡ |  |jdd d¡|¡ |  | 	¡ |¡ d S )NzSimple message.
zlatin-1)Úcharsetz            Content-Type: text/plain; charset="iso-8859-1"
            Content-Transfer-Encoding: 7bit

            Simple message.
            T)rv   zutf-8)
r   r   r>   r   r4   rY   rZ   r?   rv   r   )r   r   rw   r   r   r   Útest_set_text_charset_latin_1?  s    z0TestRawDataManager.test_set_text_charset_latin_1c             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )Nu7   et lÃ  il est montÃ© sur moi et il commence Ã  m'Ã©to.
u²               Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 8bit

            et lÃ  il est montÃ© sur moi et il commence Ã  m'Ã©to.
            zutf-8T)rv   )r   r   r>   r   ÚbytesrY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú5test_set_text_short_line_minimal_non_ascii_heuristicsL  s    zHTestRawDataManager.test_set_text_short_line_minimal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )Nus   j'ai un problÃ¨me de python. il est sorti de son vivarium.  et lÃ  il est montÃ© sur moi et il commence Ã  m'Ã©to.
a*              Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: quoted-printable

            j'ai un probl=C3=A8me de python. il est sorti de son vivari=
            um.  et l=C3=A0 il est mont=C3=A9 sur moi et il commence =
            =C3=A0 m'=C3=A9to.
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú4test_set_text_long_line_minimal_non_ascii_heuristicsY  s    zGTestRawDataManager.test_set_text_long_line_minimal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )Nu}   









j'ai un problÃ¨me de python. il est sorti de son vivarium.  et lÃ  il est montÃ© sur moi et il commence Ã  m'Ã©to.
a@              Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: quoted-printable
            










            j'ai un probl=C3=A8me de python. il est sorti de son vivari=
            um.  et l=C3=A0 il est mont=C3=A9 sur moi et il commence =
            =C3=A0 m'=C3=A9to.
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú=test_set_text_11_lines_long_line_minimal_non_ascii_heuristicsj  s    zPTestRawDataManager.test_set_text_11_lines_long_line_minimal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )Nu   Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
u               Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 8bit

            Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú*test_set_text_maximal_non_ascii_heuristics|  s    z=TestRawDataManager.test_set_text_maximal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )Nu   









Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
u£               Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 8bit
            










            Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú3test_set_text_11_lines_maximal_non_ascii_heuristics‰  s    zFTestRawDataManager.test_set_text_11_lines_maximal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t ||¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )NuÂ   Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
a¶              Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: base64

            w6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOoxJnD
            tsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TDqcOo
            xJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOgw6TD
            qcOoxJnDtsWRw6HDoMOkw6nDqMSZw7bFkcOhw6DDpMOpw6jEmcO2xZHDocOg
            w6TDqcOoxJnDtsWRLgo=
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú4test_set_text_long_line_maximal_non_ascii_heuristics–  s    	zGTestRawDataManager.test_set_text_long_line_maximal_non_ascii_heuristicsc             C   sb   |   ¡ }d}t |d¡ |  t|ƒt d¡ d¡¡ |  |jdd 	d¡|¡ |  | 
¡ |¡ d S )NuÌ   









Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
ak              Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: quoted-printable
            










            =C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=
            =A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=
            =C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=
            =A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=
            =C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=
            =91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=
            =C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=
            =A4=C3=A9=C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=
            =C3=A8=C4=99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=
            =99=C3=B6=C5=91=C3=A1=C3=A0=C3=A4=C3=A9=C3=A8=C4=99=C3=B6=
            =C5=91.
            zutf-8T)rv   )r   r   r>   r   r}   rY   rZ   r^   r?   rv   r   )r   r   rw   r   r   r   Ú=test_set_text_11_lines_long_line_maximal_non_ascii_heuristics©  s    zPTestRawDataManager.test_set_text_11_lines_long_line_maximal_non_ascii_heuristicsc          	   C   s2   |   ¡ }|  t¡ tj|ddd W d Q R X d S )Nu   Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
Ú7bit)Úcte)r   rG   ÚUnicodeErrorr   r>   )r   r   r   r   r   Ú,test_set_text_non_ascii_with_cte_7bit_raisesÈ  s    z?TestRawDataManager.test_set_text_non_ascii_with_cte_7bit_raisesc          	   C   s2   |   ¡ }|  t¡ tj|ddd W d Q R X d S )Nu   Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
Úascii)r{   )r   rG   r‡   r   r>   )r   r   r   r   r   Ú1test_set_text_non_ascii_with_charset_ascii_raisesÍ  s    zDTestRawDataManager.test_set_text_non_ascii_with_charset_ascii_raisesc          	   C   s4   |   ¡ }|  t¡ tj|dddd W d Q R X d S )Nu   Ã¡Ã Ã¤Ã©Ã¨Ä™Ã¶Å‘.
r…   r‰   )r†   r{   )r   rG   r‡   r   r>   )r   r   r   r   r   Ú>test_set_text_non_ascii_with_cte_7bit_and_charset_ascii_raisesÒ  s    zQTestRawDataManager.test_set_text_non_ascii_with_cte_7bit_and_charset_ascii_raisesc             C   s¶   |   ¡ }d|d< |   ¡ }d|d< d|d< d|d< | d¡ t ||¡ |  t|ƒt d	¡¡ | d
¡}|  || j	¡ |  t|ƒt|ƒ¡ |  | 
¡ | j	¡ |  t| 
¡ ƒt|ƒ¡ d S )NzForwarded messageÚSubjectzpython@vivarium.orgrJ   zpolice@monty.orgÚFromzget back in your boxzOr face the comfy chair.a               Subject: Forwarded message
            Content-Type: message/rfc822
            Content-Transfer-Encoding: 8bit

            To: python@vivarium.org
            From: police@monty.org
            Subject: get back in your box
            Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 7bit
            MIME-Version: 1.0

            Or face the comfy chair.
            r   )r   r>   r   r   r4   rY   rZ   r?   rs   rT   r   )r   r   rw   Zpayloadr   r   r   Útest_set_message×  s    

z#TestRawDataManager.test_set_messagec             C   s®   |   ¡ }d|d< |   ¡ }d|d< d|d< d|d< | d¡ t ||¡ |  t|ƒt d	¡ d
¡¡ |  |j| j	j
dt d¡¡ |  | ¡ | j¡ |  t| ¡ ƒt|ƒ¡ d S )NzEscape reportrŒ   zpolice@monty.orgrJ   zvictim@monty.orgr   ZHelpu:   j'ai un problÃ¨me de python. il est sorti de son vivarium.u«              Subject: Escape report
            Content-Type: message/rfc822
            Content-Transfer-Encoding: 8bit

            To: police@monty.org
            From: victim@monty.org
            Subject: Help
            Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 8bit
            MIME-Version: 1.0

            j'ai un problÃ¨me de python. il est sorti de son vivarium.
            zutf-8)ZmaxheaderlenaÐ              Subject: Escape report
            Content-Type: message/rfc822
            Content-Transfer-Encoding: 8bit

            To: police@monty.org
            From: victim@monty.org
            Subject: Help
            Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: base64
            MIME-Version: 1.0

            aidhaSB1biBwcm9ibMOobWUgZGUgcHl0aG9uLiBpbCBlc3Qgc29ydGkgZGUgc29uIHZpdmFyaXVt
            Lgo=
            )r   r>   r   r   r}   rY   rZ   r^   Z	as_stringr   rW   rs   r   rT   r4   )r   r   rw   r   r   r   Ú4test_set_message_with_non_ascii_and_coercion_to_7bitô  s    
zGTestRawDataManager.test_set_message_with_non_ascii_and_coercion_to_7bitc             C   s  |   ¡ }|   ¡ }x„d ¡ D ]x}xrd ¡ D ]f}| j||dN |  t¡}|j|||d W d Q R X t|jƒ}|  ||¡ |  ||¡ W d Q R X q(W qW d}xrd ¡ D ]f}| j||dN |  t¡}|j|||d W d Q R X t|jƒ}|  ||¡ |  ||¡ W d Q R X q¤W d S )Nzquoted-printable base64zrfc822 external-body)r†   rp   )r†   zexternal-bodyz8bit binary)	r   ri   rj   rG   Ú
ValueErrorr>   r4   Z	exceptionZassertIn)r   r   rw   r†   rp   ÚarÚexcr   r   r   Ú#test_set_message_invalid_cte_raises$  s$    

z6TestRawDataManager.test_set_message_invalid_cte_raisesc          
   C   sŠ   x„dt dƒtdƒfD ]n}| j|dX |  ¡ }t ||dd¡ |  t|ƒt 	d¡¡ |  |j
dd|¡ |  | ¡ |¡ W d Q R X qW d S )Ns   bogus content)rw   ÚimageÚjpegz¡                    Content-Type: image/jpeg
                    Content-Transfer-Encoding: base64

                    Ym9ndXMgY29udGVudA==
                    T)rv   )Ú	bytearrayÚ
memoryviewrj   r   r   r>   r   r4   rY   rZ   r?   r   )r   rw   r   r   r   r   Útest_set_image_jpg8  s    z%TestRawDataManager.test_set_image_jpgc             C   sb   |   ¡ }d}|j|dddd |  t|ƒt d¡ d¡¡ |  |jdd	|¡ |  | ¡ |¡ d S )
Nst   bÿgus	con
tent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzZaudioZaifzquoted-printable)r†   a              Content-Type: audio/aif
            Content-Transfer-Encoding: quoted-printable
            MIME-Version: 1.0

            b=FFgus=09con=0At=0Dent=20zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz=
            zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzlatin-1T)rv   )	r   r>   r   r}   rY   rZ   r^   r?   r   )r   r   rw   r   r   r   Ú,test_set_audio_aif_with_quoted_printable_cteH  s    	z?TestRawDataManager.test_set_audio_aif_with_quoted_printable_ctec             C   sj   |   ¡ }d}|j|dddd |  t|ƒt d¡ d¡d d	 ¡ |  |jd
d|¡ |  | ¡ |¡ d S )Nst   bÿgus	con
tent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzZvideoZmpegZbinary)r†   z~            Content-Type: video/mpeg
            Content-Transfer-Encoding: binary
            MIME-Version: 1.0

            r‰   s7   bÿgus	con
t
ent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzs=   zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzT)rv   )	r   r>   r   r}   rY   rZ   r^   r?   r   )r   r   rw   r   r   r   Ú#test_set_video_mpeg_with_binary_cte^  s    
z6TestRawDataManager.test_set_video_mpeg_with_binary_ctec             C   sj   |   ¡ }d}|j|dddd |  t|ƒt d¡ d¡d d	 ¡ |  |jd
d|¡ |  | ¡ |¡ d S )NsM   bÿgus	con
tent
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Úapplicationzoctet-streamZ8bit)r†   zŠ            Content-Type: application/octet-stream
            Content-Transfer-Encoding: 8bit
            MIME-Version: 1.0

            r‰   s   bÿgus	con
t
ent
s=   zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
T)rv   )	r   r>   r   r}   rY   rZ   r^   r?   r   )r   r   rw   r   r   r   Ú/test_set_application_octet_stream_with_8bit_cteo  s    
zBTestRawDataManager.test_set_application_octet_stream_with_8bit_ctec             C   sV   |   ¡ }d}| jj}tj|||ddƒ|ddƒ|ddƒfd |  t|ƒt d¡¡ d S )	NzSimple message.
rJ   zfoo@example.comr   rŒ   zI'm talking to myself.)Úheaderszù            Content-Type: text/plain; charset="utf-8"
            To: foo@example.com
            From: foo@example.com
            Subject: I'm talking to myself.
            Content-Transfer-Encoding: 7bit

            Simple message.
            )	r   r   Úheader_factoryr   r>   r   r4   rY   rZ   )r   r   rw   rž   r   r   r   Ú$test_set_headers_from_header_objects€  s    z7TestRawDataManager.test_set_headers_from_header_objectsc             C   s6   |   ¡ }d}tj||dd |  t|ƒt d¡¡ d S )NzSimple message.
)zX-Foo-Header: foozX-Bar-Header: bar)r   zÇ            Content-Type: text/plain; charset="utf-8"
            X-Foo-Header: foo
            X-Bar-Header: bar
            Content-Transfer-Encoding: 7bit

            Simple message.
            )r   r   r>   r   r4   rY   rZ   )r   r   rw   r   r   r   Útest_set_headers_from_strings’  s    z0TestRawDataManager.test_set_headers_from_stringsc          	   C   s8   |   ¡ }d}|  td¡ tj||dd W d Q R X d S )NzSimple message.
zContent-Type)zContent-Type: foo/bar)r   )r   r$   r   r   r>   )r   r   rw   r   r   r   Ú<test_set_headers_with_invalid_duplicate_string_header_raises¡  s
    zOTestRawDataManager.test_set_headers_with_invalid_duplicate_string_header_raisesc          	   C   sH   |   ¡ }d}| jj}|  td¡ tj|||ddƒfd W d Q R X d S )NzSimple message.
zContent-Typez foo/bar)r   )r   r   rž   r$   r   r   r>   )r   r   rw   rž   r   r   r   Ú<test_set_headers_with_invalid_duplicate_header_header_raises©  s    zOTestRawDataManager.test_set_headers_with_invalid_duplicate_header_header_raisesc          	   C   sF   |   ¡ }d}|  td¡$ tj||dd t|d jƒ W d Q R X d S )NzSimple message.
za@fairly@@invalid@address)zTo: a@fairly@@invalid@address)r   rJ   )r   r$   r   r   r>   ÚprintÚdefects)r   r   rw   r   r   r   Ú4test_set_headers_with_defective_string_header_raises²  s    zGTestRawDataManager.test_set_headers_with_defective_string_header_raisesc          	   C   sV   |   ¡ }d}| jj}|  td¡, tj|||ddƒfd t|d jƒ W d Q R X d S )NzSimple message.
za@fairly@@invalid@addressrJ   )r   )	r   r   rž   r$   r   r   r>   r£   r¤   )r   r   rw   rž   r   r   r   Ú4test_set_headers_with_defective_header_header_raises»  s    zGTestRawDataManager.test_set_headers_with_defective_header_header_raisesc             C   s*   |   ¡ }|jddd |  |d d¡ d S )Nr   Úinline)ÚdispositionzContent-Disposition)r   r>   r   )r   r   r   r   r   Útest_set_disposition_inlineÅ  s    z.TestRawDataManager.test_set_disposition_inlinec             C   s*   |   ¡ }|jddd |  |d d¡ d S )Nr   Z
attachment)r¨   zContent-Disposition)r   r>   r   )r   r   r   r   r   Útest_set_disposition_attachmentÊ  s    z2TestRawDataManager.test_set_disposition_attachmentc             C   s*   |   ¡ }|jddd |  |d d¡ d S )Nr   )r¨   zContent-Disposition)r   r>   r   )r   r   r   r   r   Útest_set_disposition_fooÏ  s    z+TestRawDataManager.test_set_disposition_fooc             C   s*   |   ¡ }|jddd |  |d d¡ d S )Nr   zbar.txt)ÚfilenamezContent-Dispositionzattachment; filename="bar.txt")r   r>   r   )r   r   r   r   r   Útest_set_filename×  s    
z$TestRawDataManager.test_set_filenamec             C   s,   |   ¡ }|jdddd |  |d d¡ d S )Nr   r§   zbar.txt)r¨   r¬   zContent-Dispositionzinline; filename="bar.txt")r   r>   r   )r   r   r   r   r   Ú(test_set_filename_and_disposition_inlineÝ  s    z;TestRawDataManager.test_set_filename_and_disposition_inlinec             C   s6   |   ¡ }|jddd |  t|ƒt d¡ d¡¡ d S )Nr   u   Ã¡bÃ¡rÃ®.txt)r¬   a              Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 7bit
            Content-Disposition: attachment;
             filename*=utf-8''%C3%A1b%C3%A1r%C3%AE.txt
            MIME-Version: 1.0

            foo
            r‰   )r   r>   r   r}   rY   rZ   r^   )r   r   r   r   r   Útest_set_non_ascii_filenameâ  s    z.TestRawDataManager.test_set_non_ascii_filename)rw   r   )rw   )ry   )s   content)r›   Zoctet_stream)s   content)r”   r•   r   )zexternal-body)Z
text_plainZ	text_htmlZapplication_octet_streamZ
image_jpegZmessage_rfc822Zmessage_external_bodyc             C   sB   |   ¡ }|j|f|žddiŽ |  |d d¡ |  |d d¡ d S )Nr   )zTo: foo@example.comzFrom: bar@simple.netrq   zfoo@example.comrr   zbar@simple.net)r   r>   r   )r   r;   Úmimetyper   r   r   r   Ú!content_object_as_header_receiverù  s
    z4TestRawDataManager.content_object_as_header_receiverc             C   s2   |   ¡ }|j|f|žddiŽ |  |d d¡ d S )Nr¨   r§   zContent-Disposition)r   r>   r   )r   r;   r°   r   r   r   r   Ú-content_object_as_disposition_inline_receiver  s    z@TestRawDataManager.content_object_as_disposition_inline_receiverc             C   sZ   |   ¡ }|j|f|ždddœŽ |  |d d¡ |  | ¡ d¡ |  |d jd d¡ d S )Nr§   u   bÃ¡r.txt)r¨   r¬   zContent-Dispositionu   inline; filename="bÃ¡r.txt"r¬   )r   r>   r   Úget_filenameÚparams)r   r;   r°   r   r   r   r   Ú-content_object_as_non_ascii_filename_receiver  s
    z@TestRawDataManager.content_object_as_non_ascii_filename_receiverc             C   s2   |   ¡ }|j|f|žddiŽ |  |d d¡ d S )NZcidZsome_random_stuffz
Content-ID)r   r>   r   )r   r;   r°   r   r   r   r   Úcontent_object_as_cid_receiver  s    z1TestRawDataManager.content_object_as_cid_receiverc             C   sP   |   ¡ }dddœ}|j|f|žd|iŽ t|tƒr:d|d< |  |d j|¡ d S )Nu   bÃ¡rrP   )r   rK   r´   zutf-8r{   zContent-Type)r   r>   Ú
isinstancer4   r   r´   )r   r;   r°   r   r´   r   r   r   Ú!content_object_as_params_receiver  s    

z4TestRawDataManager.content_object_as_params_receiverN)=r'   r(   r)   r   rS   Zcloner   r   rT   r[   r\   r_   r`   ra   rb   re   rf   rg   rm   rn   ro   rt   ru   rx   rz   r|   r~   r   r€   r   r‚   rƒ   r„   rˆ   rŠ   r‹   rŽ   r   r“   r˜   r™   rš   rœ   rŸ   r    r¡   r¢   r¥   r¦   r©   rª   r«   r­   r®   r¯   Zcontent_object_paramsr±   r²   rµ   r¶   r¸   r   r   r   r   rU   ‡   sx   					

	

0		
rU   Ú__main__)ZunittestZtest.test_emailr   r   rY   Zemailr   Zemail.messager   Zemail.contentmanagerr   r   r   rU   r'   Úmainr   r   r   r   Ú<module>   s    }     