o
    9X}Z];  ã                   @   s–   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ dgZG d	d„ deƒZdS )
é    N)ÚMailboxÚMessage)Úconvert_header_to_unicode)Úutils)ÚEmailMessageTestCase)Ú
force_text)ÚEmailMessageÚTestProcessEmailc                   @   s¬   e Z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)S )*r	   c                 C   sh   |   d¡}tj ¡ }| |¡}|  |j|¡ |  |jd¡ |  |jd¡ |  |j	d¡ |  |j
d¡ d S )Núgeneric_message.emlzMessage Without AttachmentzD<CAMdmm+hGH8Dgn-_0xnXJCd=PhyNAiouOYm5zFP0z-foqTO60zA@mail.gmail.com>z)Adam Coddington <test@adamcoddington.net>)Ú_get_email_objectr   ÚobjectsÚcreateÚprocess_incoming_messageÚassertEqualÚmailboxÚsubjectÚ
message_idÚfrom_headerÚ	to_header)ÚselfÚmessager   Úmsg© r   úY/var/www/html/env/lib/python3.10/site-packages/django_mailbox/tests/test_process_email.pyÚ test_message_without_attachments   s(   


þýþþz1TestProcessEmail.test_message_without_attachmentsc                 C   sn   |   d¡}tj ¡ }| |¡}|j d¡ ¡ }|  d|d  	¡ ¡ |  d|d  	¡ ¡ |  d|d  	¡ ¡ d S )	Nz+message_with_koi8r_filename_attachments.emlÚpkuI   ÐŸÐ°ÐºÐµÑ‚ Ð¿Ñ€ÐµÐ´Ð»Ð¾Ð¶ÐµÐ½Ð¸Ð¹ HSE Career Fair 8 Ð°Ð¿Ñ€ÐµÐ»Ñ� 2016.pdfr   u   Ð’ÐµÐ´Ð¾Ð¼Ð¾Ñ�Ñ‚Ð¸.pdfé   u+   ÐŸÐ°ÐºÐµÑ‚ Ð¿Ñ€ÐµÐ´Ð»Ð¾Ð¶ÐµÐ½Ð¸Ð¹ 2016.pptxé   )
r   r   r   r   r   ÚattachmentsÚorder_byÚallr   Úget_filename)r   r   r   r   r   r   r   r   Ú.test_message_with_encoded_attachment_filenames/   s$   ÿ


ü
þ
ýz?TestProcessEmail.test_message_with_encoded_attachment_filenamesc                 C   sZ   |   d¡}tj ¡ }| |¡}d}|j ¡ }|  ||¡ |j ¡ d }|  | 	¡ d¡ d S )Nzmessage_with_attachment.emlr   r   z	heart.png©
r   r   r   r   r   r   Úcountr   r    r!   )r   r   r   r   Úexpected_countÚactual_countÚ
attachmentr   r   r   Útest_message_with_attachmentsH   s   



þþz.TestProcessEmail.test_message_with_attachmentsc                 C   sn   t jdkr
|  d¡ |  d¡}tj ¡ }| |¡}d}|j 	¡ }|  
||¡ |j ¡ d }|  
| ¡ d¡ d S )N)é   r   z)This test is only relevant on Python 2.x.z.message_with_not_decoded_attachment_header.emlr   r   u   ÃƒÂ°ÃŒÂžoÃŽÂ²ÃŒÂžle.png)ÚsysÚversion_infoÚskipTestr   r   r   r   r   r   r$   r   r    r!   ©r   Úemail_objectr   r   r%   r&   r'   r   r   r   Ú/test_message_with_not_decoded_attachment_header\   s(   
ÿÿ


þþz@TestProcessEmail.test_message_with_not_decoded_attachment_headerc                 C   sx   |   d¡}tj ¡ }| |¡}d}|j ¡ }|  ||¡ |j ¡ d }|  | 	¡ d¡ |j ¡ d }|  | 	¡ d¡ dS )zg Ensure that we properly handle UTF-8 encoded attachment

        Safe for regress of #104 too
        z message_with_utf8_attachment.emlr   r   u   piÅ‚kochwyty.jpgr   u   odpowiedÅº Burmistrza.jpgNr#   r-   r   r   r   Ú(test_message_with_utf8_attachment_headerw   s*   ÿ


þþþz9TestProcessEmail.test_message_with_utf8_attachment_headerc                 C   s<   |   d¡}tj ¡ }| |¡}d}|j ¡ }|  ||¡ d S )Nzmultipart_text.emlzHello there!)r   r   r   r   r   ÚtextÚstripr   )r   r   r   r   Úexpected_resultsÚactual_resultsr   r   r   Útest_message_get_text_body–   s   



