U
    {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-worldZhelloT  r      r   tzinfo)titleslugcontentlivelast_published_atlatest_revision_created_atinstanceZUnpublishedZunpublishedF)r   r   r   r   Z	ProtectedZ	protected)pagepasswordzI have no last publish date :-(zno-last-publish-date)r   r   r   r   r   )Zis_default_site)depthzAnother sitezanother-siteZ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
   createZpage_with_no_last_publish_dater   siteother_site_homepager   clear_cache)selfr&    r5   f/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/contrib/sitemaps/tests.pysetUp   s                	            zTestSitemapGenerator.setUpc                 C   s4   t  |}| jj|jd< | jj|jd< |t|fS )N	HTTP_HOSTSERVER_PORT)r   r(   r1   r$   METAr%   r   )r4   urlrequestr5   r5   r6   get_request_and_django_siteR   s    z0TestSitemapGenerator.get_request_and_django_sitec                 C   s   t jst|}| ||S )N)r   ZUSE_TZr   Z
make_naiveassertEqual)r4   actualexpectedr5   r5   r6   assertDatesEqualX   s    
z%TestSitemapGenerator.assertDatesEqualc                 C   sX   |  d\}}t|}| }| | jjj| | | jjj| | | j	jj| d S )N/sitemap.xml)
r=   r   itemsassertInr-   page_ptrspecificassertNotInr.   r/   )r4   r<   django_sitesitemappagesr5   r5   r6   
test_items^   s    zTestSitemapGenerator.test_itemsc              	   C   sd   |  d\}}|j}t }| d dd |d||D }W 5 Q R X | d| | d| d S )NrB      c                 S   s   g | ]}|d  qS locationr5   .0r;   r5   r5   r6   
<listcomp>n   s   zFTestSitemapGenerator.test_get_urls_without_request.<locals>.<listcomp>r   http://localhost/http://localhost/hello-world/r=   schemer   assertNumQueriesget_urlsrD   r4   r<   rH   req_protocolrI   urlsr5   r5   r6   test_get_urls_without_requesth   s    z2TestSitemapGenerator.test_get_urls_without_requestc              	   C   sp   |  d\}}|j}t|}t| | d dd |d||D }W 5 Q R X | d| | d| d S )NrB      c                 S   s   g | ]}|d  qS rM   r5   rO   r5   r5   r6   rQ      s   zNTestSitemapGenerator.test_get_urls_with_request_site_cache.<locals>.<listcomp>r   rR   rS   r=   rU   r   r   Zfind_for_requestrV   rW   rD   rX   r5   r5   r6   %test_get_urls_with_request_site_cachev   s    
z:TestSitemapGenerator.test_get_urls_with_request_site_cacheT)ZWAGTAIL_I18N_ENABLEDc              	   C   sd   |  d\}}|j}t }| d dd |d||D }W 5 Q R X | d| | d| d S )NrB      c                 S   s   g | ]}|d  qS rM   r5   rO   r5   r5   r6   rQ      s   zPTestSitemapGenerator.test_get_urls_without_request_with_i18n.<locals>.<listcomp>r   rR   rS   rT   rX   r5   r5   r6   'test_get_urls_without_request_with_i18n   s    z<TestSitemapGenerator.test_get_urls_without_request_with_i18nc              	   C   sp   |  d\}}|j}t|}t| | d dd |d||D }W 5 Q R X | d| | d| d S )NrB      c                 S   s   g | ]}|d  qS rM   r5   rO   r5   r5   r6   rQ      s   zXTestSitemapGenerator.test_get_urls_with_request_site_cache_with_i18n.<locals>.<listcomp>r   rR   rS   r]   rX   r5   r5   r6   /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 )NrB   ZEventseventsT)r   r   r   r   c                 S   s   g | ]}|d  qS rM   r5   rO   r5   r5   r6   rQ      s    zDTestSitemapGenerator.test_get_urls_uses_specific.<locals>.<listcomp>r   zhttp://localhost/events/zhttp://localhost/events/past/)r=   rU   r)   r*   r   r   rW   rD   rX   r5   r5   r6   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 )NrB   r   c                 S   s    g | ]}|d  dkr|d qS )rN   rS   lastmodr5   rO   r5   r5   r6   rQ      s   zNTestSitemapGenerator.test_lastmod_uses_last_published_date.<locals>.<listcomp>r   r   r   r   c                 S   s    g | ]}|d  dkr|d qS )rN   z&http://localhost/no-last-publish-date/re   r5   rO   r5   r5   r6   rQ      s   r   )r=   rU   r   rW   rA   r+   r   r,   )r4   r<   rH   rY   rI   rZ   Zchild_page_lastmodr5   r5   r6   %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   rB   )r+   r   r,   r)   r   saver=   rU   r   rW   rA   latest_lastmodr4   r<   rH   rY   rI   r5   r5   r6   test_latest_lastmod   s$          

z(TestSitemapGenerator.test_latest_lastmodc                 C   sP   d | j _| j   | d\}}|j}t|}|d|| | t|d d S )NrB   r   ri   )	r)   r   rh   r=   rU   r   rW   ZassertFalsehasattrrj   r5   r5   r6   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 )NrB   r"   r8   r#   r9   )r   r(   r:   r   rC   rD   r2   rE   rF   rG   r-   )r4   r<   rI   rJ   r5   r5   r6   test_non_default_site   s    

z*TestSitemapGenerator.test_non_default_siteN)__name__
__module____qualname__r7   r=   rA   rK   r[   r^   r   r`   rb   rd   rf   rk   rm   rn   r5   r5   r5   r6   r      s   B


r   c                   @   s   e Zd Zdd ZdS )TestIndexViewc                 C   s.   | j d}| |jd | |d d d S )Nz/sitemap-index.xml   Content-Typeapplication/xmlclientr(   r>   status_coder4   responser5   r5   r6   test_index_view  s    zTestIndexView.test_index_viewN)ro   rp   rq   r{   r5   r5   r5   r6   rr     s   rr   c                   @   s   e Zd Zdd Zdd ZdS )TestSitemapViewc                 C   s.   | j d}| |jd | |d d d S )NrB   rs   rt   ru   rv   ry   r5   r5   r6   test_sitemap_view  s    z!TestSitemapView.test_sitemap_viewc              	   C   sJ   | j ddid | jd}W 5 Q R X | |jd | |d d d S )Nappendz5django.contrib.sites.middleware.CurrentSiteMiddleware)Z
MIDDLEWARErB   rs   rt   ru   )Zmodify_settingsrw   r(   r>   rx   ry   r5   r5   r6   .test_sitemap_view_with_current_site_middleware  s     z>TestSitemapView.test_sitemap_view_with_current_site_middlewareN)ro   rp   rq   r}   r   r5   r5   r5   r6   r|     s   r|   )r+   Zdjango.confr   Z"django.contrib.contenttypes.modelsr   Zdjango.contrib.sites.shortcutsr   Zdjango.testr   r   r   Zdjango.utilsr   Zwagtail.modelsr	   r
   r   Zwagtail.test.testapp.modelsr   r   Zsitemap_generatorr   r   rr   r|   r5   r5   r5   r6   <module>   s    x