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
7 changes: 5 additions & 2 deletions src/routers/config-strategy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { check } from 'express-validator';
import { check, query } from 'express-validator';
import History from '../models/history';
import { EnvType } from '../models/environment';
import { validate, verifyInputUpdateParameters } from '../middleware/validators';
Expand Down Expand Up @@ -27,7 +27,10 @@ router.post('/configstrategy/create', auth, [
// GET /configstrategy?limit=10&skip=20
// GET /configstrategy?sortBy=createdAt:desc
// GET /configstrategy?config=ID&env=QA
router.get('/configstrategy', auth, async (req, res) => {
router.get('/configstrategy', auth, [
query('config').isMongoId(),
query('env').optional().isLength({ max: 30 })
], validate, async (req, res) => {
try {
const config = await getConfigById(req.query.config);

Expand Down
16 changes: 12 additions & 4 deletions src/routers/metric.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import { Metric } from '../models/metric';
import { getConfig } from '../services/config';
import { check } from 'express-validator';
import { check, query } from 'express-validator';
import { auth } from '../middleware/auth';
import { verifyOwnership } from '../helpers';
import { ActionTypes, RouterTypes } from '../models/permission';
Expand Down Expand Up @@ -58,15 +58,23 @@ router.get('/metric/data/', auth, [
});

router.get('/metric/statistics/', auth, [
check('domainid').isMongoId(),
check('statistics', 'add one or more options {swicthers,components,reasons,all} separed by comma').isLength({ min: 3 })
query('domainid').isMongoId(),
query('statistics', 'add one or more options {swicthers,components,reasons,all} separed by comma').isLength({ min: 3 }),
query('dateGroupPattern', 'e.g. YYYY-MM-DD HH:mm').optional().isLength({ max: 16 }),
query('key').optional().isLength({ max: 30 }),
query('environment').optional().isLength({ max: 30 }),
query('result').optional().isBoolean(),
query('component').optional().isLength({ max: 50 }),
query('group').optional().isLength({ max: 30 }),
query('dateBefore').optional().isDate(),
query('dateAfter').optional().isDate()
], validate, async (req, res) => {
try {
const switcher = buildMetricsFilter(req);
const components = buildMetricsFilter(req);
const reasons = buildMetricsFilter(req);

const dateGroupPattern = req.query.dateGroupPattern ?
const dateGroupPattern = req.query.dateGroupPattern ?
req.query.dateGroupPattern : 'YYYY-MM';

if (req.query.key) {
Expand Down
9 changes: 8 additions & 1 deletion tests/config-strategy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,14 @@ describe('Testing reading strategies #1', () => {
await request(app)
.get('/configstrategy?config=INVALID_ID_VALUE')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(500);
.send().expect(422);
});

test('STRATEGY_SUITE - Should NOT get Config Strategy information - Invalid Environment query', async () => {
await request(app)
.get(`/configstrategy?config=${configId1}&env=${'a'.repeat(50)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);
});
});

Expand Down
74 changes: 74 additions & 0 deletions tests/metric.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,80 @@ describe('Fetch overall statistics', () => {
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);
});

test('METRIC_SUITE - Should NOT return statistics - Invalid queries', async () => {
await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&environment=${'a'.repeat(31)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&component=${'a'.repeat(51)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&key=${'a'.repeat(31)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&group=${'a'.repeat(31)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&result=not_true`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&dateBefore=2020`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&dateAfter=2020`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(422);
});

test('METRIC_SUITE - Should return statistics - Valid queries', async () => {
await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&environment=${'a'.repeat(30)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&component=${'a'.repeat(50)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&key=${'a'.repeat(30)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&group=${'a'.repeat(30)}`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&result=true`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&dateBefore=2020-10-10`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

await request(app)
.get(`/metric/statistics?domainid=${domainId}&statistics=all&dateAfter=2020-10-10`)
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);
});
});

describe('Fetch metrics', () => {
Expand Down