U
    {h                     @   sh  d dl Z d dl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mZ d dlmZ d dl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 d dlmZm Z m!Z!m"Z"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-m.Z.m/Z/ d dl0m1Z1 ddl2mZm3Z3m4Z4m5Z5 G dd deZ6G dd deZ7G dd deZ8G dd de1eZ9G dd deZ:G dd deZ;G d d! d!eZ<ed"d#d$iid%G d&d' d'eZ=ed"d#d$iid%G d(d) d)eZ>G d*d+ d+eZ?G d,d- d-eZ@G d.d/ d/eZAG d0d1 d1eZBed2gd3G d4d5 d5e1eZCG d6d7 d7eZDG d8d9 d9eZEG d:d; d;eZFdS )<    Nsettings)Group
Permission)caches)File)Storagedefault_storagestorages)SimpleUploadedFile)Prefetch)IntegrityError)SimpleTestCaseTestCaseTransactionTestCaseoverride_settings)reverse)Image)FilterPicture	RenditionResponsiveImageSourceImageIOErrorget_rendition_storage)Rect)
CollectionGroupCollectionPermissionPageReferenceIndex)	EventPageEventPageCarouselItemReimportedImageModel)WagtailTestUtils   )r   get_test_image_fileget_test_image_file_svgget_test_image_filenamec                   @   s   e Zd ZdS )CustomStorageN)__name__
__module____qualname__ r+   r+   h/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/images/tests/test_models.pyr'   (   s   r'   c                   @   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eejdddiiddd Zdd Zdd Zdd Zdd  Zd!S )"	TestImagec                 C   s   t jjdtddd| _d S )N
Test imagewhitecolourtitlefiler   objectscreater$   imageselfr+   r+   r,   setUp-   s    zTestImage.setUpc                 C   s   |  tt d S N)assertEqualr!   r   r9   r+   r+   r,   #test_get_image_model_at_import_time4   s    z-TestImage.test_get_image_model_at_import_timec                 C   s   |  | j  d S r<   )assertFalser8   Zis_portraitr9   r+   r+   r,   test_is_portrait7   s    zTestImage.test_is_portraitc                 C   s   |  | j  d S r<   )
assertTruer8   Zis_landscaper9   r+   r+   r,   test_is_landscape:   s    zTestImage.test_is_landscapec                 C   s    |  | j tdddd d S )Nr       )r=   r8   Zget_rectr   r9   r+   r+   r,   test_get_rect=   s    zTestImage.test_get_rectc                 C   sP   |  | j  d| j_d| j_d| j_d| j_| | j tdddd d S )	Nd      2      K      }      )	assertIsNoner8   Zget_focal_pointfocal_point_xfocal_point_yfocal_point_widthfocal_point_heightr=   r   r9   r+   r+   r,   test_get_focal_point@   s    zTestImage.test_get_focal_pointc                 C   sD   |  | j  d| j_d| j_d| j_d| j_| | j  d S )NrF   rG   rH   rI   )r?   r8   Zhas_focal_pointrO   rP   rQ   rR   rA   r9   r+   r+   r,   test_has_focal_pointL   s    zTestImage.test_has_focal_pointc                 C   s   |  | jj |  | jj |  | jj |  | jj | jtdddd | | jjd | | jjd | | jjd | | jjd | jd  |  | jj |  | jj |  | jj |  | jj d S )NrF      rG   i^     )	rN   r8   rO   rP   rQ   rR   Zset_focal_pointr   r=   r9   r+   r+   r,   test_set_focal_pointW   s    zTestImage.test_set_focal_pointc                 C   s   |  | j  d S r<   )rA   r8   is_stored_locallyr9   r+   r+   r,   test_is_stored_locallyk   s    z TestImage.test_is_stored_locallydefaultBACKENDz8wagtail.test.dummy_external_storage.DummyExternalStorage)STORAGESc                 C   s   |  | j  d S r<   )r?   r8   rX   r9   r+   r+   r,   ,test_is_stored_locally_with_external_storagen   s    	z6TestImage.test_is_stored_locally_with_external_storagec                 C   s&   | j  }| |t | |d d S Nr   )r8   get_file_sizeassertIsInstanceintZassertGreater)r:   	file_sizer+   r+   r,   test_get_file_sizey   s    
zTestImage.test_get_file_sizec              	   C   s4   | j jjdd | t | j   W 5 Q R X d S )NF)save)r8   r4   deleteassertRaisesr   r_   r9   r+   r+   r,   <test_get_file_size_on_missing_file_raises_sourceimageioerror~   s    zFTestImage.test_get_file_size_on_missing_file_raises_sourceimageioerrorc                 C   s   |  | j d d S )NZ(4dd0211870e130b7e1690d2ec53c499a54a48fef)r=   r8   Zget_file_hashr9   r+   r+   r,   test_file_hash   s     zTestImage.test_file_hashc                 C   s$   t jjdt d}| |  dS )zw
        Feature detection should not be run on SVGs.

        https://github.com/wagtail/wagtail/issues/11172
        zTest SVGr2   N)r   r6   r7   r%   rN   Zget_suggested_focal_pointr:   r8   r+   r+   r,   "test_get_suggested_focal_point_svg   s
    z,TestImage.test_get_suggested_focal_point_svgN)r(   r)   r*   r;   r>   r@   rB   rE   rS   rT   rW   rY   r   r   r\   r]   rc   rg   rh   rj   r+   r+   r+   r,   r-   ,   s,    
