B
    CVvg                 @   s|   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	 d dl
mZ G dd de	Zed	d
idG dd deeZdS )    )contextmanager)StaticLiveServerTestCase)modify_settings)SeleniumTestCase)MiddlewareMixin)gettextc               @   s   e Zd ZdZdd ZdS )CSPMiddlewarez5The admin's JavaScript should be compatible with CSP.c             C   s   d|j d< |S )Nzdefault-src 'self'zContent-Security-Policy)headers)selfrequestresponse r   >/tmp/pip-install-o3oxmrkh/Django/django/contrib/admin/tests.pyprocess_response   s    
zCSPMiddleware.process_responseN)__name__
__module____qualname____doc__r   r   r   r   r   r   
   s   r   appendz(django.contrib.admin.tests.CSPMiddleware)Z
MIDDLEWAREc               @   s   e Zd ZdddddgZd*ddZd+d
dZd,ddZd-ddZd.ddZd/ddZ	d0ddZ
d1ddZed2ddZd3ddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )4AdminSeleniumTestCasezdjango.contrib.adminzdjango.contrib.authzdjango.contrib.contenttypeszdjango.contrib.sessionszdjango.contrib.sites
   c             C   s"   ddl m} || j|| dS )a%  
        Block the execution of the tests until the specified callback returns a
        value that is not falsy. This method can be called, for example, after
        clicking a link or submitting a form. See the other public methods that
        call this function for more details.
        r   )WebDriverWaitN)Zselenium.webdriver.support.waitr   seleniumZuntil)r
   callbacktimeoutr   r   r   r   
wait_until   s    z AdminSeleniumTestCase.wait_until   c                s6   |   fdd| | jj| jjd  |   dS )z
        Block until `num_windows` are present and are ready (usually 2, but can
        be overridden in the case of pop-ups opening other pop-ups). Switch the
        current window to the new pop-up.
        c                s   t | j kS )N)lenwindow_handles)d)num_windowsr   r   <lambda>,       zDAdminSeleniumTestCase.wait_for_and_switch_to_popup.<locals>.<lambda>N)r   r   Z	switch_toZwindowr   wait_page_ready)r
   r    r   r   )r    r   wait_for_and_switch_to_popup&   s    z2AdminSeleniumTestCase.wait_for_and_switch_to_popupc             C   s4   ddl m} ddlm} | ||j|f| dS )zB
        Block until a CSS selector is found on the page.
        r   )By)expected_conditionsN)selenium.webdriver.common.byr&   selenium.webdriver.supportr'   r   Zpresence_of_element_locatedCSS_SELECTOR)r
   css_selectorr   r&   ecr   r   r   wait_for0   s
    zAdminSeleniumTestCase.wait_forc             C   s6   ddl m} ddlm} | ||j|f|| dS )zD
        Block until the text is found in the CSS selector.
        r   )r&   )r'   N)r(   r&   r)   r'   r   Ztext_to_be_present_in_elementr*   )r
   r+   textr   r&   r,   r   r   r   wait_for_text;   s    z#AdminSeleniumTestCase.wait_for_textc             C   s6   ddl m} ddlm} | ||j|f|| dS )zE
        Block until the value is found in the CSS selector.
        r   )r&   )r'   N)r(   r&   r)   r'   r   Z#text_to_be_present_in_element_valuer*   )r
   r+   r.   r   r&   r,   r   r   r   wait_for_valueG   s    z$AdminSeleniumTestCase.wait_for_valuec             C   s4   ddl m} ddlm} | ||j|f| dS )zS
        Block until the element described by the CSS selector is visible.
        r   )r&   )r'   N)r(   r&   r)   r'   r   Zvisibility_of_element_locatedr*   )r
   r+   r   r&   r,   r   r   r   wait_until_visibleS   s
    z(AdminSeleniumTestCase.wait_until_visiblec             C   s4   ddl m} ddlm} | ||j|f| dS )zU
        Block until the element described by the CSS selector is invisible.
        r   )r&   )r'   N)r(   r&   r)   r'   r   Zinvisibility_of_element_locatedr*   )r
   r+   r   r&   r,   r   r   r   wait_until_invisible^   s
    z*AdminSeleniumTestCase.wait_until_invisiblec             C   s   |  dd | dS )z1
        Block until the  page is ready.
        c             S   s   |  ddkS )Nzreturn document.readyState;Zcomplete)Zexecute_script)driverr   r   r   r!   n   r"   z7AdminSeleniumTestCase.wait_page_ready.<locals>.<lambda>N)r   )r
   r   r   r   r   r$   i   s    z%AdminSeleniumTestCase.wait_page_readyc             c   sB   ddl m} | jd}dV  | j|||d | j|d dS )zA
        Block until a new page has loaded and is ready.
        r   )r'   htmlN)r   )r)   r'   r   Zfind_element_by_tag_namer   Zstaleness_ofr$   )r
   r   r,   Zold_pager   r   r   wait_page_loadedr   s
    z&AdminSeleniumTestCase.wait_page_loaded/admin/c          	   C   sv   | j d| j|f  | j d}|| | j d}|| td}|   | j d|   W dQ R X dS )z&
        Log in to the admin.
        z%s%susernamepasswordzLog inz//input[@value="%s"]N)	r   getZlive_server_urlZfind_element_by_nameZ	send_keys_r5   Zfind_element_by_xpathZclick)r
   r7   r8   Z	login_urlZusername_inputZpassword_inputZ
