o
    hh                     @   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&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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!   d/var/www/html/ndineBlogger/venv/lib/python3.10/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BACKENDLOCATIONbackend_settingshttplocalhost:8000r   r   setkeysassertIsInstancer   cache_scheme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/   r0   r1   r   cloudflare_emailcloudflare_api_key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_IDAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYr*   explicit)r   r   r/   r0   r1   r   cloudfront_distribution_idclient_request_signer_credentialsmethod
access_key
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/   r0   r1   r   _resource_group_name_cdn_profile_name_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.AzureFrontDoorBackendrT   wagtail-io-front-door)r(   rW   FRONT_DOOR_NAMEr*   )r   r   r/   r0   r1   r   rZ   _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 )	NrR   rS   rT   rU   rV   fake-subscription-id)r(   rW   rX   rY   SUBSCRIPTION_IDCREDENTIALSr*   )r   	MagicMockr   r   r/   r0   _get_clientr1   r   _configsubscription_idassertIs
credentialr   mock_credentialsr    rJ   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 )Nr^   r_   rT   zwagtail-io-fake-front-door-namerd   )r(   rW   ra   re   rf   r*   )r   rg   r   rh   r   r/   r0   r1   r   ri   rj   rk   rl   rm   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 )NrR   rS   rT   rU   rV   Fake credentials)r(   rW   rX   rY   rf   r*   4http://www.wagtail.org/home/events/christmas/?test=1r      r   /home/events/christmas/?test=1http://torchbox.com/blog//blog/)r   r   r/   r0   r1   r   purgeassert_called_oncetuple	call_argsr   r   
reset_mockpurge_batchr   make_purge_call_mockr    rz   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 )Nr^   r_   rT   r`   rq   )r(   rW   ra   rf   r*   rr   r   r   rt   ru   rv   )r   r   r/   r0   r1   r   rw   rx   ry   rz   r   r{   r|   r}   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   s`   t dddi dd}| jdd}| j|d W d   n1 s w   Y  | 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levelr   ztCouldn't purge 'http://www.wagtail.org/home/events/christmas/' from HTTP cache. HTTPError: 500 Internal Server Errorr   )r   
assertLogsr   assertInoutput)r   
http_error
log_outputr!   r!   r"   test_http_httperror  s   z,TestBackendConfiguration.test_http_httperrorc                 C   sX   t dd}| jdd}| j|d W d   n1 sw   Y  | d|jd  dS )	z3Test that `HTTPBackend.purge` can handle `URLError`zjust for tests)reasonr   r   r   NzhCouldn't purge 'http://www.wagtail.org/home/events/christmas/' from HTTP cache. URLError: just for testsr   )r   r   r   r   r   )r   	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   r   )r   r   r/   r0   r1   r   side_effectgetrw   
call_countrz   full_url)r   urlopen_mockr   r    purge_request_call_kwargsr!   r!   r"   r   )  s    z4TestBackendConfiguration._test_http_with_side_effectc                 C   sB   |  t tdddiid W d    d S 1 sw   Y  d S )NrB   r(   rC   r*   )assertRaisesr   r   r   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 d    n1 s%w   Y  | td |dd	 W d    n1 sCw   Y  |dd
g | |dd | |dd d S )NrB   rC   zwww.wagtail.orgrD   )r(   rE   r*   zpUsing a `DISTRIBUTION_ID` mapping is deprecated - use `HOSTNAMES` in combination with multiple backends instead.r   ru   z/home/events/christmas/ztorchbox.com)	r   assertWarnsMessager   r   rw   assert_called_once_with
assertTrueinvalidates_hostnameassertFalse)r   _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'   r6   r7   r8   r9   r(   r:   r;   r<   r$   r5   r*   r$   r5   r   r   r/   r0   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'   r6   r7   r8   r9   r   r   r5   )r+   r    r   r   r!   r!   r"   test_filter  s   z$TestBackendConfiguration.test_filterr&   )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#   r4   rA   rQ   r]   rc   ro   rp   r   patchr   r   r   r   r   r   r   r   r   r   r
   r   r!   r!   r!   r"   r   !   s>    
'
&



$r   c                   @      e Zd Zdd ZdS )MockBackendc                 C   s   t | d S N)PURGED_URLSadd)r   r   r!   r!   r"   rw     s   zMockBackend.purgeN)r   r   r   rw   r!   r!   r!   r"   r         r   c                   @   r   )MockCloudflareBackendc                 C   s$   t || jkrtdt| d S )Nz7Cloudflare backend is not chunking requests as expected)r   
CHUNK_SIZE	Exceptionr   update)r   urlsr!   r!   r"   _purge_urls  s   z!MockCloudflareBackend._purge_urlsN)r   r   r   r   r!   r!   r!   r"   r     r   r   r$   r(   0wagtail.contrib.frontend_cache.tests.MockBackend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      t   d S r   r   clearr   r!   r!   r"   setUp  s   zTestCachePurgingFunctions.setUpc                 C   sF   | j dd td W d    n1 sw   Y  | tdh d S )NTexecutehttp://localhost/foo)captureOnCommitCallbacksr   r   r   r   r!   r!   r"   test_purge_url_from_cache  s   
z3TestCachePurgingFunctions.test_purge_url_from_cachec                 C   sL   | j dd tddg W d    n1 sw   Y  | tddh d S )NTr   r   http://localhost/bar)r   r   r   r   r   r!   r!   r"   test_purge_urls_from_cache  s   z4TestCachePurgingFunctions.test_purge_urls_from_cachec                 C   sV   | j dd tjjdd}t| W d    n1 sw   Y  | tddh d S NTr   /home/events/url_pathhttp://localhost/events/http://localhost/events/past/)r   r   objectsr   r   r   r   r   pager!   r!   r"   test_purge_page_from_cache     
z4TestCachePurgingFunctions.test_purge_page_from_cachec                 C   sN   | j dd ttj  W d    n1 sw   Y  | tddh d S )NTr   r   r   )r   r   r   r   allr   r   r   r!   r!   r"   test_purge_pages_from_cache  s   z5TestCachePurgingFunctions.test_purge_pages_from_cachec                 C   sp   | j dd  t }tjjdd}|| |d |  W d    n1 s)w   Y  | t	h d d S )NTr   r   r   r      r   r   r   )
r   r   r   r   r   add_pageadd_urlrw   r   r   )r   batchr   r!   r!   r"   test_purge_batch  s   


