a
    Dg}'                     @   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	   Z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&    r4   ]/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/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'   r0   r$   METAr%   r   )r3   urlrequestr4   r4   r5   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)r3   actualexpectedr4   r4   r5   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.   )r3   r;   django_sitesitemappagesr4   r4   r5   
test_items^   s    zTestSitemapGenerator.test_itemsc                 C   sx   |  d\}}|j}t }| d( dd |d||D }W d    n1 sR0    Y  | d| | d| d S )NrA      c                 S   s   g | ]}|d  qS locationr4   .0r:   r4   r4   r5   
<listcomp>n   s   zFTestSitemapGenerator.test_get_urls_without_request.<locals>.<listcomp>r   http://localhost/http://localhost/hello-world/r<   schemer   assertNumQueriesget_urlsrC   r3   r;   rG   req_protocolrH   urlsr4   r4   r5   test_get_urls_without_requesth   s    $z2TestSitemapGenerator.test_get_urls_without_requestc                 C   s   |  d\}}|j}t|}t| | d( dd |d||D }W d    n1 s^0    Y  | d| | d| d S )NrA      c                 S   s   g | ]}|d  qS rL   r4   rN   r4   r4   r5   rP      s   zNTestSitemapGenerator.test_get_urls_with_request_site_cache.<locals>.<listcomp>r   rQ   rR   r<   rT   r   r   Zfind_for_requestrU   rV   rC   rW   r4   r4   r5   %test_get_urls_with_request_site_cachev   s    
$z:TestSitemapGenerator.test_get_urls_with_request_site_cacheT)ZWAGTAIL_I18N_ENABLEDc                 C   sx   |  d\}}|j}t }| d( dd |d||D }W d    n1 sR0    Y  | d| | d| d S )NrA      c                 S   s   g | ]}|d  qS rL   r4   rN   r4   r4   r5   rP      s   zPTestSitemapGenerator.test_get_urls_without_request_with_i18n.<locals>.<listcomp>r   rQ   rR   rS   rW   r4   r4   r5   'test_get_urls_without_request_with_i18n   s    $z<TestSitemapGenerator.test_get_urls_without_request_with_i18nc                 C   s   |  d\}}|j}t|}t| | d( dd |d||D }W d    n1 s^0    Y  | d| | d| d S )NrA      c                 S   s   g | ]}|d  qS rL   r4   rN   r4   r4   r5   rP      s   zXTestSitemapGenerator.test_get_urls_with_request_site_cache_with_i18n.<locals>.<listcomp>r   rQ   rR   r\   rW   r4   r4   r5   /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 )NrA   ZEventseventsT)r   r   r   r   c                 S   s   g | ]}|d  qS rL   r4   rN   r4   r4   r5   rP      s   zDTestSitemapGenerator.test_get_urls_uses_specific.<locals>.<listcomp>r   zhttp://localhost/events/zhttp://localhost/events/past/)r<   rT   r(   r)   r   r   rV   rC   rW   r4   r4   r5   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 )NrA   r   c                 S   s    g | ]}|d  dkr|d qS )rM   rR   lastmodr4   rN   r4   r4   r5   rP      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 )rM   z&http://localhost/no-last-publish-date/rd   r4   rN   r4   r4   r5   rP      s   r   )r<   rT   r   rV   r@   r*   r   r+   )r3   r;   rG   rX   rH   rY   Zchild_page_lastmodr4   r4   r5   %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   rA   )r*   r   r+   r(   r   saver<   rT   r   rV   r@   latest_lastmodr3   r;   rG   rX   rH   r4   r4   r5   test_latest_lastmod   s    

z(TestSitemapGenerator.test_latest_lastmodc                 C   sP   d | j _| j   | d\}}|j}t|}|d|| | t|d d S )NrA   r   rh   )	r(   r   rg   r<   rT   r   rV   ZassertFalsehasattrri   r4   r4   r5   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 )NrA   r"   r7   r#   r8   )r   r'   r9   r   rB   rC   r1   rD   rE   rF   r,   )r3   r;   rH   rI   r4   r4   r5   test_non_default_site   s    

z*TestSitemapGenerator.test_non_default_siteN)__name__
__module____qualname__r6   r<   r@   rJ   rZ   r]   r   r_   ra   rc   re   rj   rl   rm   r4   r4   r4   r5   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r3   responser4   r4   r5   test_index_view  s    zTestIndexView.test_index_viewN)rn   ro   rp   rz   r4   r4   r4   r5   rq     s   rq   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 )NrA   rr   rs   rt   ru   rx   r4   r4   r5   test_sitemap_view  s    z!TestSitemapView.test_sitemap_viewc                 C   s^   | j ddid | jd}W d    n1 s20    Y  | |jd | |d d d S )Nappendz5django.contrib.sites.middleware.CurrentSiteMiddleware)Z
MIDDLEWARErA   rr   rs   rt   )Zmodify_settingsrv   r'   r=   rw   rx   r4   r4   r5   .test_sitemap_view_with_current_site_middleware  s    *z>TestSitemapView.test_sitemap_view_with_current_site_middlewareN)rn   ro   rp   r|   r~   r4   r4   r4   r5   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   rq   r{   r4   r4   r4   r5   <module>   s    x