o
    <gL!                     @   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   M/var/www/html/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dZed(ddZd*ddZdd Zdd Zd+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                    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>-   s    zDAdminSeleniumTestCase.wait_for_and_switch_to_popup.<locals>.<lambda>N)r   r   	switch_to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   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.supportr0   r   presence_of_element_locatedCSS_SELECTORr
   css_selectorr   r.   ecr   r   r   wait_for1   
   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)r1   r.   r2   r0   r   text_to_be_present_in_elementr4   r
   r6   textr   r.   r7   r   r   r   wait_for_text<   s   z#AdminSeleniumTestCase.wait_for_textc                 C   r:   )zE
        Block until the value is found in the CSS selector.
        r   r-   r/   N)r1   r.   r2   r0   r   #text_to_be_present_in_element_valuer4   r<   r   r   r   wait_for_valueH   s   
z$AdminSeleniumTestCase.wait_for_valuec                 C   r,   )zS
        Block until the element described by the CSS selector is visible.
        r   r-   r/   N)r1   r.   r2   r0   r   visibility_of_element_locatedr4   r5   r   r   r   wait_until_visibleV   r9   z(AdminSeleniumTestCase.wait_until_visiblec                 C   r,   )zU
        Block until the element described by the CSS selector is invisible.
        r   r-   r/   N)r1   r.   r2   r0   r   invisibility_of_element_locatedr4   r5   r   r   r   wait_until_invisiblea   r9   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;complete)execute_scriptdriverr   r   r   r&   q   s    z7AdminSeleniumTestCase.wait_page_ready.<locals>.<lambda>N)r   )r
   r   r   r   r   r*   l   s   z%AdminSeleniumTestCase.wait_page_readyc                 c   sT    ddl m} ddlm} | j|jd}dV  | j|||d | j	|d dS )zA
        Block until a new page has loaded and is ready.
        r   r-   r/   htmlN)r   )
r1   r.   r2   r0   r   find_elementTAG_NAMEr   staleness_ofr*   )r
   r   r.   r7   old_pager   r   r   wait_page_loadedv   s   z&AdminSeleniumTestCase.wait_page_loaded/admin/c                 C   s   ddl m} | jd| j|f  | j|jd}|| | j|jd}|| td}| 	  | j|j
d|   W d   dS 1 sKw   Y  dS )	z&
        Log in to the admin.
        r   r-   z%s%susernamepasswordzLog inz//input[@value="%s"]N)r1   r.   r   getlive_server_urlrJ   NAME	send_keys_rN   XPATHclick)r
   rP   rQ   	login_urlr.   username_inputpassword_input
login_textr   r   r   admin_login   s   



"z!AdminSeleniumTestCase.admin_loginc                 C   :   ddl m} ddlm} || j|j|}|| dS )z
        Select the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   r-   SelectN)r1   r.   selenium.webdriver.support.uir`   r   rJ   r4   select_by_valuer
   selectorvaluer.   r`   selectr   r   r   select_option      z#AdminSeleniumTestCase.select_optionc                 C   r^   )z
        Deselect the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   r-   r_   N)r1   r.   ra   r`   r   rJ   r4   deselect_by_valuerc   r   r   r   deselect_option   rh   z%AdminSeleniumTestCase.deselect_optionNc                 C   s4   ddl m} |p
| j}| t||j|| dS )z
        Assert number of matches for a CSS selector.

        `root_element` allow restriction to a pre-selected node.
        r   r-   N)r1   r.   r   assertEqualr!   find_elementsr4   )r
   rd   countroot_elementr.   r   r   r   assertCountSeleniumElements   s
   
z1AdminSeleniumTestCase.assertCountSeleniumElementsc                    s   ddl m  |r'| j j}g }|D ]
}||d q| || d S |   | 	 fdd W d    d S 1 sAw   Y  d S )Nr   r-   re   c                    s   |   j S r    )rl   r4   rG   r.   options_selectorr   r   r&      s    z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>)
r1   r.   r   rl   r4   r   get_attributerk   disable_implicit_waitr   )r
   rq   valuesoptionsactual_valuesoptionr   rp   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`.
        z%s > optionNrx   r
   rd   rt   r   r   r   assertSelectOptions      z)AdminSeleniumTestCase.assertSelectOptionsc                 C   ry   )z
        Assert that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedNrz   r{   r   r   r   assertSelectedOptions   r}   z+AdminSeleniumTestCase.assertSelectedOptionsc                 C   s,   ddl m} | j|j|d|dkS )zh
        Return True if the element identified by `selector` has the CSS class
        `klass`.
        r   r-   classr'   )r1   r.   r   rJ   r4   rr   find)r
   rd   klassr.   r   r   r   has_css_class   s   z#AdminSeleniumTestCase.has_css_class)r   )r   r   )rO   r    )r   r   r   available_appsr   r+   r8   r>   r@   rB   rD   r*   r   rN   r]   rg   rj   ro   rx   r|   r~   r   r   r   r   r   r      s(    




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