o
    w+f                    @   s   d dl Z d dlZzd dlmZ W n ey"   d dlmZ ejZY nw d dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZ dZe dddd	edd
ddgdddddddgigddZG dd dejZdS )    N)EmailMessage)message)Asm
AttachmentClickTrackingContentDynamicTemplateDataEmailFromMailPersonalizationSubjectSubstitutionToCcBccTrackingSettingsa  <!doctype html><html amp4email><head><meta charset="utf-8"><script async src="https://cdn.ampproject.org/v0.js"></script><style amp4email-boilerplate>body{visibility:hidden}</style><script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script><style amp-custom>.emailbody {padding: 16px;}.helloworld {font-family: Helvetica;color: red;font-size: 24px;padding-bottom: 8px;}.images {max-width: 100%;}</style></head><body><div class="emailbody"><h1 class="helloworld">Hello!</h1><amp-img src="https://amp.dev/static/samples/img/amp.jpg" width="800" height="600" layout="responsive"></amp-img></div></body></html>
text/plain)and easy to do anywhere, even with Pythontypevaluetext/x-amp-html	text/html:<strong>and easy to do anywhere, even with Python</strong>test+from@example.comExample From Nameemailnametotest+to@example.comExample To NameSending with SendGrid is Funcontentfrompersonalizationssubjectc                   @   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)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 Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:dqS )r	UnitTestsc                 C   sh   ddl m}m} t|d|g d}tdg d}| |j |j  | |j |j  d S )Nr   )GroupIdGroupsToDisplay   )r,         )sendgrid.helpers.mailr*   r+   r   assertEqualgroup_idgetgroups_to_display)selfr*   r+   asm1asm2 r7   N/var/www/html/venv/lib/python3.10/site-packages/test/unit/test_mail_helpers.pytest_asm9   s   zUnitTests.test_asmc                 C   s   ddl m}m}m}m}m} t|d|d|d|d|d}tddddd}| |j	 |j	  | |j
	 |j
	  | |j	 |j	  | |j	 |j	  | |j	 |j	  d S )Nr   )FileContentFileTypeFileNameDisposition	ContentIdBase64EncodedStringzexample.pdfapplication/pdf
attachment123)r/   r:   r;   r<   r=   r>   r   r0   file_contentr2   	file_name	file_typedisposition
content_id)r4   r:   r;   r<   r=   r>   a1a2r7   r7   r8   test_attachmentB   s(   zUnitTests.test_attachmentc                 C   s(   ddl m} |d}| d|  d S )Nr   )BatchId1)r/   rK   r0   r2   )r4   rK   b1r7   r7   r8   test_batch_idY   s   zUnitTests.test_batch_idc                 C   sj   ddl m}m}m}m}m}m} d | _||dd|dd|d|d|d	d
}| |	 t
d d S )Nr   r   r
   r   r   PlainTextContentHtmlContentr   r   r!   r"   r#   r   r   
from_email	to_emailsr(   plain_text_contenthtml_content  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "to": [
                            {
                                "email": "test+to@example.com",
                                "name": "Example To Name"
                            }
                        ]
                    }
                ],
                "subject": "Sending with SendGrid is Fun"
            }r/   r   r
   r   r   rP   rQ   maxDiffr0   r2   jsonloadsr4   r   r
   r   r   rP   rQ   r   r7   r7   r8   'test_single_email_to_a_single_recipient`   s"    	z1UnitTests.test_single_email_to_a_single_recipientc                 C   s|   ddl m}m}m}m}m}m} d| _| }|dd|_|dd|_	|d|_
|d	|_|d
|_| | td dS )zKTests bug found in Issue-451 with Content ordering causing a crash
        r   rO   Nr   r   r!   r"   r#   r   r   rW   )r/   r   r
   r   r   rP   rQ   rY   rS   r    r(   r%   r0   r2   rZ   r[   r\   r7   r7   r8   8test_single_email_to_a_single_recipient_content_reversed   s     
zBUnitTests.test_single_email_to_a_single_recipient_content_reversedc           	      C   sx   ddl m}m}m}m}m}m} d | _|dd|ddg}||dd||d	|d
|dd}| |	 t
d d S )Nr   rO   test+to0@example.comExample To Name 0test+to1@example.comExample To Name 1r   r   r#   r   r   rR   a}  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "to": [
                            {
                                "email": "test+to0@example.com",
                                "name": "Example To Name 0"
                            },
                            {
                                "email": "test+to1@example.com",
                                "name": "Example To Name 1"
                            }
                        ]
                    }
                ],
                "subject": "Sending with SendGrid is Fun"
            }rX   )	r4   r   r
   r   r   rP   rQ   rT   r   r7   r7   r8   /test_send_a_single_email_to_multiple_recipients   s(    	z9UnitTests.test_send_a_single_email_to_multiple_recipientsc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|d|d|d	d
}|dd|ddg|_	| 
| td d S )Nr   )r   r
   ReplyTor   r   rP   rQ   r   r   r_   r"   r#   r   r   rR   ztest+reply_to_1@example.com)r   ztest+reply_to_2@example.coma  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "to": [
                            {
                                "email": "test+to0@example.com",
                                "name": "Example To Name"
                            }
                        ]
                    }
                ],
                "reply_to_list": [
                    {
                        "email": "test+reply_to_1@example.com"
                    },
                    {
                        "email": "test+reply_to_2@example.com"
                    }
                ],
                "subject": "Sending with SendGrid is Fun"
            })r/   r   r
   rd   r   r   rP   rQ   rY   reply_to_listr0   r2   rZ   r[   )	r4   r   r
   rd   r   r   rP   rQ   r   r7   r7   r8   9test_send_a_single_email_with_multiple_reply_to_addresses   s   $zCUnitTests.test_send_a_single_email_with_multiple_reply_to_addressesc              	   C   s   ddl m}m}m}m}m}m}m} d | _|dd|dd|ddg|d	d
