B
    à%Q[(  ã               @   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   )ÚselfZldap_connection© r   ú[C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6i93dh7p\ldap3\ldap3\strategy\mockAsync.pyr   e   s    zMockAsyncStrategy.__init__c             C   s˜  |\}}}}g }t ƒ }|dk�r„|  ||¡\}}	d|	d< �x8|D �].}
t|
| jjj| jjj| jjƒ}d|d< | jj�rfxd|d D ]X}t	|ƒ}||d kr€|t
ttfkr€tƒ |d |< tƒ |d |< ttƒr€ttd|| ƒ q€W | jj�sfg }x>|d D ]2}t	|ƒ}d| ¡ krô| d	¡\}}}| |¡ qôW x:|D ]2}ttƒ�rLttd
|| ƒ |d |= |d |= �q0W | |¡ q@W t|	ƒ}d|d< |||f| j|< |S )NZsearchRequestZsearchResDoneÚtypeZsearchResEntryÚ
attributesZ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>)ÚdictZmock_searchr
   Ú
connectionÚserverZschemaZcustom_formatterZcheck_namesZempty_attributesÚstrr   r   r   Úlistr   r   r   Z
auto_rangeÚlowerÚ	partitionÚappendr	   Ú
_responses)r   ÚpayloadÚ
message_idÚmessage_typeÚrequestÚcontrolsZasync_responseZasync_resultÚ	responsesÚresultÚentryÚresponseZattribute_typeZattribute_nameZattrs_to_removeZ	orig_attrÚ_r   r   r   Úpost_send_searchj   sJ    





z"MockAsyncStrategy.post_send_searchc             C   s˜  |\}}}}g }d }|dkr6t |  ||¡ƒ}d|d< næ|dkrFd | _nÖ|dkrPnÌ|dkrrt|  ||¡ƒ}d|d< nª|dkr”t|  ||¡ƒ}d	|d< nˆ|d
kr¶t|  ||¡ƒ}d|d< nf|dkrØt	|  
||¡ƒ}d|d< nD|dkrút|  ||¡ƒ}d|d< n"|dk�rt|  ||¡ƒ}d|d< | |¡ | jj�r„|�r„|d tk�r„ttƒ�r^ttd|| jƒ t|d |d |d |d |d d�‚|||f| j|< |S )NZbindRequestZbindResponser   ZunbindRequestZabandonRequestZ
delRequestZdelResponseZ
addRequestZaddResponseZcompareRequestZcompareResponseZmodDNRequestZmodDNResponseZmodifyRequestZmodifyResponseZextendedReqZextendedRespr0   zoperation result <%s> for <%s>ÚdescriptionÚdnÚmessage)r0   r5   r6   r7   Úresponse_type)r   Z	mock_bindZboundr   Zmock_deleter   Zmock_addr   Zmock_comparer   Zmock_modify_dnr   Zmock_modifyr   Zmock_extendedr(   r"   Úraise_exceptionsr   r   r   r   r   r)   )r   r*   r+   r,   r-   r.   r/   r0   r   r   r   Úpost_send_single_response”   sF    








&z+MockAsyncStrategy.post_send_single_responseNFc             C   s–   || j kr| j  |¡\}}}ntdƒ‚| jjr||r||d tkr|ttƒrVttd|| jƒ t	|d |d |d |d |d d�‚|rŠ|||fS ||fS d S )	Nz#message id not in outstanding queuer0   zoperation result <%s> for <%s>r5   r6   r7   r   )r0   r5   r6   r7   r8   )
r)   Úpopr   r"   r9   r   r   r   r   r   )r   r+   ÚtimeoutÚget_requestr-   r0   r2   r   r   r   Úget_responseº   s    
&
zMockAsyncStrategy.get_response)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r4   r:   r>   r   r   r   r   r   `   s
   *&r   N)%rB   Ú r   r   r   ZmockBaser   Zasynchronousr   Zoperation.searchr	   r
   Zcore.resultsr   Z	utils.logr   r   r   r   Zcore.exceptionsr   r   Zoperation.bindr   Zoperation.deleter   Zoperation.addr   Zoperation.comparer   Zoperation.modifyDnr   Zoperation.modifyr   Zoperation.extendedr   r   r   r   r   r   Ú<module>   s    8