U
    {he                     @   s  d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d d	lmZmZmZmZmZmZ d d
l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$m%Z% G dd deZ&g 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dd 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-dS )'    )mock)	HTTPErrorURLErrorN)CdnManagementClient)FrontDoorManagementClient)ImproperlyConfigured)SimpleTestCaseTestCase)override_settings)AzureCdnBackendAzureFrontDoorBackendBaseBackendCloudflareBackendCloudfrontBackendHTTPBackend)get_backends)Page)
EventIndex)RemovedInWagtail70Warning   )
PurgeBatchpurge_page_from_cachepurge_pages_from_cachepurge_url_from_cachepurge_urls_from_cachec                   @   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
eddd Zeddd Zdd Zdd Zdd Zeddd Zd d! Zed"d#d$ Zd%d& Zd'd( Zed)d*d+d, Zd-S ).TestBackendConfigurationc                 C   s   t  }| t|d d S )Nr   )r   assertEquallenselfbackends r!   l/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/contrib/frontend_cache/tests.pytest_default"   s    z%TestBackendConfiguration.test_defaultc                 C   sb   t ddddid}| t| dh | |d t | |d jd | |d jd d S )Nvarnish3wagtail.contrib.frontend_cache.backends.HTTPBackendhttp://localhost:8000BACKENDZLOCATIONbackend_settingshttplocalhost:8000r   r   setkeysassertIsInstancer   Zcache_schemeZcache_netlocr   r!   r!   r"   test_varnish'   s    	z%TestBackendConfiguration.test_varnishc                 C   sz   t dddddddid}| t| dh | |d t | |d jd | |d jd | |d jd d S )	N
cloudflare9wagtail.contrib.frontend_cache.backends.CloudflareBackendtest@test.comthis is the api keythis is a zone idzthis is a bearer token)r(   EMAILAPI_KEYZONEIDBEARER_TOKENr)   )	r   r   r.   r/   r0   r   Zcloudflare_emailZcloudflare_api_keyZcloudflare_tokenr   r!   r!   r"   test_cloudflare7   s*      z(TestBackendConfiguration.test_cloudflarec                 C   s   t ddddddid}| t| dh | |d t | |d jd |d jjj	}| |j
d | |jd | |jd d S )	N
cloudfront9wagtail.contrib.frontend_cache.backends.CloudfrontBackendfrontendzmy-access-key-idzmy-secret-access-key)r(   DISTRIBUTION_IDZAWS_ACCESS_KEY_IDZAWS_SECRET_ACCESS_KEYr)   explicit)r   r   r.   r/   r0   r   Zcloudfront_distribution_idclientZ_request_signerZ_credentialsmethodZ
access_keyZ
secret_key)r   r    credentialsr!   r!   r"   test_cloudfrontO   s     z(TestBackendConfiguration.test_cloudfrontc                 C   sx   t ddddddid}| t| dh | |d t | |d jd | |d jd | |d jd d S )N	azure_cdn7wagtail.contrib.frontend_cache.backends.AzureCdnBackendtest-resource-groupwagtail-io-profilewagtail-io-endpoint)r(   RESOURCE_GROUP_NAMECDN_PROFILE_NAMECDN_ENDPOINT_NAMEr)   )	r   r   r.   r/   r0   r   _resource_group_nameZ_cdn_profile_nameZ_cdn_endpoint_namer   r!   r!   r"   test_azure_cdnf   s(      z'TestBackendConfiguration.test_azure_cdnc                 C   sd   t dddddid}| t| dh | |d t | |d jd | |d jd d S )Nazure_front_door=wagtail.contrib.frontend_cache.backends.AzureFrontDoorBackendrG   wagtail-io-front-door)r(   rJ   FRONT_DOOR_NAMEr)   )r   r   r.   r/   r0   r   rM   Z_front_door_namer   r!   r!   r"   test_azure_front_door|   s$    
  z.TestBackendConfiguration.test_azure_front_doorc              	   C   sv   t  }tdddddd|did}| t| dh |d  }| |t | |j	j