|dd|dd|ddgdg}|dd}	||dd||d|d|d|	dd}
| 	|

 td d S )Nr   )r   r
   r   r   rP   rQ   r   r_   zExample Name 0z-name-zExample Name Substitution 0z-github-zhttps://example.com/test0zOverride Global Subject)r   r   substitutionsr(   ra   Example Name 1zExample Name Substitution 1zhttps://example.com/test1)r   r   rg   z-time-z2019-01-01 00:00:00r   r   z	Hi -name-z8Hello -name-, your URL is -github-, email sent at -time-z[<strong>Hello -name-, your URL is <a href="-github-">here</a></strong> email sent at -time-T)rS   rT   r(   rU   rV   global_substitutionsis_multipleaR  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "Hello -name-, your URL is -github-, email sent at -time-"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>Hello -name-, your URL is <a href=\"-github-\">here</a></strong> email sent at -time-"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "substitutions": {
                            "-github-": "https://example.com/test1",
                            "-name-": "Example Name Substitution 1",
                            "-time-": "2019-01-01 00:00:00"
                        },
                        "to": [
                            {
                                "email": "test+to1@example.com",
                                "name": "Example Name 1"
                            }
                        ]
                    },
                    {
                        "subject": "Override Global Subject",
                        "substitutions": {
                            "-github-": "https://example.com/test0",
                            "-name-": "Example Name Substitution 0",
                            "-time-": "2019-01-01 00:00:00"
                        },
                        "to": [
                            {
                                "email": "test+to0@example.com",
                                "name": "Example Name 0"
                            }
                        ]
                    }
                ],
                "subject": "Hi -name-"
            })r/   r   r
   r   r   rP   rQ   r   rY   r0   r2   rZ   r[   )r4   r   r
   r   r   rP   rQ   r   rT   ri   r   r7   r7   r8   +test_multiple_emails_to_multiple_recipients"  sD   $
z5UnitTests.test_multiple_emails_to_multiple_recipientsc           	      C   st   ddl m}m}m}m}m}m}m} d | _||dd|dd|d|d|t	|d	d
}| 
| tt d S )Nr   r   r
   r   r   rP   rQ   AmpHtmlContentr   r   r!   r"   r#   r   r   )rS   rT   r(   rU   amp_html_contentrV   )r/   r   r
   r   r   rP   rQ   rm   rY   rn   r0   r2   rZ   r[   -response_content_with_all_three_mime_contents	r4   r   r
   r   r   rP   rQ   rm   r   r7   r7   r8   Ctest_single_email_with_all_three_email_contents_to_single_recipientu  s$   $zMUnitTests.test_single_email_with_all_three_email_contents_to_single_recipientc           
      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|d|t	|dd	}t
d
t	ddddgdddddddgigdd}	| | t
|	 d S )Nr   rl   r   r   r!   r"   r#   r   )rS   rT   r(   rn   rV   r   r   r   r   r    r$   r/   r   r
   r   r   rP   rQ   rm   rY   rn   rZ   dumpsr0   r2   r[   
r4   r   r
   r   r   rP   rQ   rm   r   response_contentr7   r7   r8   @test_single_email_with_amp_and_html_contents_to_single_recipient  sF   $	
zJUnitTests.test_single_email_with_amp_and_html_contents_to_single_recipientc           
      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|d|d|t	d	}t
