o
    h}'                     @   s   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	m
Z
 d dlmZ d dlmZmZmZ d dlmZmZ d	d
lmZ G dd de	ZG dd de	ZG dd de	ZdS )    N)settings)ContentType)get_current_site)RequestFactoryTestCaseoverride_settings)timezone)PagePageViewRestrictionSite)
EventIndex
SimplePage   )Sitemapc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	dddd Z
e	dddd Zdd Zdd Zdd Zdd Zdd ZdS )TestSitemapGeneratorc                 C   s:  t jjdd| _| jjtddddtjdddd	d
d
tjjdtjdddd	d
d
tjjddd| _	| jjtdddddd| _
| jjtdddddd| _tjj| jdd | jjtddddtjdddd	d
d
tjjddd| _tjjdd| _t jjdd}|jtdddddd| _tjjdd| jd tj  d S ) N   )idzHello world!zhello-worldhelloT  r      r   tzinfo)titleslugcontentlivelast_published_atlatest_revision_created_atinstanceUnpublishedunpublishedF)r   r   r   r   	Protected	protected)pagepasswordzI have no last publish date :-(zno-last-publish-date)r   r   r   r   r   )is_default_site)depthzAnother sitezanother-sitebonjourother.example.comP   )hostnameport	root_page)r	   objectsget	home_page	add_childr   datetimer   utc
child_pageunpublished_child_pageprotected_child_pager
   createpage_with_no_last_publish_dater   siteother_site_homepager   clear_cache)selfr-    r=   ^/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/contrib/sitemaps/tests.pysetUp   sp   	zTestSitemapGenerator.setUpc                 C   s4   t  |}| jj|jd< | jj|jd< |t|fS )N	HTTP_HOSTSERVER_PORT)r   r/   r9   r+   METAr,   r   )r<   urlrequestr=   r=   r>   get_request_and_django_siteR   s   z0TestSitemapGenerator.get_request_and_django_sitec                 C   s   t jst|}| ||S )N)r   USE_TZr   