r-   c                   @   s2   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
S )TestImageQuerySetztest_empty.jsonc                 C   s4   t jjdt d}t jd}| t||g d S )Nr.   r2   Test)r   r6   r7   r$   searchr=   listr:   r8   resultsr+   r+   r,   test_search_method   s    z$TestImageQuerySet.test_search_methodc                 C   sz   t jjdt d}t jjdt d}t jjddd}| t||g t jjddd}t|dd	 d
}| |||g d S )NAAA Test imager2   ZZZ Test imagezaaa testand)operatororc                 S   s   | j S r<   r3   )imgr+   r+   r,   <lambda>       z2TestImageQuerySet.test_operators.<locals>.<lambda>)key)r   r6   r7   r$   rm   r=   rn   sorted)r:   	aaa_image	zzz_imagerp   Zsorted_resultsr+   r+   r,   test_operators   s    z TestImageQuerySet.test_operatorsc                 C   s|   t jjdt d}t jjdt d}t jdjddd}| t|||g t jdjddd}| t|||g d S )	Nrr   r2   rs   r3   rl   F)Zorder_by_relevancez-title)r   r6   r7   r$   Zorder_byrm   r=   rn   )r:   r}   r~   rp   r+   r+   r,   test_custom_ordering   s$      z&TestImageQuerySet.test_custom_orderingc              	   C   sr   t ddD ]*}tjjd| t d}|jddd q
| d( d	d
 t D }| 	d|d  W 5 Q R X d S )Nr   
   zTest image %dr2   ZaardvarkZ	artichokeZ	armadillo   c                 S   s$   i | ]}|j d d |j D qS )c                 S   s   g | ]
}|j qS r+   name).0tagr+   r+   r,   
<listcomp>   s     zUTestImageQuerySet.test_search_indexing_prefetches_tags.<locals>.<dictcomp>.<listcomp>)r3   tagsallr   r8   r+   r+   r,   
<dictcomp>   s    zJTestImageQuerySet.test_search_indexing_prefetches_tags.<locals>.<dictcomp>zTest image 0)
ranger   r6   r7   r$   r   addassertNumQueriesZget_indexed_objectsZassertIn)r:   ir8   rp   r+   r+   r,   $test_search_indexing_prefetches_tags   s    z6TestImageQuerySet.test_search_indexing_prefetches_tagsN)r(   r)   r*   fixturesrq   r   r   r   r+   r+   r+   r,   rk      s
   rk   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestImagePermissionsc                 C   s   | j dddd| _| j dddd| _| j dddd| _| jjtjjd	d
 | j	dddd| _
tjjdd
}tjj|t tjjddd | jj| tjjd| jt d| _d S )Nuserzuser@email.compassword)usernameemailr   ownerzowner@email.comeditorzeditor@email.comZEditorsr   administratorzadministrator@email.comzImage adders	add_image)codename)groupZ
collectionZ
permissionr.   )r3   Zuploaded_by_userr4   )Zcreate_userr   r   r   groupsr   r   r6   getZcreate_superuserr   r7   r   r   Zget_first_root_noder   r   r$   r8   )r:   Zimage_adders_groupr+   r+   r,   r;      sB          zTestImagePermissions.setUpc                 C   s   |  | j| j d S r<   )rA   r8   is_editable_by_userr   r9   r+   r+   r,   test_administrator_can_edit   s    z0TestImagePermissions.test_administrator_can_editc                 C   s   |  | j| j d S r<   )rA   r8   r   r   r9   r+   r+   r,   test_editor_can_edit   s    z)TestImagePermissions.test_editor_can_editc                 C   s   |  | j| j d S r<   )rA   r8   r   r   r9   r+   r+   r,   test_owner_can_edit   s    z(TestImagePermissions.test_owner_can_editc                 C   s   |  | j| j d S r<   )r?   r8   r   r   r9   r+   r+   r,   test_user_cant_edit  s    z(TestImagePermissions.test_user_cant_editN)r(   r)   r*   r;   r   r   r   r   r+   r+   r+   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 )TestFiltersc                 C   s   |  tddg d S )N	width-100r=   r   Zexpand_specr9   r+   r+   r,   test_expand_spec_single  s    z#TestFilters.test_expand_spec_singlec                 C   s   |  tddg d S )Nzwidth-100 jpegquality-20width-100|jpegquality-20r   r9   r+   r+   r,   test_expand_spec_flat  s     z!TestFilters.test_expand_spec_flatc                 C   s   |  tddg d S )Nr   r   r9   r+   r+   r,   test_expand_spec_pipe  s     z!TestFilters.test_expand_spec_pipec                 C   s   |  tddgdg d S )Nr   zjpegquality-20r   r   r9   r+   r+   r,   test_expand_spec_list  s    z!TestFilters.test_expand_spec_listc                 C   s   |  tdddg d S )Nzwidth-{100,200}r   z	width-200r   r9   r+   r+   r,   test_expand_spec_braced  s     z#TestFilters.test_expand_spec_bracedc                 C   s   |  tdddg d S )Nzwidth-{100,200} jpegquality-40width-100|jpegquality-40width-200|jpegquality-40r   r9   r+   r+   r,   test_expand_spec_mixed   s    z"TestFilters.test_expand_spec_mixedc                 C   s   |  tdddg d S )Nzwidth-{100,200}|jpegquality-40r   r   r   r9   r+   r+   r,   test_expand_spec_mixed_pipe&  s    z'TestFilters.test_expand_spec_mixed_pipec                 C   s   |  tdddddg d S )Nz-width-{100,200} jpegquality-{40,80} grayscalez"width-100|jpegquality-40|grayscalez"width-100|jpegquality-80|grayscalez"width-200|jpegquality-40|grayscalez"width-200|jpegquality-80|grayscaler   r9   r+   r+   r,    test_expand_spec_multiple_braces,  s    z,TestFilters.test_expand_spec_multiple_bracesN)r(   r)   r*   r   r   r   r   r   r   r   r   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 )TestResponsiveImagec                 C   s&   t jjdt d| _| jd| _d S Nr.   r2   width-10r   r6   r7   r$   r8   get_renditionrendition_10r9   r+   r+   r,   r;   9  s
    zTestResponsiveImage.setUpc                 C   s(   t i }| |jg  | |jd  d S r<   )r   r=   