þz+TestProcessEmail.test_message_get_text_bodyc                    s8   t ƒ }|  d¡‰ ‡ fdd„|_|j}d}|  ||¡ d S )Nz message_with_long_text_lines.emlc                      s   ˆ S )Nr   r   ©r.   r   r   Ú<lambda>ª   s    z\TestProcessEmail.test_get_text_body_properly_recomposes_line_continuations.<locals>.<lambda>z_The one of us with a bike pump is far ahead, but a man stopped to help us and gave us his pump.)r   r   Úget_email_objectr1   r   )r   r   Úactual_textÚexpected_textr   r6   r   Ú9test_get_text_body_properly_recomposes_line_continuations¤   s   ÿÿþzJTestProcessEmail.test_get_text_body_properly_recomposes_line_continuationsc                 C   st   t tj tj t¡d¡ƒ�}t | ¡ ¡}W d   ƒ n1 sw   Y  t	ƒ }||_
|}| ¡  ¡ }|  ||¡ d S )Nzmessages/generic_message.eml)ÚopenÚosÚpathÚjoinÚdirnameÚ__file__ÚsixÚuÚreadr   Úbodyr8   Ú	as_stringr   )r   ÚfÚunicode_bodyr   Úexpected_bodyÚactual_bodyr   r   r   Ú+test_get_body_properly_handles_unicode_body·   s"   
þÿúþz<TestProcessEmail.test_get_body_properly_handles_unicode_bodyc                 C   sp   |   d¡}d}z| ¡  W n   d}Y d}z| j |¡ W n ty)   d}Y nw |  |d¡ |  d|¡ dS )zF Ensure that we properly handle incorrectly encoded messages

        úemail_issue_82.emlÚworkszdo not worksTFN)r   rF   r   r   Ú
ValueErrorr   )r   r.   ÚitÚsuccessr   r   r   Útest_message_issue_82Ë   s   
ÿz&TestProcessEmail.test_message_issue_82c                 C   s*  |   d¡}d}tƒ }| j|_d|v rt|d ƒdd… |_d|v r*|d dd… |_d|v r5t|d ƒ|_d|v rAt|d ƒ|_nd	|v rLt|d	 ƒ|_| ¡  | 	¡ }| j 
||¡}z| 	¡ }W n   d
}Y | |¡ |d r‰ztjj|d d�d |_W n	 tyˆ   Y nw | ¡  |  d|¡ dS )zj Ensure that the email object is good before and after
        calling _get_dehydrated_message()

        rL   Tr   r   éÿ   z
message-idÚfromÚtozDelivered-ToFzin-reply-to)r   N)r   r   r   r   r   r   r   r   ÚsaverF   Ú_get_dehydrated_messageÚset_bodyr   ÚfilterÚin_reply_toÚ
IndexErrorr   )r   r   rP   r   Ústr_msgr   r   r   Útest_message_issue_82_bisá   sD   

ÿ
þÿz*TestProcessEmail.test_message_issue_82_bisc                 C   s6   |   d¡}| j |¡}t d¡}|j}|  ||¡ dS )zÔ Ensure that we properly handle incorrectly encoded messages

        ``message_with_utf8_char.eml``'s primary text payload is marked
        as being iso-8859-1 data, but actually contains UTF-8 bytes.

        zmessage_with_utf8_char.emluY   This message contains funny UTF16 characters like this one: "Ã‚Â " and this one "Ã¢ÂœÂ¿".N)r   r   r   rB   rC   r1   r   )r   r.   r   r:   r9   r   r   r   Ú(test_message_with_misplaced_utf8_content  s   
ÿþz9TestProcessEmail.test_message_with_misplaced_utf8_contentc                 C   s    |   d¡}| j |¡}|j dS )aX   Ensure that we gracefully handle mis-encoded bodies.

        Should a payload body be misencoded, we should:

        - Not explode

        Note: there is (intentionally) no assertion below; the only guarantee
        we make via this library is that processing this e-mail message will
        not cause an exception to be raised.

        z6message_with_invalid_content_for_declared_encoding.emlN)r   r   r   r1   ©r   r.   r   r   r   r   Ú7test_message_with_invalid_content_for_declared_encoding'  s
   ÿ
zHTestProcessEmail.test_message_with_invalid_content_for_declared_encodingc                 C   s6   |   d¡}| j |¡}|j}t d¡}|  ||¡ d S )Nz%message_with_single_byte_encoding.emluQ   Ð­Ñ‚Ð¾ Ñ�Ð¾Ð¾Ð±Ñ‰ÐµÐ½Ð¸Ðµ Ð¸Ð¼ÐµÐµÑ‚ Ð½ÐµÐ¿Ñ€Ð°Ð²Ð¸Ð»ÑŒÐ½ÑƒÑŽ ÐºÐ¾Ð´Ð¸Ñ€Ð¾Ð²ÐºÐ°.)r   r   r   r1   rB   rC   r   )r   r.   r   r9   rI   r   r   r   Ú7test_message_with_valid_content_in_single_byte_encoding;  s   ÿÿ	þzHTestProcessEmail.test_message_with_valid_content_in_single_byte_encodingc                 C   sh   |   d¡}| j |¡}t d¡}|j}|  ||¡ tjdkr$t d¡}nt d¡}|j	}|  ||¡ d S )Nz6message_with_single_byte_extended_subject_encoding.emluF   Ã“Ã§Ã­Ã Ã© ÃªÃ Ãª Ã§Ã Ã°Ã Ã¡Ã Ã²Ã»Ã¢Ã Ã²Ã¼ Ã®Ã² 1000$ Ã¢ Ã­Ã¥Ã¤Ã¥Ã«Ã¾!)r)   r)   ztest test <mr.test32@mail.ru>ztest test<mr.test32@mail.ru>)
r   r   r   rB   rC   r   r   r*   r+   r   )r   r.   r   Úexpected_subjectÚactual_subjectÚexpected_fromÚactual_fromr   r   r   Ú.test_message_with_single_byte_subject_encodingQ  s   ÿÿ