make_naiveassertEqual)r<   actualexpectedr=   r=   r>   assertDatesEqualX   s   
z%TestSitemapGenerator.assertDatesEqualc                 C   sX   |  d\}}t|}| }| | jjj| | | jjj| | | j	jj| d S )N/sitemap.xml)
rE   r   itemsassertInr4   page_ptrspecificassertNotInr5   r6   )r<   rD   django_sitesitemappagesr=   r=   r>   
test_items^   s   zTestSitemapGenerator.test_itemsc                 C   x   |  d\}}|j}t }| d dd |d||D }W d    n1 s)w   Y  | d| | d| d S )NrL      c                 S      g | ]}|d  qS locationr=   .0rC   r=   r=   r>   
<listcomp>n       zFTestSitemapGenerator.test_get_urls_without_request.<locals>.<listcomp>r   http://localhost/http://localhost/hello-world/rE   schemer   assertNumQueriesget_urlsrN   r<   rD   rR   req_protocolrS   urlsr=   r=   r>   test_get_urls_without_requesth   s   z2TestSitemapGenerator.test_get_urls_without_requestc                 C      |  d\}}|j}t|}t| | d dd |d||D }W d    n1 s/w   Y  | d| | d| d S )NrL      c                 S   rX   rY   r=   r[   r=   r=   r>   r]      r^   zNTestSitemapGenerator.test_get_urls_with_request_site_cache.<locals>.<listcomp>r   r_   r`   rE   rb   r   r   find_for_requestrc   rd   rN   re   r=   r=   r>   %test_get_urls_with_request_site_cachev   s   
z:TestSitemapGenerator.test_get_urls_with_request_site_cacheT)WAGTAIL_I18N_ENABLEDc                 C   rV   )NrL      c                 S   rX   rY   r=   r[   r=   r=   r>   r]      r^   zPTestSitemapGenerator.test_get_urls_without_request_with_i18n.<locals>.<listcomp>r   r_   r`   ra   re   r=   r=   r>   'test_get_urls_without_request_with_i18n   s   z<TestSitemapGenerator.test_get_urls_without_request_with_i18nc                 C   ri   )NrL      c                 S   rX   rY   r=   r[   r=   r=   r>   r]      r^   zXTestSitemapGenerator.test_get_urls_with_request_site_cache_with_i18n.<locals>.<listcomp>r   r_   r`   rk   re   r=   r=   r>   /test_get_urls_with_request_site_cache_with_i18n   s   
zDTestSitemapGenerator.test_get_urls_with_request_site_cache_with_i18nc                 C   sh   |  d\}}|j}| jjtddddd t|}dd |d	||D }| d
| | d| d S )NrL   EventseventsT)r   r   r   r   c                 S   rX   rY   r=   r[   r=   r=   r>   r]      s    zDTestSitemapGenerator.test_get_urls_uses_specific.<locals>.<listcomp>r   zhttp://localhost/events/zhttp://localhost/events/past/)rE   rb   r0   r1   r   r   rd   rN   re   r=   r=   r>   test_get_urls_uses_specific   s   z0TestSitemapGenerator.test_get_urls_uses_specificc                 C   s   |  d\}}|j}t|}|d||}dd |D d }| |tjddddddtjjd d	d |D d }| |tjdd
ddddtjjd d S )NrL   r   c                 S       g | ]}|d  dkr|d qS )rZ   r`   lastmodr=   r[   r=   r=   r>   r]      
    zNTestSitemapGenerator.test_lastmod_uses_last_published_date.<locals>.<listcomp>r   r   r   r   c                 S   rv   )rZ   z&http://localhost/no-last-publish-date/rw   r=   r[   r=   r=   r>   r]      rx   r   )rE   rb   r   rd   rK   r2   r   r3   )r<   rD   rR   rf   rS   rg   child_page_lastmodr=   r=   r>   %test_lastmod_uses_last_published_date   s,   z:TestSitemapGenerator.test_lastmod_uses_last_published_datec                 C   s~   t j ddddddt jjd| j_| j  | d\}}|j}t|}|	d|| | 
|jt j ddddddt jjd d S )Nr      r   r   r   r   rL   )r2   r   r3   r0   r   saverE   rb   r   rd   rK   latest_lastmodr<   rD   rR   rf   rS   r=   r=   r>   test_latest_lastmod   s   

z(TestSitemapGenerator.test_latest_lastmodc                 C   sP   d | j _| j   | d\}}|j}t|}|d|| | t|d d S )NrL   r   r}   )	r0   r   r|   rE   rb   r   rd   assertFalsehasattrr~   r=   r=   r>   test_latest_lastmod_missing   s   
z0TestSitemapGenerator.test_latest_lastmod_missingc                 C   sX   t  d}d|jd< d|jd< t|}| }| | jjj| | 	| j
jj| d S )NrL   r)   r@   r*   rA   )r   r/   rB   r   rM   rN   r:   rO   rP   rQ   r4   )r<   rD   rS   rT   r=   r=   r>   test_non_default_site   s   

z*TestSitemapGenerator.test_non_default_siteN)__name__
__module____qualname__r?   rE   rK   rU   rh   rm   r   rp   rr   ru   rz   r   r   r   r=   r=   r=   r>   r      s     B


r   c                   @   s   e Zd Zdd ZdS )TestIndexViewc                 C   .   | j d}| |jd | |d d d S )Nz/sitemap-index.xml   Content-Typeapplication/xmlclientr/   rH   status_coder<   responser=   r=   r>   test_index_view     zTestIndexView.test_index_viewN)r   r   r   r   r=   r=   r=   r>   r     s    r   c                   @   s   e Zd Zdd Zdd ZdS )TestSitemapViewc                 C   r   )NrL   r   r   r   r   r   r=   r=   r>   test_sitemap_view  r   z!TestSitemapView.test_sitemap_viewc                 C   s^   | j ddid | jd}W d    n1 sw   Y  | |jd | |d d d S )Nappendz5django.contrib.sites.middleware.CurrentSiteMiddleware)
MIDDLEWARErL   r   r   r   )modify_settingsr   r/   rH   r   r   r=   r=   r>   .test_sitemap_view_with_current_site_middleware  s   z>TestSitemapView.test_sitemap_view_with_current_site_middlewareN)r   r   r   r   r   r=   r=   r=   r>   r     s    r   )r2   django.confr   "django.contrib.contenttypes.modelsr   django.contrib.sites.shortcutsr   django.testr   r   r   django.utilsr   wagtail.modelsr	   r
   r   wagtail.test.testapp.modelsr   r   sitemap_generatorr   r   r   r   r=   r=   r=   r>   <module>   s     x