d | |j	j| d S )	NrE   rF   rG   rH   rI   fake-subscription-id)r(   rJ   rK   rL   SUBSCRIPTION_IDCREDENTIALSr)   )r   	MagicMockr   r   r.   r/   _get_clientr0   r   _configsubscription_idassertIs
credentialr   Zmock_credentialsr    rA   r!   r!   r"   test_azure_cdn_get_client   s"    z2TestBackendConfiguration.test_azure_cdn_get_clientc                 C   st   t  }tddddd|did}|d  }| t| dh | |t | |j	j
d | |j	j| d S )NrO   rP   rG   zwagtail-io-fake-front-door-namerT   )r(   rJ   rR   rU   rV   r)   )r   rW   r   rX   r   r.   r/   r0   r   rY   rZ   r[   r\   r]   r!   r!   r"    test_azure_front_door_get_client   s     z9TestBackendConfiguration.test_azure_front_door_get_clientzNwagtail.contrib.frontend_cache.backends.azure.AzureCdnBackend._make_purge_callc                 C   s   t dddddddid}| t| dh | |d t |d d	 |  t|j	d
 }| t
|d | |d
 t | |d dg |  |d d	dg |  t|j	d
 }| |d
 t | |d ddg d S )NrE   rF   rG   rH   rI   Fake credentials)r(   rJ   rK   rL   rV   r)   4http://www.wagtail.org/home/events/christmas/?test=1r      r   /home/events/christmas/?test=1http://torchbox.com/blog//blog/)r   r   r.   r/   r0   r   purgeassert_called_oncetuple	call_argsr   r   
