o
    پi(                     @   sh   d Z ddlZddlZddlZddlZdd Ze ZejZej	Z	G dd dej
Zedkr2e  dS dS )zg
Unit tests for HTTP server admin auth.

Usage:
    python3 -m pytest test/test_http_server_auth.py -v
    Nc                  C   s   t jt} t jt j| dd}t j|dddd}d}tj||}|dur.|j	dus0J tj
|}|tj|< |j	| |S )zLoad auth.py directly, avoiding importing the full sglang package.

    This keeps the test importable even if optional runtime deps (e.g. orjson/httpx)
    are not installed in the unit test environment.
    z..sglangsrtutilszauth.py_sglang_srt_utils_auth_for_testN)ospathdirname__file__abspathjoin	importlibutilspec_from_file_locationloadermodule_from_specsysmodulesexec_module)this_dir
python_dir	auth_pathmodule_namespecm r   U/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/test_http_server_auth.py_load_auth_module   s   
r   c                   @   sj   e Zd ZdedededB dedB dedB defdd	Zd
d Zdd Zdd Zdd Z	dd Z
dd ZdS )TestHttpServerAdminAuthmethodr   authorization_headerNapi_keyadmin_api_key
auth_levelc                C   s   t ||||||dS )Nr   r   r   r    r!   r"   )decide_request_auth)selfr   r   r   r    r!   r"   r   r   r   _decide(   s   
zTestHttpServerAdminAuth._decidec              
   C   sv   |  | jddd d d tjdj |  | jddd d d tjdj | jddd d d tjd}| |j | |j	d d S )NGET
/v1/modelsr#   POST/admin_optional_demo/admin_force_demo  )

assertTruer&   	AuthLevelNORMALallowedADMIN_OPTIONALADMIN_FORCEassertFalseassertEqualerror_status_coder%   dr   r   r   test_no_keys_configured;   s@   
z/TestHttpServerAdminAuth.test_no_keys_configuredc              
   C   s   |  | jddd dd tjdj | | jddddd tjdj |  | jddddd tjdj | | jddddd tjdj | jdd	ddd tjd}|  |j | |j	d
 d S )Nr'   r(   userr#   Bearer userr)   r*   zBearer wrongr+   r,   )
r3   r&   r.   r/   r0   r-   r1   r2   r4   r5   r6   r   r   r   test_api_key_only^   sl   

z)TestHttpServerAdminAuth.test_api_key_onlyc              
   C   s   |  | jdddd dtjdj |  | jddd d dtjdj |  | jdddd dtjdj | | jdddd dtjdj | jdd	dd dtjd}|  |j d S )
Nr'   r(   r:   adminr#   r)   r*   Bearer adminr+   )r-   r&   r.   r/   r0   r1   r3   r2   r6   r   r   r   test_admin_api_key_only   sj   

z/TestHttpServerAdminAuth.test_admin_api_key_onlyc              
   C   s   |  | jdddddtjdj | | jdddddtjdj | | jdd	dddtjdj |  | jdd	dddtjdj | | jdd
dddtjdj |  | jdd
dddtjdj d S )Nr'   r(   r:   r9   r<   r#   r=   r)   r*   r+   )r-   r&   r.   r/   r0   r3   r1   r2   r%   r   r   r   test_with_both_api_keys   s   



z/TestHttpServerAdminAuth.test_with_both_api_keysc              
   C   s$   |  | jddd ddtjdj d S )NOPTIONSr(   r9   r<   r#   )r-   r&   r.   r2   r0   r?   r   r   r   test_options_is_always_allowed  s   z6TestHttpServerAdminAuth.test_options_is_always_allowedc                 C   s   t d d dt dd dt d ddt dddg}g d}|D ]#}|D ]}| j| jd|d |d |d tjdjd	|d
|d q qd S )N)r    r!   r9   r<   )z/healthz/health_generatez/metricsz	/metrics/z/metrics/prometheusr'   r    r!   r#   zexpected allowed for path=z with keys=)msg)dictr-   r&   r.   r2   r0   )r%   combospaths_allowedkeysr   r   r   r   *test_health_and_metrics_are_always_allowed   s,   



zBTestHttpServerAdminAuth.test_health_and_metrics_are_always_allowed)__name__
__module____qualname__strr.   r&   r8   r;   r>   r@   rB   rH   r   r   r   r   r   '   s(    
#99Cr   __main__)__doc__importlib.utilr   r   r   unittestr   _authr$   r.   TestCaser   rI   mainr   r   r   r   <module>   s      