d
dddt	dgdddddddgigdd}	| | t
|	 d S )Nr   rl   r   r   r!   r"   r#   r   )rS   rT   r(   rU   rn   r   r   r   r   r    r$   rr   rt   r7   r7   r8   Atest_single_email_with_amp_and_plain_contents_to_single_recipient  sF   $	
zKUnitTests.test_single_email_with_amp_and_plain_contents_to_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|d	|_	|t
|_	|d
|_	| | tt d S Nr   rl   r   r   r!   r"   r#   rS   rT   r(   r   r   r/   r   r
   r   r   rP   rQ   rm   rY   r%   rn   r0   r2   rZ   r[   ro   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_plain_amp_html_content_single_recipient  $   $
zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_plain_amp_html_content_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|d	|_	|d
|_	|t
|_	| | tt d S rx   rz   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_plain_html_amp_content_single_recipient  $   $
zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_plain_html_amp_content_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|d	|_	|d
|_	|t
|_	| | tt d S Nr   rl   r   r   r!   r"   r#   ry   r   r   rz   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_html_plain_amp_content_single_recipient  r~   zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_html_plain_amp_content_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|d	|_	|t
|_	|d
|_	| | tt d S r   rz   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_html_amp_plain_content_single_recipient   r|   zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_html_amp_plain_content_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|t	|_
|d	|_
|d
|_
| | tt d S r   r/   r   r
   r   r   rP   rQ   rm   rY   rn   r%   r0   r2   rZ   r[   ro   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_amp_html_plain_content_single_recipient4  $   $
zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_amp_html_plain_content_single_recipientc           	      C   s   ddl m}m}m}m}m}m}m} d | _||dd|dd|dd}|t	|_
|d	|_
|d
|_
| | tt d S rx   r   rp   r7   r7   r8   gtest_single_email_with_all_three_contents_in_collapsed_order_of_amp_plain_html_content_single_recipientH  r   zqUnitTests.test_single_email_with_all_three_contents_in_collapsed_order_of_amp_plain_html_content_single_recipientc                 C   sz   ddl m}m} d | _ddgddgg}| t ttdd|td	|d
|dd W d    d S 1 s6w   Y  d S )Nr   rP   rQ   r_   r`   ra   rb   r   r   r#   r   r   rR   )	r/   rP   rQ   rY   assertRaises
ValueErrorr   r
   r   r4   rP   rQ   rT   r7   r7   r8   <test_value_error_is_raised_on_to_emails_set_to_list_of_lists^  s$   "zFUnitTests.test_value_error_is_raised_on_to_emails_set_to_list_of_listsc                 C   s|   ddl m}m} d | _ddg}ttdd|td|d|d	d
}| t ddg|_	W d    d S 1 s7w   Y  d S )Nr   r   r_   r`   ra   rb   r   r   r#   r   r   rR   ztest+reply_to0@example.comztest+reply_to1@example.com
r/   rP   rQ   rY   r   r
   r   r   r   re   r4   rP   rQ   rT   mailr7   r7   r8   Dtest_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_strsp  s&   "zNUnitTests.test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_strsc                 C   sz   ddl m}m} d | _ddg}ttdd|td|d|d	d
}| t dg|_	W d    d S 1 s6w   Y  d S )Nr   r   r   r   r   r   r#   r   r   rR   )ztest+reply_to@example.comz	Test Namer   r   r7   r7   r8   Ftest_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_tuples  s&   
"zPUnitTests.test_value_error_is_raised_on_to_emails_set_to_reply_to_list_of_tuplesc                 C   F   ddl m}m} d | _ddg}ttdd|td|d|d	d
 d S )Nr   r   r   r   r   r   r#   r   r   rR   r/   rP   rQ   rY   r   r
   r   r   r7   r7   r8   ;test_error_is_not_raised_on_to_emails_set_to_list_of_tuples  s    
zEUnitTests.test_error_is_not_raised_on_to_emails_set_to_list_of_tuplesc                 C   r   )Nr   r   r_   ra   r   r   r#   r   r   rR   r   r   r7   r7   r8   9test_error_is_not_raised_on_to_emails_set_to_list_of_strs  s   
zCUnitTests.test_error_is_not_raised_on_to_emails_set_to_list_of_strsc                 C   B   ddl m}m} d | _d}ttdd|td|d|dd	 d S )
Nr   r   r_   r   r   r#   r   r   rR   r   r   r7   r7   r8   2test_error_is_not_raised_on_to_emails_set_to_a_str     
z<UnitTests.test_error_is_not_raised_on_to_emails_set_to_a_strc                 C   r   )
Nr   r   r   r   r   r#   r   r   rR   r   r   r7   r7   r8   4test_error_is_not_raised_on_to_emails_set_to_a_tuple  r   z>UnitTests.test_error_is_not_raised_on_to_emails_set_to_a_tuplec                 C   sZ   ddl m}m} d | _tddtddtddg}ttd	d
|t	d|d|dd d S )Nr   r   r_   r`   ztest+bcc@example.comzExample Bcc Name 1ztest+cc@example.comzExample Cc Name 2r   r   r#   r   r   rR   )
r/   rP   rQ   rY   r   r   r   r   r
   r   r   r7   r7   r8   5test_error_is_not_raised_on_to_emails_includes_bcc_cc  s"   
z?UnitTests.test_error_is_not_raised_on_to_emails_includes_bcc_ccc                 C   B   d | _ t }tdd}|| || | | g|j d S Nr_   r`   rY   r   r   	add_emailr0   r2   tos)r4   pto_emailr7   r7   r8   >test_personalization_add_email_filters_out_duplicate_to_emails     