z*TestCachePurgingFunctions.test_purge_batchr$   r   zexample.com)r(   	HOSTNAMESr   c              	   C   s   | j dd"}| jdd td W d    n1 sw   Y  W d    n1 s+w   Y  | tt  | d|jd  | jdd td W d    n1 sUw   Y  | tdh d S )	NINFOr   Tr   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!   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   r5   z:wagtail.contrib.frontend_cache.tests.MockCloudflareBackendzonetoken)r(   r<   r=   c                   @   s   e Zd Zdd Zdd ZdS )#TestCloudflareCachePurgingFunctionsc                 C   r   r   r   r   r!   r!   r"   r        z)TestCloudflareCachePurgingFunctions.setUpc                 C   sl   | j dd t }dd tddD }|| |  W d    n1 s'w   Y  | tt| d S )NTr   c                 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   r   rangeadd_urlsrw   assertCountEqualr   r/   )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g d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   r   r   r   r   r!   r!   r"   r   %  r   zTestCachePurgingSignals.setUpc                 C   Z   | j dd tjjdd}|   W d    n1 sw   Y  | tddh d S r   r   r   r   r   save_revisionpublishr   r   r   r!   r!   r"   test_purge_on_publish)  s   z-TestCachePurgingSignals.test_purge_on_publishc                 C   sV   | j dd tjjdd}|  W d    n1 sw   Y  | tddh d S r   )r   r   r   r   	unpublishr   r   r   r!   r!   r"   test_purge_on_unpublish1  r   z/TestCachePurgingSignals.test_purge_on_unpublishc                 C   sn   | j dd  tjjdd}tdd}|j|d |   W d    n1 s)w   Y  | t	t
  d S )NTr   /r   znew top-level page)title)instance)r   r   r   r   r   	add_childr   r   r   r   r/   )r   rootr   r!   r!   r"   test_purge_with_unroutable_page9  s   
z7TestCachePurgingSignals.test_purge_with_unroutable_pagezwagtail.test.urls_multilangen)r  frzpt-br)ROOT_URLCONFLANGUAGE_CODEWAGTAILFRONTENDCACHE_LANGUAGESc                 C   Z   | j dd tjjdd}|   W d    n1 sw   Y  | th d d S )NTr   r   r   >   http://localhost/pt-br/events/#http://localhost/pt-br/events/past/http://localhost/en/events/http://localhost/fr/events/ http://localhost/en/events/past/ http://localhost/fr/events/past/r   r   r!   r!   r"   &test_purge_on_publish_in_multilang_envA  s   z>TestCachePurgingSignals.test_purge_on_publish_in_multilang_envT)r  English)r  French)r  r  WAGTAIL_I18N_ENABLEDWAGTAIL_CONTENT_LANGUAGESc                 C   r  )NTr   r   r   >   r  r  r  r  r   r   r!   r!   r"   'test_purge_on_publish_with_i18n_enabledW  s   z?TestCachePurgingSignals.test_purge_on_publish_with_i18n_enabled)r  r  r  c                 C   r   )NTr   r   r   r  r  r   r   r!   r!   r"   *test_purge_on_publish_without_i18n_enabledl  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     s2    

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h d S )Nr   )r   r   r   r   r   r   r!   r!   r"   test_add_url  s   
z TestPurgeBatchClass.test_add_urlc                 C   s*   t  }|ddg | |jddh d S )Nr   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h d S )Nr   r   r   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h d S )Nr   r   )r   	add_pagesr   r   r   r   r   r  r!   r!   r"   test_add_pages  s
   
z"TestPurgeBatchClass.test_add_pagesc                 C   sF   t jjdd}t }|| |d |  | |jh d d S )Nr   r   r   r   )	r   r   r   r   r   r   rw   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 }|d | jdd$}| jdd |j|d W d    n1 sCw   Y  W d    n1 sRw   Y  | 	d|j
d  d S )Nr5   r6   r7   r8   r9   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#  r   r   )responser   r   r   Tr   r*   zICouldn't purge 'http://localhost/events/' from Cloudflare. HTTPError: 500r   )requests
exceptionsr   r   r   r   r   r   rw   r   r   )r   requests_delete_mockr+   r#  r   r   r   r!   r!   r"   )test_http_error_on_cloudflare_purge_batch  s0   	
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  |  s    
r  )/unittestr   urllib.errorr   r   r%  azure.mgmt.cdnr   azure.mgmt.frontdoorr   django.core.exceptionsr   django.testr   r	   django.test.utilsr
   'wagtail.contrib.frontend_cache.backendsr   r   r   r   r   r   $wagtail.contrib.frontend_cache.utilsr   wagtail.modelsr   wagtail.test.testapp.modelsr   wagtail.utils.deprecationr   utilsr   r   r   r   r   r   r/   r   r   r   r   r   r   r  r!   r!   r!   r"   <module>   sZ     	   H	Z