renditionsattrsr:   rx   r+   r+   r,   test_construct_emptyA  s    z(TestResponsiveImage.test_construct_emptyc                 C   s(   d| j i}t|}| |j| j g d S )Na)r   r   r=   r   r:   r   rx   r+   r+   r,   test_construct_with_renditionsF  s    
z2TestResponsiveImage.test_construct_with_renditionsc                 C   s>   |  ti  |  ti ddi d| ji}| t| d S )Nsizes100vwr   )r?   r   r   rA   r:   r   r+   r+   r,   test_evaluate_valueK  s    
z'TestResponsiveImage.test_evaluate_valuec                 C   sp   d| j i}t|}t|}td| jdi}t|ddi}| || | |d | || | || d S )Nr   zwidth-15r   r   i90  )r   r   r8   r   assertNotEqualr=   )r:   r   Zvalue1Zvalue2Zvalue3Zvalue4r+   r+   r,   test_compare_valueR  s    
z&TestResponsiveImage.test_compare_valuec                 C   s^   | j | jdd}t| jdt| jdg}| tt| |d  d|d  d d S )Nwidth-90r   r   r   r    10w, r#   z 90w)	r   r8   r   r&   r=   r   get_width_srcsetrn   values)r:   r   	filenamesr+   r+   r,   test_get_width_srcset]  s    


z)TestResponsiveImage.test_get_width_srcsetc                 C   s0   d| j i}| tt| t| jd d S Nr   )r   r=   r   r   rn   r   r&   r8   r   r+   r+   r,   &test_get_width_srcset_single_renditionk  s
    

z:TestResponsiveImage.test_get_width_srcset_single_renditionc              
   C   sh   | j | jdd}t|}t| jdt| jdg}| | d|d  d|d  d|d  d	 d S )
Nr   r   r   zT
                <img
                    alt="Test image"
                    src="r   z"
                    srcset="r   r#   zb 90w"
                    width="10"
                    height="7"
                >
            )r   r8   r   r   r&   assertHTMLEqual__html__r:   r   rx   r   r+   r+   r,   test_renderr  s"    


zTestResponsiveImage.test_renderc                 C   s(   t d| ji}| | | j  d S r   )r   r   r   r   img_tagr   r+   r+   r,   (test_render_single_image_same_as_img_tag  s    z<TestResponsiveImage.test_render_single_image_same_as_img_tagN)r(   r)   r*   r;   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r   8  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 )TestPicturec                 C   s&   t jjdt d| _| jd| _d S r   r   r9   r+   r+   r,   r;     s
    zTestPicture.setUpc                 C   s6   | j | j d}t|}| |j| j g| j gd d S )N)format-jpegformat-webpjpegwebpr   r   r=   formatsr   r+   r+   r,   test_formats  s     zTestPicture.test_formatsc                 C   s$   d| j i}t|}| |ji  d S )Nr   r   r   r+   r+   r,   test_single_format  s    
zTestPicture.test_single_formatc                 C   s>   | j | j | j d}t|}| |j| j g| j | j gd d S )N)r   r   zformat-webp-losslessr   r   r   r+   r+   r,   test_mixed_format  s    
zTestPicture.test_mixed_formatc           	      C   s   d| j i}d| j i}d| j i}d| j i}d| j i}||||||||||||||d}| D ]\}}| t| | qdd S )Nzformat-avifr   r   z
format-pngz
format-gif)gifpngr   r   )r   itemsr=   r   Zget_fallback_format)	r:   Zavifr   r   r   r   Z	fallbacksfmtr   r+   r+   r,   test_fallback_format  s    





