o
    uvXj…  ã                   @   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   úW/var/www/html/myproject/venv/lib/python3.10/site-packages/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)Ú
MIDDLEWAREc                   @   sœ   e Zd Zg 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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 )'ÚAdminSeleniumTestCase)z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)Úselenium.webdriver.support.waitr   ÚseleniumÚ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)ÚlenÚ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   ó4   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   Úpresence_of_element_locatedÚCSS_SELECTOR©r	   Úcss_selectorr   r(   Úecr   r   r   Úwait_for+   ó   þzAdminSeleniumTestCase.wait_forc                 C   ó6   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   Útext_to_be_present_in_elementr.   ©r	   r0   Útextr   r(   r1   r   r   r   Úwait_for_text6   ó   
ÿýz#AdminSeleniumTestCase.wait_for_textc                 C   r4   )zE
        Block until the value is found in the CSS selector.
        r   r'   r)   N)r+   r(   r,   r*   r   Ú#text_to_be_present_in_element_valuer.   r6   r   r   r   Úwait_for_valueB   r9   z$AdminSeleniumTestCase.wait_for_valuec                 C   r&   )zS
        Block until the element described by the CSS selector is visible.
        r   r'   r)   N)r+   r(   r,   r*   r   Úvisibility_of_element_locatedr.   r/   r   r   r   Úwait_until_visibleN   r3   z(AdminSeleniumTestCase.wait_until_visiblec                 C   r&   )zU
        Block until the element described by the CSS selector is invisible.
        r   r'   r)   N)r+   r(   r,   r*   r   Úinvisibility_of_element_locatedr.   r/   r   r   r   Úwait_until_invisibleY   r3   z*AdminSeleniumTestCase.wait_until_invisiblec                 C   s2   ddl m} z|  d¡ W dS  |y   Y dS w )z7
        Block until page has started to load.
        r   )ÚTimeoutExceptionÚbodyN)Úselenium.common.exceptionsr@   r2   )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   ÚgetÚlive_server_urlÚfind_element_by_nameÚ	send_keysÚ_Úfind_element_by_xpathÚclickrC   )r	   rE   rF   Ú	login_urlÚusername_inputÚpassword_inputÚ
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   Úexecute_script)r	   ÚselectorÚ	attributer   r   r   Úget_css_value€   s   
ÿz#AdminSeleniumTestCase.get_css_valuec                 C   sL   ddl m} | j d| ¡}|D ]}| d¡|kr|  S q|d||f ƒ‚)zŠ
        Return the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   )ÚNoSuchElementExceptionú%s > optionÚvaluezOption "%s" not found in "%s")rB   rW   r   Úfind_elements_by_css_selectorÚget_attribute)r	   rT   rY   rW   ÚoptionsÚoptionr   r   r   Úget_select_optionˆ   s   ÿz'AdminSeleniumTestCase.get_select_optionc                    s~   |r| j  ˆ ¡}g }|D ]
}| | d¡¡ q|  ||¡ d S |  ¡ � |  ‡ fdd„¡ W d   ƒ d S 1 s8w   Y  d S )NrY   c                    s   |   ˆ ¡ S r   )rZ   )Údriver©Úoptions_selectorr   r   r$   ¡   s    z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)r   rZ   r   r[   ÚassertEqualÚdisable_implicit_waitr   )r	   ra   Úvaluesr\   Úactual_valuesr]   r   r`   r   Ú_assertOptionsValues”   s   

ÿ"ÿz*AdminSeleniumTestCase._assertOptionsValuesc                 C   ó   |   d| |¡ dS )z{
        Assert that the <SELECT> widget identified by `selector` has the
        options with the given `values`.
        rX   N©rf   ©r	   rT   rd   r   r   r   ÚassertSelectOptions¤   ó   z)AdminSeleniumTestCase.assertSelectOptionsc                 C   rg   )z„
        Assert that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedNrh   ri   r   r   r   ÚassertSelectedOptions«   rk   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   Úfind_element_by_css_selectorr[   Úfind)r	   rT   Úklassr   r   r   Úhas_css_class²   s   
ÿz#AdminSeleniumTestCase.has_css_classN)r   )r   r   )rD   )r   r   r   Úavailable_appsr   r%   r2   r8   r;   r=   r?   rC   rR   rV   r^   rf   rj   rl   rr   r   r   r   r   r      s"    



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