ldap3
ldap3 copied to clipboard
Renames in mock LDAP connection cause the entry to stop being found by search
Hello,
I am running ldap3 2.9.1 for a work project, and I am using the mock functionality provided in order to create unit tests. One of the calls in said project attempts to move a given entry to a different superior DN.
When I do the move on a real server, the move happens correctly, and the new entry can be found at the new DN. However, when attempting the same at the mock, the mock reports that the move was completed successfully, but running search()
for the uid of the entry (same as in the server case) turns out no results, even with the most generic search base I have (dc=localdomain).
Am I doing something wrong? Any ideas welcome.
Found the problem. Leaving the cause here for any others that stumble upon it:
By default modify_dn()
is called with delete_old_dn=True
. In case the DN after the move happens to correspond to the DN before the move (therefore actually no move should take place), the new entry will first be assigned to the same key as the old one (replacing old data) and then will be deleted from the dictionary (mockBase.py:445-454
):
if dn in self.connection.server.dit:
if new_superior and new_rdn: # performs move in the DIT
new_dn = safe_dn(dn_components[0] + ',' + new_superior)
self.connection.server.dit[new_dn] = self.connection.server.dit[dn].copy()
moved_entry = self.connection.server.dit[new_dn]
if delete_old_rdn:
del self.connection.server.dit[dn]
result_code = RESULT_SUCCESS
message = 'entry moved'
moved_entry['entryDN'] = [to_raw(new_dn)]
AFAIK this doesn't happen in real servers when moving, therefore I believe it's a bug in ldap3's mock. I can take up the PR to fix it later if you want it.