z TestPicture.test_fallback_formatc                 C   s   | j d| j d| j d| j dd}t|ddi}t| j dt| j d	t| j d
t| j dg}| | d|d  d|d  d|d  d|d  d|d  d d S )Nformat-jpeg|width-10format-jpeg|width-90format-webp|width-10format-webp|width-90)r   r   r   r   r   r   zformat-jpeg.width-10zformat-jpeg.width-90zformat-webp.width-10zformat-webp.width-90z?
                <picture>
                    <source srcset="r   r      z 90w" sizes="100vw" type="image/webp">
                    <img
                        alt="Test image"
                        sizes="100vw"
                        src="r   z""
                        srcset="r#   z 90w"
                        width="10"
                        height="7"
                    >
                </picture>
            )r8   r   r   r&   r   r   r   r+   r+   r,   test_render_multi_format_sizes  s2    







z*TestPicture.test_render_multi_format_sizesc                 C   s0   t d| ji}| | d| j  d d S )Nr   z	<picture>z
</picture>)r   r   r   r   r   r   r+   r+   r,   r     s
     z4TestPicture.test_render_single_image_same_as_img_tagN)
r(   r)   r*   r;   r   r   r   r   r   r   r+   r+   r+   r,   r     s   
r   rZ   r[   +django.core.cache.backends.dummy.DummyCacheZCACHESc                   @   sd  e Zd 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dJeeedd d!Zd"d# Zed$d%d&iid'd(d) Zd*d+ Zd,d- Zd.d/ Zed$d%d&iid'd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Ze d:d; Z ed<d=d>d? Z!ee" d=d@dA Z#ed%dBid%dCid%d<idDdEdFdGdH Z$dIS )KTestRenditions)	height-66r   	width-400c                 C   s   t jjdt d| _d S Nr.   r2   r5   r9   r+   r+   r,   r;     s    zTestRenditions.setUpc                 C   s   |  t t d S r<   )assertIsr   Zget_rendition_modelr   r9   r+   r+   r,   test_get_rendition_model  s    z'TestRenditions.test_get_rendition_modelc                 C   s:   | j d}| |jd | |jd | |jd d S )Nr   i  i,  )r8   r   r=   widthheightfilter_specr:   	renditionr+   r+   r,   test_minification  s    z TestRenditions.test_minificationc                 C   s,   | j d}| |jd | |jd d S )Nmax-100x100rF   rJ   r8   r   r=   r   r   r   r+   r+   r,   test_resize_to_max  s    z!TestRenditions.test_resize_to_maxc                 C   s,   | j d}| |jd | |jd d S )Nzmin-120x120   x   r   r   r+   r+   r,   test_resize_to_min  s    z!TestRenditions.test_resize_to_minc                 C   s,   | j d}| |jd | |jd d S )NoriginalrC   rD   r   r   r+   r+   r,   test_resize_to_original  s    z&TestRenditions.test_resize_to_originalc                 C   s(   | j d}| j d}| || d S Nr   )r8   r   r=   r:   Zfirst_renditionsecond_renditionr+   r+   r,   
test_cache  s    zTestRenditions.test_cachec                 C   s,   | j d}| j td}| || d S r   )r8   r   r   r=   r  r+   r+   r,   test_get_with_filter_instance"  s    z,TestRenditions.test_get_with_filter_instancec              	   C   sj   |  d | jd}W 5 Q R X tjdj| jjd}|  d |d}W 5 Q R X | || d S )N   r   r   pkr   )	r   r8   r   r   r6   prefetch_relatedr   r  r=   r:   original_renditionr8   r  r+   r+   r,   test_prefetched_rendition_found*  s    z.TestRenditions.test_prefetched_rendition_foundc              	   C   sj   |  d | jd}W 5 Q R X tjdj| jjd}|  d |d}W 5 Q R X | || d S )Nr  r   r  r   )	r   r8   r   r   r6   prefetch_renditionsr   r  r=   r	  r+   r+   r,   test_prefetch_renditions_found9  s    z-TestRenditions.test_prefetch_renditions_foundc              	   C   s   |  d | jd}W 5 Q R X tjdj| jjd}|  d |d}W 5 Q R X | || |  d |d}W 5 Q R X | 	|| d S )Nr  r   r   r     r   r   )
r   r8   r   r   r6   r  r   r  r   r   r:   r
  r8   r  Zthird_renditionr+   r+   r,   #test_prefetched_rendition_not_foundE  s    z2TestRenditions.test_prefetched_rendition_not_foundc              	   C   s   |  d | jd}W 5 Q R X tjdj| jjd}|  d |d}W 5 Q R X | || |  d |d}W 5 Q R X | 	|| d S )Nr  r   r  r  r   r   )
