text: "use strict";
  
  const coreModules = require("soajs.core.modules");
  const core = coreModules.core;
  const helper = require("../helper.js");
  const driver = helper.requireModule('./index.js');
  const assert = require('assert');
  
  describe("Integration test for: ldap", function () {
      let soajs = {
          "meta": core.meta,
          "tenant": {
              "code": "TES0",
              "id": "5c0e74ba9acc3c5a84a51259"
          },
          "servicesConfig": {
              "urac": {
                  "model": "mongo"
              }
          },
          "registry": {
              "tenantMetaDB": {
                  "urac": {
                      "prefix": "",
                      "cluster": "test_cluster",
                      "name": "#TENANT_NAME#_urac",
                      "servers": [
                          {
                              "host": "127.0.0.1",
                              "port": 27017
                          }
                      ],
                      "credentials": null,
                      "streaming": {
                          "batchSize": 1000
                      },
                      "URLParam": {
                          "bufferMaxEntries": 0
                      },
                      "timeConnected": 1552747598093
                  }
              }
          },
          "log": {
              "error": (msg) => {
                  console.log(msg);
              },
              "debug": (msg) => {
                  console.log(msg);
              }
          },
          "config": helper.requireModule("./config.js")
      };
  
      // initiate the server with the following configuration
      // the test cases are simulated vice versa, since the service configuration is static
      let serverConfig = {
          host: '127.0.0.1',
          port: 10389,
          baseDN: 'ou=users,ou=system',
          adminUser: 'uid=admin, ou=system',
          adminPassword: 'secret'
      };
      // wrong admin password
      let serverConfig2 = {
          host: '127.0.0.1',
          port: 10389,
          baseDN: 'ou=users,ou=system',
          adminUser: 'uid=admin, ou=system',
          adminPassword: 'secret2'
      };
      // wrong admin user
      let serverConfig3 = {
          host: '127.0.0.1',
          port: 10389,
          baseDN: 'ou=users,ou=system',
          adminUser: 'uid=admin2, ou=system',
          adminPassword: 'secret'
      };
  
      it("test - ldapLogin - with the correct credentials", function (done) {
          soajs.servicesConfig.ldapServer = {
              host: 'ldap://127.0.0.1',
              port: 10389,
              baseDN: 'ou=users,ou=system',
              adminUser: 'uid=admin, ou=system',
              adminPassword: 'secret'
          };
          let ldapServer = require('./ldapServer.js');
          ldapServer.startServer(serverConfig, function (server) {
              let data = {
                  "username": "owner",
                  "password": "password"
              };
              driver.ldapLogin(soajs, data, (error, record) => {
                  assert.equal(record.email, 'antoine@soajs.org');
                  ldapServer.killServer(server);
                  done();
              });
          });
      });
      it("test - ldapLogin - with the wrong password", function (done) {
          soajs.servicesConfig.ldapServer = {
              host: 'ldap://127.0.0.1',
              port: 10389,
              baseDN: 'ou=users,ou=system',
              adminUser: 'uid=admin, ou=system',
              adminPassword: 'secret'
          };
          let ldapServer = require('./ldapServer.js');
          ldapServer.startServer(serverConfig, function (server) {
              let data = {
                  "username": "owner",
                  "password": "passw"
              };
              driver.ldapLogin(soajs, data, (error, record) => {
                  assert.equal(error.code, '703');
                  ldapServer.killServer(server);
                  done();
              });
          });
      });
      it("test - ldapLogin - with the wrong adminPassword in provision information", function (done) {
          soajs.servicesConfig.ldapServer = {
              host: 'ldap://127.0.0.1',
              port: 10389,
              baseDN: 'ou=users,ou=system',
              adminUser: 'uid=admin, ou=system',
              adminPassword: 'secret'
          };
          let ldapServer = require('./ldapServer.js');
          ldapServer.startServer(serverConfig2, function (server) {
              let data = {
                  "username": "owner",
                  "password": "password"
              };
              driver.ldapLogin(soajs, data, (error, record) => {
                  assert.equal(error.code, '702');
                  ldapServer.killServer(server);
                  done();
              });
          });
      });
      it("test - ldapLogin - with the wrong adminUser in provision information", function (done) {
          soajs.servicesConfig.ldapServer = {
              host: 'ldap://127.0.0.1',
              port: 10389,
              baseDN: 'ou=users,ou=system',
              adminUser: 'uid=admin, ou=system',
              adminPassword: 'secret'
          };
          let ldapServer = require('./ldapServer.js');
          ldapServer.startServer(serverConfig3, function (server) {
              let data = {
                  "username": "owner",
                  "password": "password"
              };
              driver.ldapLogin(soajs, data, (error, record) => {
                  assert.equal(error.code, '701');
                  ldapServer.killServer(server);
                  done();
              });
          });
      });
      it("test - ldapLogin - with no ldap ON", function (done) {
          soajs.servicesConfig.ldapServer = {
              host: 'ldap://127.0.0.1',
              port: 10389,
              baseDN: 'ou=users,ou=system',
              adminUser: 'uid=admin, ou=system',
              adminPassword: 'secret'
          };
          let data = {
              "username": "owner",
              "password": "password"
          };
          driver.ldapLogin(soajs, data, (error, record) => {
              assert.equal(error.code, '700');
              done();
          });
      });
  });