a
    Dg                     @   sp  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 eFy   dZEY n0 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   s   t jjdt d| _d S N
Test imagetitlefiler   objectscreater)   imageself r8   Y/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/images/tests/tests.pysetUp2   s    zTestImageTag.setUpc                 C   s*   t d| d }t d|i}||S )N0{% load wagtailimages_tags %}{% image image_obj z%}	image_objr   TemplateContextrenderr7   r5   Zfilter_spectempcontextr8   r8   r9   render_image_tag9   s
    
zTestImageTag.render_image_tagc                 C   s6   |  | jd}| d| | d| | d| d S Nz	width-400width="400"height="300"zalt="Test image"rD   r5   assertInr7   resultr8   r8   r9   test_image_tag@   s    zTestImageTag.test_image_tagc                 C   s   |  d d}| |d d S )N	width-500 )rD   assertEqualrJ   r8   r8   r9   test_image_tag_noneH   s    z TestImageTag.test_image_tag_nonec                 C   s:   |  t | dd W d    n1 s,0    Y  d S )NZfoobarrM   )assertRaises
ValueErrorrD   r6   r8   r8   r9   test_image_tag_wrong_typeL   s    z&TestImageTag.test_image_tag_wrong_typec                 C   s*   t d| d }t d|i}||S )Nr;   z+ as test_img %}<img {{ test_img.attrs }} />r<   r=   rA   r8   r8   r9   render_image_tag_asP   s    z TestImageTag.render_image_tag_asc                 C   s6   |  | jd}| d| | d| | d| d S rE   )rT   r5   rI   rJ   r8   r8   r9   test_image_tag_attrsY   s    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<   r0   r=   )r7   r5   r0   rB   rC   r8   r8   r9   &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 TitlerF   rG   zclass="photo"zalt="Alternate"ztitle="my wonderful title")rV   r5   rI   rJ   r8   r8   r9   $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 %})Zimage_primaryZimage_alternater=   )r7   r5   rB   rC   r8   r8   r9   render_image_tag_with_filtersu   s
    z*TestImageTag.render_image_tag_with_filtersc                 C   s(   |  | j}| d| | d| d S )NrF   rG   )rX   r5   rI   rJ   r8   r8   r9   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"rH   rJ   r8   r8   r9   #test_image_tag_with_chained_filters   s    z0TestImageTag.test_image_tag_with_chained_filtersc                 C   sx   |  tj | | jd W d    n1 s00    Y  |  tj | | jd W d    n1 sj0    Y  d S )Nfill-200x200|height-150zfill-800x600 alt"test")rQ   r   TemplateSyntaxErrorrD   r5   r6   r8   r8   r9   ,test_filter_specs_must_match_allowed_pattern   s    ,z9TestImageTag.test_filter_specs_must_match_allowed_patternc                 C   sT   |  tj4 td}td| ji}|| W d    n1 sF0    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<   rQ   r   r\   r>   r?   r5   r@   r7   rB   rC   r8   r8   r9   *test_context_may_only_contain_one_argument   s    z7TestImageTag.test_context_may_only_contain_one_argumentc                 C   sT   |  tj4 td}td| ji}|| W d    n1 sF0    Y  d S )Nz2{% load wagtailimages_tags %}{% image image_obj %}r<   r^   r_   r8   r8   r9   test_no_image_filter_provided   s    z*TestImageTag.test_no_image_filter_providedc                 C   sT   |  tj4 td}td| ji}|| W d    n1 sF0    Y  d S )Nz9{% load wagtailimages_tags %}{% image image_obj as foo %}r<   r^   r_   r8   r8   r9   +test_no_image_filter_provided_when_using_as   s    z8TestImageTag.test_no_image_filter_provided_when_using_asc                 C   sT   |  tj4 td}td| ji}|| W d    n1 sF0    Y  d S )NzE{% load wagtailimages_tags %}{% image image_obj class="cover-image"%}r<   r^   r_   r8   r8   r9   5test_no_image_filter_provided_but_attributes_provided   s    zBTestImageTag.test_no_image_filter_provided_but_attributes_providedc                 C   s*   t d| d }t d|i}||S )NzA{% load wagtailimages_tags %}{% image_url image_obj "width-400" "z" %}r<   r=   )r7   r5   Z	view_namerB   rC   r8   r8   r9   render_image_url_tag   s    z!TestImageTag.render_image_url_tagc                 C   s4   |  | jd}| |d| jjjdd  d S )Nwagtailimages_servez/images/.*/width-400/{}/rd   r5   assertRegexformatr1   namesplitrJ   r8   r8   r9   test_image_url   s
    zTestImageTag.test_image_urlc                 C   s4   |  | jd}| |d| jjjdd  d S )NZwagtailimages_serve_custom_viewz'/testimages/custom_view/.*/width-400/{}rf   rg   rh   rJ   r8   r8   r9   test_image_url_custom_view   s    z'TestImageTag.test_image_url_custom_viewc                 C   sR   |  t4 td}td| ji}|| W d    n1 sD0    Y  d S )Nza{% load wagtailimages_tags %}{% image_url image_obj "width-400" "mynonexistingimageserve_view" %}r<   )rQ   r   r   r>   r?   r5   r@   r_   r8   r8   r9   'test_image_url_no_imageserve_view_added   s    z4TestImageTag.test_image_url_no_imageserve_view_addedN)__name__