r   r8   r   r   r6   r  r   r  r   r   r  r+   r+   r,   "test_prefetch_renditions_not_founda  s    z1TestRenditions.test_prefetch_renditions_not_foundc                 C   sD   t | jd }t | jtd }| |d |d  d S )Nr   r   )rn   r8   get_renditionsr   r   r=   )r:   firstsecondr+   r+   r,   (test_get_renditions_with_filter_instancer  s    z7TestRenditions.test_get_renditions_with_filter_instancec                 C   s:   | j d dddg}t| j j|  }| || d S )Nzwidth-40zwidth-30zwidth-50)r8   r   rn   r  keysr=   )r:   specsZrenditions_keysr+   r+   r,   test_get_renditions_key_orderz  s    
z,TestRenditions.test_get_renditions_key_orderF)db_queries_expectedprefetch_restrictedprefetch_allc              	   C   s`   t j }|r|d}n|r*|j| j }|j| jjd}| 	| |j
| j  W 5 Q R X d S )Nr   id)r   r6   r   r  r  SPECSr   r8   r  r   r  )r:   r  r  r  querysetr8   r+   r+   r,    _test_get_renditions_performance  s    
z/TestRenditions._test_get_renditions_performancec                 C   s4   |  d |  d | j ddd | j ddd d S )Nr   r#   r   Tr  )r  r   r9   r+   r+   r,   ?test_get_renditions_performance_with_rendition_caching_disabled  s    

zNTestRenditions.test_get_renditions_performance_with_rendition_caching_disabledr   r[   z-django.core.cache.backends.locmem.LocMemCacher   c                 C   s&   |  d |  d | j ddd d S )Nr   r   Tr!  r"  r9   r+   r+   r,   >test_get_renditions_performance_with_rendition_caching_enabled  s    

zMTestRenditions.test_get_renditions_performance_with_rendition_caching_enabledc              	   C   sn  dd | j D }| d | jj| }W 5 Q R X | D ]\}}| |j|j q:| dd | D dd |D  | d | jj| }W 5 Q R X | D ]\}}| |j|j q| dd | D d	d |D  | d | jj| }W 5 Q R X | || | jj	j
| j d
 d  | d | jj| }W 5 Q R X | || | | |  d S )Nc                 S   s   g | ]}t |qS r+   )r   )r   specr+   r+   r,   r     s     z9TestRenditions.test_create_renditions.<locals>.<listcomp>r   c                 S   s   h | ]
}|j qS r+   r%  r   filterr+   r+   r,   	<setcomp>  s     z8TestRenditions.test_create_renditions.<locals>.<setcomp>c                 S   s   h | ]
}|j qS r+   r&  r'  r+   r+   r,   r)    s     r#   c                 S   s   h | ]
}|j qS r+   r&  r'  r+   r+   r,   r)    s     c                 S   s   h | ]
}|j qS r+   r&  r'  r+   r+   r,   r)    s     r   )r   )r  r   r8   Zcreate_renditionsr   r=   r%  r   r  r   r(  re   r   )r:   Zfilter_listresultr(  r   Zsecond_resultZthird_resultr+   r+   r,   test_create_renditions  s2    z%TestRenditions.test_create_renditionsc                 C   s   | j d}| |jd d S )Nr   r.   )r8   r   r=   Zaltr   r+   r+   r,   test_alt_attribute  s    z!TestRenditions.test_alt_attributec                 C   s:   | j d}|j}|jjdd }| |d|  d S )Nr   /r#   zhttp://testserver/media/images/)r8   r   full_urlr4   r   splitr=   )r:   Zren_imgr.  Zimg_namer+   r+   r,   test_full_url  s    zTestRenditions.test_full_urlc              	   C   sB  t j}| jd}| }| ||| d|_||| | 	d | jd}W 5 Q R X | |jd t
jdj| jjd}| 	d |d}W 5 Q R X | t|d tdd| j_| j  | jj   | jd}| t|d td	d| j_| j  | jj   | jd}| t|d d S )
Nz	width-500r   r   r   r  _markgreenr0   r/   )r   cache_backendr8   r   Zget_cache_keyr=   r   r1  setr   r   r6   r  r  r?   hasattrr$   r4   rd   r   r   re   )r:   cacher   Zrendition_cache_keyZnew_renditionZfresh_imageZprefetched_renditionr+   r+   r,   test_renditions_cache  s.    

z$TestRenditions.test_renditions_cachec              	   C   s:   t ddiddidd | tjtd  W 5 Q R X d S )Nr[   r   )rZ   r   r   r   r   r=   r   r3  r   r9   r+   r+   r,   $test_prefers_rendition_cache_backend'  s      
z3TestRenditions.test_prefers_rendition_cache_backendc              	   C   s4   t dddiid | tjtd  W 5 Q R X d S )NrZ   r[   r   r   r8  r9   r+   r+   r,   0test_uses_default_cache_when_no_renditions_cache4  s     z?TestRenditions.test_uses_default_cache_when_no_renditions_cachec                 C   s   d| j _d| j _d| j _d| j _| j   | j d}| |j	 t
dddd	 | |jjjd | |jjjd | |jjd
 | |jjd | |jd d S )NrF   rG   rH   rI   	width-320%   _   ?   i      r   zbackground-position: 15% 41%;)r8   rO   rP   rQ   rR   rd   r   r=   Zfocal_pointroundr   Zcentroidxyr   r   background_position_styler   r+   r+   r,   test_focal_point>  s    
 zTestRenditions.test_focal_pointc                 C   s   | j d}| |jd d S )Nr;  zbackground-position: 50% 50%;)r8   r   r=   rD  r   r+   r+   r,   &test_background_position_style_defaultR  s
     z5TestRenditions.test_background_position_style_defaultc                 C   s&   t `| tt d | t t d S )NWAGTAILIMAGES_RENDITION_STORAGE)r   rG  r?   r5  r=   r   r	   r9   r+   r+   r,   %test_rendition_storage_setting_absentZ  s    z4TestRenditions.test_rendition_storage_setting_absentz.wagtail.images.tests.test_models.CustomStorage)rG  c                 C   s   |  t t d S r<   )r`   r   r'   r9   r+   r+   r,   0test_rendition_storage_setting_given_dotted_path`  s    z?TestRenditions.test_rendition_storage_setting_given_dotted_pathc                 C   s   |  t tj d S r<   )r=   r   r   rG  r9   r+   r+   r,   5test_rendition_storage_setting_given_storage_instancef  s     zDTestRenditions.test_rendition_storage_setting_given_storage_instancez+django.core.files.storage.FileSystemStoragez5django.contrib.staticfiles.storage.StaticFilesStorage)rZ   Zstaticfilescustom_storagerK  )r\   rG  c                 C   s   |  t ttj  d S r<   )r=   r   r
   r   rG  r9   r+   r+   r,   2test_rendition_storage_setting_given_storage_aliasl  s     zATestRenditions.test_rendition_storage_setting_given_storage_aliasN)FF)%r(   r)   r*   r  r;   r   r   r   r   r   r  r  r  r  r  r  r  r  ra   boolr   r#  r   r$  r+  r,  r0  r7  r9  r:  rE  rF  rH  rI  r'   rJ  rL  r+   r+   r+   r,   r     s   
   