zHUnitTests.test_personalization_add_email_filters_out_duplicate_to_emailsc                 C   L   d | _ t }tdd}tdd}|| || | | g|j d S Nr_   r`   test+TO0@example.comr   )r4   r   r   to_email_with_capsr7   r7   r8   Ltest_personalization_add_email_filters_out_duplicate_to_emails_ignoring_case     



zVUnitTests.test_personalization_add_email_filters_out_duplicate_to_emails_ignoring_casec                 C   s6   d | _ t }tdd}|| | | |j d S )Nr   zExample From)rY   r   r
   set_fromr0   r2   rS   )r4   r   rS   r7   r7   r8   #test_personalization_set_from_email  s
   

z-UnitTests.test_personalization_set_from_emailc                 C   r   )Ntest+cc0@example.comExample Cc Name 0rY   r   r   r   r0   r2   ccs)r4   r   cc_emailr7   r7   r8   4test_personalization_filters_out_duplicate_cc_emails  r   z>UnitTests.test_personalization_filters_out_duplicate_cc_emailsc                 C   r   Nr   r   test+CC0@example.comr   r4   r   r   cc_email_with_capsr7   r7   r8   Btest_personalization_filters_out_duplicate_cc_emails_ignoring_case  r   zLUnitTests.test_personalization_filters_out_duplicate_cc_emails_ignoring_casec                 C   r   )Ntest+bcc0@example.comExample Bcc Name 0rY   r   r   r   r0   r2   bccs)r4   r   	bcc_emailr7   r7   r8   5test_personalization_filters_out_duplicate_bcc_emails  r   z?UnitTests.test_personalization_filters_out_duplicate_bcc_emailsc                 C   r   Nr   r   test+BCC0@example.comr   r4   r   r   bcc_email_with_capsr7   r7   r8   Ctest_personalization_filters_out_duplicate_bcc_emails_ignoring_case   r   zMUnitTests.test_personalization_filters_out_duplicate_bcc_emails_ignoring_casec                 C   :   d | _ t }dddgd }||_| |d g|j d S )Nr_   r`   r   r-   r   rY   r   r   r0   r4   r   rT   r7   r7   r8   Atest_personalization_tos_setter_filters_out_duplicate_dict_emails+  
   zKUnitTests.test_personalization_tos_setter_filters_out_duplicate_dict_emailsc                 C   B   d | _ t }ddd}ddd}||g}||_| |g|j d S )Nr_   r`   r   r   r   r4   r   r   r   rT   r7   r7   r8   Otest_personalization_tos_setter_filters_out_duplicate_dict_emails_ignoring_case4     

zYUnitTests.test_personalization_tos_setter_filters_out_duplicate_dict_emails_ignoring_casec                 C   >   d | _ t }tddgd }||_| |d  g|j d S )Nr_   r`   r-   r   rY   r   r   r   r0   r2   r   r7   r7   r8   ?test_personalization_tos_setter_filters_out_duplicate_to_emails?  
   zIUnitTests.test_personalization_tos_setter_filters_out_duplicate_to_emailsc                 C   sF   d | _ t }tdd}tdd}||g}||_| | g|j d S r   r   r   r7   r7   r8   Mtest_personalization_tos_setter_filters_out_duplicate_to_emails_ignoring_caseI  s   

zWUnitTests.test_personalization_tos_setter_filters_out_duplicate_to_emails_ignoring_casec                 C   r   )Nr   r   r   r-   r   rY   r   r   r0   r4   r   	cc_emailsr7   r7   r8   Atest_personalization_ccs_setter_filters_out_duplicate_dict_emailsT  r   zKUnitTests.test_personalization_ccs_setter_filters_out_duplicate_dict_emailsc                 C   r   )Nr   r   r   r   r   )r4   r   r   r   r   r7   r7   r8   Otest_personalization_ccs_setter_filters_out_duplicate_dict_emails_ignoring_case]  r   zYUnitTests.test_personalization_ccs_setter_filters_out_duplicate_dict_emails_ignoring_casec                 C   r   )Nr   r   r-   r   rY   r   r   r   r0   r2   r   r7   r7   r8   ?test_personalization_ccs_setter_filters_out_duplicate_cc_emailsh  r   zIUnitTests.test_personalization_ccs_setter_filters_out_duplicate_cc_emailsc                 C   B   d | _ t }tdd}tdd}||g|_| | g|j d S r   r   r   r7   r7   r8   Mtest_personalization_ccs_setter_filters_out_duplicate_cc_emails_ignoring_caseq  r   zWUnitTests.test_personalization_ccs_setter_filters_out_duplicate_cc_emails_ignoring_casec                 C   r   )Nr   r   r   r-   r   rY   r   r   r0   r4   r   
bcc_emailsr7   r7   r8   Btest_personalization_bccs_setter_filters_out_duplicate_dict_emails{  r   zLUnitTests.test_personalization_bccs_setter_filters_out_duplicate_dict_emailsc                 C   r   )Nr   r   r   r   r   )r4   r   r   r   r   r7   r7   r8   Ptest_personalization_bccs_setter_filters_out_duplicate_dict_emails_ignoring_case  r   zZUnitTests.test_personalization_bccs_setter_filters_out_duplicate_dict_emails_ignoring_casec                 C   r   )Nr   r   r-   r   rY   r   r   r   r0   r2   r   r7   r7   r8   Atest_personalization_bccs_setter_filters_out_duplicate_bcc_emails  r   zKUnitTests.test_personalization_bccs_setter_filters_out_duplicate_bcc_emailsc                 C   r   r   r   r   r7   r7   r8   Otest_personalization_bccs_setter_filters_out_duplicate_bcc_emails_ignoring_case  r   zYUnitTests.test_personalization_bccs_setter_filters_out_duplicate_bcc_emails_ignoring_casec                 C   F   d | _ t }tdd}|| || | g}| ||j d S r   )rY   r   r   add_tor2   r0   r   )r4   r   r   expectedr7   r7   r8   ;test_personalization_add_to_filters_out_duplicate_to_emails     



