o
    h                     @   sn  d dl Z d dlZd dlmZ d dlZd dlmZmZ d dlm	Z	 d dl
mZmZ d dlmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZ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( d dl)m*Z* d dl+m,Z- d dl.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z: d dl;m<Z<m=Z= d dl>m?Z? ddl@m,Z,mAZAmBZBmCZC zd dlDZDdZEW n eFy   dZEY nw G dd deZGG dd  d eZHG d!d" d"e<eZIG d#d$ d$eZJG d%d& d&eZKG d'd( d(eZLG d)d* d*eZMG d+d, d,e<eZNG d-d. d.eZOG d/d0 d0eZPG d1d2 d2e<eZQG d3d4 d4eZRdS )5    N)BytesIO)formstemplate)settings)ImproperlyConfiguredValidationError)InMemoryUploadedFileTemporaryUploadedFile)TestCaseoverride_settings)setting_changed)reverse)TagField	TagWidget)AvifImageFilePNGImageFileSvgImageFile)	ImageFile)get_image_modelget_image_model_string)WagtailImageField)Formatget_image_formatregister_image_format)get_image_formImage)update_permission_policy)RectVector)generate_signatureverify_signature)	ServeView)CustomImageCustomImageFilePath)WagtailTestUtilsdisconnect_signal_receiver)RemovedInWagtail70Warning   )r   get_test_image_fileget_test_image_file_avifget_test_image_file_svgTFc                   @   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+S ),TestImageTagc                 C      t jjdt d| _d S N
Test imagetitlefiler   objectscreater)   imageself r9   Z/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/images/tests/tests.pysetUp2      zTestImageTag.setUpc                 C   *   t d| d }t d|i}||S )N0{% load wagtailimages_tags %}{% image image_obj z%}	image_objr   TemplateContextrenderr8   r6   filter_spectempcontextr9   r9   r:   render_image_tag9   s
   

zTestImageTag.render_image_tagc                 C   6   |  | jd}| d| | d| | d| d S Nz	width-400width="400"height="300"zalt="Test image"rH   r6   assertInr8   resultr9   r9   r:   test_image_tag@      zTestImageTag.test_image_tagc                 C   s   |  d d}| |d d S )N	width-500 )rH   assertEqualrO   r9   r9   r:   test_image_tag_noneH   s   z TestImageTag.test_image_tag_nonec                 C   s<   |  t | dd W d    d S 1 sw   Y  d S )NfoobarrS   )assertRaises
ValueErrorrH   r7   r9   r9   r:   test_image_tag_wrong_typeL      "z&TestImageTag.test_image_tag_wrong_typec                 C   r=   )Nr>   z+ as test_img %}<img {{ test_img.attrs }} />r?   r@   rD   r9   r9   r:   render_image_tag_asP      
z TestImageTag.render_image_tag_asc                 C   rI   rJ   )r\   r6   rN   rO   r9   r9   r:   test_image_tag_attrsY   rR   z!TestImageTag.test_image_tag_attrsc                 C   s$   t d}t ||d}||S )Nzx{% load wagtailimages_tags %}{% image image_obj width-400             class="photo" title=title|lower alt="Alternate" %})r?   r1   r@   )r8   r6   r1   rF   rG   r9   r9   r:   &render_image_tag_with_extra_attributesa   s
   
z3TestImageTag.render_image_tag_with_extra_attributesc                 C   sN   |  | jd}| d| | d| | d| | d| | d| d S )NzMy Wonderful TitlerK   rL   zclass="photo"zalt="Alternate"ztitle="my wonderful title")r_   r6   rN   rO   r9   r9   r:   $test_image_tag_with_extra_attributesi   s   z1TestImageTag.test_image_tag_with_extra_attributesc                 C   s$   t d}t d |d}||S )NzX{% load wagtailimages_tags %}{% image image_primary|default:image_alternate width-400 %})image_primaryimage_alternater@   )r8   r6   rF   rG   r9   r9   r:   render_image_tag_with_filtersu   s
   
