UNPKG

soajs.multitenant

Version:
2,116 lines (1,944 loc) 241 kB
/** * @license * Copyright SOAJS All Rights Reserved. * * Use of this source code is governed by an Apache license that can be * found in the LICENSE file at the root of this repository */ "use strict"; const helper = require("../../helper.js"); const BL = helper.requireModule('bl/tenant.js'); const assert = require('assert'); const nock = require("nock"); describe("Unit test for: BL - tenant", () => { let soajs = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 457: "Unable to find application", 460: "Unable to find product", 461: "Unable to find package", 462: "You are not allowed to remove the tenant you are currently logged in with", 463: "Invalid product code or package code provided", 466: "You are not allowed to remove the product you are currently logged in with", 467: "Package already exists", 468: "Product already exists", 470: "Unable to update product", 471: "Unable to update tenant", 472: "Unable to get the tenant application", 473: "Unable to get the tenant application key", 500: "You cannot modify or delete a locked record", 501: "Environment record not found!", 601: "Model not found", 602: "Model error: ", }, "console": { "product": "DSBRD" }, }, tenant: { id: "5c0e74ba9acc3c5a84a51259", main: { id: "5d8387fd1873f9079b863da0" }, application: { product: "TPROD", package: "TPROD_TEST", } }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; describe("Testing list tenants", () => { afterEach((done) => { BL.modelObj = null; done(); }); it("Success - List tenants - empty object", (done) => { BL.modelObj = { listTenants: (nullObject, cb) => { return cb(null, {"items": []}); } }; BL.list(soajs, {}, (err, records) => { assert.ok(records); assert(Array.isArray(records.items)); done(); }); }); it("Fails - List tenants - null data", (done) => { BL.modelObj = { listTenants: (nullObject, cb) => { return cb(true, null); } }; BL.list(soajs, null, (err, records) => { assert.ok(err); assert.equal(records, null); assert.deepEqual(err, {code: 400, msg: soajs.config.errors[400]}); done(); }); }); it("Fails - List tenants - listTenants error", (done) => { BL.modelObj = { listTenants: (nullObject, cb) => { return cb(true, null); } }; BL.list(soajs, {}, (err, records) => { assert.ok(err); assert.equal(records, null); assert.deepEqual(err.code, 602); done(); }); }); it("Success - List tenants - empty object - client tenant", (done) => { let soajsClient = { config: {}, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.listTenants = (nullObject, cb) => { return cb(null, {"items": []}); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; BL.list(soajsClient, {}, (err, records) => { assert.ok(records); assert(Array.isArray(records.items)); done(); }); }); it("Fails - List tenants - error - client tenant", (done) => { let soajsClient = { config: { "errors": { 400: "Business logic required data are missing" }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Product() { console.log("Product"); } Product.prototype.listTenants = (data, cb) => { return cb(true, null); }; Product.prototype.closeConnection = () => { }; BL.model = Product; BL.list(soajsClient, null, (err, records) => { assert.ok(err); assert.equal(records, null); assert.deepEqual(err, {code: 400, msg: soajsClient.config.errors[400]}); done(); }); }); }); describe("Testing list console tenants", () => { afterEach((done) => { BL.modelObj = null; done(); }); it("Success - List console tenants - empty object", (done) => { BL.modelObj = { listConsoleTenants: (nullObject, cb) => { return cb(null, []); } }; BL.listConsole(soajs, {}, (err, records) => { assert.ok(records); assert(Array.isArray(records)); done(); }); }); it("Fails - List console tenants - null data", (done) => { BL.modelObj = { listConsoleTenants: (nullObject, cb) => { return cb(true, null); } }; BL.listConsole(soajs, null, (err, records) => { assert.ok(err); assert.equal(records, null); assert.deepEqual(err, {code: 400, msg: soajs.config.errors[400]}); done(); }); }); it("Fails - List console tenants - listTenants error", (done) => { BL.modelObj = { listConsoleTenants: (nullObject, cb) => { return cb(true, null); } }; BL.listConsole(soajs, {}, (err, records) => { assert.ok(err); assert.equal(records, null); assert.deepEqual(err.code, 602); done(); }); }); it("Success - List console tenants - empty object - client tenant", (done) => { let soajsClient = { config: {}, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.listConsoleTenants = (nullObject, cb) => { return cb(null, []); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; BL.listConsole(soajsClient, {}, (err, records) => { assert.ok(records); assert(Array.isArray(records)); done(); }); }); }); describe("Testing Get tenant", () => { afterEach((done) => { BL.modelObj = null; done(); }); it("Success - Get tenant - code", (done) => { let inputMask = { code: "test" }; BL.modelObj = { getTenant: (inputMask, cb) => { return cb(null, { "code": "test", "name": "test tenant", "description": "this is a description for test tenant", }); } }; BL.get(soajs, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "test tenant"); done(); }); }); it("Success - Get tenant - no code no id", (done) => { let inputMask = {}; BL.modelObj = { getTenant: (inputMask, cb) => { return cb(null, { "id": "5c0e74ba9acc3c5a84a51259", "code": "test", "name": "test tenant", "description": "this is a description for test tenant", }); } }; BL.get(soajs, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "test tenant"); done(); }); }); it("Success - Get tenant - id", (done) => { let inputMask = { id: "testid" }; BL.modelObj = { getTenant: (inputMask, cb) => { return cb(null, { "_id": "testid", "name": "test tenant", "description": "this is a description for test tenant", }); } }; BL.get(soajs, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "test tenant"); done(); }); }); it("Fails - Get tenant - null data", (done) => { BL.modelObj = { getTenant: (nullObject, cb) => { return cb(null, null); } }; BL.get(soajs, null, (err) => { assert.ok(err); assert.equal(err.code, 400); done(); }); }); it("Success - Get tenant - code - client tenant", (done) => { let soajsClient = { tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.getTenant = (data, cb) => { return cb(null, { "code": "test", "name": "test tenant", "description": "this is a description for test tenant", }); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; let inputMask = { code: "test" }; BL.get(soajsClient, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "test tenant"); done(); }); }); it("Success - Get tenant - id - client tenant", (done) => { let soajsClient = { config: { "errors": { 400: "Business logic required data are missing" }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.getTenant = (inputMask, cb) => { return cb(null, { "_id": "testid", "name": "test tenant", "description": "this is a description for test tenant", }); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; let inputMask = { id: "testid" }; BL.get(soajsClient, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "test tenant"); done(); }); }); it("Fail - Get tenant - null data - client tenant", (done) => { let soajsClient = { config: { "errors": { 400: "Business logic required data are missing" }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.getTenant = (data, cb) => { return cb(true, null); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; BL.get(soajsClient, null, (err) => { assert.ok(err); assert.deepEqual(err.code, 400); done(); }); }); it("Fail - Get tenant - null record - client tenant", (done) => { let soajsClient = { config: { "errors": { 450: "Unable to find tenant", 601: "Model not found", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.getTenant = (data, cb) => { return cb(null, null); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; BL.get(soajsClient, {id: "notfound"}, (err) => { assert.ok(err); assert.deepEqual(err.code, 450); done(); }); }); it("Fail - Get tenant - mongo error when getting Tenant", (done) => { let soajsClient = { config: { "errors": { 450: "Unable to find tenant", 601: "Model not found", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } } }; function Tenant() { console.log(""); } Tenant.prototype.getTenant = (data, cb) => { return cb(true, null); }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; BL.get(soajsClient, {id: "found"}, (err) => { assert.deepEqual(err.code, 602); assert.ok(err); done(); }); }); }); describe("Testing Get console tenant", () => { afterEach((done) => { BL.modelObj = null; done(); }); it("Success - Get tenant - code", (done) => { let inputMask = { code: "DBTN" }; BL.modelObj = { getTenant: (inputMask, cb) => { return cb(null, { "code": "DBTN", "name": "DBTN Tenant", "description": "this is a description for DBTN tenant", }); } }; BL.get(soajs, inputMask, (err, record) => { assert.ok(record); assert.deepEqual(record.name, "DBTN Tenant"); done(); }); }); }); describe("Testing Add tenant", () => { afterEach((done) => { BL.modelObj = null; nock.cleanAll(); done(); }); it("Success - add tenant - only", (done) => { let inputMask = { "name": "tenant only name", "code": "twr2", "description": "3221", "type": "product", "profile": {}, "tag": "tag" }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "1231231231" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.model = Tenant; BL.localConfig = { "tenant": { "generatedCodeLength": 5, "character": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "expDateTTL": 86400000 }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; BL.add(soajsClient, inputMask, {}, (err, record) => { assert.ok(record); done(); }); }); it("success - add tenant - with application no internal key", (done) => { let inputMask = { "name": "tenant only name", "code": "twr2", "description": "3221", "type": "client", "mainTenant": "1231231231", "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6" } }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "1231231231" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.model = Tenant; BL.localConfig = { "tenant": { "generatedCodeLength": 5, "character": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "expDateTTL": 86400000 }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; BL.add(soajsClient, inputMask, {}, (err, record) => { assert.ok(record); done(); }); }); it("success - add tenant - with application with internal key", (done) => { let inputMask = { "name": "tenant only name", "code": "twr2", "description": "3221", "type": "client", "mainTenant": "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": {} } }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "1231231231" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.model = Tenant; BL.localConfig = { "tenant": { "generatedCodeLength": 5, "character": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "expDateTTL": 86400000 }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; let soajs = { core: { key: { generateExternalKey: (key, opt, opt1, opt2, cb) => { return cb(null, "2313131312312"); } } }, provision: { generateInternalKey: (cb) => { return cb(null, "232423423423432"); } } }; BL.add(soajsClient, inputMask, soajs, (err, record) => { assert.ok(record); done(); }); }); it("success - add tenant - with application with internal key and extkey", (done) => { let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "mainTenant": "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": { "extKey": { "label": "ttestkeylabel", "env": "KUBE" } } } }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, []); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock.cleanAll(); nock('http://www.example.com') .get('/registry/key?env=kube') .reply(200, { "result": true, "data": { algorithm: "123", password: "123" } }); BL.model = Tenant; BL.localConfig = { "tenant": { "generatedCodeLength": 5, "character": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "expDateTTL": 86400000 }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; let soajs = { core: { key: { generateExternalKey: (key, opt, opt1, opt2, cb) => { return cb(null, "2313131312312"); } } }, provision: { generateInternalKey: (cb) => { return cb(null, "232423423423432"); } } }; BL.add(soajsClient, inputMask, soajs, (err, record) => { assert.ok(record); done(); }); }); it("success - add tenant - with application with internal key and extkey with initial fail", (done) => { let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "mainTenant": "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": { "extKey": { "label": "ttestkeylabel", "env": "KUBE" } } } }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "1231231231" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; let fail = true; Tenant.prototype.addTenant = (data, cb) => { if (fail) { fail = false; return cb({message: "MongoDB Error: E11000 duplicate key error collection: local_core_provision.tenants index: code_1 dup key: { : \"twr2\" }"}); } else { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "random", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); } }; nock('http://www.example.com') .persist() .get('/registry/key?env=kube') .reply(200, { "result": true, "data": { algorithm: "123", password: "123" } }); BL.model = Tenant; BL.localConfig = { "tenant": { "generatedCodeLength": 5, "character": "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "expDateTTL": 86400000 }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; let soajs = { core: { key: { generateExternalKey: (key, opt, opt1, opt2, cb) => { return cb(null, "2313131312312"); } } }, provision: { generateInternalKey: (cb) => { return cb(null, "232423423423432"); } } }; BL.add(soajsClient, inputMask, soajs, (err, record) => { assert.ok(record); done(); }); }); it("Success - add tenant - client no maintenant", (done) => { let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "profile": {}, "tag": "tag" }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {}, id: "5c0e74ba9acc3c5a84a51259", main: { id: "5d8387fd1873f9079b863da0" }, }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "5d8387fd1873f9079b863da0" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.model = Tenant; BL.localConfig = { "tenant": { "id": "5c0e74ba9acc3c5a84a51259", "main": { "id": "5d8387fd1873f9079b863da0" }, }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; BL.add(soajsClient, inputMask, {}, (err, record) => { assert.ok(record); done(); }); }); it("Success - add tenant - product no maintenant", (done) => { let inputMask = { "name": "tenant only name", "description": "3221", "type": "product", "profile": {}, "tag": "tag" }; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }, tenant: { type: "client", dbConfig: {}, id: "5c0e74ba9acc3c5a84a51259", main: { id: "5d8387fd1873f9079b863da0" }, }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; function Tenant() { console.log(""); } Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(null, { code: "mainTenant", _id: "1231231231" }); }; Tenant.prototype.listAllTenants = (data, cb) => { return cb(null, [{ code: "mainTenant", _id: "5c0e74ba9acc3c5a84a51259" }]); }; Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.addTenant = (data, cb) => { return cb(null, { "_id": "5d823afc89ace01605cd0e14", "type": "product", "code": "twr2", "name": "tenant only name", "description": "3221", "oauth": { "secret": "this is a secret", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 1, "type": 2, "loginMode": "urac" } }); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.model = Tenant; BL.localConfig = { "tenant": { "id": "5c0e74ba9acc3c5a84a51259", "main": { "id": "5d8387fd1873f9079b863da0" }, }, "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 466: "You are not allowed to remove the product you are currently logged in with.", 467: "Package already exists", 468: "Product already exists.", 470: "Unable to update product.", 500: "You cannot modify or delete a locked record.", 501: "Environment record not found!", 601: "Model not found.", 602: "Model error: " }, }; BL.add(soajsClient, inputMask, {}, (err, record) => { assert.ok(record); done(); }); }); it("Fails - add tenant - empty data", (done) => { BL.modelObj = {}; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); BL.add(soajs, null, {}, (err) => { assert.ok(err); assert.deepEqual(err, { code: 400, msg: soajs.config.errors[400] }); done(); }); }); it("Fails - add tenant - tenant check error", (done) => { BL.modelObj = {}; function Tenant() { console.log(""); } Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.countTenants = (data, cb) => { return cb(true, 0); }; nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "mainTenant": "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": { "extKey": { "label": "ttestkeylabel", "env": "KUBE" } } } }; Tenant.prototype.closeConnection = () => { }; BL.model = Tenant; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 462: "You are not allowed to remove the tenant you are currently logged in with", 466: "You are not allowed to remove the product you are currently logged in with", 467: "Package already exists", 468: "Product already exists", 470: "Unable to update product", 471: "Unable to update tenant", 500: "You cannot modify or delete a locked record", 501: "Environment record not found!", 601: "Model not found", 602: "Model error: ", }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; BL.add(soajsClient, inputMask, {}, (err) => { assert.ok(err); assert.deepEqual(err.code, 602); done(); }); }); it("Fails - add tenant - tenant already exist ", (done) => { BL.modelObj = {}; let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "mainTenant": "1231231231", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": { "extKey": { "label": "ttestkeylabel", "env": "KUBE" } } } }; function Tenant() { console.log(""); } nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.countTenants = (data, cb) => { return cb(null, 1); }; BL.model = Tenant; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 462: "You are not allowed to remove the tenant you are currently logged in with", 466: "You are not allowed to remove the product you are currently logged in with", 467: "Package already exists", 468: "Product already exists", 470: "Unable to update product", 471: "Unable to update tenant", 500: "You cannot modify or delete a locked record", 501: "Environment record not found!", 601: "Model not found", 602: "Model error: ", }, }, tenant: { type: "client", dbConfig: {} }, log: { error: () => { console.log(); } }, awareness: { connect: (service, version, cb) => { return cb({ headers: {}, host: "www.example.com" }); } } }; BL.add(soajsClient, inputMask, {}, (err) => { assert.ok(err); assert.deepEqual(err.code, 451); done(); }); }); it("Fails - add tenant - error getting tenant", (done) => { BL.modelObj = {}; let inputMask = { "name": "tenant only name", "description": "3221", "type": "client", "mainTenant": "12313", "oauth": { "secret": "this is a secret test", "redirectURI": "http://domain.com", "grants": [ "password", "refresh_token" ], "disabled": 0, "type": 1, "loginMode": "ouath" }, "application": { "productCode": "tyrv", "packageCode": "sdfw", "description": "123", "_TTL": "6", "appKey": { "extKey": { "label": "ttestkeylabel", "env": "KUBE" } } } }; function Tenant() { console.log(""); } nock('http://www.example.com') .get('/registry/key') .query({ env: 'kube', }) .reply(200, { "result": true, "data": { key: { algorithm: "123", password: "123" } } }); Tenant.prototype.closeConnection = () => { }; Tenant.prototype.generateId = () => { return "idgenerated"; }; Tenant.prototype.countTenants = (data, cb) => { return cb(null, 0); }; Tenant.prototype.getTenant = (data, cb) => { return cb(true, 0); }; BL.model = Tenant; let soajsClient = { config: { "errors": { 400: "Business logic required data are missing", 450: "Unable to find tenant", 451: "Tenant already exists", 452: "Main Tenant id is required!", 453: "Main Tenant is not found!", 454: "Unable to add tenant application", 455: "Unable to add a new key to the tenant application", 456: "Unable to add the tenant application ext Key", 460: "Unable to find product", 461: "Unable to find package", 462: "You are not allowed to remove the tenant you are currently logged in with", 466: "You are not allowed to remove the product you are currently logged in with", 467: "Package already exists", 468: "Product already exists", 470: "Unable to update product", 471: "Unable to update tenant", 500: "You cannot modify or delete a locked record", 501: "Environment record not found!", 601: "Model not found", 602: "Model error: ",