Here is my AD class code I am using ldapjs library and trying to mock its add method but facing some warning which cause code coverage issue
const ldap = require('ldapjs');
class AD {
/**
* Create Connection Active Directory
*/
async createConnection(){
return new Promise((resolve, reject) => {
var options = {
'rejectUnauthorized': false,
};
this.ADConnnection = ldap.createClient({
url: [
process.env.LDAP_SERVER_1 + ':' + process.env.LDAP_SERVER_PORT,
],
reconnect: true,
tlsOptions: options
});
this.ADConnnection.on('error', (err) => {
reject(err)
})
this.ADConnnection.bind(this.ldapUsername, this.ldapPassword, async (err) => {
if (err) {
reject(err)
}
});
resolve(true)
});
}
/**
* Create Record in Active Directory
* @param {*} oRequest
* @param {*} oEntry
*/
async create(oRequest, oADCreateUserDT) {
const sUsername = oRequest.vpnUsername;
this.adOu = oRequest.apiUser;
let oEntry = oADCreateUserDT.ADCreateUserData();
if(oEntry.hasOwnProperty('msRADIUSFramedIPAddress')){
this.adOu = ADConstant.AD_PARAMS.OU_DED;
oADCreateUserDT.setTitle(ADConstant.AD_PARAMS.OU_DED);
oEntry = oADCreateUserDT.ADCreateUserData();
}
return new Promise(async (resolve, reject) => {
this.ADConnnection.add('cn=' + sUsername + ',ou=' + this.adOu + ',' + this.dc, oEntry, (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
await this.closeConnection()
});
}
async closeConnection() {
this.ADConnnection.unbind(err => {
if (err) {
reject(err)
}
}, () => {
this.ADConnnection.destroy();
});
}
}
module.exports = AD;
Now this is my test class (I am using jest for nodejs testing)
const AD = require("../../app/libraries/AD");
const ldap = require('ldapjs');
jest.mock('ldapjs', () => {
return jest.fn().mockImplementation(() => {
return {
createClient: jest.fn(),
add: jest.fn(() => Promise.resolve(true)),
}
})
});
const isDedicatedIP = true;
const oRequest = { uuid: "vpn00s01" }
const oEntry = { UserAccountControl: ADConstant.AD_PARAMS.AD_ACC_CONTROL_ENABLE }
const oSearch = { attributes: ["cn", "Accountexpires", "UserAccountControl"], scope: "sub", filter: "" }
describe('should test all cases of ldap function', () => {
test('should test constructor', async () => {
const oAD = new AD()
const response = oAD
expect(JSON.stringify(response)).toBe(JSON.stringify({ "dc": "dc=undefined,dc=undefined", "adOu": "purevpn", "objectclass": "user" }));
});
test('should set Adu', async () => {
const oAD = new AD()
const response = oAD.setAdOu(isDedicatedIP)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
test('should test create form ldap', async () => {
const oAD = new AD()
const response = oAD.create(oRequest, oADCreateUserDT)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
});
While running this jest test facing this issue
I don't understand how to mock my ldapjs methods. Even after adding in in mock implementation still having the same issue