
    ho!                         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 dZ G d d	e	      Z ed
di       G d dee             Zy)    )contextmanager)StaticLiveServerTestCase)modify_settings)SeleniumTestCase)MiddlewareMixin)gettextTc                       e Zd ZdZd Zy)CSPMiddlewarez5The admin's JavaScript should be compatible with CSP.c                 $    d|j                   d<   |S )Nzdefault-src 'self'zContent-Security-Policy)headers)selfrequestresponses      W/var/www/django_project/virt/lib/python3.12/site-packages/django/contrib/admin/tests.pyprocess_responsezCSPMiddleware.process_response   s    6J23    N)__name__
__module____qualname____doc__r    r   r   r
   r
      s
    ?r   r
   appendz(django.contrib.admin.tests.CSPMiddleware)
MIDDLEWAREc                       e Zd Zg dZddZddZddZddZddZddZ	ddZ
dd	Zedd
       ZddZd Zd ZddZd Zd Zd Zd Zy)AdminSeleniumTestCase)zdjango.contrib.adminzdjango.contrib.authzdjango.contrib.contenttypeszdjango.contrib.sessionszdjango.contrib.sitesc                 T    ddl m}  || j                  |      j                  |       y)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   s       r   
wait_untilz AdminSeleniumTestCase.wait_until   s!     	BdmmW-33H=r   c                     | j                  fd|       | j                  j                  j                  | j                  j                  d          | j                          y)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                 4    t        | j                        k(  S N)lenwindow_handles)dnum_windowss    r   <lambda>zDAdminSeleniumTestCase.wait_for_and_switch_to_popup.<locals>.<lambda>0   s    #a&6&6"7;"F r   N)r#   r   	switch_towindowr(   wait_page_ready)r   r*   r"   s    ` r   wait_for_and_switch_to_popupz2AdminSeleniumTestCase.wait_for_and_switch_to_popup*   sG     	FP&&t}}'C'CB'GHr   c                 v    ddl m} ddlm} | j	                  |j                  |j                  |f      |       y)zB
        Block until a CSS selector is found on the page.
        r   Byexpected_conditionsN)selenium.webdriver.common.byr3   selenium.webdriver.supportr5   r#   presence_of_element_locatedCSS_SELECTORr   css_selectorr"   r3   ecs        r   wait_forzAdminSeleniumTestCase.wait_for4   s/     	4H**BOO\+JKW	
r   c                 x    ddl m} ddlm} | j	                  |j                  |j                  |f|      |       y)zD
        Block until the text is found in the CSS selector.
        r   r2   r4   N)r6   r3   r7   r5   r#   text_to_be_present_in_elementr9   r   r;   textr"   r3   r<   s         r   wait_for_textz#AdminSeleniumTestCase.wait_for_text?   s2     	4H,,boo|-LdS	
r   c                 x    ddl m} ddlm} | j	                  |j                  |j                  |f|      |       y)zE
        Block until the value is found in the CSS selector.
        r   r2   r4   N)r6   r3   r7   r5   r#   #text_to_be_present_in_element_valuer9   r@   s         r   wait_for_valuez$AdminSeleniumTestCase.wait_for_valueK   s7     	4H22,/ 		
r   c                 v    ddl m} ddlm} | j	                  |j                  |j                  |f      |       y)zS
        Block until the element described by the CSS selector is visible.
        r   r2   r4   N)r6   r3   r7   r5   r#   visibility_of_element_locatedr9   r:   s        r   wait_until_visiblez(AdminSeleniumTestCase.wait_until_visibleY   s/     	4H,,boo|-LMw	
r   c                 v    ddl m} ddlm} | j	                  |j                  |j                  |f      |       y)zU
        Block until the element described by the CSS selector is invisible.
        r   r2   r4   N)r6   r3   r7   r5   r#   invisibility_of_element_locatedr9   r:   s        r   wait_until_invisiblez*AdminSeleniumTestCase.wait_until_invisibled   s0     	4H../NOQX	
r   c                 *    | j                  d |       y)z1
        Block until the  page is ready.
        c                 *    | j                  d      dk(  S )Nzreturn document.readyState;complete)execute_script)drivers    r   r+   z7AdminSeleniumTestCase.wait_page_ready.<locals>.<lambda>t   s    6001NO r   N)r#   )r   r"   s     r   r/   z%AdminSeleniumTestCase.wait_page_readyo   s     		
r   c              #      K   ddl m} ddlm} | j                  j                  |j                  d      }d | j                  |j                  |      |       | j                  |       yw)zA
        Block until a new page has loaded and is ready.
        r   r2   r4   htmlN)r"   )