zEUnitTests.test_personalization_add_to_filters_out_duplicate_to_emailsc                 C   r   r   )rY   r   r   add_bccr2   r0   r   )r4   r   r   r   r7   r7   r8   =test_personalization_add_bcc_filters_out_duplicate_bcc_emails  r   zGUnitTests.test_personalization_add_bcc_filters_out_duplicate_bcc_emailsc                 C   r   r   )rY   r   r   add_ccr2   r0   r   )r4   r   r   r   r7   r7   r8   ;test_personalization_add_cc_filters_out_duplicate_cc_emails  r   zEUnitTests.test_personalization_add_cc_filters_out_duplicate_cc_emailsc                 C   sf   d | _ tddtddidtddddidg}ttd	d
|tddddd}| | t	d d S )Nztest+to+0@example.comzExample To 0 Namer   zExample 0 Name)r   r   dynamic_template_dataztest+to+1@example.comzExample To 1 NamezExample 1 Nametest@example.comr   zHi!zHello!z<strong>Hello!</strong>T)rS   rT   r(   rU   rV   rj   a  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "Hello!"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>Hello!</strong>"
                    }
                ],
                "from": {
                    "email": "test@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "dynamic_template_data": {
                            "name": "Example 1 Name"
                        },
                        "to": [
                            {
                                "email": "test+to+1@example.com",
                                "name": "Example To 1 Name"
                            }
                        ]
                    },
                    {
                        "dynamic_template_data": {
                            "name": "Example 0 Name"
                        },
                        "to": [
                            {
                                "email": "test+to+0@example.com",
                                "name": "Example To 0 Name"
                            }
                        ]
                    }
                ],
                "subject": "Hi!"
            })
rY   r   r   r   r
   r   r0   r2   rZ   r[   )r4   rT   r   r7   r7   r8   test_dynamic_template_data  s,   
