o
    j                     @   s   d Z ddlm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 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 ddlmZmZ ddlm Z m!Z!m"Z"m#Z# G dd dee
Z$dS )
   )DO_NOT_RAISE_EXCEPTIONS   )MockBaseStrategy)ALL_ATTRIBUTESALL_OPERATIONAL_ATTRIBUTESNO_ATTRIBUTES)SyncStrategy)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)#search_result_done_response_to_dict$search_result_entry_response_to_dict)extended_response_to_dict)LDAPSocketOpenErrorLDAPOperationResult)loglog_enabledERRORPROTOCOLc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MockSyncStrategyz
    This strategy create a mock LDAP server, with synchronous 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/mockSync.pyr   /   s   zMockSyncStrategy.__init__c                 C   s  |\}}}}g | j _t | j _|dkr| ||\}}|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sg }|	d D ]}
t|
}d| v r|d\}}}|| qq|D ]}
ttrttd	|
|  |	d |
= |	d |
= q| j j|	 qt|}d
|d< || j _| j jr|r|d tvrttrttd|| j  t|d |d |d |d |d d| j jS )NsearchRequestsearchResEntrytype
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>searchResDoneresultoperation result <%s> for <%s>descriptiondnmessager&   r(   r)   r*   response_type)
connectionresponsedictr&   mock_searchr   serverschemacustom_formattercheck_namesempty_attributesstrr   r   r   listr   r   r   
auto_rangelower	partitionappendr   raise_exceptionsr   r   )r   payload
message_idmessage_typerequestcontrols	responsesr&   entryr.   attribute_typeattribute_nameattrs_to_remove	orig_attr_r   r   r   post_send_search3   sX   


&z!MockSyncStrategy.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rt| ||}d|d< || j_|| | jjr|r|d tvrttrttd|| j t|d |d |d |d |d d|S )NbindRequestbindResponser!   unbindRequestabandonRequest
delRequestdelResponse
addRequestaddResponsecompareRequestcompareResponsemodDNRequestmodDNResponsemodifyRequestmodifyResponseextendedReqextendedRespr&   r'   r(   r)   r*   r+   )r
   	mock_bindboundr   mock_deleter   mock_addr   mock_comparer   mock_modify_dnr   mock_modifyr   mock_extendedr-   r&   r;   r<   r   r   r   r   r   )r   r=   r>   r?   r@   rA   rB   r&   r   r   r   post_send_single_responsea   sF   






&z*MockSyncStrategy.post_send_single_responseN)__name__
__module____qualname____doc__r   rI   rb   r   r   r   r   r   *   s
    .r   N)%rf   core.resultsr   mockBaser    r   r   r   syncr	   operation.bindr
   operation.deleter   operation.addr   operation.comparer   operation.modifyDnr   operation.modifyr   operation.searchr   r   operation.extendedr   core.exceptionsr   r   	utils.logr   r   r   r   r   r   r   r   r   <module>   s     