Mark Robson
Mark Robson

Reputation: 1328

ldapjs create mock users and perform search

I'm creating an auth middleware app using LDAP and I'm having difficulty getting a search to work.

I'm using Mocha for tests and I created a util.js to create a mock ldapjs server:

let server = require('../mocks/ldapServer');
const ldap = require('ldapjs');    

let ldapEntities = {
  'cn=root,dc=example,dc=com,o=example': {
    dn: 'cn=root,dc=example,dc=com',
    objectclass: 'organizationalRole',
    cn: 'Manager'
  },
  'cn=ABC,dc=example,dc=com,o=example': {
    dn: 'cn=ABC,dc=example,dc=com,o=example',
    objectclass: 'organizationalUnit',
    cn: 'ABC'
  },
  'cn=Bob Johnson,ou=ABC,dc=example,dc=com,o=example': {
    cn: 'Bob Johnson',
    dn: 'cn=Bob Johnson,ou=ABC,dc=example,dc=com,o=example',
    sn: 'Johnson',
    objectClass: 'inetOrgPerson'
  },
  'cn=Don Doddson,dc=example,dc=com,o=example': {
    cn: 'Don Doddson',
    dn: 'cn=Don Doddson,dc=example,dc=com,o=example',
    sn: 'Doddson',
    objectClass: 'inetOrgPerson',
    userPassword: 'rubbishpassword'
  }
};    

before(done => {
  server.start(1389, () => {    

    let ldapClient = ldap.createClient({
      url: process.env.DIRECTORY_SYSTEM_AGENT
    });    

    ldapClient.bind(process.env.LDAP_ADMIN, process.env.LDAP_SECRET, error => {    

      if (error) {
        return console.log('Error binding to LDAP: ' + error.message);
      }    

      //Bound to server, creating test accounts
      for (let i in ldapEntities) {
        ldapClient.add(i, ldapEntities[i], error => {
          if (error) {
            console.error(error);
          }
        });
      }    

    });
    done();
  });
});    

after(done => {
  server.stop(done);
});

I'm searching using:

let dn = 'cn=root,dc=example,dc=com,o=example';
let opts = {
  filter: '(cn=Don Doddson)',
  scope: 'sub'
}

client.search(dn, opts, (error, result) => { ...

Nothing is 'breaking', but I'm getting a SearchEntry messageID of 2 and not the details I want.

Thanks!

Upvotes: 2

Views: 1642

Answers (2)

Mark Robson
Mark Robson

Reputation: 1328

The issue is I didn't set the end point for each ldapEntities.

Upvotes: 1

Denys Kurochkin
Denys Kurochkin

Reputation: 1497

Looks like misprint in root's dn in the ldapEntities definition

dn: 'cn=root,dc=example,dc=com,o=example',

Upvotes: 1

Related Questions