1 
(




   r   c                   @   s"   e Zd ZdgZdd Zdd ZdS )TestPrefetchRenditions	test.jsonc                 C   s   g | _ g | _tj d d }t|D ]H\}}tjjdt d |_	}|j
dgd | j | | j|j q&dd | j D | _dd | j D | _d S )	Nr   zTest image {i}r2   
feed_image)Zupdate_fieldsc                 S   s   g | ]}| d qS r   r   r   r+   r+   r,   r     s    z0TestPrefetchRenditions.setUp.<locals>.<listcomp>c                 S   s   g | ]}| d qS zmin-300x600rR  r   r+   r+   r,   r     s    )imagesevent_pages_pksr   r6   r   	enumerater   r7   r$   rP  rd   appendr  small_renditionslarge_renditions)r:   Zevent_pagesr   pager8   r+   r+   r,   r;     s"    zTestPrefetchRenditions.setUpc              	   C   s   t dtjdd}| d  ttj|j| j	d}W 5 Q R X | d dd |D }W 5 Q R X | 
| j| | d d	d |D }W 5 Q R X | 
| j| d S )
NrP  r   )r  r   )Zpk__inr   c                 S   s   g | ]}|j d qS rQ  rP  r   r   rZ  r+   r+   r,   r     s    zZTestPrefetchRenditions.test_prefetch_renditions_on_non_image_querysets.<locals>.<listcomp>c                 S   s   g | ]}|j d qS rS  r[  r\  r+   r+   r,   r     s    )r   r   r6   r  r   rn   r   r  r(  rU  ZassertListEqualrX  rY  )r:   Z$prefetch_images_and_small_renditionsZpagesrX  rY  r+   r+   r,   /test_prefetch_renditions_on_non_image_querysets  s,     
zFTestPrefetchRenditions.test_prefetch_renditions_on_non_image_querysetsN)r(   r)   r*   r   r;   r]  r+   r+   r+   r,   rN    s   rN  c                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	TestUsageCountrO  c                 C   s   t jjdt d| _d S r   r5   r9   r+   r+   r,   r;     s    zTestUsageCount.setUpc                 C   s   |  | j  d d S r^   )r=   r8   	get_usagecountr9   r+   r+   r,   test_unused_image_usage_count  s    z,TestUsageCount.test_unused_image_usage_countc                 C   sD   t jjdd}t }||_| j|_|  | | j 	 d d S )Nr  r  r#   )
r   r6   r   r    rZ  r8   rd   r=   r_  r`  r:   rZ  Zevent_page_carousel_itemr+   r+   r,   $test_used_image_document_usage_count  s    z3TestUsageCount.test_used_image_document_usage_countN)r(   r)   r*   r   r;   ra  rc  r+   r+   r+   r,   r^    s   r^  c                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	TestGetUsagerO  c                 C   s   t jjdt d| _d S r   r5   r9   r+   r+   r,   r;     s    zTestGetUsage.setUpc                 C   s   |  t| j g  d S r<   )r=   rn   r8   r_  r9   r+   r+   r,   test_unused_image_get_usage  s    z(TestGetUsage.test_unused_image_get_usagec                 C   s   t jjdd}t }||_| j|_|  | | j d t	 | | j d d t
 | | j d d t | | j d d d t d S )Nr  r  r   r#   )r   r6   r   r    rZ  r8   rd   r`   r_  tupler   rn   r   rb  r+   r+   r,   "test_used_image_document_get_usage  s    z/TestGetUsage.test_used_image_document_get_usageN)r(   r)   r*   r   r;   re  rg  r+   r+   r+   r,   rd    s   rd  c                   @   sB   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestGetWillowImagerO  c                 C   s   t jjdt d| _d S r   r5   r9   r+   r+   r,   r;     s    zTestGetWillowImage.setUpc              	   C   s&   | j  }| |t W 5 Q R X d S r<   )r8   get_willow_imager`   WillowImage)r:   willow_imager+   r+   r,   !test_willow_image_object_returned  s    z4TestGetWillowImage.test_willow_image_object_returnedc              
   C   sD   t jjdd}| t" |  |   W 5 Q R X W 5 Q R X d S )Nr#   r  )r   r6   r   rf   r   ri  Zfail)r:   Z	bad_imager+   r+   r,   test_with_missing_image  s    
