U
    {h~                     @   sD  d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ G d	d
 d
eZG dd deeZG dd deZG dd deeZG dd deZG dd deeZG dd deZG dd deeZ G dd deZ!G dd deeZ"G dd deeZ#dS )     )mail)ValidationError)TestCaseoverride_settings)FormSubmission)make_form_page%make_form_page_with_custom_submissionmake_form_page_with_redirectmake_types_test_form_page)Page)CustomFormPageSubmissionExtendedFormField	FormFieldFormFieldWithCustomSubmissionFormPageWithCustomFormBuilderJadeFormPage)WagtailTestUtilsc                   @   sn   e Zd Zdd Zdd Zedd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 )TestFormSubmissionc                 C   s   t  | _d S Nr   	form_pageself r   o/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/contrib/forms/tests/test_models.pysetUp   s    zTestFormSubmission.setUpc                 C   sL   | j d}| |d | |d | |d | |d | |d d S )N/contact-us/-<label for="id_your_email">Your email</label>tests/form_page.htmltests/form_page_landing.htmlz%&lt;em&gt;please&lt;/em&gt; be polite<p>hello world</p>)clientgetassertContainsassertTemplateUsedassertTemplateNotUsedr   responser   r   r   test_get_form   s     z TestFormSubmission.test_get_formT)Z!WAGTAILFORMS_HELP_TEXT_ALLOW_HTMLc                 C   s   | j d}| |d d S )Nr   z<em>please</em> be polite)r!   r"   r#   r&   r   r   r   (test_get_form_without_help_text_escaping-   s    z;TestFormSubmission.test_get_form_without_help_text_escapingc                 C   s2   t jjddjdd | jd}| |d d S )NzYour message)labelzYour <em>wonderful</em> messager   zP<label for="id_your_message">Your &lt;em&gt;wonderful&lt;/em&gt; message</label>)r   objectsfilterupdater!   r"   r#   r&   r   r   r   test_label_escaping3   s    z&TestFormSubmission.test_label_escapingc                 C   s>   | j ddddd}| |d | |d | |d d S )	Nr   bobhello world 
your_emailyour_messageyour_choicesEnter a valid email address.r   r   r!   postr#   r$   r%   r&   r   r   r   test_post_invalid_form=   s    
z)TestFormSubmission.test_post_invalid_formc              	   C   s   | j dddddddd}| |d | |d | |d	 | |d
 | |d | ttjd | tjd j	d | 
dtjd j | tjd jdg | tjd jd tjjdd}| tjj|dd  d S )Nr   bob@example.comr0   r1   foobarbazr2   Thank you for your feedback.r   r   r    z$<li>your_email: bob@example.com</li>   r   The subjectYour message: hello worldto@email.comfrom@email.com/home/contact-us/Zurl_pathpageform_data__your_message)r!   r8   r#   r%   r$   assertEquallenr   outboxsubjectassertInbodyto
from_emailr   r+   r"   
assertTruer   r,   existsr   r'   r   r   r   r   test_post_valid_formH   s0    

 z'TestFormSubmission.test_post_valid_formc              	   C   sd   | j dddddddd | ttjd | dtjd	 j tj	
 }| |jd
 d d S Nr   r:   u   こんにちは、世界r1   r;   r2   r@   u&   Your message: こんにちは、世界r   r4   )r!   r8   rJ   rK   r   rL   rN   rO   r   r+   r"   	form_datar   
submissionr   r   r   test_post_unicode_charactersl   s    


z/TestFormSubmission.test_post_unicode_charactersc              	   C   s   | j dddddddd}| |d | |d | |d	 tjjd
d}tjj	|dd}| 
|d jd dddg | 
ttjd | dtjd j | dtjd j | dtjd j d S )Nr   r:   r0   onr;   r2   r?   r   r   rE   rF   rG   r   r5   r<   r=   r>   r@   )r!   r8   r#   r%   r$   r   r+   r"   r   r,   rJ   rW   rK   r   rL   rN   rO   r   r'   r   rY   r   r   r   test_post_multiple_values~   s(    

 z,TestFormSubmission.test_post_multiple_valuesc                 C   sd   | j dddi d}| |d | |d | |d | ttjd | 	d	tjd
 j
 d S )Nr   r:   r0   r2   r?   r   r   r@   zYour choices: r   r!   r8   r#   r%   r$   rJ   rK   r   rL   rN   rO   r&   r   r   r   test_post_blank_checkbox   s    
