o
    BZc5                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ G d	d
 d
eZG dd deZdS )    N)ImproperlyConfigured)RequestFactoryTestCaseoverride_settings)Request)DEFAULT_NO_REQUEST_IDlocalSession)RequestIDMiddleware)	test_viewc                   @   sf   e Zd Zdd Zdd Zdd Zdd Zed	d
dd Zdd Z	dd Z
dd Zdd Zdd ZdS )RequestIDLoggingTestCasec                 C   sB   t  | _tdjd | _g | j_zt`W d S  t	y    Y d S w )Ntestprojectr   )
r   factorylogging	getLoggerhandlershandlermessagesr   
request_idAttributeErrorself r   J/var/www/chikooza/env/lib/python3.10/site-packages/log_request_id/tests.pysetUp   s   
zRequestIDLoggingTestCase.setUpc                 C   sP   | j d}t }|| | t|d t| | |j| jj	d v  d S )N/idr   )
r   getr   process_request
assertTruehasattrr   r   r   r   r   request
middlewarer   r   r   test_id_generation   s   
z+RequestIDLoggingTestCase.test_id_generationc                 C   s   | j dd2 | jd}d|jd< t }|| | |jd t| | 	d| j
jd v  W d    d S 1 s;w   Y  d S )NREQUEST_ID_HEADERLOG_REQUEST_ID_HEADERr   some_request_idr   )settingsr   r   METAr   r   assertEqualr   r   r    r   r   r"   r   r   r   test_external_id_in_http_header"   s   

"z8RequestIDLoggingTestCase.test_external_id_in_http_headerc                 C   s.   | j d}t| | t| jjd v  d S )Nr   r   )r   r   r   r    r   r   r   r   r#   r   r   r   "test_default_no_request_id_is_used,   s   z;RequestIDLoggingTestCase.test_default_no_request_id_is_used-)NO_REQUEST_IDc                 C   s.   | j d}t| | d| jjd v  d S )Nr   z[-]r   )r   r   r   r    r   r   r.   r   r   r   test_custom_request_id_is_used1   s   z7RequestIDLoggingTestCase.test_custom_request_id_is_usedc                 C   s   | j ddd/ | jd}t }|| | t|d t| | |j| j	j
d v  W d    d S 1 s9w   Y  d S )Nr&   T)r(   $GENERATE_REQUEST_ID_IF_NOT_IN_HEADERr   r   r   )r*   r   r   r   r   r    r!   r   r   r   r   r"   r   r   r   Gtest_external_id_missing_in_http_header_should_fallback_to_generated_id7   s   
"z`RequestIDLoggingTestCase.test_external_id_missing_in_http_header_should_fallback_to_generated_idc                 C   s   G dd dt }| jdd: | jd}| |_t }|| t|}||| | 	t
| jjd | d| jjd v  W d    d S 1 sKw   Y  d S )	Nc                   @   s   e Zd ZdZdS )z=RequestIDLoggingTestCase.test_log_requests.<locals>.DummyUserfake_pkN)__name__
__module____qualname__pkr   r   r   r   	DummyUserB   s    r:   T)LOG_REQUESTSr      r5      objectr*   r   r   userr   r   r   process_responser,   lenr   r   r    r   r:   r#   r$   responser   r   r   test_log_requests@   s   
"z*RequestIDLoggingTestCase.test_log_requestsc                 C   s   G dd dt }| jddd: | jd}| |_t }|| t|}||| | 	t
| jjd | d| jjd	 v  W d    d S 1 sLw   Y  d S )
Nc                   @   s   e Zd ZdZdZdS )zCRequestIDLoggingTestCase.test_log_user_attribute.<locals>.DummyUserr5   fake_usernameN)r6   r7   r8   r9   usernamer   r   r   r   r:   Q   s    r:   TrG   )r;   LOG_USER_ATTRIBUTEr   r<   rF   r=   r>   rC   r   r   r   test_log_user_attributeO   s   
"z0RequestIDLoggingTestCase.test_log_user_attributec                 C   sp   | j dd( | jd}d|jd< t }|| t|}| |d W d    d S 1 s1w   Y  d S )Nr&   r'   r   r)   
REQUEST_ID)	r*   r   r   r+   r   r   r   assertFalse
has_headerr   r#   r$   rD   r   r   r   test_response_header_unset_   s   

"z3RequestIDLoggingTestCase.test_response_header_unsetc                 C   s~   | j ddd. | jd}d|jd< t }|| t|}||| | |	d W d    d S 1 s8w   Y  d S )Nr&   rJ   )r(   REQUEST_ID_RESPONSE_HEADERr   r)   )
r*   r   r   r+   r   r   r   rA   r    rL   rM   r   r   r   test_response_header_seth   s   

"z1RequestIDLoggingTestCase.test_response_header_setN)r6   r7   r8   r   r%   r-   r/   r   r2   r4   rE   rI   rN   rP   r   r   r   r   r      s    

		r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
RequestIDPassthroughTestCasec                 C   s   t  | _d S N)r   r   r   r   r   r   r   t   s   z"RequestIDPassthroughTestCase.setUpc                 C   s   | j ddd9 | jd}d|jd< t }|| | |jd t }t	dd}|
| | |jd d W d    d S 1 sCw   Y  d S )Nr&   OUTGOING_REQUEST_ID_HEADER)r(   rS   r   r)   r   http://nowherer*   r   r   r+   r   r   r,   r   r
   r   prepare_requestheadersr   r#   r$   sessionoutgoingr   r   r   .test_request_id_passthrough_with_custom_headerw   s   



"zKRequestIDPassthroughTestCase.test_request_id_passthrough_with_custom_headerc                 C   s   | j dd9 | jd}d|jd< t }|| | |jd t }t	dd}|
| | |jd d W d    d S 1 sBw   Y  d S )Nr&   r'   r   r)   r   rT   rU   rX   r   r   r   test_request_id_passthrough   s   



"z8RequestIDPassthroughTestCase.test_request_id_passthroughc                 C   s   dd }|  t| d S )Nc                   S   s
   t   d S rR   r	   r   r   r   r   inner   s   
zKRequestIDPassthroughTestCase.test_misconfigured_for_sessions.<locals>.inner)assertRaisesr   )r   r]   r   r   r   test_misconfigured_for_sessions   s   z<RequestIDPassthroughTestCase.test_misconfigured_for_sessionsN)r6   r7   r8   r   r[   r\   r_   r   r   r   r   rQ   s   s
    rQ   )r   django.core.exceptionsr   django.testr   r   r   requestsr   log_request_idr   r   log_request_id.sessionr
   log_request_id.middlewarer   testproject.viewsr   r   rQ   r   r   r   r   <module>   s    f