LDAP Server Mock

6.0.1 · active · verified Tue Apr 21

ldap-server-mock provides a streamlined, in-memory mock LDAP server designed for testing and development purposes. It is built upon the `ldapjs` library and simplifies the process of simulating LDAP authentication without the overhead of a full-fledged LDAP instance. The package's current stable version is 6.0.1, released in November 2022, with a release cadence that addresses breaking changes and feature enhancements (e.g., v6.0.0 and v6.0.1 in November 2022, v5.0.0 in February 2022). It specifically focuses on basic user search and retrieval for authentication, not full SASL authentication or complex LDAP operations. Key differentiators include its ease of setup via API or command-line using simple JSON configuration files for users and server settings, making it an ideal choice for unit and integration testing where minimal LDAP functionality is required.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically start and stop an `LdapServerMock` instance, configure its users and server settings, and optionally enable TLS with dummy certificates. It defines two mock users and sets up a server on port 3004, then starts and stops it after a short delay.

import * as fs from 'node:fs/promises';
import { LdapServerMock } from 'ldap-server-mock';

async function main() {
  // Define your mock LDAP users
  const ldapUsers = [
    {
      dn: 'cn=testuser,dc=example,dc=com',
      attributes: {
        objectClass: 'person',
        cn: 'testuser',
        uid: 'testuser',
        mail: 'testuser@example.com'
      }
    },
    {
      dn: 'cn=anotheruser,dc=example,dc=com',
      attributes: {
        objectClass: 'person',
        cn: 'anotheruser',
        uid: 'anotheruser',
        mail: 'anotheruser@example.com'
      }
    }
  ];

  // Configure the LDAP server mock
  const serverConfiguration = {
    port: 3004,
    searchBase: 'dc=example,dc=com'
  };

  // For a runnable example, we'll use dummy certificate buffers.
  // In a real scenario, you'd load these from files as shown in the README.
  const certificatePublicKey = Buffer.from('-----BEGIN CERTIFICATE-----\n... (your cert content) ...\n-----END CERTIFICATE-----\n');
  const certificatePrivateKey = Buffer.from('-----BEGIN PRIVATE KEY-----\n... (your key content) ...\n-----END PRIVATE KEY-----\n');

  // Create an instance of the mock server
  const ldapServer = new LdapServerMock(
    ldapUsers,
    serverConfiguration,
    certificatePublicKey,
    certificatePrivateKey
  );

  try {
    console.log('Starting LDAP mock server...');
    await ldapServer.start();
    console.log(`LDAP mock server started on port ${serverConfiguration.port}`);

    // Simulate some work or keep the server running for tests
    // For a quick example, we'll stop it after a short delay
    await new Promise(resolve => setTimeout(resolve, 2000));

    console.log('Stopping LDAP mock server...');
    await ldapServer.stop();
    console.log('LDAP mock server stopped.');
  } catch (error) {
    console.error('Failed to run LDAP mock server:', error);
  }
}

main();

view raw JSON →