z+TestFormSubmission.test_post_blank_checkboxc              	   C   s$   |  t tdd W 5 Q R X d S )Nnot an email)from_addressZassertRaisesr   r   r   r   r   r   test_invalid_from_address   s    z,TestFormSubmission.test_invalid_from_addressc                 C   sF   | j dddi d | tj d tj }| dt| dS )z
        Ensure that a form submission can be logged / printed without error.
        Broke when converting field to JSON - see #8927
        r   r:   r0   r2   r@   N)	r!   r8   ZassertGreaterEqualr   r+   countfirstrN   strrX   r   r   r   *test_string_representation_form_submission   s    	
z=TestFormSubmission.test_string_representation_form_submissionN)__name__
__module____qualname__r   r(   r   r)   r.   r9   rU   rZ   r]   r_   rc   rg   r   r   r   r   r      s   

$r   c                   @   sT   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S )TestFormWithCustomSubmissionc                 C   s   t  | _|  | _d S r   r   r   loginuserr   r   r   r   r      s    z"TestFormWithCustomSubmission.setUpc                 C   s`   | j d}| |d | |d | |d | j|ddd | j|ddd | |d	 d S 
Nr   r   +tests/form_page_with_custom_submission.html3tests/form_page_with_custom_submission_landing.html!<div>You must log in first.</div>Thtml<p>Boring intro text</p>r    )r!   r"   r#   r$   r%   assertNotContainsr&   r   r   r   r(      s      z*TestFormWithCustomSubmission.test_get_formc                 C   sj   | j   | j d}| |d | |d | |d | j|ddd | j|ddd | |d	 d S ro   )r!   Zlogoutr"   rv   r$   r%   r#   r&   r   r   r   !test_get_form_with_anonymous_user   s    
  z>TestFormWithCustomSubmission.test_get_form_with_anonymous_userc                 C   s>   | j ddddd}| |d | |d | |d d S )	Nr   r/   r0   r1   r2   r6   rp   rq   r7   r&   r   r   r   r9      s    
 z3TestFormWithCustomSubmission.test_post_invalid_formc              	   C   s   | j dddddddd}| |d | |d | |d	 | |d
 | |d | ttjd | tjd j	d | 
dtjd j | tjd jdg | tjd jd tjjdd}| tjj|dd  d S )Nr   r:   r0   r1   r;   r2   Thank you for your patience!rp   rq   r    z!<p>User email: test@email.com</p>r@   r   rA   rB   rC   rD   rE   rF   rG   )r!   r8   r#   r%   r$   rJ   rK   r   rL   rM   rN   rO   rP   rQ   r   r+   r"   rR   r   r,   rS   rT   r   r   r   rU      s<    

   z1TestFormWithCustomSubmission.test_post_valid_formc              	   C   sD  | j dddddddd}| |d | |d | j|d	d
d | j|dd
d tjj| j	| j
d}| | d | |jdd  | j dddddddd}| |d | |d | j|d	d
d | j|dd
d | j|dd
d | j|dd
d tjj| j	| j
d}| | d | | jd d d S )Nr   r:   r0   r1   r;   r2   rp   rq   z#<p>Thank you for your patience!</p>Trs   z&<div>The form is already filled.</div>)rn   rH   r@   )rI   rr   ru   r4   )r!   r8   r%   r$   r#   rv   r   r+   r,   rn   r   rJ   rd   rR   rS   r"   rW   )r   r'   Zsubmissions_qsr   r   r   test_post_form_twice'  sv    

     

      z1TestFormWithCustomSubmission.test_post_form_twicec              	   C   sd   | j dddddddd | ttjd | dtjd	 j tj	
 }| |jd
 d d S rV   )r!   r8   rJ   rK   r   rL   rN   rO   r   r+   r"   rW   rX   r   r   r   rZ   f  s    