z*TestGetWillowImage.test_with_missing_imagec              	   C   s:   | j   | | j jj W 5 Q R X | | j jj d S r<   )r8   ri  r?   r4   closedrA   r9   r+   r+   r,   test_closes_image  s    z$TestGetWillowImage.test_closes_imagec              	   C   s\   z2| j   | | j jj tdW 5 Q R X W n tk
rF   Y nX | | j jj d S )NzSomething went wrong!)r8   ri  r?   r4   rn  
ValueErrorrA   r9   r+   r+   r,   test_closes_image_on_exception  s    z1TestGetWillowImage.test_closes_image_on_exceptionc              	   C   sD   | j jd | j   W 5 Q R X | | j jj | j j  d S )Nrb)r8   r4   openri  r?   rn  closer9   r+   r+   r,   test_doesnt_close_open_image  s
    
z/TestGetWillowImage.test_doesnt_close_open_imageN)
r(   r)   r*   r   r;   rl  rm  ro  rq  ru  r+   r+   r+   r,   rh    s   
rh  c                   @   s   e Zd ZdZdd ZdS )TestIssue573z
    This tests for a bug which causes filename limit on Renditions to be reached
    when the Image has a long original filename and a big focal point key
    c                 C   s*   t jjdtdddddd}|d d S )Nr.   zYthisisaverylongfilename-cdefghijklmnopqrstuvwxyzab-supercalifragilisticexpialidocious.pngi  )r3   r4   rO   rP   rQ   rR   zfill-800x600)r   r6   r7   r$   r   ri   r+   r+   r,   test_issue_573#  s    zTestIssue573.test_issue_573N)r(   r)   r*   __doc__rw  r+   r+   r+   r,   rv    s   rv  elasticsearch)Z _WAGTAILSEARCH_FORCE_AUTO_UPDATEc                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestIssue613c                 C   s4   ddl m} ddlm} d|jkr,td|dS )Nr   r   )get_search_backendry  zNo elasticsearch backend active)django.confr   Zwagtail.search.backendsr{  ZWAGTAILSEARCH_BACKENDSunittestZSkipTest)r:   r   r{  r+   r+   r,   get_elasticsearch_backend7  s
    

z&TestIssue613.get_elasticsearch_backendc                 C   s   |   | _|   d S r<   )r~  search_backendloginr9   r+   r+   r,   r;   A  s    
zTestIssue613.setUpc                 K   s   dt dt j d}|| | jtd|}| |td t	j
jdd}| | d | }| |jd | |jd	 |S )
Nr.   ztest.pngr2   zwagtailimages:addwagtailimages:indexrw   r#   rC   rD   )r   r$   r4   getvalueupdateclientpostr   assertRedirectsr   r6   r(  r=   r`  r  r   r   )r:   params	post_dataresponserT  r8   r+   r+   r,   r   E  s     

zTestIssue613.add_imagec                 K   sv   t jjdt d| _ddi}|| | jtd| jj	fd|}| 
|td t jj| jj	d}| |jd |S )	Nr.   r2   r3   ZEditedzwagtailimages:edit)argsr  r  )r   r6   r7   r$   r8   r  r  r  r   r  r  r   r=   r3   )r:   r  r  r  r8   r+   r+   r,   
edit_image]  s      
 zTestIssue613.edit_imagec                 C   sb   | j   | j t | jdd}| j   | j dt}| t|d | |d j	|j	 d S NZhello)r   r#   r   )
r  reset_indexadd_typer   r   refresh_indexrm   r=   lenr  ro   r+   r+   r,   test_issue_613_on_addu  s    