z*TestImageTag.render_image_tag_with_filtersc                 C   s(   |  | j}| d| | d| d S )NrK   rL   )rc   r6   rN   rO   r9   r9   r:   test_image_tag_with_filters|   s   z(TestImageTag.test_image_tag_with_filtersc                 C   s*   |  | jd}| d| | d| d S )Nzfill-200x200 height-150zwidth="150"zheight="150"rM   rO   r9   r9   r:   #test_image_tag_with_chained_filters      z0TestImageTag.test_image_tag_with_chained_filtersc                 C   sz   |  tj | | jd W d    n1 sw   Y  |  tj | | jd W d    d S 1 s6w   Y  d S )Nfill-200x200|height-150zfill-800x600 alt"test")rX   r   TemplateSyntaxErrorrH   r6   r7   r9   r9   r:   ,test_filter_specs_must_match_allowed_pattern   s   "z9TestImageTag.test_filter_specs_must_match_allowed_patternc                 C   V   |  tj td}td| ji}|| W d    d S 1 s$w   Y  d S )Nz{% load wagtailimages_tags %}{% image image_obj fill-200x200 as test_img this_one_should_not_be_there %}<img {{ test_img.attrs }} />r?   rX   r   rh   rA   rB   r6   rC   r8   rF   rG   r9   r9   r:   *test_context_may_only_contain_one_argument   s   "z7TestImageTag.test_context_may_only_contain_one_argumentc                 C   rj   )Nz2{% load wagtailimages_tags %}{% image image_obj %}r?   rk   rl   r9   r9   r:   test_no_image_filter_provided      "z*TestImageTag.test_no_image_filter_providedc                 C   rj   )Nz9{% load wagtailimages_tags %}{% image image_obj as foo %}r?   rk   rl   r9   r9   r:   +test_no_image_filter_provided_when_using_as   ro   z8TestImageTag.test_no_image_filter_provided_when_using_asc                 C   rj   )NzE{% load wagtailimages_tags %}{% image image_obj class="cover-image"%}r?   rk   rl   r9   r9   r:   5test_no_image_filter_provided_but_attributes_provided   ro   zBTestImageTag.test_no_image_filter_provided_but_attributes_providedc                 C   r=   )NzA{% load wagtailimages_tags %}{% image_url image_obj "width-400" "z" %}r?   r@   )r8   r6   	view_namerF   rG   r9   r9   r:   render_image_url_tag   r]   z!TestImageTag.render_image_url_tagc                 C   4   |  | jd}| |d| jjjdd  d S )Nwagtailimages_servez/images/.*/width-400/{}/rs   r6   assertRegexformatr2   namesplitrO   r9   r9   r:   test_image_url   s
   zTestImageTag.test_image_urlc                 C   rt   )Nwagtailimages_serve_custom_viewz'/testimages/custom_view/.*/width-400/{}rv   rw   rx   rO   r9   r9   r:   test_image_url_custom_view   s   z'TestImageTag.test_image_url_custom_viewc                 C   sT   |  t td}td| ji}|| W d    d S 1 s#w   Y  d S )Nza{% load wagtailimages_tags %}{% image_url image_obj "width-400" "mynonexistingimageserve_view" %}r?   )rX   r   r   rA   rB   r6   rC   rl   r9   r9   r:   'test_image_url_no_imageserve_view_added   s   "z4TestImageTag.test_image_url_no_imageserve_view_addedN)__name__