r6   r3   r7   r5   r   find_elementTAG_NAMEr#   staleness_ofr/   )r   r"   r3   r<   old_pages        r   wait_page_loadedz&AdminSeleniumTestCase.wait_page_loadedy   sU     
 	4H==--bkk6B17CW-s   A,A.c                    ddl m} | j                  j                  | j                  |       | j                  j                  |j                  d      }|j                  |       | j                  j                  |j                  d      }|j                  |       t        d      }| j                         5  | j                  j                  |j                  d|z        j                          ddd       y# 1 sw Y   yxY w)z&
        Log in to the admin.
        r   r2   usernamepasswordzLog inz//input[@value="%s"]N)r6   r3   r   getlive_server_urlrS   NAME	send_keys_rW   XPATHclick)r   rY   rZ   	login_urlr3   username_inputpassword_input
login_texts           r   admin_loginz!AdminSeleniumTestCase.admin_login   s     	4D$8$8)DE33BGGZH  *33BGGZH  *x[
""$ 	MM&&0:=eg	 	 	s   98C::Dc                     ddl m} ddlm}  || j                  j                  |j                  |            }|j                  |       y)z
        Select the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   r2   SelectN)r6   r3   selenium.webdriver.support.uiri   r   rS   r9   select_by_valuer   selectorvaluer3   ri   selects         r   select_optionz#AdminSeleniumTestCase.select_option   s7    
 	48222??HMNu%r   c                     ddl m} ddlm}  || j                  j                  |j                  |            }|j                  |       y)z
        Deselect the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   r2   rh   N)r6   r3   rj   ri   r   rS   r9   deselect_by_valuerl   s         r   deselect_optionz%AdminSeleniumTestCase.deselect_option   s7    
 	48222??HMN  'r   Nc                     ddl m} |xs | j                  }| j                  t	        |j                  |j                  |            |       y)z
        Assert number of matches for a CSS selector.

        `root_element` allow restriction to a pre-selected node.
        r   r2   N)r6   r3   r   assertEqualr'   find_elementsr9   )r   rm   countroot_elementr3   s        r   assertCountSeleniumElementsz1AdminSeleniumTestCase.assertCountSeleniumElements   s>     	4#4t}}**2??HEF	
r   c                 P   ddl m |rb| j                  j                  j                        }g }|D ]"  }|j                  |j                  d             $ | j                  ||       y | j                         5  | j                  fd       d d d        y # 1 sw Y   y xY w)Nr   r2   rn   c                 >    | j                  j                         S r&   )rv   r9   )rP   r3   options_selectors    r   r+   z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>   s!    v';';)9( $ r   )
r6   r3   r   rv   r9   r   get_attributeru   disable_implicit_waitr#   )r   r|   valuesoptionsactual_valuesoptionr3   s    `    @r   _assertOptionsValuesz*AdminSeleniumTestCase._assertOptionsValues   s    3mm11"//CSTGM! D$$V%9%9'%BCDV]3
 ++-   s   =BB%c                 .    | j                  d|z  |       y)z{
        Assert that the <SELECT> widget identified by `selector` has the
        options with the given `values`.
        z%s > optionNr   r   rm   r   s      r   assertSelectOptionsz)AdminSeleniumTestCase.assertSelectOptions   s    
 	!!-(":FCr   c                 .    | j                  d|z  |       y)z
        Assert that the <SELECT> widget identified by `selector` has the
        selected options with the given `values`.
        z%s > option:checkedNr   r   s      r   assertSelectedOptionsz+AdminSeleniumTestCase.assertSelectedOptions   s    
 	!!"7("BFKr   c                 ~    ddl m} | j                  j                  |j                  |      j                  d      dk(  S )zk
        Return True if the element identified by `selector` has the `disabled`
        attribute.
        r   r2   disabledtrue)r6   r3   r   rS   r9   r}   )r   rm   r3   s      r   is_disabledz!AdminSeleniumTestCase.is_disabled   s>    
 	4 MM&&rAOO 	
r   )
   )   r   )z/admin/r&   )r   r   r   available_appsr#   r0   r=   rB   rE   rH   rK   r/   r   rW   rf   rp   rs   ry   r   r   r   r   r   r   r   r   r      so    N	>	



	
	

 . ."	&	(
(DL
r   r   N)
contextlibr   "django.contrib.staticfiles.testingr   django.testr   django.test.seleniumr   django.utils.deprecationr   django.utils.translationr   r_   
__unittestr
   r   r   r   r   <module>r      sX    % G ' 1 4 1 
O  X'QRSS
,.F S
 TS
r   