reset_mockpurge_batchr   Zmake_purge_call_mockr    ri   r!   r!   r"   test_azure_cdn_purge   s<    z-TestBackendConfiguration.test_azure_cdn_purgezTwagtail.contrib.frontend_cache.backends.azure.AzureFrontDoorBackend._make_purge_callc                 C   s   t ddddddid}| t| dh | |d t |d d |  t|j	d	 }| |d	 t
 | |d
 dg |  |d ddg |  t|j	d	 }| |d	 t
 | |d
 ddg d S )NrO   rP   rG   rQ   r`   )r(   rJ   rR   rV   r)   ra   r   r   rc   rd   re   )r   r   r.   r/   r0   r   rf   rg   rh   ri   r   rj   rk   rl   r!   r!   r"   test_azure_front_door_purge   s8    z4TestBackendConfiguration.test_azure_front_door_purgec                 C   s   | j dd dS )z9Test that `HTTPBackend.purge` works when urlopen succeedsNurlopen_side_effect)_test_http_with_side_effectr   r!   r!   r"   	test_http
  s    z"TestBackendConfiguration.test_httpc              	   C   sL   t dddi dd}| jdd}| j|d W 5 Q R X | d	|jd
  dS )z4Test that `HTTPBackend.purge` can handle `HTTPError`,http://localhost:8000/home/events/christmas/  zInternal Server ErrorN)urlcodemsghdrsfpERRORlevelro   ztCouldn't purge 'http://www.wagtail.org/home/events/christmas/' from HTTP cache. HTTPError: 500 Internal Server Errorr   )r   
assertLogsrq   assertInoutput)r   
http_error
log_outputr!   r!   r"   test_http_httperror  s    z,TestBackendConfiguration.test_http_httperrorc              	   C   sD   t dd}| jdd}| j|d W 5 Q R X | d|jd  dS )	z3Test that `HTTPBackend.purge` can handle `URLError`zjust for tests)reasonr{   r|   ro   zhCouldn't purge 'http://www.wagtail.org/home/events/christmas/' from HTTP cache. URLError: just for testsr   N)r   r~   rq   r   r   )r   Z	url_errorr   r!   r!   r"   test_http_urlerror  s    
z+TestBackendConfiguration.test_http_urlerrorz4wagtail.contrib.frontend_cache.backends.http.urlopenc                 C   s|   t ddddid}| t| dh | |d t ||_|dd | |j	d |j
\\}}| |jd d S )	Nr$   r%   r&   r'   r)   -http://www.wagtail.org/home/events/christmas/r   rt   )r   r   r.   r/   r0   r   side_effectgetrf   Z
call_countri   full_url)r   Zurlopen_mockrp   r    Zpurge_requestZ_call_kwargsr!   r!   r"   rq   )  s"     z4TestBackendConfiguration._test_http_with_side_effectc              	   C   s,   |  t tdddiid W 5 Q R X d S )Nr<   r(   r=   r)   )ZassertRaisesr   r   rr   r!   r!   r"   (test_cloudfront_validate_distribution_idD  s     zATestBackendConfiguration.test_cloudfront_validate_distribution_idzYwagtail.contrib.frontend_cache.backends.cloudfront.CloudfrontBackend._create_invalidationc              	   C   s   t ddddidid}| td |dd W 5 Q R X | td |dd	 W 5 Q R X |dd
g | |dd | |dd d S )Nr<   r=   zwww.wagtail.orgr>   )r(   r?   r)   zpUsing a `DISTRIBUTION_ID` mapping is deprecated - use `HOSTNAMES` in combination with multiple backends instead.r   rd   z/home/events/christmas/ztorchbox.com)	r   ZassertWarnsMessager   r   rf   Zassert_called_once_withZ
assertTrueZinvalidates_hostnameZassertFalse)r   Z_create_invalidationr    r!   r!   r"   'test_cloudfront_distribution_id_mappingN  s>     

 z@TestBackendConfiguration.test_cloudfront_distribution_id_mappingc                 C   s<   t ddddddddd	d
}| t| ddh d S )Nr%   http://localhost:8000/r'   r3   r4   r5   r6   r(   r7   r8   r9   r$   r2   r)   r$   r2   r   r   r.   r/   r   r!   r!   r"   test_multipleu  s    z&TestBackendConfiguration.test_multiplec                 C   s>   t ddddddddd	d
gd}| t| d
h d S )Nr%   r   r'   r3   r4   r5   r6   r   r   r2   )r*   r    r   r   r!   r!   r"   test_filter  s    z$TestBackendConfiguration.test_filterr&   )ZWAGTAILFRONTENDCACHE_LOCATIONc                 C   sT   t  }| t| dh | |d t | |d jd | |d jd d S )Ndefaultr+   r,   r-   r   r!   r!   r"   test_backwards_compatibility  s
    z5TestBackendConfiguration.test_backwards_compatibilityN)__name__
__module____qualname__r#   r1   r;   rD   rN   rS   r^   r_   r   patchrm   rn   rs   r   r   rq   r   r   r   r   r
   r   r!   r!   r!   r"   r   !   s<   
'
&



$r   c                   @   s   e Zd Zdd ZdS )MockBackendc                 C   s   t | d S N)PURGED_URLSappend)r   rv   r!   r!   r"   rf     s    zMockBackend.purgeN)r   r   r   rf   r!   r!   r!   r"   r     s   r   c                   @   s   e Zd Zdd ZdS )MockCloudflareBackendc                 C   s$   t || jkrtdt| d S )Nz7Cloudflare backend is not chunking requests as expected)r   Z
CHUNK_SIZE	Exceptionr   extend)r   urlsr!   r!   r"   _purge_urls  s    z!MockCloudflareBackend._purge_urlsN)r   r   r   r   r!   r!   r!   r"   r     s   r   r$   r(   0wagtail.contrib.frontend_cache.tests.MockBackendZWAGTAILFRONTENDCACHEc                   @   s`   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	e
dddgdiddd ZdS )TestCachePurgingFunctions	test.jsonc                 C   s   g t d d < d S r   r   rr   r!   r!   r"   setUp  s    zTestCachePurgingFunctions.setUpc                 C   s   t d | tdg d S Nhttp://localhost/foo)r   r   r   rr   r!   r!   r"   test_purge_url_from_cache  s    z3TestCachePurgingFunctions.test_purge_url_from_cachec                 C   s    t ddg | tddg d S Nr   zhttp://localhost/bar)r   r   r   rr   r!   r!   r"   test_purge_urls_from_cache  s    z4TestCachePurgingFunctions.test_purge_urls_from_cachec                 C   s*   t jjdd}t| | tddg d S N/home/events/Zurl_pathhttp://localhost/events/http://localhost/events/past/)r   objectsr   r   r   r   r   pager!   r!   r"   test_purge_page_from_cache  s     z4TestCachePurgingFunctions.test_purge_page_from_cachec                 C   s"   t tj  | tddg d S Nr   r   )r   r   r   allr   r   rr   r!   r!   r"   test_purge_pages_from_cache  s
     z5TestCachePurgingFunctions.test_purge_pages_from_cachec                 C   sF   t  }tjjdd}|| |d |  | tdddg d S Nr   r   r   r   r   )	r   r   r   r   add_pageadd_urlrf   r   r   )r   batchr   r!   r!   r"   test_purge_batch  s    