__module____qualname__r;   rH   rQ   rV   rZ   r\   r^   r_   r`   rc   rd   re   ri   rm   rn   rp   rq   rs   r}   r   r   r9   r9   r9   r:   r,   1   s,    						r,   c                   @   s&   e Zd ZdZdgZdd Zdd ZdS )TestMissingImagea-  
    Missing image files in media/original_images should be handled gracefully, to cope with
    pulling live databases to a development instance without copying the corresponding image files.
    In this case, it's acceptable to render broken images, but not to fail rendering the page outright.
    z	test.jsonc                 C       | j d}| j|ddd d S )Nz/events/christmas/zf<img src="/media/not-found" width="0" height="0" alt="A missing image"             class="feed-image">ThtmlclientgetassertContainsr8   responser9   r9   r:   !test_image_tag_with_missing_image      
z2TestMissingImage.test_image_tag_with_missing_imagec                 C   r   )Nz/events/final-event/z|<img class="richtext-image full-width" src="/media/not-found"             width="0" height="0" alt="where did my image go?">Tr   r   r   r9   r9   r:   !test_rich_text_with_missing_image   r   z2TestMissingImage.test_rich_text_with_missing_imageN)r   r   r   __doc__fixturesr   r   r9   r9   r9   r:   r      s
    
r   c                   @   sL   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S )
TestFormatc                 C   s(   t dddd| _tjjdt d| _d S )N	test namez
test labeltest is-primaryoriginalr/   r0   )r   rz   r   r4   r5   r)   r6   r7   r9   r9   r:   r;      s
   zTestFormat.setUpc                 C   s.   | j | jd}| |ddd| jjd d S )Ntest alt textr6   r   )zdata-altzdata-embedtypezdata-formatzdata-id)rz   editor_attributesr6   rU   pkrO   r9   r9   r:   test_editor_attributes   s   z!TestFormat.test_editor_attributesc                 C   s,   | j | jd}| jd| jj |dd d S )Nr   z<img data-embedtype="image" data-id="%d" data-format="test name" data-alt="test alt text" class="test is-primary" width="640" height="480" alt="test alt text" >Tallow_extra_attrs)rz   image_to_editor_htmlr6   assertTagInHTMLr   rO   r9   r9   r:   test_image_to_editor_html  s   
z$TestFormat.test_image_to_editor_htmlc                 C   s0   | j | jd}d| jj }| j||dd d S )NArthur "two sheds" Jacksonz<img data-embedtype="image" data-id="%d" data-format="test name" data-alt="Arthur &quot;two sheds&quot; Jackson" class="test is-primary" width="640" height="480" alt="Arthur &quot;two sheds&quot; Jackson" >Tr   )rz   r   r6   r   r   )r8   rP   expected_htmlr9   r9   r:   &test_image_to_editor_html_with_quoting  s   z1TestFormat.test_image_to_editor_html_with_quotingc                 C   s4   d | j _| j | jd}| jd|dd d| j _d S )Nr   z2<img width="640" height="480" alt="test alt text">Tr   r   )rz   	classnameimage_to_htmlr6   r   rO   r9   r9   r:    test_image_to_html_no_classnames!  s   
z+TestFormat.test_image_to_html_no_classnamesc                 C   s$   | j | jd}| jd|dd d S )Nr   za<img class="test is-primary" width="640" height="480" alt="Arthur &quot;two sheds&quot; Jackson">Tr   )rz   r   r6   r   rO   r9   r9   r:   test_image_to_html_with_quoting-  s   
z*TestFormat.test_image_to_html_with_quotingc                 C   s$   t | j td}| || j d S )Nr   )r   rz   r   rU   rO   r9   r9   r:   test_get_image_format6  s   
z TestFormat.test_get_image_formatc                 C   sB   |  t | jj}W d    n1 sw   Y  | |d d S )Nr   )assertWarnsr'   rz   
classnamesrU   )r8   r   r9   r9   r:   *test_deprecated_classnames_property_access;  s   
z5TestFormat.test_deprecated_classnames_property_accessN)r   r   r   r;   r   r   r   r   r   r   r   r9   r9   r9   r:   r      s    	
	r   c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSignatureGenerationc                 C   s   |  tddd d S )Nd   fill-800x600xnZOzQyUg6pkfciqcfRJRosOrGg=)rU   r    r7   r9   r9   r:   test_signature_generationB     
z1TestSignatureGeneration.test_signature_generationc                 C      |  tddd d S )Nr   r   r   )
assertTruer!   r7   r9   r9   r:   test_signature_verificationG  r   z3TestSignatureGeneration.test_signature_verificationc                 C   r   )Nr      r   assertFalser!   r7   r9   r9   r:   "test_signature_changes_on_image_idL  r   z:TestSignatureGeneration.test_signature_changes_on_image_idc                 C   r   )Nr   r   zfill-800x700r   r7   r9   r9   r:   %test_signature_changes_on_filter_specQ  r   z=TestSignatureGeneration.test_signature_changes_on_filter_specN)r   r   r   r   r   r   r   r9   r9   r9   r:   r   A  s
    r   c                   @   s   e Zd Zdd Zdd Zdd Zeddi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d Zdd Zdd  Zd!S )"TestFrontendServeViewc                 C   r-   r.   r3   r7   r9   r9   r:   r;   X  r<   zTestFrontendServeView.setUpc                 C   s   t | jjd}| jtd|| jjdfd}| |jd | |j	 | |d d | |d d | |d	 d
 t
jd|j}| |t dS )z6
        Test a valid GET request to the view
        r   ru   argsr   Content-Type	image/pngContent-Security-Policydefault-src 'none'X-Content-Type-Optionsnosniff    Nr    r6   idr   r   r   rU   status_coder   	streamingwillowr   openjoinstreaming_contentassertIsInstancer   r8   	signaturer   r6   r9   r9   r:   test_get_  s   zTestFrontendServeView.test_getc                 C   s   t jjdt d}t|jd}| jtd||jdfd}| 	|j
d | |j | 	|d d | 	|d	 d
 | 	|d d tj td|j}| |t d S )NzTest SVGr0   r   ru   r   r   r   zimage/svg+xmlr   r   r   r   r   )r   r4   r5   r+   r    r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r8   r6   r   r   r9   r9   r:   test_get_svgw  s   z"TestFrontendServeView.test_get_svgavif) WAGTAILIMAGES_FORMAT_CONVERSIONSc                 C   s   t jjdt d}t|jd}| jtd||jdfd}| 	|j
d | |j | 	|d d tj d	|j}| |t d S )
Nz	Test AVIFr0   r   ru   r   r   r   z
image/avifr   )r   r4   r5   r*   r    r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r9   r9   r:   test_get_avif  s   z#TestFrontendServeView.test_get_avifc                 C   s~   t | jjd}| jtd|| jjdfdd }| |jd | |j	 | |d d t
jd|j}| |t d	S )
zU
        Test that a filename can be optionally added to the end of the URL.
        r   ru   r   test.pngr   r   r   r   Nr   r   r9   r9   r:   test_get_with_extra_component  s   z3TestFrontendServeView.test_get_with_extra_componentc                 C   B   t | jjd}| jtd|| jjdfdd }| |jd dS )za
        A filename can be appended to the end of the URL, but it must not contain a '/'
        r   ru   r   ztest/test.pngi  Nr    r6   r   r   r   r   rU   r   r8   r   r   r9   r9   r:   'test_get_with_too_many_extra_components  s   z=TestFrontendServeView.test_get_with_too_many_extra_componentsc                 C   s   t | jjd}| jtd|| jjdfd}| |jd | |j	 | |d d | |d d | |d	 d
 t
jd|j}| |t d S )Nr    wagtailimages_serve_action_server   r   r   r   r   r   r   r   r   r   r   r9   r9   r:   test_get_with_serve_action  s   z0TestFrontendServeView.test_get_with_serve_actionc                 C   sd   t | jjd}| jtd|| jjdfd}djtj	tj
| jjjd}| j||ddd d S )	Nr   #wagtailimages_serve_action_redirectr   z>/media/images/{filename[0]}.2e16d0ba.fill-800x600{filename[1]}filenamei.  F)r   fetch_redirect_response)r    r6   r   r   r   r   rz   ospathsplitextbasenamer2   assertRedirects)r8   r   r   expected_redirect_urlr9   r9   r:   test_get_with_redirect_action  s"   
z3TestFrontendServeView.test_get_with_redirect_actionc                 C   s<   |  t tjdd W d    d S 1 sw   Y  d S )Nunknown)action)rX   r   r"   as_viewr7   r9   r9   r:   *test_init_with_unknown_action_raises_error  r[   z@TestFrontendServeView.test_init_with_unknown_action_raises_errorc                 C   sf   t | jjddd}| jtd|| jjdfdd }| |jd tj	
d|j}| |t d	S )
z>
        Test that the key can be changed on the view
        r   custom)keywagtailimages_serve_custom_keyr   r   r   r   N)r    r6   r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r9   r9   r:   test_get_with_custom_key  s   	z.TestFrontendServeView.test_get_with_custom_keyc                 C   r   )z
        Test that the key can be changed on the view

        This tests that the default key no longer works when the key is changed on the view
        r   r   r   r     Nr   r   r9   r9   r:   *test_get_with_custom_key_using_default_key  s   	z@TestFrontendServeView.test_get_with_custom_key_using_default_keyc                 C   sB   t | jjd d}| jtd|| jjdfd}| |jd dS )zG
        Test that an invalid signature returns a 403 response
        r(   r   ru   r   r   Nr   r   r9   r9   r:   test_get_invalid_signature   s   z0TestFrontendServeView.test_get_invalid_signaturec                 C   s>   t | jjd}| jtd|| jjdfd}| |jd dS )a3  
        Test that an invalid filter spec returns a 400 response

        This is very unlikely to happen in reality. A user would have
        to create signature for the invalid filter spec which can't be
        done with Wagtails built in URL generator. We should test it
        anyway though.
        zbad-filter-specru   r     Nr   r   r9   r9   r:   test_get_invalid_filter_spec1  s   
z2TestFrontendServeView.test_get_invalid_filter_specc                 C   sN   t | jjj t| jjd}| jt	d|| jjdfd}| 
|jd dS )z
        Test that a missing image file gives a 410 response

        When the source image file is missing, it is presumed deleted so we
        return a 410 "Gone" response.
        r   ru   r   i  N)r   remover6   r2   r   r    r   r   r   r   rU   r   r   r9   r9   r:   "test_get_missing_source_image_fileH  s   z8TestFrontendServeView.test_get_missing_source_image_filec                 C   s@   t | jjd}| jtd|| jjdfd}| |d d d S )Nr   r   r   zCache-Controlzmax-age=3600, public)r    r6   r   r   r   r   rU   r   r9   r9   r:   test_get_cache_control]  s   z,TestFrontendServeView.test_get_cache_controlN)r   r   r   r;   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   r9   r9   r:   r   W  s"    
r   c                   @   sF   e Zd Zdd Zeddee ddd Zedddd	 Z	d
S )TestFrontendSendfileViewc                 C   r-   r.   r3   r7   r9   r9   r:   r;   i  s   zTestFrontendSendfileView.setUpzsendfile.backends.development)SENDFILE_BACKENDzMissing django-sendfile app.c                 C   sn   t | jjd}| jtd|| jjdfd}| |jd | |d d | |d d | |d	 d
 d S )Nr   wagtailimages_sendfiler   r   r   r   r   r   r   r   r   r   r9   r9   r:   test_sendfile_nobackendo  s   z0TestFrontendSendfileView.test_sendfile_nobackendc                 C   sn   t | jjd}| jtd|| jjdfd}| |jd | j|j	dd | |d d | |d	 d
 d S )Nr   wagtailimages_sendfile_dummyr   r   zDummy backend response)msgr   r   r   r   )
r    r6   r   r   r   r   rU   r   r   contentr   r9   r9   r:   test_sendfile_dummy_backend  s   z4TestFrontendSendfileView.test_sendfile_dummy_backendN)
r   r   r   r;   r   unittestskipIfsendfile_modr   r   r9   r9   r9   r:   r   h  s    r   c                   @   sl   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S )TestRectc                 C   sJ   t dddd}| |jd | |jd | |jd | |jd d S )Nr      r      )r   rU   lefttoprightbottomr8   rectr9   r9   r:   	test_init  
   zTestRect.test_initc                 C   sD   |  tddddtdddd | tddddtdddd d S )	Nr   r  r   r  
            )rU   r   assertNotEqualr7   r9   r9   r:   test_equality  s    $zTestRect.test_equalityc                 C   sb   t dddd}| |d d | |d d | |d d | |d d | t|jd	 d S )
Nr   r  r   r  r   r(            )r   rU   rX   
IndexError__getitem__r
  r9   r9   r:   test_getitem  s   zTestRect.test_getitemc                 C   s"   t dddd}| | d d S )Nr   r  r   r  )r   r  r   r  )r   rU   as_tupler
  r9   r9   r:   test_as_tuple  s   zTestRect.test_as_tuplec                 C   sJ   t dddd}| |jt | |jd | |jd | |jd d S )Nr   r  r   ^  )r   r   )r   r   sizer   rU   widthheightr
  r9   r9   r:   	test_size  r  zTestRect.test_sizec                 C   $   t dddd}d|_| |d d S )Nr   r  r   r  )r   r   2   r#  r    )r   r  rU   r
  r9   r9   r:   test_set_size_with_tuple     z!TestRect.test_set_size_with_tuplec                 C   s*   t dddd}tdd|_| |d d S )Nr   r  r   r  r   r"  )r   r   r  rU   r
  r9   r9   r:   test_set_size_with_vector  rf   z"TestRect.test_set_size_with_vectorc                 C   sf   t dddd}| |jt | |jd | |jd | |jd | |jd | |jd d S )Nr   r  r   r  )r  r  r  )	r   r   centroidr   rU   xy
centroid_x
centroid_yr
  r9   r9   r:   test_centroid  s   zTestRect.test_centroidc                 C   r!  )Nr   r  r   r  )  r.  r$  r   i&  iX  )r   r(  rU   r
  r9   r9   r:   test_set_centroid_with_tuple  r&  z%TestRect.test_set_centroid_with_tuplec                 C   s*   t dddd}tdd|_| |d d S )Nr   r  r   r  r.  r/  )r   r   r(  rU   r
  r9   r9   r:   test_set_centroid_with_vector  rf   z&TestRect.test_set_centroid_with_vectorc                 C   s"   t dddd}| t|d d S )Nr   r  r   r  z2Rect(left: 100, top: 150, right: 200, bottom: 250))r   rU   reprr
  r9   r9   r:   	test_repr  s   zTestRect.test_reprc                 C   s*   t dddd}| |t dddd d S )	Nr   r   r#  r  K      }      )r   
from_pointrU   r
  r9   r9   r:   test_from_point  s   zTestRect.test_from_pointN)r   r   r   r  r  r  r  r   r%  r'  r-  r0  r1  r3  r9  r9   r9   r9   r:   r    s    	r  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestGetImageFormc                 C   &   t t}| t|j g d d S )N)	r1   r2   description
collectiontagsfocal_point_xfocal_point_yfocal_point_widthfocal_point_height)r   r   rU   listbase_fieldskeysr8   formr9   r9   r:   test_fields  
   zTestGetImageForm.test_fieldsc                 C   r;  )N)r1   r2   r<  r=  r>  r?  r@  rA  rB  captionfancy_caption)r   r#   rU   rC  rD  rE  rF  r9   r9   r:    test_admin_form_fields_attribute  rI  z1TestGetImageForm.test_admin_form_fields_attributec                 C   s4   t t}| |jd t | |jd jtj d S )Nr2   )r   WagtailImager   rD  r   widgetr   	FileInputrF  r9   r9   r:   test_file_field  s   z TestGetImageForm.test_file_fieldc                 C   s2   t t}| |jd t | |jd jt d S )Nr>  )r   rM  r   rD  r   rN  r   rF  r9   r9   r:   test_tags_field  s   z TestGetImageForm.test_tags_fieldc                 C   s   t t}| |jd tj | |jd tj | |jd tj | |jd tj | |jd jtj | |jd jtj | |jd jtj | |jd jtj d S )Nr?  r@  rA  rB  )r   rM  r   rD  r   IntegerFieldrN  HiddenInputrF  r9   r9   r:   test_focal_point_fields	  s&   z(TestGetImageForm.test_focal_point_fieldsN)r   r   r   rH  rL  rP  rQ  rT  r9   r9   r9   r:   r:    s    r:  c                   @   r   )
TestRenditionFilenamesc                 C   4   t jjdtddd}|d}| |jjd d S )Nr/   ztest_rf1.pngr   r0   z	width-100zimages/test_rf1.width-100.pngr   r4   r5   r)   get_renditionrU   r2   r{   r8   r6   	renditionr9   r9   r:   test_normal_filter&  s   
z)TestRenditionFilenames.test_normal_filterc                 C   rV  )Nr/   ztest_rf2.pngr   r0   fill-100x100z)images/test_rf2.2e16d0ba.fill-100x100.pngrW  rY  r9   r9   r:   test_fill_filter/  s   
z'TestRenditionFilenames.test_fill_filterc                 C   P   t jjdtddd}|tdddd |  |d}| |j	j
d d S )	Nr/   ztest_rf3.pngr   r0   r   r   r\  z)images/test_rf3.15ee4958.fill-100x100.pngr   r4   r5   r)   set_focal_pointr   saverX  rU   r2   r{   rY  r9   r9   r:   !test_fill_filter_with_focal_point:     
z8TestRenditionFilenames.test_fill_filter_with_focal_pointc                 C   r^  )	Nr/   ztest_rf4.pngr   r0   r   r   rg   z4images/test_rf4.15ee4958.fill-200x200.height-150.pngr_  rY  r9   r9   r:   !test_filter_with_pipe_gets_dottedH  rc  z8TestRenditionFilenames.test_filter_with_pipe_gets_dottedN)r   r   r   r[  r]  rb  rd  r9   r9   r9   r:   rU  !  s
    	rU  c                   @   s   e Zd Zdd ZdS )TestDifferentUploadc                 C   sD   t jjddt d}t jjddtddd}| |jj|jj d S )Nr/   zA test description)r1   r<  r2   z
Test Imageblack)colour)r$   r4   r5   r)   r  r2   url)r8   r6   second_imager9   r9   r:   test_upload_pathX  s   z$TestDifferentUpload.test_upload_pathN)r   r   r   rj  r9   r9   r9   r:   re  W  s    re  c                   @   s   e Zd Zedddd Zedddd Ze dd Ze d	d
 Zee	e
dedddd Zee	e
dedddd ZdS )TestGetImageModeltests.CustomImage)WAGTAILIMAGES_IMAGE_MODELc                 C   s   |  t t dS )z.Test get_image_model with a custom image modelN)assertIsr   r#   r7   r9   r9   r:   test_custom_get_image_modelk     z-TestGetImageModel.test_custom_get_image_modelc                 C   s   |  t d dS )z5Test get_image_model_string with a custom image modelrl  N)rU   r   r7   r9   r9   r:   "test_custom_get_image_model_stringp  rp  z4TestGetImageModel.test_custom_get_image_model_stringc                 C   s"   t `ddlm} | t | dS )z6Test get_image_model with no WAGTAILIMAGES_IMAGE_MODELr   r   N)r   rm  wagtail.images.modelsr   rn  r   )r8   r   r9   r9   r:   test_standard_get_image_modelu  s   z/TestGetImageModel.test_standard_get_image_modelc                 C   s   t `| t d dS )z=Test get_image_model_STRING with no WAGTAILIMAGES_IMAGE_MODELzwagtailimages.ImageN)r   rm  rU   r   r7   r9   r9   r:   $test_standard_get_image_model_string}  s   z6TestGetImageModel.test_standard_get_image_model_string)signalreceiverztests.UnknownModelc                 C   6   |  t t  W d   dS 1 sw   Y  dS )z*Test get_image_model with an unknown modelNrX   r   r   r7   r9   r9   r:   test_unknown_get_image_model     "z.TestGetImageModel.test_unknown_get_image_modelzinvalid-stringc                 C   rw  )z1Test get_image_model with an invalid model stringNrx  r7   r9   r9   r:   test_invalid_get_image_model  rz  z.TestGetImageModel.test_invalid_get_image_modelN)r   r   r   r   ro  rq  rs  rt  r&   r   r   ry  r{  r9   r9   r9   r:   rk  j  s&    



rk  c                   @   s8   e Zd Zedd Zdd Zdd Zdd Zd	d
 ZdS )TestWagtailImageFieldc                 C   s(   d| _ t| j dj| _| j j| _d S )Nzwagtailimagefield.pngr   )r   r)   r2   r6   	getbuffernbytes
image_size)clsr9   r9   r:   setUpTestData  s   z#TestWagtailImageField.setUpTestDatac                 C   sT   t  }| jd t| jd| jd| jd }||}| |jt | 	|j
d d S )Nr   rT   r   r   r6   seekr   r   r  	to_pythonr   WillowImageFilerU   content_typer8   fr2   r  r9   r9   r:    test_to_python_with_inmemoryfile     
z6TestWagtailImageField.test_to_python_with_inmemoryfilec                 C   sT   t  }| jd t| jd| jd| jd }||}| |jt | 	|j
d d S )Nr   rT   z
image/jpegr   r  r  r9   r9   r:   ,test_to_python_gets_content_type_from_willow  r  zBTestWagtailImageField.test_to_python_gets_content_type_from_willowc                 C   s   t  }tdd| jd /}| jd || j  |d ||}| |jt	 | 
|jd W d    d S 1 s=w   Y  d S )Ntest_temp.pngr   r   )r   r	   r  r6   r  writereadr  r   r  rU   r  )r8   r  tmp_filer  r9   r9   r:   "test_to_python_with_temporary_file  s   


"z8TestWagtailImageField.test_to_python_with_temporary_filec              	   C   s   d}t  }tdddd #}| t| || W d    n1 s#w   Y  W d    n1 s2w   Y  | td || j W d    d S 1 sOw   Y  d S )NzYUpload a valid image. The file you uploaded was either not an image or a corrupted image.r  r       z;No file was submitted. Check the encoding type on the form.)r   r	   assertRaisesMessager   r  r6   )r8   r   r  r  r9   r9   r:   3test_to_python_raises_error_with_invalid_image_file  s   "zITestWagtailImageField.test_to_python_raises_error_with_invalid_image_fileN)	r   r   r   classmethodr  r  r  r  r  r9   r9   r9   r:   r|    s    


r|  )Sr   r   ior   r   djangor   r   django.confr   django.core.exceptionsr   r   django.core.files.uploadedfiler   r	   django.testr
   r   django.test.signalsr   django.urlsr   taggit.formsr   r   willow.imager   r   r   r   r  wagtail.imagesr   r   wagtail.images.fieldsr   wagtail.images.formatsr   r   r   wagtail.images.formsr   rr  r   rM  wagtail.images.permissionsr   wagtail.images.rectr   r   wagtail.images.utilsr    r!   wagtail.images.views.server"   wagtail.test.testapp.modelsr#   r$   wagtail.test.utilsr%   r&   wagtail.utils.deprecationr'   utilsr)   r*   r+   sendfiler  ImportErrorr,   r   r   r   r   r   r  r:  rU  re  rk  r|  r9   r9   r9   r:   <module>   s`     'L  'GK6,