3
(h                 @   sp   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 )    )StaticLiveServerTestCase)modify_settings)SeleniumTestCase)MiddlewareMixin)ugettextc               @   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|d< |S )Nzdefault-src 'self'zContent-Security-Policy )selfrequestresponser   r   >/tmp/pip-install-q3hcpn_q/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	d.ddZ
dd Zd/ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'S )0AdminSeleniumTestCasezdjango.contrib.adminzdjango.contrib.authzdjango.contrib.contenttypeszdjango.contrib.sessionszdjango.contrib.sites
   c             C   s"   ddl m} || j|j| dS )a=  
        Helper function that blocks the execution of the tests until the
        specified callback returns a value that is not falsy. This function 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                s   | j  fdd| dS )z
        Block until `num_windows` are present (usually 2, but can be
        overridden in the case of pop-ups opening other pop-ups).
        c                s   t | j kS )N)lenZwindow_handles)d)num_windowsr   r   <lambda>)   s    z6AdminSeleniumTestCase.wait_for_popup.<locals>.<lambda>N)r   )r	   r   r   r   )r   r   wait_for_popup$   s    z$AdminSeleniumTestCase.wait_for_popupc             C   s4   ddl m} ddlm} | j|j|j|f| dS )zX
        Helper function that blocks 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_for+   s
    zAdminSeleniumTestCase.wait_forc             C   s6   ddl m} ddlm} | j|j|j|f|| dS )zZ
        Helper function that blocks 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_text6   s    z#AdminSeleniumTestCase.wait_for_textc             C   s6   ddl m} ddlm} | j|j|j|f|| dS )z[
        Helper function that blocks 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_valueB   s    z$AdminSeleniumTestCase.wait_for_valuec             C   s4   ddl m} ddlm} | j|j|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_visibleN   s
    z(AdminSeleniumTestCase.wait_until_visiblec             C   s4   ddl m} ddlm} | j|j|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_invisibleY   s
    z*AdminSeleniumTestCase.wait_until_invisiblec             C   s4   ddl m} y| jd W n |k
r.   Y nX dS )z7
        Block until page has started to load.
        r   )TimeoutExceptionbodyN)selenium.common.exceptionsr-   r'   )r	   r-   r   r   r   wait_page_loadedd   s
    z&AdminSeleniumTestCase.wait_page_loaded/admin/c             C   sj   | j jd| j|f  | j jd}|j| | j jd}|j| td}| j jd| j  | j  dS )z8
        Helper function to log into the admin.
        z%s%susernamepasswordzLog inz//input[@value="%s"]N)	r   getZlive_server_urlZfind_element_by_nameZ	send_keys_Zfind_element_by_xpathZclickr0   )r	   r2   r3   Z	login_urlZusername_inputZpassword_inputZ
login_textr   r   r   admin_loginr   s    

z!AdminSeleniumTestCase.admin_loginc             C   s   | j jd||f S )z
        Helper function that returns the value for the CSS attribute of an
        DOM element specified by the given selector. Uses the jQuery that ships
        with Django.
        z$return django.jQuery("%s").css("%s"))r   Zexecute_script)r	   selector	attributer   r   r   get_css_value   s    z#AdminSeleniumTestCase.get_css_valuec             C   sP   ddl m} | jjd| }x|D ]}|jd|kr"|S q"W |d||f dS )z
        Returns the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   )NoSuchElementExceptionz%s > optionvaluezOption "%s" not found in "%s"N)r/   r:   r   find_elements_by_css_selectorget_attribute)r	   r7   r;   r:   optionsoptionr   r   r   get_select_option   s    
z'AdminSeleniumTestCase.get_select_optionc                sj   |r@| j j }g }x|D ]}|j|jd qW | j|| n&| j  | j fdd W d Q R X d S )Nr;   c                s   t | j dkS )Nr   )r   r<   )Zdriver)options_selectorr   r   r      s    z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)r   r<   r   r=   ZassertEqualZdisable_implicit_waitr   )r	   rA   valuesr>   Zactual_valuesr?   r   )rA   r   _assertOptionsValues   s    

z*AdminSeleniumTestCase._assertOptionsValuesc             C   s   | j d| | dS )z|
        Asserts that the <SELECT> widget identified by `selector` has the
        options with the given `values`.
        z%s > optionN)rC   )r	   r7   rB   r   r   r   assertSelectOptions   s    z)AdminSeleniumTestCase.assertSelectOptionsc             C   s   | j d| | dS )z
        Asserts that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedN)rC   )r	   r7   rB   r   r   r   assertSelectedOptions   s    z+AdminSeleniumTestCase.assertSelectedOptionsc             C   s   | j j|jdj|dkS )zi
        Returns True if the element identified by `selector` has the CSS class
        `klass`.
        class   )r   Zfind_element_by_css_selectorr=   find)r	   r7   klassr   r   r   has_css_class   s    z#AdminSeleniumTestCase.has_css_classN)r   )r   r   )r   )r   )r   )r   )r   )r1   )r   r   r   Zavailable_appsr   r   r'   r)   r*   r+   r,   r0   r6   r9   r@   rC   rD   rE   rK   r   r   r   r   r      s(   


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