B
    ¢0H[‹  ã               @   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)Ú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   ú^C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\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|ƒ |¡ 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                s   |   ‡ 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|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_for+   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_text6   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_valueB   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_visibleN   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_invisibleY   s
    z*AdminSeleniumTestCase.wait_until_invisiblec             C   s4   ddl m} y|  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  d| j|f ¡ | j  d¡}| |¡ | j  d¡}| |¡ tdƒ}| j  d| ¡ ¡  |  ¡  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Ú_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  d||f ¡S )zš
        Return the value for the CSS attribute of a 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 d| ¡}x|D ]}| d¡|kr"|S q"W |d||f ƒ‚dS )zŠ
        Return 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  ˆ ¡}g }x|D ]}| | d¡¡ qW |  ||¡ n&|  ¡ � |  ‡ fdd„¡ W d Q R X d S )Nr;   c                s   t |  ˆ ¡ƒ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   |   d| |¡ dS )z{
        Assert 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   |   d| |¡ dS )z„
        Assert 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  |¡ d¡ |¡dkS )zh
        Return 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   rJ   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   