z9TestFormWithCustomSubmission.test_post_unicode_charactersc              	   C   s   | j dddddddd}| |d | |d | |d	 tjjd
d}tjj	|dd}| 
|d jd dddg d S )Nr   r:   r0   r[   r;   r2   rx   rp   rq   rE   rF   rG   r   r5   r<   r=   r>   )r!   r8   r#   r%   r$   r   r+   r"   r   r,   rJ   rW   r\   r   r   r   r]   x  s,    

   z6TestFormWithCustomSubmission.test_post_multiple_valuesc                 C   sd   | j dddi d}| |d | |d | |d | ttjd | 	d	tjd
 j
 d S )Nr   r:   r0   r2   rx   rp   rq   r@   zYour choices: Noner   r^   r&   r   r   r   r_     s$    
  z5TestFormWithCustomSubmission.test_post_blank_checkboxN)rh   ri   rj   r   r(   rw   r9   rU   ry   rZ   r]   r_   r   r   r   r   rk      s   (?rk   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )(TestFormSubmissionWithMultipleRecipientsc                 C   s   t dd| _d S Nzto@email.com, another@email.com
to_addressr   r   r   r   r   r     s    z.TestFormSubmissionWithMultipleRecipients.setUpc              	   C   sD   |  t tdd W 5 Q R X |  t tdd W 5 Q R X d S )Nr`   r|   zto@email.com, not an emailrb   r   r   r   r   test_invalid_to_address  s    z@TestFormSubmissionWithMultipleRecipients.test_invalid_to_addressc              	   C   s   | j dddddddd}| |d | |d | |d	 | |d
 | ttjd | tjd j	d | 
dtjd j | tjd jd | ttjd jddh tjjdd}| tjj|dd  d S )Nr   r:   r0   r1   r;   r2   r?   r   r   r    r@   r   rA   rB   rD   rC   another@email.comrE   rF   rG   )r!   r8   r#   r%   r$   rJ   rK   r   rL   rM   rN   rO   rQ   setrP   r   r+   r"   rR   r   r,   rS   rT   r   r   r   rU     s.    

 z=TestFormSubmissionWithMultipleRecipients.test_post_valid_formN)rh   ri   rj   r   r~   rU   r   r   r   r   rz     s   rz   c                   @   s   e Zd Zdd Zdd ZdS )?TestFormSubmissionWithMultipleRecipientsAndWithCustomSubmissionc                 C   s   t dd| _|  | _d S r{   rl   r   r   r   r   r     s    zETestFormSubmissionWithMultipleRecipientsAndWithCustomSubmission.setUpc              	   C   s   | j dddddddd}| |d | |d | |d	 | |d
 | ttjd | tjd j	d | 
dtjd j | tjd jd | ttjd jddh tjjdd}| tjj|dd  d S )Nr   r:   r0   r1   r;   r2   rx   rp   rq   r    r@   r   rA   rB   rD   rC   r   rE   rF   rG   )r!   r8   r#   r%   r$   rJ   rK   r   rL   rM   rN   rO   rQ   r   rP   r   r+   r"   rR   r   r,   rS   rT   r   r   r   rU     s:    

   zTTestFormSubmissionWithMultipleRecipientsAndWithCustomSubmission.test_post_valid_formNrh   ri   rj   r   rU   r   r   r   r   r     s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestFormWithRedirectc                 C   s   t dd| _d S r{   )r	   r   r   r   r   r   r     s    zTestFormWithRedirect.setUpc              	   C   s   | j dddddddd}| |d | ttjd | tjd	 jd
 | dtjd	 j	 | tjd	 j
d | ttjd	 jddh tjjdd}| tjj|dd  d S )Nr   r:   r0   r1   r;   r2   /r@   r   rA   rB   rD   rC   r   rE   rF   rG   )r!   r8   ZassertRedirectsrJ   rK   r   rL   rM   rN   rO   rQ   r   rP   r   r+   r"   rR   r   r,   rS   rT   r   r   r   rU     s(    

 z)TestFormWithRedirect.test_post_valid_formNr   r   r   r   r   r     s   r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
!TestFormPageWithCustomFormBuilderc                 C   sX   t jjdd}|jtddddddd	}tjj|d
dddd tjj|d
dddd d S )N/home/rF   zSupport Requestzsupport-requestzit@jenkins.comzsupport@jenkins.comzSupport Request SubmittedtitleZslugr}   ra   rM   instancer@   NameZ
singlelineTrH   Z
sort_orderr*   
field_typerequiredzDevice IP Address	ipaddress)r   r+   r"   	add_childr   r   create)r   	home_pager   r   r   r   r   2  s0    	z'TestFormPageWithCustomFormBuilder.setUpc                 C   s   | j d}| |d | |d | j|ddd | j|ddd | |j}|dd	d
i}| |d d | |d d | |d d d S )N/support-request/-tests/form_page_with_custom_form_builder.html5tests/form_page_with_custom_form_builder_landing.htmlz<title>Support Request</title>Trs   zG<input type="text" name="name" required maxlength="120" id="id_name" />inputnamedevice_ip_addresstypetextr   r1   idZid_device_ip_address)	r!   r"   r$   r%   r#   Zget_soupcontentfindrJ   )r   r'   Zsoupr   r   r   r   r(   L  s(      z/TestFormPageWithCustomFormBuilder.test_get_formc                 C   st   | j dddd}| |d | |d | |d | j ddd	d}| |d
 | |d | |d d S )Nr   z{very long name longer than 120 charactersvery long name longer than 120 charactersvery long name longer than 120 characters
192.0.2.30r   r   z9Ensure this value has at most 120 characters (it has 123)r   r   Ron Johnsonz3300.192.0.2.30z#Enter a valid IPv4 or IPv6 address.r7   r&   r   r   r   r9   f  sB         z8TestFormPageWithCustomFormBuilder.test_post_invalid_formc                 C   sT   | j dddd}| |d | |d | |d | |d | |d d S )Nr   r   r   r   z+Thank you for submitting a Support Request.r   r   )r!   r8   r#   r%   r$   r&   r   r   r   rU     s"    	  z6TestFormPageWithCustomFormBuilder.test_post_valid_formN)rh   ri   rj   r   r(   r9   rU   r   r   r   r   r   1  s   #r   c                   @   sp   e Zd Zdd Zdd Zdd Zeddd	d
 Zedddd Zeddddd Z	eddddd Z
dS )TestCleanedDataEmailsc                 C   s   t  | _d S r   )r
   r   r   r   r   r   r     s    zTestCleanedDataEmails.setUpc                 C   s  | j di  | ttjd | dtjd j | dtjd j | dtjd j | dtjd j | dtjd j | d	tjd j | d
tjd j | dtjd j | dtjd j | dtjd j | dtjd j | dtjd j d S )Nr   r@   Single line text: r   Multiline: Email: Number: URL: 
Checkbox: Checkboxes: Drop down: Multiple select: Radio buttons: Date: 
Datetime: r!   r8   rJ   rK   r   rL   rN   rO   r   r   r   r   test_empty_field_presence  s    z/TestCleanedDataEmails.test_empty_field_presencec                 C   sv   | j di  dddddddd	d
dddg}| ttjd tjd jd}|D ]}|d}| 	|
| qRd S )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r@   r   
)r!   r8   rJ   rK   r   rL   rO   splitpoprR   
startswith)r   Zline_beginningsZemail_linesZ	beginningZmessage_liner   r   r   test_email_field_order  s&    
z,TestCleanedDataEmails.test_email_field_orderzm/d/Y)ZSHORT_DATE_FORMATc                 C   st   | j dddi | ttjd | dtjd j | j dddi | ttjd | d	tjd j d S )
Nr   datez12/31/17r@   zDate: 12/31/2017r   z
12/31/1917   zDate: 12/31/1917r   r   r   r   r   test_date_normalization  s       z-TestCleanedDataEmails.test_date_normalizationzm/d/Y P)ZSHORT_DATETIME_FORMATc                 C   s   | j dddi | ttjd | dtjd j | j dddi | ttjd | d	tjd j | j ddd
i | ttjd | dtjd j d S )Nr   datetimez12/31/17 4:00:00r@   zDatetime: 12/31/2017 4 a.m.r   z12/31/1917 21:19r   zDatetime: 12/31/1917 9:19 p.m.1910-12-21 21:19:12   zDatetime: 12/21/1910 9:19 p.m.r   r   r   r   r   test_datetime_normalization  s0       z1TestCleanedDataEmails.test_datetime_normalizationTde)ZUSE_I18NZLANGUAGE_CODEc                 C   s<   | j dddi | ttjd | dtjd j d S )Nr   r   z
2017-12-31r@   zDate: 31.12.2017r   r   r   r   r   r   test_date_localization  s     z,TestCleanedDataEmails.test_date_localizationc                 C   s<   | j dddi | ttjd | dtjd j d S )Nr   r   r   r@   zDatetime: 21.12.1910 21:19r   r   r   r   r   r   test_datetime_localization  s     z0TestCleanedDataEmails.test_datetime_localizationN)rh   ri   rj   r   r   r   r   r   r   r   r   r   r   r   r   r     s   




r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestIssue798	test.jsonc                 C   s8   | j ddd tjjddj| _tjj| jddd d S )	N
siteeditorpasswordusernamer   rE   rF   zYour favourite numbernumberrH   r*   r   )rm   r   r+   r"   specificr   r   r   r   r   r   r   r   %  s    zTestIssue798.setUpc              	   C   sh   | j ddddddddd}| |d | tjj| jdd	  | tjj| jdd
  d S )Nr   r:   r0   r1   r;   z7.3)r3   r4   r5   Zyour_favourite_numberr   rG   )rH   Z form_data__your_favourite_number)	r!   r8   r$   rR   r   r+   r,   r   rS   r&   r   r   r   	test_post0  s*    
  zTestIssue798.test_postN)rh   ri   rj   fixturesr   r   r   r   r   r   r   "  s   r   c                   @   s   e Zd ZdgZdd ZdS )TestNonHtmlExtensionr   c                 C   s   t dd}| |jd d S )Ntest)r   ztests/form_page_landing.jade)r   rJ   Zlanding_page_template)r   r   r   r   r   test_non_html_extensionN  s
    
 z,TestNonHtmlExtension.test_non_html_extensionN)rh   ri   rj   r   r   r   r   r   r   r   K  s   r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestFormFieldCleanNameCreationr   c                 C   s$   | j ddd tjjddj| _d S )Nr   r   r   z/home/contact-us-one-more-time/rF   )rm   r   r+   r"   r   r   r   r   r   r   r   X  s    z$TestFormFieldCleanNameCreation.setUpc                 C   s&   t jj| jddd}| |jd dS )zGcreating a new field should use clean_name format (anyascii snake_case)u   Telefón-nummerr   r   Ztelefon_nummerN)r   r+   r   r   rJ   
clean_namer   fieldr   r   r   #test_form_field_clean_name_creation^  s    zBTestFormFieldCleanNameCreation.test_form_field_clean_name_creationN)rh   ri   rj   r   r   r   r   r   r   r   r   U  s   r   c                   @   s   e Zd Zdd Zdd ZdS )&TestFormFieldCleanNameCreationOverridec                 C   s.   t jjdd}|jtddddddd| _d S )	Nr   rF   zRichiesta Gelatozice-cream-requestzscoops@pro-eis.co.itzGelato in arrivor   r   )r   r+   r"   r   r   r   )r   r   r   r   r   r   k  s    z,TestFormFieldCleanNameCreationOverride.setUpc                 C   s*   t jj| jddddd}| |jd dS )z
        Creating a new field should use the overridden method
        See ExtendedFormField get_field_clean_name method
        r@   u   quanti ge·là·to?r   Tr   znumber_field--quanti_gelatoN)r   r+   r   r   rJ   r   r   r   r   r   #test_form_field_clean_name_overridey  s    zJTestFormFieldCleanNameCreationOverride.test_form_field_clean_name_overrideN)rh   ri   rj   r   r   r   r   r   r   r   j  s   r   N)$Zdjango.corer   Zdjango.core.exceptionsr   Zdjango.testr   r   Zwagtail.contrib.forms.modelsr   Z!wagtail.contrib.forms.tests.utilsr   r   r	   r
   Zwagtail.modelsr   Zwagtail.test.testapp.modelsr   r   r   r   r   r   Zwagtail.test.utilsr   r   rk   rz   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s0     0 e/
 2%m )
