o
    ßý°j(  ã                   @   sî   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZmZ dd	lmZ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dl m!Z! ddl	m
Z
mZ ddl"m#Z# G dd„ deeƒZ$dS )Ú
é   )ÚALL_ATTRIBUTESÚALL_OPERATIONAL_ATTRIBUTESÚNO_ATTRIBUTESé   )ÚMockBaseStrategy)ÚAsyncStrategy)Ú#search_result_done_response_to_dictÚ$search_result_entry_response_to_dict)ÚDO_NOT_RAISE_EXCEPTIONS)ÚlogÚlog_enabledÚERRORÚPROTOCOL)ÚLDAPResponseTimeoutErrorÚLDAPOperationResult)Úbind_response_to_dict)Údelete_response_to_dict)Úadd_response_to_dict)Úcompare_response_to_dict)Úmodify_dn_response_to_dict)Úmodify_response_to_dict)Úextended_response_to_dictc                   @   s2   e Zd ZdZdd„ Zdd„ Zdd„ Zdd
d„ZdS )ÚMockAsyncStrategyz�
    This strategy create a mock LDAP server, with asynchronous access
    It can be useful to test LDAP without accessing a real Server
    c                 C   s   t  | |¡ t | ¡ d S )N)r   Ú__init__r   )ÚselfÚldap_connection© r   ú‹/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/strategy/mockAsync.pyr   e   s   zMockAsyncStrategy.__init__c                 C   sz  |\}}}}g }t ƒ }|dkr³|  ||¡\}}	d|	d< |D ]�}
t|
| jjj| jjj| jjƒ}d|d< | jjr¥|d D ],}t	|ƒ}||d vrf|t
ttfvrftƒ |d |< tƒ |d |< ttƒrfttd|| ƒ q:| jjs¥g }|d D ]}t	|ƒ}d| ¡ v rŠ| d	¡\}}}| |¡ qq|D ]}ttƒršttd
|| ƒ |d |= |d |= q�| |¡ qt|	ƒ}d|d< |||f| j|< |S )NÚsearchRequestÚsearchResDoneÚtypeÚsearchResEntryÚ
attributesÚraw_attributesz>attribute set to empty list for missing attribute <%s> in <%s>z;rangeú;ziattribute type <%s> removed in response because of same attribute returned as range by the server in <%s>)ÚdictÚmock_searchr
   Ú
connectionÚserverÚschemaÚcustom_formatterÚcheck_namesÚempty_attributesÚstrr   r   r   Úlistr   r   r   Ú
auto_rangeÚlowerÚ	partitionÚappendr	   Ú
_responses)r   ÚpayloadÚ
message_idÚmessage_typeÚrequestÚcontrolsÚasync_responseÚasync_resultÚ	responsesÚresultÚentryÚresponseÚattribute_typeÚattribute_nameÚattrs_to_removeÚ	orig_attrÚ_r   r   r   Úpost_send_searchj   sR   ÿ€
€þ
z"MockAsyncStrategy.post_send_searchc                 C   sŽ  |\}}}}g }d }|dkrt |  ||¡ƒ}d|d< nr|dkr#d | _nj|dkr(ne|dkr9t|  ||¡ƒ}d|d< nT|dkrJt|  ||¡ƒ}d	|d< nC|d
kr[t|  ||¡ƒ}d|d< n2|dkrlt	|  
||¡ƒ}d|d< n!|dkr}t|  ||¡ƒ}d|d< n|dkr�t|  ||¡ƒ}d|d< | |¡ | jjr½|r½|d tvr½ttƒrªttd|| jƒ t|d |d |d |d |d d�‚|||f| j|< |S )NÚbindRequestÚbindResponser!   ÚunbindRequestÚabandonRequestÚ
delRequestÚdelResponseÚ
addRequestÚaddResponseÚcompareRequestÚcompareResponseÚmodDNRequestÚmodDNResponseÚmodifyRequestÚmodifyResponseÚextendedReqÚextendedRespr=   úoperation result <%s> for <%s>ÚdescriptionÚdnÚmessage©r=   rW   rX   rY   Úresponse_type)r   Ú	mock_bindÚboundr   Úmock_deleter   Úmock_addr   Úmock_comparer   Úmock_modify_dnr   Úmock_modifyr   Úmock_extendedr3   r(   Úraise_exceptionsr   r   r   r   r   r4   )r   r5   r6   r7   r8   r9   r<   r=   r   r   r   Úpost_send_single_response”   sF   






&z+MockAsyncStrategy.post_send_single_responseNFc                 C   s’   || j v r| j  |¡\}}}ntdƒ‚| jjr>|r>|d tvr>ttƒr+ttd|| jƒ t	|d |d |d |d |d d�‚|rE|||fS ||fS )	Nz#message id not in outstanding queuer=   rV   rW   rX   rY   r!   rZ   )
r4   Úpopr   r(   rd   r   r   r   r   r   )r   r6   ÚtimeoutÚget_requestr8   r=   r?   r   r   r   Úget_responseº   s   
&
zMockAsyncStrategy.get_response)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rE   re   ri   r   r   r   r   r   `   s    *&r   N)%rm   Ú r   r   r   ÚmockBaser   Úasynchronousr   Úoperation.searchr	   r
   Úcore.resultsr   Ú	utils.logr   r   r   r   Úcore.exceptionsr   r   Úoperation.bindr   Úoperation.deleter   Úoperation.addr   Úoperation.comparer   Úoperation.modifyDnr   Úoperation.modifyr   Úoperation.extendedr   r   r   r   r   r   Ú<module>   s"    8