z*TestCachePurgingFunctions.test_purge_batchr$   r   zexample.com)r(   Z	HOSTNAMESr   c              	   C   sX   | j dd}td W 5 Q R X | tg  | d|jd  td | tdg d S )NINFOr|   r   z*Unable to find purge backend for localhostr   zhttp://example.com/foo)r~   r   r   r   r   r   )r   r   r!   r!   r"   !test_invalidate_specific_location  s    	z;TestCachePurgingFunctions.test_invalidate_specific_locationN)r   r   r   fixturesr   r   r   r   r   r   r
   r   r!   r!   r!   r"   r     s   r   r2   z:wagtail.contrib.frontend_cache.tests.MockCloudflareBackendzonetoken)r(   r9   r:   c                   @   s   e Zd Zdd Zdd ZdS )#TestCloudflareCachePurgingFunctionsc                 C   s   g t d d < d S r   r   rr   r!   r!   r"   r     s    z)TestCloudflareCachePurgingFunctions.setUpc                 C   s<   t  }dd tddD }|| |  | t| d S )Nc                 S   s   g | ]}d | qS )zhttps://localhost/foor!   ).0ir!   r!   r"   
<listcomp>  s     z[TestCloudflareCachePurgingFunctions.test_cloudflare_purge_batch_chunked.<locals>.<listcomp>r   A   )r   rangeadd_urlsrf   assertCountEqualr   )r   r   r   r!   r!   r"   #test_cloudflare_purge_batch_chunked  s
    
zGTestCloudflareCachePurgingFunctions.test_cloudflare_purge_batch_chunkedN)r   r   r   r   r   r!   r!   r!   r"   r     s   
r   c                   @   s   e Zd ZdgZdd Zdd Zdd Zdd	 Zed
ddddgddd Z	ed
ddddgddd Z
ed
dddgddd ZdS )TestCachePurgingSignalsr   c                 C   s   g t d d < d S r   r   rr   r!   r!   r"   r     s    zTestCachePurgingSignals.setUpc                 C   s.   t jjdd}|   | tddg d S r   )r   r   r   save_revisionpublishr   r   r   r!   r!   r"   test_purge_on_publish"  s     z-TestCachePurgingSignals.test_purge_on_publishc                 C   s*   t jjdd}|  | tddg d S r   )r   r   r   Z	unpublishr   r   r   r!   r!   r"   test_purge_on_unpublish)  s     z/TestCachePurgingSignals.test_purge_on_unpublishc                 C   s@   t jjdd}tdd}|j|d |   | tg  d S )N/r   znew top-level page)title)instance)	r   r   r   r   Z	add_childr   r   r   r   )r   rootr   r!   r!   r"   test_purge_with_unroutable_page0  s
    
