| Current Path : /proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/test/ |
| Current File : //proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/test/indaco-module.test.js |
// DEPENDENCIES ====================================
const { expect, assert } = require('chai');
const { dotenv } = require('dotenv').config();
const mongoose = require('mongoose');
// RESOURCES =======================================
const { indacoModuleModel } = require('../models/mongoose/indaco-module');
const { subscriptionPlanModel } = require('../models/mongoose/subscription-plan');
const { partnerModel } = require('../models/mongoose/partner');
const { userModel } = require('../models/mongoose/user');
const indacoModuleService = require('../services/indaco-module');
const emptySubscriptionPlan = {
name: 'Empty test plan',
indacoModulesIds: [],
royaltyRate: 10,
};
const testModule = {
code: 'TestModule',
name: 'Test module',
moduleAccessibleFeatures: [
{
code: 'TestFeature',
name: 'Test feature'
},
{
code: 'TestFeature2',
name: 'Test feature2'
}
],
moduleCost: 10,
};
describe('indacoModule', function () {
this.timeout(10000);
// Initialization --------------------------------
before((done) => {
mongoose.connect(process.env.database_url);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function () {
console.log('We are connected to test database!');
done();
});
});
let subscriptionPlanId;
it('create empty subscription plan', (done) => {
console.log('creating an empty subscription plan');
const subscriptionPlan = new subscriptionPlanModel(emptySubscriptionPlan);
console.log('after empty subscription plan creation');
subscriptionPlan.save({ lean: true })
.then(plan => {
console.log(`empty subscription plan created with id ${plan.id}`);
subscriptionPlanId = plan.id;
})
.catch(err => {
console.log('error creating empty plan');
console.log(err);
expect(false, 'catched error in empty subscription plan creation').to.be.true;
})
.finally(done)
});
let partnerId;
it('createPartnerWithEmptyPlan', (done) => {
console.log('creating partner with empty subscription plan')
const partner = new partnerModel({
companyName: 'Test partner',
active: true,
subscriptionPlanId,
vatNumber: true
});
partner.save({ lean: true })
.then(p => {
console.log(`created partner with empty subscription plan ${p.id}`);
partnerId = p.id;
})
.catch(err => {
console.log('error creating partner with empty subscription plan');
console.log(err);
expect(false, 'catched error in partner with empty subscription plan creation').to.be.true;
})
.finally(done);
});
it('cannotPerformOperation', (done) => {
console.log('checking if the test module result inactive');
indacoModuleService.isAccessibleModuleActiveAsync(partnerId, testModule.code)
.then(active => {
console.log('checking test module active value');
expect(active).to.be.false;
})
.catch(err => {
console.log('error in checking if test module result inactive');
console.log(err);
expect(false, 'catched error in checking if the test module result inactive').to.be.true;
}).
finally(done);
});
let moduleId;
it('createTestModule', (done) => {
console.log('creating a test module');
const module = new indacoModuleModel(testModule);
module.save({ lean: true })
.then(m => {
console.log(`test module created with id ${m.id}`);
moduleId = m.id;
})
.catch(err => {
console.log('error creating test module');
console.log(err);
expect(false, 'catched error in test module creation').to.be.true;
})
.finally(done)
});
it('assingTestModuleToPlan', (done) => {
console.log('assingning test module to test plan');
subscriptionPlanModel.findById(subscriptionPlanId)
.then(p => {
console.log('fetched plan to edit');
p.indacoModulesIds = [moduleId];
console.log('assigned moduleId');
return p.save();
})
.catch(err => {
console.log('error assigning test module to test plan');
console.log(err);
expect(false, 'catched error in test module assignation').to.be.true;
})
.finally(done);
})
it('canPerformOperation', (done) => {
console.log('checking if the test module result active');
indacoModuleService.isAccessibleModuleActiveAsync(partnerId, testModule.code)
.then(active => {
console.log('checking test module active value');
expect(active).to.be.true;
})
.catch(err => {
console.log('error in checking if test module result active');
console.log(err);
expect(false, 'catched error in checking if the test module result active').to.be.true;
}).
finally(done);
});
let userId;
it('createTestUser', (done) => {
console.log('creating new test user');
const newUser = new userModel();
newUser.username = "test" + Date.now();
newUser.password = "password" + Date.now();
newUser.partnerId = partnerId;
newUser.email = "test@test.it";
newUser.userType = 'STD';
newUser.userModuleAccessibleFeatures = [['TestModule', 'TestFeature2']];
newUser.save()
.then(u => {
console.log('created new test user');
userId = u.id;
})
.catch(err => {
console.log('error in creating test user');
console.log(err);
expect(false, 'catched error in creating test user').to.be.true;
})
.finally(done);
});
it('canUseFeature', (done) => {
console.log('checking if user can use a feature');
indacoModuleService.isModuleAccessibleFeatureEnabled(userId, testModule.code, testModule.moduleAccessibleFeatures[1].code)
.then(active => {
console.log('checking accessible feature value');
expect(active).to.be.true;
}).
catch(err => {
console.log('error in checking if accessible feature result active');
console.log(err);
expect(false, 'catched error in checking if the test module result active').to.be.true;
})
.finally(done);
});
it('cannotUseFeature', (done) => {
console.log('checking if user cannot use a feature');
indacoModuleService.isModuleAccessibleFeatureEnabled(userId, testModule.code, 'NON_EXISTING_FEATURE_CODE')
.then(active => {
console.log('checking accessible feature value');
expect(active).to.be.false;
}).
catch(err => {
console.log('error in checking if accessible feature result inactive');
console.log(err);
expect(false, 'catched error in checking if the test module result inactive').to.be.true;
})
.finally(done);
});
after(async () => {
try {
console.log('removing user');
if (userId) {
await userModel.deleteOne({ _id: userId });
}
console.log('removing partner');
if (partnerId) {
await partnerModel.deleteOne({ _id: partnerId });
}
console.log('removing subscription plan');
if (subscriptionPlanId) {
await subscriptionPlanModel.deleteOne({ _id: subscriptionPlanId });
}
console.log('removing test module');
if (moduleId) {
await indacoModuleModel.deleteOne({ _id: moduleId });
}
} catch (err) {
console.log('error in after operations')
console.log(err);
} finally {
console.log('finalied after operations')
}
});
});