z"TestIssue613.test_issue_613_on_addc                 C   sb   | j   | j t | jdd}| j   | j dt}| t|d | |d j	|j	 d S r  )
r  r  r  r   r  r  rm   r=   r  r  ro   r+   r+   r,   test_issue_613_on_edit  s    

z#TestIssue613.test_issue_613_on_editN)	r(   r)   r*   r~  r;   r   r  r  r  r+   r+   r+   r,   rz  5  s   
rz  c                   @   s   e Zd Zdd ZdS )TestIssue312c              	   C   s\   t jjdt d}|d}|d}| || | jttjj|j	|j
|j|j|jd d S )Nr.   r2   zfill-100x100)r8   r   r   r   focal_point_key)r   r6   r7   r$   r   r=   rf   r   r   r8   r   r   r   r  )r:   r8   Zrend1Zrend2r+   r+   r,   test_duplicate_renditions  s     

z&TestIssue312.test_duplicate_renditionsN)r(   r)   r*   r  r+   r+   r+   r,   r    s   r  c                   @   s    e Zd ZdZdd Zdd ZdS )TestFilenameReductionzi
    This tests for a bug which results in filenames without extensions
    causing an infinite loop
    c                 C   s0   t jjdtdd}|  | d|jj d S )Nr.   z^thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpialidocioussuperlongr2   z^original_images/thisisaverylongfilename-defghijklmnopqrstuvwxyzabc-supercalifragilisticexpialir   r6   r7   r$   rd   r=   r4   r   ri   r+   r+   r,   test_filename_reduction_no_ext  s    z4TestFilenameReduction.test_filename_reduction_no_extc                 C   s0   t jjdtdd}|  | d|jj d S )Nr.   zbthisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexpialidocioussuperlong.pngr2   z^original_images/thisisaverylongfilename-efghijklmnopqrstuvwxyzabcd-supercalifragilisticexp.pngr  ri   r+   r+   r,   test_filename_reduction_ext  s    z1TestFilenameReduction.test_filename_reduction_extN)r(   r)   r*   rx  r  r  r+   r+   r+   r,   r    s   r  c                   @   sX   e Zd 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 )TestRenditionOrientationa  
    This tests for a bug where images with exif orientations which
    required rotation for display were cropped and sized based on the
    unrotated image dimensions.

    For example images with specified dimensions of 640x450 but an exif orientation of 6
    should appear as a 450x640 portrait, but instead were still cropped to 640x450.

    Actual image files are used so that exif orientation data will exist for the rotation to function correctly.
    c              	   C   s   ddl m} | }||}W 5 Q R X | | d |jdd}| j	|d ddd | j	|d	 d
dd | j	|d ddd |jdd}| j	|d ddd | j	|d	 ddd | j	|d ddd dS )z
        Check that the image has the correct colored pixels in the right places
        so that we know the image did not physically rotate.
        r   )PillowImage)X    RGB)   i     r@  )deltar#   &   r   .   )iy  i  U   ]   A   N)
Zwillow.plugins.pillowr  ri  rs  r=   get_sizer8   convertZgetpixelZassertAlmostEqual)r:   r   r  rk  r8   r1   r+   r+   r,   -assert_orientation_landscape_image_is_correct  s    
zFTestRenditionOrientation.assert_orientation_landscape_image_is_correctc              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_1.jpgrr  r.   r2   r  r  r   
rs  r   r6   r7   r   r=   r   r   r   r  r:   fr8   r   r+   r+   r,   test_jpeg_with_orientation_1  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_1c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_2.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_2	  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_2c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_3.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_3  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_3c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_4.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_4%  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_4c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S Nz0wagtail/images/tests/image_files/landscape_6.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_56  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_5c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S r  r  r  r+   r+   r,   test_jpeg_with_orientation_6D  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_6c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_7.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_7R  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_7c              	   C   sz   t dd}tjjdt|d}W 5 Q R X | |jd | |jd |d}| |jd | |jd | 	| d S )Nz0wagtail/images/tests/image_files/landscape_8.jpgrr  r.   r2   r  r  r   r  r  r+   r+   r,   test_jpeg_with_orientation_8`  s    
z5TestRenditionOrientation.test_jpeg_with_orientation_8N)r(   r)   r*   rx  r  r  r  r  r  r  r  r  r  r+   r+   r+   r,   r    s   r  )Gr}  r|  r   Zdjango.contrib.auth.modelsr   r   Zdjango.core.cacher   Zdjango.core.filesr   Zdjango.core.files.storager   r	   r
   Zdjango.core.files.uploadedfiler   Zdjango.db.modelsr   Zdjango.db.utilsr   Zdjango.testr   r   r   r   Zdjango.urlsr   Zwillow.imager   rj  Zwagtail.images.modelsr   r   r   r   r   r   Zwagtail.images.rectr   Zwagtail.modelsr   r   r   r   Zwagtail.test.testapp.modelsr   r    r!   Zwagtail.test.utilsr"   utilsr$   r%   r&   r'   r-   rk   r   r   r   r   r   rN  r^  rd  rh  rv  rz  r  r  r  r+   r+   r+   r,   <module>   sX    iB01V\
   
75
`'