From b293b1df07c03ebaa02d726dfc979823646f4942 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 20 Oct 2023 22:02:59 -0700 Subject: [PATCH] Embedded owner name to find element by Id payloads --- src/api-docs/schemas/config-strategy.js | 13 +++++++++++++ src/api-docs/schemas/config.js | 13 +++++++++++++ src/api-docs/schemas/domain.js | 13 +++++++++++++ src/api-docs/schemas/group-config.js | 13 +++++++++++++ src/models/config-strategy.js | 7 +++++++ src/models/config.js | 7 +++++++ src/models/domain.js | 7 +++++++ src/models/group-config.js | 7 +++++++ src/routers/config-strategy.js | 2 +- src/routers/config.js | 2 +- src/routers/domain.js | 2 +- src/routers/group-config.js | 2 +- src/services/config-strategy.js | 7 ++++++- src/services/config.js | 7 ++++++- src/services/domain.js | 7 ++++++- src/services/group-config.js | 7 ++++++- tests/config-strategy.test.js | 20 ++------------------ tests/config.test.js | 2 ++ tests/domain.test.js | 20 +++----------------- tests/group-config.test.js | 17 ++--------------- 20 files changed, 117 insertions(+), 58 deletions(-) diff --git a/src/api-docs/schemas/config-strategy.js b/src/api-docs/schemas/config-strategy.js index 77a518c..9ab0633 100644 --- a/src/api-docs/schemas/config-strategy.js +++ b/src/api-docs/schemas/config-strategy.js @@ -43,6 +43,19 @@ export const configStrategy = { type: 'uuid', description: 'The owner id of the config strategy' }, + admin: { + type: 'object', + properties: { + _id: { + type: 'uuid', + description: 'The unique identifier of the admin' + }, + name: { + type: 'string', + description: 'The name of the admin who created the config strategy' + } + } + }, createdAt: { type: 'string', description: 'The date when the config strategy was created' diff --git a/src/api-docs/schemas/config.js b/src/api-docs/schemas/config.js index dea8d27..d973d24 100644 --- a/src/api-docs/schemas/config.js +++ b/src/api-docs/schemas/config.js @@ -90,6 +90,19 @@ export const config = (components) => ({ type: 'uuid', description: 'The owner id of the config' }, + admin: { + type: 'object', + properties: { + _id: { + type: 'uuid', + description: 'The unique identifier of the admin' + }, + name: { + type: 'string', + description: 'The name of the admin who created the config' + } + } + }, disable_metrics: { type: 'object', additionalProperties: { diff --git a/src/api-docs/schemas/domain.js b/src/api-docs/schemas/domain.js index ffcd434..4704e3f 100644 --- a/src/api-docs/schemas/domain.js +++ b/src/api-docs/schemas/domain.js @@ -42,6 +42,19 @@ export const domain = { type: 'uuid', description: 'The owner id of the domain' }, + admin: { + type: 'object', + properties: { + _id: { + type: 'uuid', + description: 'The unique identifier of the admin' + }, + name: { + type: 'string', + description: 'The name of the admin who created the domain' + } + } + }, transfer: { type: 'boolean', description: 'The domain is set to be transferred' diff --git a/src/api-docs/schemas/group-config.js b/src/api-docs/schemas/group-config.js index 6c4b9b1..10cb0d0 100644 --- a/src/api-docs/schemas/group-config.js +++ b/src/api-docs/schemas/group-config.js @@ -27,6 +27,19 @@ const groupConfig = { type: 'uuid', description: 'The owner id of the config' }, + admin: { + type: 'object', + properties: { + _id: { + type: 'uuid', + description: 'The unique identifier of the admin' + }, + name: { + type: 'string', + description: 'The name of the admin who created the group config' + } + } + }, createdAt: { type: 'string', description: 'The date when the group config was created' diff --git a/src/models/config-strategy.js b/src/models/config-strategy.js index 9493dfc..978e768 100644 --- a/src/models/config-strategy.js +++ b/src/models/config-strategy.js @@ -359,6 +359,13 @@ const configStrategySchema = new mongoose.Schema({ timestamps: true }); +configStrategySchema.virtual('admin', { + ref: 'Admin', + localField: 'owner', + foreignField: '_id', + justOne: true +}); + configStrategySchema.options.toJSON = { getters: true, virtuals: true, diff --git a/src/models/config.js b/src/models/config.js index cfdd0d7..88d60cc 100644 --- a/src/models/config.js +++ b/src/models/config.js @@ -110,6 +110,13 @@ const configSchema = new mongoose.Schema({ configSchema.index({ key: 1 }); +configSchema.virtual('admin', { + ref: 'Admin', + localField: 'owner', + foreignField: '_id', + justOne: true +}); + configSchema.virtual('component_list', { ref: 'Component', localField: 'components', diff --git a/src/models/domain.js b/src/models/domain.js index 7da487b..50dd5d6 100644 --- a/src/models/domain.js +++ b/src/models/domain.js @@ -58,6 +58,13 @@ const domainSchema = new mongoose.Schema({ timestamps: true }); +domainSchema.virtual('admin', { + ref: 'Admin', + localField: 'owner', + foreignField: '_id', + justOne: true +}); + domainSchema.virtual('groupConfig', { ref: 'GroupConfig', localField: '_id', diff --git a/src/models/group-config.js b/src/models/group-config.js index c0538d8..2a12c7e 100644 --- a/src/models/group-config.js +++ b/src/models/group-config.js @@ -61,6 +61,13 @@ async function recordGroupHistory(group, modifiedField) { } } +groupConfigSchema.virtual('admin', { + ref: 'Admin', + localField: 'owner', + foreignField: '_id', + justOne: true +}); + groupConfigSchema.virtual('config', { ref: 'Config', localField: '_id', diff --git a/src/routers/config-strategy.js b/src/routers/config-strategy.js index 70ad1c6..a294f31 100644 --- a/src/routers/config-strategy.js +++ b/src/routers/config-strategy.js @@ -58,7 +58,7 @@ router.get('/configstrategy/:id', auth, [ check('id').isMongoId() ], validate, async (req, res) => { try { - let configStrategy = await Services.getStrategyById(req.params.id); + let configStrategy = await Services.getStrategyById(req.params.id, true); configStrategy = await verifyOwnership(req.admin, configStrategy, configStrategy.domain, ActionTypes.READ, RouterTypes.STRATEGY); res.send(configStrategy); diff --git a/src/routers/config.js b/src/routers/config.js index 9aa586c..8502556 100644 --- a/src/routers/config.js +++ b/src/routers/config.js @@ -69,7 +69,7 @@ router.get('/config/:id', auth, [ check('id').isMongoId() ], validate, async (req, res) => { try { - let config = await Services.getConfigById(req.params.id); + let config = await Services.getConfigById(req.params.id, true); config = await verifyOwnership(req.admin, config, config.domain, ActionTypes.READ, RouterTypes.CONFIG, true); if (req.query.resolveComponents === 'true') { diff --git a/src/routers/domain.js b/src/routers/domain.js index 7fed89d..429ec31 100644 --- a/src/routers/domain.js +++ b/src/routers/domain.js @@ -42,7 +42,7 @@ router.get('/domain/:id', auth, [ check('id').isMongoId() ], validate, async (req, res) => { try { - let domain = await Services.getDomainById(req.params.id); + let domain = await Services.getDomainById(req.params.id, false, true); domain = await verifyOwnership(req.admin, domain, domain._id, ActionTypes.READ, RouterTypes.DOMAIN, true); res.send(domain); } catch (e) { diff --git a/src/routers/group-config.js b/src/routers/group-config.js index 37ac62e..3fc6ef4 100644 --- a/src/routers/group-config.js +++ b/src/routers/group-config.js @@ -51,7 +51,7 @@ router.get('/groupconfig/:id', auth, [ check('id').isMongoId() ], validate, async (req, res) => { try { - let groupconfig = await Services.getGroupConfigById(req.params.id); + let groupconfig = await Services.getGroupConfigById(req.params.id, false, true); groupconfig = await verifyOwnership(req.admin, groupconfig, groupconfig.domain, ActionTypes.READ, RouterTypes.GROUP, true); res.send(groupconfig); diff --git a/src/services/config-strategy.js b/src/services/config-strategy.js index 012f0de..6248745 100644 --- a/src/services/config-strategy.js +++ b/src/services/config-strategy.js @@ -26,8 +26,13 @@ function validateValues(values) { } } -export async function getStrategyById(id) { +export async function getStrategyById(id, populateAdmin = false) { let strategy = await ConfigStrategy.findById(id).exec(); + + if (strategy && populateAdmin) { + await strategy.populate({ path: 'admin', select: 'name' }); + } + return response(strategy, 'Strategy not found'); } diff --git a/src/services/config.js b/src/services/config.js index 39379c2..3e884cc 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -17,8 +17,13 @@ async function verifyAddComponentInput(configId, admin) { return verifyOwnership(admin, config, config.domain, ActionTypes.UPDATE, RouterTypes.CONFIG); } -export async function getConfigById(id) { +export async function getConfigById(id, populateAdmin = false) { let config = await Config.findById(id).exec(); + + if (config && populateAdmin) { + await config.populate({ path: 'admin', select: 'name' }); + } + return response(config, 'Config not found'); } diff --git a/src/services/domain.js b/src/services/domain.js index e629ed2..9373030 100644 --- a/src/services/domain.js +++ b/src/services/domain.js @@ -22,8 +22,13 @@ export async function removeDomainStatus(domain, environmentName) { } } -export async function getDomainById(id, lean = false) { +export async function getDomainById(id, lean = false, populateAdmin = false) { let domain = await Domain.findById(id, null, { lean }).exec(); + + if (!lean && domain && populateAdmin) { + await domain.populate({ path: 'admin', select: 'name' }); + } + return response(domain, 'Domain not found'); } diff --git a/src/services/group-config.js b/src/services/group-config.js index 8d6d8d2..0091373 100644 --- a/src/services/group-config.js +++ b/src/services/group-config.js @@ -13,8 +13,13 @@ async function verifyGroupInput(groupId, admin) { return verifyOwnership(admin, groupconfig, groupconfig.domain, ActionTypes.UPDATE, RouterTypes.GROUP); } -export async function getGroupConfigById(id, lean = false) { +export async function getGroupConfigById(id, lean = false, populateAdmin = false) { let group = await GroupConfig.findById(id, null, { lean }).exec(); + + if (!lean && group && populateAdmin) { + await group.populate({ path: 'admin', select: 'name' }); + } + return response(group, 'Group Config not found'); } diff --git a/tests/config-strategy.test.js b/tests/config-strategy.test.js index 9f82c02..f7ebf97 100644 --- a/tests/config-strategy.test.js +++ b/tests/config-strategy.test.js @@ -10,6 +10,7 @@ import { EnvType } from '../src/models/environment'; import { ConfigStrategy, StrategiesType, OperationsType, strategyRequirements } from '../src/models/config-strategy'; import { setupDatabase, + adminMasterAccount, adminMasterAccountId, adminMasterAccountToken, domainId, @@ -384,24 +385,7 @@ describe('Testing reading strategies #2', () => { expect(response.body.operation).toEqual(configStrategyDocument.operation); expect(String(response.body.owner)).toEqual(String(configStrategyDocument.owner)); expect(response.body.activated[EnvType.DEFAULT]).toEqual(configStrategyDocument.activated.get(EnvType.DEFAULT)); - - // Adding new Config Strategy - response = await request(app) - .post('/configstrategy/create') - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send({ - description: 'Description of my new Config Strategy', - strategy: StrategiesType.NETWORK, - operation: OperationsType.EXIST, - values: ['192.168.0.1/16'], - config: configId1, - env: EnvType.DEFAULT - }).expect(201); - - await request(app) - .get('/configstrategy/' + response.body._id) - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send().expect(200); + expect(response.body.admin.name).toEqual(adminMasterAccount.name); }); test('STRATEGY_SUITE - Should not found Config Strategy information by Id', async () => { diff --git a/tests/config.test.js b/tests/config.test.js index 8e10246..12d25ae 100644 --- a/tests/config.test.js +++ b/tests/config.test.js @@ -10,6 +10,7 @@ import { EnvType } from '../src/models/environment'; import { ConfigStrategy } from '../src/models/config-strategy'; import { setupDatabase, + adminMasterAccount, adminMasterAccountId, adminMasterAccountToken, domainId, @@ -165,6 +166,7 @@ describe('Testing fetch configuration info', () => { expect(String(response.body.group)).toEqual(String(config1Document.group)); expect(response.body.activated[EnvType.DEFAULT]).toEqual(config1Document.activated.get(EnvType.DEFAULT)); expect(response.body.components[0].name).toEqual(component1.name); + expect(response.body.admin.name).toEqual(adminMasterAccount.name); }); test('CONFIG_SUITE - Should get Config information by Id - resolveComponents as false', async () => { diff --git a/tests/domain.test.js b/tests/domain.test.js index 41b82ab..d38b17d 100644 --- a/tests/domain.test.js +++ b/tests/domain.test.js @@ -74,7 +74,7 @@ describe('Testing Domain insertion', () => { }); }); -describe('Testing fect Domain info', () => { +describe('Testing fetch Domain info', () => { beforeAll(setupDatabase); test('DOMAIN_SUITE - Should get Domain information', async () => { @@ -127,24 +127,10 @@ describe('Testing fect Domain info', () => { expect(String(response.body._id)).toEqual(String(domainDocument._id)); expect(response.body.name).toEqual(domainDocument.name); expect(String(response.body.owner)).toEqual(String(domainDocument.owner)); - expect(response.body.token).toEqual(domainDocument.token); - - // Adding new Domain - response = await request(app) - .post('/domain/create') - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send({ - name: 'New Domain 2', - description: 'Description of my new Domain 2' - }).expect(201); - - await request(app) - .get('/domain/' + response.body._id) - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send().expect(200); + expect(response.body.admin.name).toBe(adminMasterAccount.name); }); - test('DOMAIN_SUITE - Should NOT found Domain information by Id', async () => { + test('DOMAIN_SUITE - Should NOT return Domain information by Id', async () => { await request(app) .get('/domain/INVALID_ID') .set('Authorization', `Bearer ${adminMasterAccountToken}`) diff --git a/tests/group-config.test.js b/tests/group-config.test.js index 267960e..6487aca 100644 --- a/tests/group-config.test.js +++ b/tests/group-config.test.js @@ -10,6 +10,7 @@ import { EnvType } from '../src/models/environment'; import { ConfigStrategy } from '../src/models/config-strategy'; import { setupDatabase, + adminMasterAccount, adminMasterAccountId, adminMasterAccountToken, adminAccountToken, @@ -145,21 +146,7 @@ describe('Testing fetch Group info', () => { expect(response.body.name).toEqual(groupConfigDocument.name); expect(String(response.body.owner)).toEqual(String(groupConfigDocument.owner)); expect(response.body.activated[EnvType.DEFAULT]).toEqual(groupConfigDocument.activated.get(EnvType.DEFAULT)); - - // Adding new Group Config - response = await request(app) - .post('/groupconfig/create') - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send({ - name: 'New Group Config 3', - description: 'Description of my new Group Config 3', - domain: domainId - }).expect(201); - - await request(app) - .get('/groupconfig/' + response.body._id) - .set('Authorization', `Bearer ${adminMasterAccountToken}`) - .send().expect(200); + expect(response.body.admin.name).toBe(adminMasterAccount.name); }); test('GROUP_SUITE - Should NOT get Group Config information by Id', async () => {