z$UnitTests.test_dynamic_template_datac           ;      C   sX  ddl m}m}m}m}m}m}m}m}m	}	m
}
m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m}m } m!}!m"}"m#}#m$}$m%}%m&}&m'}'m(}(m)})m*}*m+}+m,},m-}-m.}.m/}/m0}0m1}1m2}2m3}3m4}4m5}5m6}6m7}7 d | _8| }8|dddd|8_9|dddd|dd	ddg|8_9|d
ddd|8_:|dddd|ddddg|8_:|dddd|8_;|dddd|ddddg|8_;|ddd|8_<|dddd|8_=|dddd|8_=|dddd|ddddg|8_=|dd dd|8_>|d!d"dd|8_>|d#d$dd|d%d&ddg|8_>|	d'd(dd|8_?|	d)d*dd|8_?|	d+d*dd|	d,d(ddg|8_?|
d-dd|8_@|d.d/d0d|8_9|d1d2d0d|d3d4d0dg|8_9|d5d6d0d|8_:|d7d8d0d|d9d:d0dg|8_:|d;d<d0d|8_;|d=d>d0d|d?d@d0dg|8_;|dAdBd0d|8_=|dCdDd0d|8_=|dEdFd0d|dGdHd0dg|8_=|dIdJd0d|8_>|dKdLd0d|8_>|dMdNd0d|dOdPd0dg|8_>|	dQd(d0d|8_?|	dRd*d0d|8_?|	dSd*d0d|	dTd(d0dg|8_?|
dUd0d|8_@|dVd0d|8_<|dWdX|8_A|dYdZ|8_B|d[|8_<||jCd\|8_D||jEd]|8_D|d^d_|d`dag|8_D||db|dc|dd|de|df|8_F||dg|dh|di|dj|dk||dl|dm|di|dj|dng|8_F|do|8_G|dpdq|8_H|drds|dtdug|8_H|dvdw|8_=|dxdy|8_=|dzd{|d|d}g|8_=|d~|8_I|d|8_I|d~|dg|8_I|	dd*|8_?|	dd(|8_?|	dd(|	dd*g|8_?|
d|8_@|d|8_J||d0|g d|8_K|d|8_L| }9|d|d|9_M|d|9_N| d|9_O|!d|9_P|"d|9_Q|#d|$d|%d|9_R|&d|9_S|'d|(d|)d|9_T|9|8_U|* }:|+dd|:_V|0d|1d|:_W|,d|-d|.d|/d|:_X|2d|3d|4d|5d|6d|7d|:_Y|:|8_Z| [|8\ t]^d d S )Nr   )7r   r
   r   r   r   r   r   Header	CustomArgSendAtr   MimeTyper   r<   r:   r;   r=   r>   
TemplateIdSectionrd   CategoryrK   r   r*   r+   
IpPoolNameMailSettingsBccSettingsBccSettingsEmailBypassBounceManagementBypassListManagementBypassSpamManagementBypassUnsubscribeManagementFooterSettings
FooterText
FooterHtmlSandBoxMode	SpamCheckSpamThresholdSpamUrlr   r   SubscriptionTrackingSubscriptionTextSubscriptionHtmlSubscriptionSubstitutionTagOpenTrackingOpenTrackingSubstitutionTag
Ganalytics	UtmSource	UtmMediumUtmTerm
UtmContentUtmCampaignztest1@example.comzExample User1r   ztest2@example.comzExample User2ztest3@example.comzExample User3ztest4@example.comzExample User4ztest5@example.comzExample User5ztest6@example.comzExample User6ztest7@example.comzExample User7ztest8@example.comzExample User8ztest9@example.comzExample User9zSending with SendGrid is Fun 0zX-Test1Test1zX-Test2Test2zX-Test3Test3zX-Test4Test4z%name1%rh   z%city1%zExample City 1z%name2%zExample Name 2z%city2%zExample City 2
marketing1truetransactional1false
marketing2transactional2i Wztest10@example.comzExample User10r,   ztest11@example.comzExample User11ztest12@example.comzExample User12ztest13@example.comzExample User13ztest14@example.comzExample User14ztest15@example.comzExample User15ztest16@example.comzExample User16ztest17@example.comzExample User17ztest18@example.comzExample User18zX-Test5Test5zX-Test6Test6zX-Test7Test7zX-Test8Test8z%name3%zExample Name 3z%city3%zExample City 3z%name4%zExample Name 4z%city4%zExample City 4
marketing3transactional3
marketing4transactional4i WzSending with SendGrid is Fun 1zhelp@twilio.comzTwilio SendGridzhelp_reply@twilio.comzTwilio SendGrid ReplyzSending with SendGrid is Fun 2r   r   ztext/calendarzParty Time!!ztext/customzParty Time 2!!zbase64 encoded content 1zbalance_001.pdfr@   rA   zContent ID 1zbase64 encoded content 2z
banner.pngz	image/pnginlinezContent ID 2zbase64 encoded content 3zbanner2.pngzContent ID 3z$13b8f94f-bcae-4ec6-b752-70d6cb59f932z
%section1%zSubstitution for Section 1 Tagz
%section2%zSubstitution for Section 2 Tagz
%section3%zSubstitution for Section 3 TagzX-Test9Test9zX-Test10Test10zX-Test11Test11zX-Test12Test12z
Category 1z
Category 2
marketing5transactional5
marketing6transactional6i W HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi)r,   r-   r.      zIP Pool NameFzbcc@twilio.comTw00tz<string>w00t!<strong>   zhttps://example.comopen_trackingGoodbyez<strong>Goodbye!</strong>unsubscribe
utm_source
utm_mediumutm_termutm_contentutm_campaigna+  {
                "asm": {
                    "group_id": 1,
                    "groups_to_display": [
                        1,
                        2,
                        3,
                        4
                    ]
                },
                "attachments": [
                    {
                        "content": "base64 encoded content 3",
                        "content_id": "Content ID 3",
                        "disposition": "inline",
                        "filename": "banner2.png",
                        "type": "image/png"
                    },
                    {
                        "content": "base64 encoded content 2",
                        "content_id": "Content ID 2",
                        "disposition": "inline",
                        "filename": "banner.png",
                        "type": "image/png"
                    },
                    {
                        "content": "base64 encoded content 1",
                        "content_id": "Content ID 1",
                        "disposition": "attachment",
                        "filename": "balance_001.pdf",
                        "type": "application/pdf"
                    }
                ],
                "batch_id": "HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi",
                "categories": [
                    "Category 2",
                    "Category 1",
                    "Category 2",
                    "Category 1"
                ],
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    },
                    {
                        "type": "text/calendar",
                        "value": "Party Time!!"
                    },
                    {
                        "type": "text/custom",
                        "value": "Party Time 2!!"
                    }
                ],
                "custom_args": {
                    "marketing5": "false",
                    "marketing6": "true",
                    "transactional5": "true",
                    "transactional6": "false"
                },
                "from": {
                    "email": "help@twilio.com",
                    "name": "Twilio SendGrid"
                },
                "headers": {
                    "X-Test10": "Test10",
                    "X-Test11": "Test11",
                    "X-Test12": "Test12",
                    "X-Test9": "Test9"
                },
                "ip_pool_name": "IP Pool Name",
                "mail_settings": {
                    "bcc": {
                        "email": "bcc@twilio.com",
                        "enable": false
                    },
                    "bypass_bounce_management": {
                        "enable": false
                    },
                    "bypass_list_management": {
                        "enable": false
                    },
                    "bypass_spam_management": {
                        "enable": false
                    },
                    "bypass_unsubscribe_management": {
                        "enable": false
                    },
                    "footer": {
                        "enable": true,
                        "html": "<string>w00t!<strong>",
                        "text": "w00t"
                    },
                    "sandbox_mode": {
                        "enable": true
                    },
                    "spam_check": {
                        "enable": true,
                        "post_to_url": "https://example.com",
                        "threshold": 5
                    }
                },
                "personalizations": [
                    {
                        "bcc": [
                            {
                                "email": "test7@example.com",
                                "name": "Example User7"
                            },
                            {
                                "email": "test8@example.com",
                                "name": "Example User8"
                            },
                            {
                                "email": "test9@example.com",
                                "name": "Example User9"
                            }
                        ],
                        "cc": [
                            {
                                "email": "test4@example.com",
                                "name": "Example User4"
                            },
                            {
                                "email": "test5@example.com",
                                "name": "Example User5"
                            },
                            {
                                "email": "test6@example.com",
                                "name": "Example User6"
                            }
                        ],
                        "custom_args": {
                            "marketing1": "true",
                            "marketing2": "false",
                            "transactional1": "false",
                            "transactional2": "true"
                        },
                        "headers": {
                            "X-Test1": "Test1",
                            "X-Test2": "Test2",
                            "X-Test3": "Test3",
                            "X-Test4": "Test4"
                        },
                        "send_at": 1461775051,
                        "subject": "Sending with SendGrid is Fun 0",
                        "substitutions": {
                            "%city1%": "Example City 1",
                            "%city2%": "Example City 2",
                            "%name1%": "Example Name 1",
                            "%name2%": "Example Name 2"
                        },
                        "to": [
                            {
                                "email": "test1@example.com",
                                "name": "Example User1"
                            },
                            {
                                "email": "test2@example.com",
                                "name": "Example User2"
                            },
                            {
                                "email": "test3@example.com",
                                "name": "Example User3"
                            }
                        ]
                    },
                    {
                        "bcc": [
                            {
                                "email": "test16@example.com",
                                "name": "Example User16"
                            },
                            {
                                "email": "test17@example.com",
                                "name": "Example User17"
                            },
                            {
                                "email": "test18@example.com",
                                "name": "Example User18"
                            }
                        ],
                        "cc": [
                            {
                                "email": "test13@example.com",
                                "name": "Example User13"
                            },
                            {
                                "email": "test14@example.com",
                                "name": "Example User14"
                            },
                            {
                                "email": "test15@example.com",
                                "name": "Example User15"
                            }
                        ],
                        "custom_args": {
                            "marketing3": "true",
                            "marketing4": "false",
                            "transactional3": "false",
                            "transactional4": "true"
                        },
                        "headers": {
                            "X-Test5": "Test5",
                            "X-Test6": "Test6",
                            "X-Test7": "Test7",
                            "X-Test8": "Test8"
                        },
                        "send_at": 1461775052,
                        "subject": "Sending with SendGrid is Fun 1",
                        "substitutions": {
                            "%city3%": "Example City 3",
                            "%city4%": "Example City 4",
                            "%name3%": "Example Name 3",
                            "%name4%": "Example Name 4"
                        },
                        "to": [
                            {
                                "email": "test10@example.com",
                                "name": "Example User10"
                            },
                            {
                                "email": "test11@example.com",
                                "name": "Example User11"
                            },
                            {
                                "email": "test12@example.com",
                                "name": "Example User12"
                            }
                        ]
                    }
                ],
                "reply_to": {
                    "email": "help_reply@twilio.com",
                    "name": "Twilio SendGrid Reply"
                },
                "sections": {
                    "%section1%": "Substitution for Section 1 Tag",
                    "%section2%": "Substitution for Section 2 Tag",
                    "%section3%": "Substitution for Section 3 Tag"
                },
                "send_at": 1461775053,
                "subject": "Sending with SendGrid is Fun 2",
                "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932",
                "tracking_settings": {
                    "click_tracking": {
                        "enable": true,
                        "enable_text": false
                    },
                    "ganalytics": {
                        "enable": true,
                        "utm_campaign": "utm_campaign",
                        "utm_content": "utm_content",
                        "utm_medium": "utm_medium",
                        "utm_source": "utm_source",
                        "utm_term": "utm_term"
                    },
                    "open_tracking": {
                        "enable": true,
                        "substitution_tag": "open_tracking"
                    },
                    "subscription_tracking": {
                        "enable": true,
                        "html": "<strong>Goodbye!</strong>",
                        "substitution_tag": "unsubscribe",
                        "text": "Goodbye"
                    }
                }
            })_r/   r   r
   r   r   r   r   r   r   r   r   r   r   r   r<   r:   r;   r=   r>   r   r   rd   r   rK   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  r  r  rY   r    ccbccr(   headersubstitution
custom_argsend_atrS   reply_totextr%   htmlrA   template_idsectioncategorybatch_idasmip_pool_namebcc_settingsbypass_bounce_managementbypass_list_managementbypass_spam_managementbypass_unsubscribe_managementfooter_settingssandbox_mode
spam_checkmail_settingsclick_trackingr/  subscription_tracking
ganalyticstracking_settingsr0   r2   rZ   r[   );r4   r   r
   r   r   r   r   r   r   r   r   r   r   r   r<   r:   r;   r=   r>   r   r   rd   r   rK   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  r  r  r   rN  rR  r7   r7   r8   test_kitchen_sink  sJ  










zUnitTests.test_kitchen_sinkc                 C   s   ddl m}m}m}m}m}m} d | _||dd|dd|d|d|d	d
}tdddddddddddddgdddddddd	|_	| 
| td d S )Nr   rO   r   r   r!   r"   r#   r   r   rR   z$ 239.85zNew Line Sneakerszhttps://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.pngz$ 79.95)r>  imagepricezOld Line Sneakerszhttps://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.pngzBlue Line Sneakerszhttps://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.pngTzSample Namez1234 Fake St.zApt. 123PlaceCO80202)	totalitemsreceiptr   	address01	address02citystatezipa
  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "dynamic_template_data": {
                            "address01": "1234 Fake St.",
                            "address02": "Apt. 123",
                            "city": "Place",
                            "items": [
                                {
                                    "image": "https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png",
                                    "price": "$ 79.95",
                                    "text": "New Line Sneakers"
                                },
                                {
                                    "image": "https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png",
                                    "price": "$ 79.95",
                                    "text": "Old Line Sneakers"
                                },
                                {
                                    "image": "https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png",
                                    "price": "$ 79.95",
                                    "text": "Blue Line Sneakers"
                                }
                            ],
                            "name": "Sample Name",
                            "receipt": true,
                            "state": "CO",
                            "total": "$ 239.85",
                            "zip": "80202"
                        },
                        "to": [
                            {
                                "email": "test+to@example.com",
                                "name": "Example To Name"
                            }
                        ]
                    }
                ],
                "subject": "Sending with SendGrid is Fun"
            })r/   r   r
   r   r   rP   rQ   rY   r   r   r0   r2   rZ   r[   r\   r7   r7   r8   >test_single_email_to_a_single_recipient_with_dynamic_templates  sP    
zHUnitTests.test_single_email_to_a_single_recipient_with_dynamic_templatesc                 C   s   d| _ t }td|_d|_t }|td || z|t	dd |t	dd | 
tj| dd	d
 W n
 tyF   Y dS w | d dS )z7Tests if including SendGrid API will throw an ExceptionNr   z,Hello World from the SendGrid Python Libraryr   zsome SG.2123b1B.1212lBaC herer   z8<html><body>some SG.Ba2BlJSDba.232Ln2 here</body></html>T)	sort_keysa-  {"content": [{"type": "text/plain", "value": "some text here"}, {"type": "text/html", "value": "<html><body>some text here</body></html>"}], "from": {"email": "test@example.com"}, "personalizations": [{"to": [{"email": "test@example.com"}]}], "subject": "Hello World from the SendGrid Python Library"}z/Should have failed as SendGrid API key included)max_diffr   r	   rS   r(   r   r   add_personalizationadd_contentr   r0   rZ   rs   r2   	Exceptionfail)r4   r   personalizationr7   r7   r8   test_sendgrid_api_keyV  s>   

zUnitTests.test_sendgrid_api_keyc                 C   sz   ddl m}m}m}m}m}m} d | _||dd|dd|d|d|d	d
}tdddd|_	| 
| td d S )Nr   rO   r   r   r!   r"   r#   r   r   rR   z%city%u
   Αθήναr,   r  ue  {
                "content": [
                    {
                        "type": "text/plain",
                        "value": "and easy to do anywhere, even with Python"
                    },
                    {
                        "type": "text/html",
                        "value": "<strong>and easy to do anywhere, even with Python</strong>"
                    }
                ],
                "from": {
                    "email": "test+from@example.com",
                    "name": "Example From Name"
                },
                "personalizations": [
                    {
                        "to": [
                            {
                                "email": "test+to@example.com",
                                "name": "Example To Name"
                            }
                        ]
                    },
                    {
                        "substitutions": {
                            "%city%": "Αθήνα"
                        }
                    }
                ],
                "subject": "Sending with SendGrid is Fun"
            })r/   r   r
   r   r   rP   rQ   rY   r   r:  r0   r2   rZ   r[   r\   r7   r7   r8   +test_unicode_values_in_substitutions_helper  s$    z5UnitTests.test_unicode_values_in_substitutions_helperc                 C   s   |  ttdttd d S )Nr,      )r   r   r   listrange)r4   r7   r7   r8   test_asm_display_group_limit  s   z&UnitTests.test_asm_display_group_limitc                 C   s0   t  }tdd|_| | ddddi d S )NFrO  )enableenable_text)r   r   rO  r0   r2   )r4   rR  r7   r7   r8   test_disable_tracking  s   zUnitTests.test_disable_trackingc                 C   s<   ddl m}m} | }|d|_| | dddii d S )Nr   )r   r   TrH  ro  )r/   r   r   rH  r0   r2   )r4   r   r   rN  r7   r7   r8   test_bypass_list_management  s   
z%UnitTests.test_bypass_list_managementc                 C   sd   ddl m}m}m}m} | }|d|_|d|_|d|_| |	 ddiddiddid d S )Nr   )r   r   r   r   Tro  )rG  rI  rJ  )
r/   r   r   r   r   rG  rI  rJ  r0   r2   )r4   r   r   r   r   rN  r7   r7   r8   test_v3_bypass_filters  s   


z UnitTests.test_v3_bypass_filtersN);__name__
__module____qualname__r9   rJ   rN   r]   r^   rc   rf   rk   rq   rv   rw   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   r   r   r   r   r   r   r   rS  ra  ri  rj  rn  rq  rr  rs  r7   r7   r7   r8   r)   7   sx    	,.44S-.
	

	
		
		
@   ra.1	r)   )rZ   unittestemail.messager   ImportErrorr   r   Messager/   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   rn   rs   ro   TestCaser)   r7   r7   r7   r8   <module>   sD   
D	
 