o
    ^i                  
   @   sr  d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlZddlmZmZ dZd	gZd
d eD Zeejddej dZejdededeeef ddfddZ ejdededeeef ddfddZ!ej"dddej#de	eeef ddf fddZ$dd Z%	d!dedede&de
e' fdd Z(dS )"z@Integration tests of authorization running under jupyter-server.    N)	GeneratorOptionalTuple)	HTTPErrorURLError)urlopen   )KNOWN_SERVERSextra_node_rootsz	127.0.0.1z/lsp/statusc                 C   s   g | ]}d | qS )z/lsp/ws/ ).0lsr   r   V/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyter_lsp/tests/test_auth.py
<listcomp>   s    r   JLSP_TEST_SUBPROCESS_PREFIXz["z", "-m"]routea_server_url_and_tokenreturnc              
   C   s   |\}}t ||  t ||  d| d}|dusJ d|  d}z
t|d W n tjjyA } z|}W Y d}~nd}~ww |rNJ d|  d| dS )zBVerify a REST route only provides access to an authenticated user.z?token=   Nzno response received from utf-8zthe response for z was not JSON: )verify_responsejsonloadsdecodedecoderJSONDecodeError)r   r   base_urltokenraw_bodydecode_errorerrr   r   r   test_auth_rest   s   
r!   c                 C   s   t |d |  dS )zFVerify a WebSocket does not provide access to an unauthenticated user.r   N)r   )r   r   r   r   r   test_auth_websocket.   s   r"   module)scopetmp_path_factoryc                 c   s   t t }t }| d}| d}|d }|jjdd dddd}||d}i t }|d	|i|d
}	|jt	
|	dd g tdd| d}
td|
 ttj}|jt |t |t |jd tj|
t ||tjd}dt d| }d}d}|s|rz	t| d}W n ty   td| ddd |d8 }td Y nw |s|s|std||fV  ztd t| d| g d W n ty   td |  |d  |  |  Y nw |  |jd!usJ d"d!S )#z+Start a temporary, isolated jupyter server.root_dirhomezetc/jupyter/jupyter_config.jsonT)parentsF)jupyter_lsp
jupyterlab	nbclassic)jpserver_extensionsr   r   )	ServerAppIdentityProviderLanguageServerManagerr   )encodingjupyter_serverz--port=z--no-browserzserver args)HOMEUSERPROFILEJUPYTER_CONFIG_DIR)cwdenvstdinzhttp://:   z/favicon.ico[z
 / 20] ...)flushr   zthe server did not startzshutting down with API...z/api/shutdown?token=)datazshutting down the hard way...s   y
Nz%jupyter-server probably still running)struuiduuid4get_unused_portmktempparentmkdirr
   
write_textr   dumpsSUBPROCESS_PREFIXprintdictosenvironupdate
subprocessPopenPIPE	LOCALHOSTr   r   timesleepRuntimeError	terminatecommunicatewaitkill
returncode)r%   r   portr&   r'   server_conf
extensionsapplsmconfig_dataargsr6   procurlretriesokr   r   r   r   4   sf   







c                  C   s@   t  t jt j} | tdf | d |  d }|   |S )zGet an unused port by trying to listen to any random port.

    Probably could introduce race conditions if inside a tight loop.
    r   r   )socketAF_INETSOCK_STREAMbindrO   listengetsocknameclose)sockrX   r   r   r   r@   n   s   
r@     r   expect_codec              
   C   s   d}d}|  | }zt |}| }| }W n ty. } z
| }W Y d}~nd}~ww ||ks@J d| d| d| |S )z2Verify that a response returns the expected error.NzHTTP z (not expected z) for )r   getcodereadr   )r   r   rl   bodycoder`   resr    r   r   r   r   {   s   "r   )rk   ))__doc__r   rI   rc   rL   sysrP   r>   typingr   r   r   urllib.errorr   r   urllib.requestr   pytestconftestr	   r
   rO   REST_ROUTES	WS_ROUTESr   rJ   get
executablerF   markparametrizer=   r!   r"   fixtureTempPathFactoryr   r@   intbytesr   r   r   r   r   <module>   sP      
9