Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/api-docs/schemas/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions src/api-docs/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
13 changes: 13 additions & 0 deletions src/api-docs/schemas/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 13 additions & 0 deletions src/api-docs/schemas/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions src/models/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions src/models/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions src/models/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/routers/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/routers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion src/routers/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/routers/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/services/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
20 changes: 2 additions & 18 deletions tests/config-strategy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EnvType } from '../src/models/environment';
import { ConfigStrategy } from '../src/models/config-strategy';
import {
setupDatabase,
adminMasterAccount,
adminMasterAccountId,
adminMasterAccountToken,
domainId,
Expand Down Expand Up @@ -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 () => {
Expand Down
20 changes: 3 additions & 17 deletions tests/domain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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}`)
Expand Down
17 changes: 2 additions & 15 deletions tests/group-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EnvType } from '../src/models/environment';
import { ConfigStrategy } from '../src/models/config-strategy';
import {
setupDatabase,
adminMasterAccount,
adminMasterAccountId,
adminMasterAccountToken,
adminAccountToken,
Expand Down Expand Up @@ -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 () => {
Expand Down