
    h                         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e	      Z ed	d
i       G d dee             Zy)    )contextmanager)StaticLiveServerTestCase)modify_settings)SeleniumTestCase)MiddlewareMixin)gettextc                       e Zd ZdZd Zy)CSPMiddlewarez5The admin's JavaScript should be compatible with CSP.c                     d|d<   |S )Nzdefault-src 'self'zContent-Security-Policy )selfrequestresponses      ]/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/contrib/admin/tests.pyprocess_responsezCSPMiddleware.process_response   s    .B*+    N)__name__
__module____qualname____doc__r   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 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>,   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.byr2   selenium.webdriver.supportr4   r"   presence_of_element_locatedCSS_SELECTORr   css_selectorr!   r2   ecs        r   wait_forzAdminSeleniumTestCase.wait_for0   s0     	4H**BOO\+JK	
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   r1   r3   N)r5   r2   r6   r4   r"   text_to_be_present_in_elementr8   r   r:   textr!   r2   r;   s         r   wait_for_textz#AdminSeleniumTestCase.wait_for_text;   s5     	4H,,,/7	
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   r1   r3   N)r5   r2   r6   r4   r"   #text_to_be_present_in_element_valuer8   r?   s         r   wait_for_valuez$AdminSeleniumTestCase.wait_for_valueG   s5     	4H22,/7	
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   r1   r3   N)r5   r2   r6   r4   r"   visibility_of_element_locatedr8   r9   s        r   wait_until_visiblez(AdminSeleniumTestCase.wait_until_visibleS   s0     	4H,,boo|-LM	
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   r1   r3   N)r5   r2   r6   r4   r"   invisibility_of_element_locatedr8   r9   s        r   wait_until_invisiblez*AdminSeleniumTestCase.wait_until_invisible^   s0     	4H../NO	
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>n   s    6001NOS]] r   N)r"   )r   r!   s     r   r.   z%AdminSeleniumTestCase.wait_page_readyi   s     	]	
r   c              #      K   ddl m} | j                  j                  d      }d | j	                  |j                  |      |       | j                  |       yw)zA
        Block until a new page has loaded and is ready.
        r   r3   htmlN)r!   )r6   r4   r   find_element_by_tag_namer"   staleness_ofr.   )r   r!   r;   old_pages       r   wait_page_loadedz&AdminSeleniumTestCase.wait_page_loadedr   sM     
 	I==99&A17CW-s   AAc                    | j                   j                  | j                  |       | j                   j                  d      }|j	                  |       | j                   j                  d      }|j	                  |       t        d      }| j                         5  | j                   j                  d|z        j                          ddd       y# 1 sw Y   yxY w)z&
        Log in to the admin.
        usernamepasswordzLog inz//input[@value="%s"]N)	r   getlive_server_urlfind_element_by_name	send_keys_rU   find_element_by_xpathclick)r   rW   rX   	login_urlusername_inputpassword_input
login_texts          r   admin_loginz!AdminSeleniumTestCase.admin_login~   s     	D$8$8)DE;;JG  *;;JG  *x[
""$ 	]MM//0F0STZZ\	] 	] 	]s   -CCc                 t    ddl m}  || j                  j                  |            }|j	                  |       y)z
        Select the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   SelectN)selenium.webdriver.support.uirg   r   find_element_by_css_selectorselect_by_valuer   selectorvaluerg   selects        r   select_optionz#AdminSeleniumTestCase.select_option   s.    
 	9BB8LMu%r   c                 t    ddl m}  || j                  j                  |            }|j	                  |       y)z
        Deselect the <OPTION> with the value `value` inside the <SELECT> widget
        identified by the CSS selector `selector`.
        r   rf   N)rh   rg   r   ri   deselect_by_valuerk   s        r   deselect_optionz%AdminSeleniumTestCase.deselect_option   s.    
 	9BB8LM  'r   c                 *   |rW| 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)Nrm   c                 (    | j                         S r%   )find_elements_by_css_selector)rO   options_selectors    r   r*   z<AdminSeleniumTestCase._assertOptionsValues.<locals>.<lambda>   s    v'K'KL\']#] r   )r   ru   r   get_attributeassertEqualdisable_implicit_waitr"   )r   rv   valuesoptionsactual_valuesoptions    `    r   _assertOptionsValuesz*AdminSeleniumTestCase._assertOptionsValues   s    mmAABRSG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   rl   rz   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                 z    | j                   j                  |      j                  d      j                  |      dk7  S )zh
        Return True if the element identified by `selector` has the CSS class
        `klass`.
        classr+   )r   ri   rw   find)r   rl   klasss      r   has_css_classz#AdminSeleniumTestCase.has_css_class   s4    
 ::8Dw'Ur: 	;r   N)
   )   r   )z/admin/)r   r   r   available_appsr"   r/   r<   rA   rD   rG   rJ   r.   r   rU   rd   ro   rr   r~   r   r   r   r   r   r   r   r      sk    N>	




	
	

 	. 	.]&( DL;r   r   N)
contextlibr   "django.contrib.staticfiles.testingr   django.testr   django.test.seleniumr   django.utils.deprecationr   django.utils.translationr   r]   r
   r   r   r   r   <module>r      sQ    % G ' 1 4 1O  X'QRSo;,.F o; To;r   