o
    üYTØ4  ã                   @   sˆ   d dl mZmZ d dlmZ d dlZd dlmZmZ G dd„ deƒZ	G dd„ de	ƒZ
G d	d
„ d
e
ƒZG dd„ de
ƒZG dd„ de	ƒZdS )é    )ÚTestCaseÚClient)ÚsettingsN)ÚutilsÚmodelsc                       s   e Zd Z‡ fdd„Z‡  ZS )ÚBaseTestc                    s.   t dd�| _dddddœ| _tt| ƒ ¡  d S )NT)Úenforce_csrf_checkszTest SubjectzHi, I am a test bodyzemail@example.com)úother_email@example.com)ÚsubjectÚbodyÚ
from_emailÚto)r   ÚclientÚ
email_dataÚsuperr   ÚsetUp©Úself©Ú	__class__© úK/var/www/html/env/lib/python3.10/site-packages/sendgrid/tests/test_views.pyr   
   s   ýzBaseTest.setUp)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r   	   s    r   c                   @   sD   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S )ÚViewTestCasec              	   C   s®   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt d|jdddœg¡d	d
�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )z Test email callback.
        é   r   Ú	initiatedú/sendgrid_callback/r	   Ú	processedÚ	123456789©ÚemailÚuuidÚeventÚ	timestampúapplication/json©ÚdataÚcontent_typeéÈ   Nr   ©r   ÚSendgridEmailMessager   ÚsendÚassertEqualr   ÚEmailÚobjectsÚcountÚallr%   r   ÚpostÚjsonÚdumpsr$   Ústatus_code©r   ÚmessageÚresponser   r   r   Útest_callback_view   s    üùzViewTestCase.test_callback_viewc              
   C   sÌ   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ |  tjj¡� | jjdt ddddd	œg¡d
d� W d  ƒ n1 sHw   Y  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )ú5 Test the callback view with erroneous data.
        r   r   r   r   r	   Ú333r    r!   r"   r'   r(   Nr   )r   r-   r   r.   r/   r   r0   r1   r2   r3   r%   ÚassertRaisesÚDoesNotExistr   r4   r5   r6   ©r   r9   r   r   r   Útest_callback_view_error-   s    ýûÿ	z%ViewTestCase.test_callback_view_errorc              	   C   sÆ   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ |  t¡� | jjdt ddddœg¡d	d
� W d  ƒ n1 sEw   Y  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )r<   r   r   r   r   r	   r    r!   ©r#   r%   r&   r'   r(   Nr   )r   r-   r   r.   r/   r   r0   r1   r2   r3   r%   r>   ÚKeyErrorr   r4   r5   r6   r@   r   r   r   Útest_callback_with_missing_dataD   s   þüÿz,ViewTestCase.test_callback_with_missing_datac              	   C   óÀ   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ tjj 	¡ d }|  |j
d¡ d|_
| ¡  | jjdt d|jddd	œg¡d
d�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )zD Test what happens if we try to do a wrong state transition
        r   r   r   Úbouncer   r	   r    r!   r"   r'   r(   r+   Nr   ©r   r-   r   r.   r/   r   r0   r1   r2   r3   r%   Úsaver   r4   r5   r6   r$   r7   ©r   r9   Ú_mailr:   r   r   r   Útest_wrong_transitionZ   ó&   üùz"ViewTestCase.test_wrong_transitionc              	   C   s  t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt d|jdddœg¡d	d
�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt d|jdddœg¡d	d
�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )z( Test normal state transitions.
        r   r   r   r   r	   r    r!   r"   r'   r(   r+   Ú	deliveredÚ	123459999Nr   r,   r8   r   r   r   Útest_state_transitionsw   s8   üùüùz#ViewTestCase.test_state_transitionsc              
   C   s°   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt d|jdddd	œg¡d
d�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )z] Test what happens if we send a null value as a reason, opposed to a missing reason.
        r   r   r   r   r	   r    r!   N)r#   r$   r%   r&   Úreasonr'   r(   r+   r   r,   r8   r   r   r   Útest_null_reasonŸ   s"   ûø	zViewTestCase.test_null_reasonc              
   C   s°   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ tjj 	¡ d }|  |j