login_textr   r   r   admin_login~   s    


z!AdminSeleniumTestCase.admin_loginc             C   s*   ddl m} || j|}|| dS )z
        Select the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   )SelectN)selenium.webdriver.support.uir<   r   find_element_by_css_selectorZselect_by_value)r
   selectorvaluer<   selectr   r   r   select_option   s    z#AdminSeleniumTestCase.select_optionc             C   s*   ddl m} || j|}|| dS )z
        Deselect the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   )r<   N)r=   r<   r   r>   Zdeselect_by_value)r
   r?   r@   r<   rA   r   r   r   deselect_option   s    z%AdminSeleniumTestCase.deselect_optionc          	      sj   |r@| j  }g }x|D ]}||d qW | || n&|   |  fdd W d Q R X d S )Nr@   c                s   |    S )N)find_elements_by_css_selector)r3   )options_selectorr   r   r!      r"   z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)r   rD   r   get_attributeZassertEqualZdisable_implicit_waitr   )r
   rE   valuesoptionsZactual_valuesoptionr   )rE   r   _assertOptionsValues   s    

z*AdminSeleniumTestCase._assertOptionsValuesc             C   s   |  d| | dS )z{
        Assert that the <SELECT> widget identified by `selector` has the
        options with the given `values`.
        z%s > optionN)rJ   )r
   r?   rG   r   r   r   assertSelectOptions   s    z)AdminSeleniumTestCase.assertSelectOptionsc             C   s   |  d| | dS )z
        Assert that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedN)rJ   )r
   r?   rG   r   r   r   assertSelectedOptions   s    z+AdminSeleniumTestCase.assertSelectedOptionsc             C   s   | j |d|dkS )zh
        Return True if the element identified by `selector` has the CSS class
        `klass`.
        classr#   )r   r>   rF   find)r
   r?   klassr   r   r   has_css_class   s    z#AdminSeleniumTestCase.has_css_classN)r   )r   r   )r   )r   )r   )r   )r   )r   )r   )r6   )r   r   r   Zavailable_appsr   r%   r-   r/   r0   r1   r2   r$   r   r5   r;   rB   rC   rJ   rK   rL   rP   r   r   r   r   r      s,   









	
		r   N)
contextlibr   Z"django.contrib.staticfiles.testingr   Zdjango.testr   Zdjango.test.seleniumr   Zdjango.utils.deprecationr   Zdjango.utils.translationr   r:   r   r   r   r   r   r   <module>   s   