Skip to content

Commit

Permalink
Added validators and field query to /groupconfig route (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Apr 23, 2024
1 parent 4c3384e commit 5a5905e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"express-rate-limit": "^7.2.0",
"express-validator": "^7.0.1",
"graphql": "^16.8.1",
"graphql-http": "^1.22.0",
"graphql-http": "^1.22.1",
"graphql-tag": "^2.12.6",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
Expand All @@ -67,6 +67,9 @@
"sinon": "^17.0.1",
"supertest": "^6.3.4"
},
"overrides": {
"formidable": "^3.5.1"
},
"repository": {
"type": "git",
"url": "https://github.com/switcherapi/switcher-api"
Expand Down
11 changes: 10 additions & 1 deletion src/routers/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Services from '../services/group-config.js';
import { getHistory, deleteHistory } from '../services/history.js';
import { getDomainById } from '../services/domain.js';
import { SwitcherKeys } from '../external/switcher-api-facade.js';
import { getFields } from './common/index.js';

const router = new express.Router();

Expand All @@ -25,7 +26,11 @@ router.post('/groupconfig/create', auth, async (req, res) => {
// GET /groupconfig?domain=ID&sortBy=createdAt:desc
// GET /groupconfig?domain=ID
router.get('/groupconfig', auth, [
query('domain', 'Please, specify the \'domain\' id').isMongoId()
query('domain', 'Please, specify the \'domain\' id').isMongoId(),
query('fields').isString().optional(),
query('limit').isInt().optional(),
query('skip').isInt().optional(),
query('sortBy').isString().optional()
], validate, async (req, res) => {
try {
const domain = await getDomainById(req.query.domain);
Expand All @@ -42,6 +47,10 @@ router.get('/groupconfig', auth, [
groups = await verifyOwnership(req.admin, groups, domain._id, ActionTypes.READ, RouterTypes.GROUP, true);
await Services.populateAdmin(groups);

if (req.query.fields) {
groups = getFields(groups, req.query.fields);
}

res.send(groups);
} catch (e) {
responseException(res, e, 500);
Expand Down
12 changes: 12 additions & 0 deletions tests/group-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ describe('Testing fetch Group info', () => {
expect(response.body.length).toEqual(2);
});

test('GROUP_SUITE - Should get Group Config information - only fields (name, activated.default)', async () => {
let response = await request(app)
.get('/groupconfig?domain=' + domainId + '&fields=name,activated.default')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send().expect(200);

expect(response.body[0].name).toBeDefined();
expect(response.body[0].activated.default).toBeDefined();
expect(response.body[0].description).toBeUndefined();
expect(response.body[0].owner).toBeUndefined();
});

test('GROUP_SUITE - Should get Group Config information by Id', async () => {
let response = await request(app)
.get('/groupconfig/' + groupConfigId)
Expand Down

0 comments on commit 5a5905e

Please sign in to comment.