U
    8ÄT_ª  ã                   @   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|d< |S )Nzdefault-src 'self'zContent-Security-Policy© )ÚselfÚrequestÚresponser	   r	   ú>/tmp/pip-unpacked-wheel-dv63caxw/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(   r0   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-   r2   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   Ú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   Ú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©Z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   r0   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 5 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Ú_r:   Zfind_element_by_xpathZclick)r
   r<   r=   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.uirB   r   Úfind_element_by_css_selectorZselect_by_value©r
   ÚselectorÚvaluerB   Ú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   rA   N)rC   rB   r   rD   Zdeselect_by_valuerE   r	   r	   r   Údeselect_option”   s    z%AdminSeleniumTestCase.deselect_optionc              	      sf   |r<| j  ˆ ¡}g }|D ]}| | d¡¡ q|  ||¡ n&|  ¡ � |  ‡ fdd„¡ W 5 Q R X d S )NrG   c                    s   |   ˆ ¡ S r   )Úfind_elements_by_css_selectorr8   ©Úoptions_selectorr	   r   r"   ª   r#   z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)r   rK   r   Úget_attributeÚassertEqualZdisable_implicit_waitr   )r
   rM   ÚvaluesÚoptionsZactual_valuesÚoptionr	   rL   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©rS   ©r
   rF   rP   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:checkedNrT   rU   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   rD   rN   Úfind)r
   rF   Úklassr	   r	   r   Úhas_css_class»   s    ÿÿÿz#AdminSeleniumTestCase.has_css_classN)r   )r   r   )r   )r   )r   )r   )r   )r   )r   )r;   )r   r   r   Zavailable_appsr   r&   r1   r4   r5   r6   r7   r%   r   r:   r@   rI   rJ   rS   rV   rW   r[   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   