z?TestProcessEmail.test_message_with_single_byte_subject_encodingc                 C   sè   t ddddgƒ}| j | ¡ ¡}|  |j¡ d}t dd|dgƒ}tj |d¡� | 	|¡}W d   ƒ n1 s7w   Y  |  
|j|¡ |  
||j¡ d |_tj |d¡� | 	|¡}W d   ƒ n1 sdw   Y  |  
| jj|j¡ d S )NzTest subjectz	Test bodyzusername@example.comzmr.test32@mail.ruÚsend)r   r   Úrecord_outgoing_messager   Ú
assertTrueÚoutgoingÚmockÚpatchÚobjectÚreplyr   rY   r   Ú
from_email)r   r.   r   rd   Úreply_email_objectÚ	reply_msgÚsecond_reply_msgr   r   r   Útest_message_replyk  s2   üüÿÿz#TestProcessEmail.test_message_replyc                 C   s>   |   d¡}| j |¡}|  |j ¡  ¡ d¡ |  d|j¡ d S )Nz message_with_text_attachment.emlr   z#Has an attached text document, too!)r   r   r   r   r   r    r$   r1   r^   r   r   r   Ú!test_message_with_text_attachmentŒ  s   ÿz2TestProcessEmail.test_message_with_text_attachmentc                 C   sF   |   d¡}tt| ¡ ƒƒ}| j |¡}|  |tt| ¡  ¡ ƒƒ¡ d S )Nzmessage_with_long_content.eml)r   Úlenr   rF   r   r   r   r8   )r   r.   Úsizer   r   r   r   Útest_message_with_long_content–  s   ÿÿz/TestProcessEmail.test_message_with_long_contentc                 C   sÔ   |   d¡}t ¡ }t d¡�}t |¡}d|d< ||_| j 	|¡}W d   ƒ n1 s+w   Y  | 
¡ }|  |jd ¡ |  |jj d¡¡ t|jjdƒ�}|  | ¡ |  d¡¡ W d   ƒ d S 1 scw   Y  d S )Nr
   ú!django_mailbox.utils.get_settingsTÚstore_original_messagez.emlÚrb)r   r   Úget_settingsrj   rk   ÚcopyÚdeepcopyÚreturn_valuer   r   r8   ÚassertNotEqualsÚemlrh   ÚnameÚendswithr<   r   rD   Ú_get_email_as_text©r   r   Údefault_settingsrz   Úalteredr   Úactual_email_objectrG   r   r   r   Útest_message_saved¡  s    

û
ÿ"ÿz#TestProcessEmail.test_message_savedc                 C   sr   |   d¡}t ¡ }t d¡�}t |¡}d|d< ||_| j 	|¡}W d   ƒ n1 s+w   Y  |  
|jd ¡ d S )Nr
   rw   Frx   )r   r   rz   rj   rk   r{   r|   r}   r   r   ÚassertEqualsr   )r   r   r„   rz   r…   r   r   r   r   Útest_message_saving_ignored·  s   

ûz,TestProcessEmail.test_message_saving_ignoredc                 C   sÐ   |   d¡}t ¡ }t d¡�}t |¡}d|d< d|d< ||_| j 	|¡}W d   ƒ n1 s/w   Y  | 
¡ }|  |jj d¡¡ t |jjd¡�}|  | ¡ |  d¡¡ W d   ƒ d S 1 saw   Y  d S )Nr
   rw   TÚcompress_original_messagerx   z.eml.gzry   )r   r   rz   rj   rk   r{   r|   r}   r   r   r8   rh   r   r€   r�   Úgzipr<   r   rD   r‚   rƒ   r   r   r   Útest_message_compressedÅ  s    

ú
ÿ"ÿz(TestProcessEmail.test_message_compressedN)Ú__name__Ú
__module__Ú__qualname__r   r"   r(   r/   r0   r5   r;   rK   rQ   r\   r]   r_   r`   re   rr   rs   rv   r‡   r‰   rŒ   r   r   r   r   r	      s*    0!
)r‹   Úos.pathr=   r*   r{   rj   rB   Údjango_mailbox.modelsr   r   Údjango_mailbox.utilsr   Údjango_mailboxr   Údjango_mailbox.tests.baser   Údjango.utils.encodingr   Údjango.core.mailr   Ú__all__r	   r   r   r   r   Ú<module>   s    