-
Notifications
You must be signed in to change notification settings - Fork 14
[OGUI-1729] Implement restrictions endpoint #2952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f2631ea
feat: add configuration endpoint
piechnikk 82407cd
feat: implement getConfigurationByKey endpoint, refactor QCConfigurat…
piechnikk 1e340a8
small code refactor
piechnikk 09ad06f
change error handling in qcConfigurationController and add mocha test…
bf8c19b
implement api end-test
piechnikk 25fd74a
feat: add configuration endpoint
piechnikk c9b39c3
feat: implement getConfigurationByKey endpoint, refactor QCConfigurat…
piechnikk 07ffad4
small code refactor
piechnikk b63fed3
change error handling in qcConfigurationController and add mocha test…
1a35aba
implement api end-test
piechnikk 42d62eb
feature: add endpoint for retrieving configuration restrictions
Deaponn 1ae5ffe
chore: add test suites for the code
Deaponn 2ecef47
Merge branch 'dev' into feature/CNF/OGUI-1698/implement-configuration…
piechnikk aad8275
Merge branch 'feature/CNF/OGUI-1698/implement-configuration-endpoint'…
Deaponn 96c65ac
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn 7265a22
fix: remove public true from the endpoints
Deaponn 4cbf5cc
test: fix test on non-existing configuration file
Deaponn dc15fb0
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn 6b30fe9
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn c2c6d0c
docs: improve documentation
Deaponn c0cf971
test: fix failing test
Deaponn ff91d63
test: improve code coverage
Deaponn bcb6c45
refactor: move helpers to adapters directory
Deaponn e626a7b
test: fix tests
Deaponn 97392f3
test: add test coverage for new features
Deaponn a25c636
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn fba02a3
feature: address review comments
Deaponn 7d3fd2b
docs: minor improvements
Deaponn f441b4c
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn 79b18ea
Merge branch 'dev' into feature/CNF/OGUI-1729/restrictions-endpoint
Deaponn 32d941f
docs: improve docs
Deaponn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @license | ||
| * Copyright CERN and copyright holders of ALICE O2. This software is | ||
| * distributed under the terms of the GNU General Public License v3 (GPL | ||
| * Version 3), copied verbatim in the file "COPYING". | ||
| * | ||
| * See http://alice-o2.web.cern.ch/license for full licensing information. | ||
| * | ||
| * In applying this license CERN does not waive the privileges and immunities | ||
| * granted to it by virtue of its status as an Intergovernmental Organization | ||
| * or submit itself to any jurisdiction. | ||
| */ | ||
|
|
||
| const { LogManager } = require('@aliceo2/web-ui'); | ||
|
|
||
| /** | ||
| * QCConfigurationAdapter - Given aconfiguration object, construct Restrictions | ||
| * which is a set of restrictions based on the values contained in this configuration | ||
| */ | ||
| class QCConfigurationAdapter { | ||
| /** | ||
| * Derive type of value for every key-val pair | ||
| * of given configuration object | ||
| * @param {Object} configuration object we want to get restrictions of | ||
| * @returns {Restrictions} derived restrictions for a given configuration | ||
| */ | ||
| static computeRestrictions = (value) => { | ||
| const restrictions = {}; | ||
| if (typeof value !== 'object' || Array.isArray(value) || value === null) { | ||
| return restrictions; | ||
| } | ||
| Object.entries(value).forEach(([key, val]) => (restrictions[key] = QCConfigurationAdapter.deriveValueType(val))); | ||
| return restrictions; | ||
| }; | ||
|
|
||
| /** | ||
| * Derive the type of value and return it as a string | ||
| * possible types are 'string', 'boolean', 'number', 'array<`${NestedRestrictions}`>', `${NestedRestrictions}` | ||
| * @param {string | Array | Object} value that we want to get the Restrictions of | ||
| * @returns {string | Restrictions} derived type from the given value, could be a string, or further nested Restrictions | ||
| */ | ||
| static deriveValueType = (value) => { | ||
| // TODO OGUI-1803: implement function _combineTypes, so we can derive Type of value[0] and | ||
| // then combine it with Types of value[1], value[2] and so on to get the overall Type of values held in the array | ||
| if (Array.isArray(value)) { return 'array'; } | ||
| if (value instanceof Object) { return QCConfigurationAdapter.computeRestrictions(value); } | ||
| if (value.toLocaleLowerCase() === 'true' || value.toLocaleLowerCase() === 'false') { | ||
| return 'boolean'; | ||
| } | ||
| if (!Number.isNaN(Number(value))) { return 'number'; } | ||
| if (typeof value === 'string') { return 'string'; } | ||
| const logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/qc-conf-adapter`); | ||
| logger.warnMessage(`Unknown value encountered while calculating restrictions from a configuration: ${value}`); | ||
| return 'unknown'; | ||
| }; | ||
| } | ||
|
|
||
| module.exports = QCConfigurationAdapter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * @license | ||
| * Copyright CERN and copyright holders of ALICE O2. This software is | ||
| * distributed under the terms of the GNU General Public License v3 (GPL | ||
| * Version 3), copied verbatim in the file "COPYING". | ||
| * | ||
| * See http://alice-o2.web.cern.ch/license for full licensing information. | ||
| * | ||
| * In applying this license CERN does not waive the privileges and immunities | ||
| * granted to it by virtue of its status as an Intergovernmental Organization | ||
| * or submit itself to any jurisdiction. | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {Object.<string, RestrictionsEntry>} Restrictions | ||
| * | ||
| * Object which is a map of types. | ||
| * Keys are taken from existing configuration. | ||
| * Values are describing what is expected type of value held there. | ||
| * | ||
| * For example for the given configuration: | ||
| * ``` | ||
| * { | ||
| active: 'false', | ||
| moduleName: 'QualityControl', | ||
| extendedTaskParameters: { | ||
| default: { | ||
| default: { | ||
| verbose : 'false', | ||
| retryDelay: '10', | ||
| databaseUrl: 'https://alice-ccdb.cern.ch' | ||
| } | ||
| } | ||
| }, | ||
| dataSources: [ | ||
| { | ||
| name: 'CTP Config', | ||
| path: 'CTP/Config/Config', | ||
| active: 'true' | ||
| }, | ||
| { | ||
| name: 'CTP Scalers', | ||
| path: 'CTP/Calib/Scalers', | ||
| active: 'false' | ||
| } | ||
| ], | ||
| } | ||
| * ``` | ||
| * | ||
| * The Restrictions are: | ||
| * ``` | ||
| * { | ||
| * active: 'boolean', | ||
| * moduleName: 'string', | ||
| * extendedTaskParameters: { | ||
| * default: { | ||
| * default: { | ||
| * verbose: 'boolean', | ||
| * retryDelay: 'number', | ||
| * databaseUrl: 'string' | ||
| * } | ||
| * } | ||
| * }, | ||
| * dataSources: [ | ||
| * { | ||
| * name: 'string', | ||
| * path: 'string', | ||
| * active: 'boolean' | ||
| * } | ||
| * ] | ||
| * } | ||
| * ``` | ||
| */ | ||
|
|
||
| /** | ||
| * A value in a `Restrictions` object can be: | ||
| * - a string literal 'string', 'boolean', 'number' or 'array' | ||
| * - nested Restrictions | ||
| * | ||
| * @typedef { 'string' | 'boolean' | 'number' | Restrictions | Restrictions[] } RestrictionsEntry | ||
| */ | ||
76 changes: 76 additions & 0 deletions
76
Control/test/api/configuration/api-get-configuration-restrictions.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
|
|
||
| /** | ||
| * @license | ||
| * Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
| * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
| * All rights not expressly granted are reserved. | ||
| * | ||
| * This software is distributed under the terms of the GNU General Public | ||
| * License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
| * | ||
| * In applying this license CERN does not waive the privileges and immunities | ||
| * granted to it by virtue of its status as an Intergovernmental Organization | ||
| * or submit itself to any jurisdiction. | ||
| */ | ||
|
|
||
| const request = require('supertest'); | ||
| const { ADMIN_TEST_TOKEN, TEST_URL } = require('../generateToken.js'); | ||
|
|
||
| describe(`'API - GET - /configurations/restrictions' test suite`, () => { | ||
| it('should successfully get a Restrictions object for given configurations', async () => { | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get(`/key1?token=${ADMIN_TEST_TOKEN}`) | ||
| .expect(200); | ||
| }); | ||
|
|
||
| it('should return 400 when the key parameter is missing', async () => { | ||
| const expectedError = { | ||
| message: 'Missing configuration key', | ||
| status: 400, | ||
| title: 'Invalid Input' | ||
| }; | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get(`/%20?token=${ADMIN_TEST_TOKEN}`) | ||
| .expect(400, expectedError); | ||
| }); | ||
|
|
||
| it('should return 403 unauthorized for missing token requests', async () => { | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get('/') | ||
| .expect(403, { | ||
| error: '403 - Json Web Token Error', | ||
| message: 'You must provide a JWT token' | ||
| }); | ||
| }); | ||
|
|
||
| it('should return 403 unauthorized for invalid token requests', async () => { | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get('/?token=invalid-token') | ||
| .expect(403, { | ||
| error: '403 - Json Web Token Error', | ||
| message: 'Invalid JWT token provided' | ||
| }); | ||
| }); | ||
|
|
||
| it('should return 404 when the configuration key does not exist', async () => { | ||
| const expectedError = { | ||
| message: 'Configuration not found for key: nonexistent', | ||
| status: 404, | ||
| title: 'Not Found' | ||
| }; | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get(`/nonexistent?token=${ADMIN_TEST_TOKEN}`) | ||
| .expect(404, expectedError); | ||
| }); | ||
|
|
||
| it('should return 503 when Consul fails to respond', async () => { | ||
| const expectedError = { | ||
| message: 'Consul service unavailable', | ||
| status: 503, | ||
| title: 'Service Unavailable' | ||
| }; | ||
| await request(`${TEST_URL}/api/configurations/restrictions`) | ||
| .get(`/consul-failure?token=${ADMIN_TEST_TOKEN}`) | ||
| .expect(503, expectedError); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.