__module____qualname__r:   rD   rL   rP   rS   rT   rU   rV   rW   rX   rY   rZ   r]   r`   ra   rb   rc   rd   rm   rn   ro   r8   r8   r8   r9   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   s    | 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getZassertContainsr7   responser8   r8   r9   !test_image_tag_with_missing_image   s    z2TestMissingImage.test_image_tag_with_missing_imagec                 C   s    | j d}| j|ddd d S )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?">Trt   rv   ry   r8   r8   r9   !test_rich_text_with_missing_image   s    z2TestMissingImage.test_rich_text_with_missing_imageN)rp   rq   rr   __doc__Zfixturesr{   r|   r8   r8   r8   r9   rs      s   
rs   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.   r/   )r   rj   r   r3   r4   r)   r5   r6   r8   r8   r9   r:      s
    zTestFormat.setUpc                 C   s.   | j | jd}| |ddd| jjd d S )Ntest alt textr5   r   )zdata-altzdata-embedtypezdata-formatzdata-id)rj   Zeditor_attributesr5   rO   pkrJ   r8   r8   r9   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Zallow_extra_attrs)rj   image_to_editor_htmlr5   assertTagInHTMLr   rJ   r8   r8   r9   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   )rj   r   r5   r   r   )r7   rK   Zexpected_htmlr8   r8   r9   &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   )rj   	classnameimage_to_htmlr5   r   rJ   r8   r8   r9    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   )rj   r   r5   r   rJ   r8   r8   r9   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   rj   r   rO   rJ   r8   r8   r9   test_get_image_format6  s    
z TestFormat.test_get_image_formatc                 C   sB   |  t | jj}W d    n1 s(0    Y  | |d d S )Nr   )ZassertWarnsr'   rj   Z
classnamesrO   )r7   r   r8   r8   r9   *test_deprecated_classnames_property_access;  s    &z5TestFormat.test_deprecated_classnames_property_accessN)rp   rq   rr   r:   r   r   r   r   r   r   r   r8   r8   r8   r9   r~      s   	
	r~   c                   @   s,   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=)rO   r    r6   r8   r8   r9   test_signature_generationB  s    
z1TestSignatureGeneration.test_signature_generationc                 C   s   |  tddd d S )Nr   r   r   )
assertTruer!   r6   r8   r8   r9   test_signature_verificationG  s    
z3TestSignatureGeneration.test_signature_verificationc                 C   s   |  tddd d S )Nr      r   ZassertFalser!   r6   r8   r8   r9   "test_signature_changes_on_image_idL  s    
z:TestSignatureGeneration.test_signature_changes_on_image_idc                 C   s   |  tddd d S )Nr   r   zfill-800x700r   r6   r8   r8   r9   %test_signature_changes_on_filter_specQ  s    
z=TestSignatureGeneration.test_signature_changes_on_filter_specN)rp   rq   rr   r   r   r   r   r8   r8   r8   r9   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   s   t jjdt d| _d S r-   r2   r6   r8   r8   r9   r:   X  s    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   re   argsr   Content-Type	image/pngContent-Security-Policydefault-src 'none'X-Content-Type-Optionsnosniff    Nr    r5   idrw   rx   r   rO   status_coder   	streamingwillowr   openjoinstreaming_contentassertIsInstancer   r7   	signaturerz   r5   r8   r8   r9   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 SVGr/   r   re   r   r   r   zimage/svg+xmlr   r   r   r   r   )r   r3   r4   r+   r    r   rw   rx   r   rO   r   r   r   r   r   r   r   r   r   r   r7   r5   r   rz   r8   r8   r9   test_get_svgw  s    z"TestFrontendServeView.test_get_svgZavif)Z 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 AVIFr/   r   re   r   r   r   z
image/avifr   )r   r3   r4   r*   r    r   rw   rx   r   rO   r   r   r   r   r   r   r   r   r   r   r8   r8   r9   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   re   r   test.pngr   r   r   r   Nr   r   r8   r8   r9   test_get_with_extra_component  s    z3TestFrontendServeView.test_get_with_extra_componentc                 C   sB   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   re   r   ztest/test.pngi  Nr    r5   r   rw   rx   r   rO   r   r7   r   rz   r8   r8   r9   '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   r8   r8   r9   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   Z#wagtailimages_serve_action_redirectr   z>/media/images/{filename[0]}.2e16d0ba.fill-800x600{filename[1]}filenamei.  F)r   Zfetch_redirect_response)r    r5   r   rw   rx   r   rj   ospathsplitextbasenamer1   ZassertRedirects)r7   r   rz   Zexpected_redirect_urlr8   r8   r9   test_get_with_redirect_action  s"    z3TestFrontendServeView.test_get_with_redirect_actionc                 C   s:   |  t tjdd W d    n1 s,0    Y  d S )Nunknown)action)rQ   r   r"   Zas_viewr6   r8   r8   r9   *test_init_with_unknown_action_raises_error  s    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   Zcustom)keywagtailimages_serve_custom_keyr   r   r   r   N)r    r5   r   rw   rx   r   rO   r   r   r   r   r   r   r   r   r   r8   r8   r9   test_get_with_custom_key  s    	z.TestFrontendServeView.test_get_with_custom_keyc                 C   sB   t | jjd}| jtd|| jjdfdd }| |jd dS )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   r8   r8   r9   *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   re   r   r   Nr   r   r8   r8   r9   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-specre   r     Nr   r   r8   r8   r9   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   re   r   i  N)r   remover5   r1   r   r    r   rw   rx   r   rO   r   r   r8   r8   r9   "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    r5   r   rw   rx   r   rO   r   r8   r8   r9   test_get_cache_control]  s    z,TestFrontendServeView.test_get_cache_controlN)rp   rq   rr   r:   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   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   s   t jjdt d| _d S r-   r2   r6   r8   r8   r9   r:   i  s    zTestFrontendSendfileView.setUpzsendfile.backends.development)Z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   Zwagtailimages_sendfiler   r   r   r   r   r   r   r   r   r   r8   r8   r9   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   Zwagtailimages_sendfile_dummyr   r   zDummy backend response)msgr   r   r   r   )
r    r5   r   rw   rx   r   rO   r   r   contentr   r8   r8   r9   test_sendfile_dummy_backend  s    z4TestFrontendSendfileView.test_sendfile_dummy_backendN)
rp   rq   rr   r:   r   unittestZskipIfsendfile_modr   r   r8   r8   r8   r9   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   rO   lefttoprightZbottomr7   Zrectr8   r8   r9   	test_init  s
    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   
            )rO   r   assertNotEqualr6   r8   r8   r9   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   rO   rQ   
IndexError__getitem__r   r8   r8   r9   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   rO   as_tupler   r8   r8   r9   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   rO   widthheightr   r8   r8   r9   	test_size  s
    zTestRect.test_sizec                 C   s$   t dddd}d|_| |d d S )Nr   r   r   r   )r   r   2   r   r     )r   r   rO   r   r8   r8   r9   test_set_size_with_tuple  s    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   rO   r   r8   r8   r9   test_set_size_with_vector  s    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   rO   xyZ
centroid_xZ
centroid_yr   r8   r8   r9   test_centroid  s    zTestRect.test_centroidc                 C   s$   t dddd}d|_| |d d S )Nr   r   r   r   )  r   r   r   i&  iX  )r   r   rO   r   r8   r8   r9   test_set_centroid_with_tuple  s    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   rO   r   r8   r8   r9   test_set_centroid_with_vector  s    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   rO   reprr   r8   r8   r9   	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   Z
from_pointrO   r   r8   r8   r9   test_from_point  s    zTestRect.test_from_pointN)rp   rq   rr   r   r   r   r   r   r   r   r   r   r   r  r  r8   r8   r8   r9   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   s&   t t}| t|j g d d S )N)	r0   r1   description
collectiontagsfocal_point_xfocal_point_yfocal_point_widthfocal_point_height)r   r   rO   listbase_fieldskeysr7   formr8   r8   r9   test_fields  s
    zTestGetImageForm.test_fieldsc                 C   s&   t t}| t|j g d d S )N)r0   r1   r  r	  r
  r  r  r  r  captionZfancy_caption)r   r#   rO   r  r  r  r  r8   r8   r9    test_admin_form_fields_attribute  s
    z1TestGetImageForm.test_admin_form_fields_attributec                 C   s4   t t}| |jd t | |jd jtj d S )Nr1   )r   WagtailImager   r  r   widgetr   Z	FileInputr  r8   r8   r9   test_file_field  s    z TestGetImageForm.test_file_fieldc                 C   s2   t t}| |jd t | |jd jt d S )Nr
  )r   r  r   r  r   r  r   r  r8   r8   r9   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  r  r  )r   r  r   r  r   ZIntegerFieldr  ZHiddenInputr  r8   r8   r9   test_focal_point_fields	  s&    z(TestGetImageForm.test_focal_point_fieldsN)rp   rq   rr   r  r  r  r  r  r8   r8   r8   r9   r    s
   r  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestRenditionFilenamesc                 C   s4   t jjdtddd}|d}| |jjd d S )Nr.   ztest_rf1.pngr   r/   z	width-100zimages/test_rf1.width-100.pngr   r3   r4   r)   get_renditionrO   r1   rk   r7   r5   Z	renditionr8   r8   r9   test_normal_filter&  s    
z)TestRenditionFilenames.test_normal_filterc                 C   s4   t jjdtddd}|d}| |jjd d S )Nr.   ztest_rf2.pngr   r/   fill-100x100z)images/test_rf2.2e16d0ba.fill-100x100.pngr  r  r8   r8   r9   test_fill_filter/  s    
z'TestRenditionFilenames.test_fill_filterc                 C   sP   t jjdtddd}|tdddd |  |d}| |j	j
d d S )	Nr.   ztest_rf3.pngr   r/   r   r   r!  z)images/test_rf3.15ee4958.fill-100x100.pngr   r3   r4   r)   Zset_focal_pointr   saver  rO   r1   rk   r  r8   r8   r9   !test_fill_filter_with_focal_point:  s    
z8TestRenditionFilenames.test_fill_filter_with_focal_pointc                 C   sP   t jjdtddd}|tdddd |  |d}| |j	j
d d S )	Nr.   ztest_rf4.pngr   r/   r   r   r[   z4images/test_rf4.15ee4958.fill-200x200.height-150.pngr#  r  r8   r8   r9   !test_filter_with_pipe_gets_dottedH  s    
z8TestRenditionFilenames.test_filter_with_pipe_gets_dottedN)rp   rq   rr   r   r"  r%  r&  r8   r8   r8   r9   r  !  s   	r  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)r0   r  r1   z
Test ImageZblack)Zcolour)r$   r3   r4   r)   r   r1   url)r7   r5   Zsecond_imager8   r8   r9   test_upload_pathX  s    z$TestDifferentUpload.test_upload_pathN)rp   rq   rr   r)  r8   r8   r8   r9   r'  W  s   r'  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#   r6   r8   r8   r9   test_custom_get_image_modelk  s    z-TestGetImageModel.test_custom_get_image_modelc                 C   s   |  t d dS )z5Test get_image_model_string with a custom image modelr+  N)rO   r   r6   r8   r8   r9   "test_custom_get_image_model_stringp  s    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   r,  wagtail.images.modelsr   r-  r   )r7   r   r8   r8   r9   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   r,  rO   r   r6   r8   r8   r9   $test_standard_get_image_model_string}  s    z6TestGetImageModel.test_standard_get_image_model_string)signalZreceiverztests.UnknownModelc                 C   s4   |  t t  W d   n1 s&0    Y  dS )z*Test get_image_model with an unknown modelNrQ   r   r   r6   r8   r8   r9   test_unknown_get_image_model  s    z.TestGetImageModel.test_unknown_get_image_modelzinvalid-stringc                 C   s4   |  t t  W d   n1 s&0    Y  dS )z1Test get_image_model with an invalid model stringNr4  r6   r8   r8   r9   test_invalid_get_image_model  s    z.TestGetImageModel.test_invalid_get_image_modelN)rp   rq   rr   r   r.  r/  r1  r2  r&   r   r   r5  r6  r8   r8   r8   r9   r*  j  s$   



r*  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)   r1   r5   	getbuffernbytes
image_size)clsr8   r8   r9   setUpTestData  s    z#TestWagtailImageField.setUpTestDatac                 C   sT   t  }| jd t| jd| jd| jd }||}| |jt | 	|j
d d S )Nr   rN   r   r   r5   seekr   r   r:  	to_pythonr   WillowImageFilerO   content_typer7   fr1   r?  r8   r8   r9    test_to_python_with_inmemoryfile  s    
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   rN   z
image/jpegr   r=  rB  r8   r8   r9   ,test_to_python_gets_content_type_from_willow  s    
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    n1 sx0    Y  d S )Ntest_temp.pngr   r   )r   r	   r:  r5   r>  writereadr?  r   r@  rO   rA  )r7   rC  tmp_filer?  r8   r8   r9   "test_to_python_with_temporary_file  s    


z8TestWagtailImageField.test_to_python_with_temporary_filec              	   C   s   d}t  }tdddd F}| t| || W d    n1 sF0    Y  W d    n1 sd0    Y  | td || j W d    n1 s0    Y  d S )NzYUpload a valid image. The file you uploaded was either not an image or a corrupted image.rF  r       z;No file was submitted. Check the encoding type on the form.)r   r	   ZassertRaisesMessager   r?  r5   )r7   r   rC  rI  r8   r8   r9   3test_to_python_raises_error_with_invalid_image_file  s    FzITestWagtailImageField.test_to_python_raises_error_with_invalid_image_fileN)	rp   rq   rr   classmethodr<  rD  rE  rJ  rL  r8   r8   r8   r9   r7    s   


r7  )Sr   r   ior   r   Zdjangor   r   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.core.files.uploadedfiler   r	   Zdjango.testr
   r   Zdjango.test.signalsr   Zdjango.urlsr   Ztaggit.formsr   r   Zwillow.imager   r   r   r   r@  Zwagtail.imagesr   r   Zwagtail.images.fieldsr   Zwagtail.images.formatsr   r   r   Zwagtail.images.formsr   r0  r   r  Zwagtail.images.permissionsr   Zwagtail.images.rectr   r   Zwagtail.images.utilsr    r!   Zwagtail.images.views.server"   Zwagtail.test.testapp.modelsr#   r$   Zwagtail.test.utilsr%   r&   Zwagtail.utils.deprecationr'   utilsr)   r*   r+   sendfiler   ImportErrorr,   rs   r~   r   r   r   r   r  r  r'  r*  r7  r8   r8   r8   r9   <module>   s\   
 'L  'GK6,