d¡ d|_
| ¡  |  t¡� | jjdt d|jddd	œg¡d
d� W d  ƒ dS 1 sQw   Y  dS )úI Test what happens if we saved a non-existent state in our email
        r   r   r   Úbaconr   r	   r    r!   r"   r'   r(   Nr   )r   r-   r   r.   r/   r   r0   r1   r2   r3   r%   rH   r>   rC   r   r4   r5   r6   r$   )r   r9   rJ   r   r   r   Útest_wrong_saved_state¹   s"   ýû"ÿz#ViewTestCase.test_wrong_saved_stateN)
r   r   r   r;   rA   rD   rK   rO   rQ   rT   r   r   r   r   r      s    (r   c                       ó,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚViewTZTestCasez* Test explicitly with USE_TZ enabled.
    c                    ó    t j| _dt _tt| ƒ ¡  d S ©NT)r   ÚUSE_TZÚ_old_use_tzr   rV   r   r   r   r   r   r   Ô   ó   zViewTZTestCase.setUpc                    ó   | j t_tt| ƒ ¡  d S ©N)rZ   r   rY   r   rV   ÚtearDownr   r   r   r   r^   Ù   ó   zViewTZTestCase.tearDown©r   r   r   Ú__doc__r   r^   r   r   r   r   r   rV   Ñ   ó    rV   c                       rU   )ÚViewNoTZTestCasez+ Test explicitly with USE_TZ disabled.
    c                    rW   )NF)r   rY   rZ   r   rc   r   r   r   r   r   r   á   r[   zViewNoTZTestCase.setUpc                    r\   r]   )rZ   r   rY   r   rc   r^   r   r   r   r   r^   æ   r_   zViewNoTZTestCase.tearDownr`   r   r   r   r   rc   Þ   rb   rc   c                       sD   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚIgnoreMissingTestCasez< Test cases for SENDGRID_EVENTS_IGNORE_MISSING enabled.
    c                    s   t t| ƒ ¡  dt_d S rX   )r   rd   r   r   ÚSENDGRID_EVENTS_IGNORE_MISSINGr   r   r   r   r   î   s   
zIgnoreMissingTestCase.setUpc                    s   t `tt| ƒ ¡  d S r]   )r   re   r   rd   r^   r   r   r   r   r^   ò   s   zIgnoreMissingTestCase.tearDownc              	   C   s¬   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt ddddd	œg¡d
d�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )z= Test callback using a non-existent, malformed UUID.
        r   r   r   r   r	   r=   r    r!   r"   r'   r(   r+   Nr   ©r   r-   r   r.   r/   r   r0   r1   r2   r3   r%   r   r4   r5   r6   r7   r8   r   r   r   Ú!test_callback_view_lenient_errorsö   s    üùz7IgnoreMissingTestCase.test_callback_view_lenient_errorsc                 C   sª   t jdi | j¤Ž}| ¡  |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ | jjdt ddddœg¡d	d
�}|  |jd¡ |  tjj ¡ d¡ |  tjj 	¡ d j
d¡ dS )z~ Test callback with missing data (no UUID).

        This is the default case if emails are not sent with our Classes
        r   r   r   r   r	   r    r!   rB   r'   r(   r+   Nr   rf   r8   r   r   r   rD     s   ýúz5IgnoreMissingTestCase.test_callback_with_missing_datac              	   C   rE   )rR   r   r   r   rS   r   r	   r    r!   r"   r'   r(   r+   Nr   rG   rI   r   r   r   rT   )  rL   z,IgnoreMissingTestCase.test_wrong_saved_state)
r   r   r   ra   r   r^   rg   rD   rT   r   r   r   r   r   rd   ë   s    rd   )Údjango.testr   r   Údjango.confr   r5   Úsendgridr   r   r   r   rV   rc   rd   r   r   r   r   Ú<module>   s    
 ?