z7TestCachePurgingSignals.test_purge_with_unroutable_pagezwagtail.test.urls_multilangenfrzpt-br)ROOT_URLCONFLANGUAGE_CODEZWAGTAILFRONTENDCACHE_LANGUAGESc              	   C   sB   g t d d < tjjdd}|   | t ddddddg d S )	Nr   r   http://localhost/en/events/ http://localhost/en/events/past/http://localhost/fr/events/ http://localhost/fr/events/past/zhttp://localhost/pt-br/events/z#http://localhost/pt-br/events/past/r   r   r   r   r   r   r   r   r!   r!   r"   &test_purge_on_publish_in_multilang_env7  s    z>TestCachePurgingSignals.test_purge_on_publish_in_multilang_envT)r   ZEnglish)r   ZFrench)r   r   ZWAGTAIL_I18N_ENABLEDWAGTAIL_CONTENT_LANGUAGESc                 C   s>   g t d d < tjjdd}|   | t ddddg d S )Nr   r   r   r   r   r   r   r   r!   r!   r"   'test_purge_on_publish_with_i18n_enabledM  s    z?TestCachePurgingSignals.test_purge_on_publish_with_i18n_enabled)r   r   r   c                 C   s:   g t d d < tjjdd}|   | t ddg d S )Nr   r   r   r   r   r   r!   r!   r"   *test_purge_on_publish_without_i18n_enabledb  s    zBTestCachePurgingSignals.test_purge_on_publish_without_i18n_enabledN)r   r   r   r   r   r   r   r   r
   r   r   r   r!   r!   r!   r"   r     s0   

r   c                   @   sL   e Zd ZdgZdd Zdd Zdd Zdd	 Zd
d Ze	
ddd ZdS )TestPurgeBatchClassr   c                 C   s$   t  }|d | |jdg d S r   )r   r   r   r   r   r   r!   r!   r"   test_add_urlx  s    
z TestPurgeBatchClass.test_add_urlc                 C   s*   t  }|ddg | |jddg d S r   )r   r   r   r   r   r!   r!   r"   test_add_urls~  s    z!TestPurgeBatchClass.test_add_urlsc                 C   s4   t jjdd}t }|| | |jddg d S r   )r   r   r   r   r   r   r   r   r   r   r!   r!   r"   test_add_page  s    
 z!TestPurgeBatchClass.test_add_pagec                 C   s,   t  }|tj  | |jddg d S r   )r   Z	add_pagesr   r   r   r   r   r   r!   r!   r"   test_add_pages  s     z"TestPurgeBatchClass.test_add_pagesc                 C   sH   t jjdd}t }|| |d |  | |jdddg d S r   )	r   r   r   r   r   r   rf   r   r   r   r!   r!   r"   test_multiple_calls  s    

z'TestPurgeBatchClass.test_multiple_callszBwagtail.contrib.frontend_cache.backends.cloudflare.requests.deletec              	   C   s   ddddddi}G dd d}t jj|d	d
d}||_tjjdd}t }|| | j	dd}|j
|d W 5 Q R X | d|jd  d S )Nr2   r3   r4   r5   r6   r   c                   @   s   e Zd ZdddZdS )zSTestPurgeBatchClass.test_http_error_on_cloudflare_purge_batch.<locals>.MockResponse   c                 S   s
   || _ d S r   status_code)r   r   r!   r!   r"   __init__  s    z\TestPurgeBatchClass.test_http_error_on_cloudflare_purge_batch.<locals>.MockResponse.__init__N)r   )r   r   r   r   r!   r!   r!   r"   MockResponse  s   r   ru   r   )responser   r   r{   r|   r)   zICouldn't purge 'http://localhost/events/' from Cloudflare. HTTPError: 500r   )requests
exceptionsr   r   r   r   r   r   r   r~   rf   r   r   )r   Zrequests_delete_mockr*   r   r   r   r   r   r!   r!   r"   )test_http_error_on_cloudflare_purge_batch  s*    	
z=TestPurgeBatchClass.test_http_error_on_cloudflare_purge_batchN)r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r   r  s   
r   ).Zunittestr   urllib.errorr   r   r   Zazure.mgmt.cdnr   Zazure.mgmt.frontdoorr   Zdjango.core.exceptionsr   Zdjango.testr   r	   Zdjango.test.utilsr
   Z'wagtail.contrib.frontend_cache.backendsr   r   r   r   r   r   Z$wagtail.contrib.frontend_cache.utilsr   Zwagtail.modelsr   Zwagtail.test.testapp.modelsr   Zwagtail.utils.deprecationr   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s\    	    B	 W