Skip to content
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

#802 Update boolean query parameters to accept 0,1,true,false,yes,no … #952

Merged
merged 4 commits into from
Dec 14, 2022
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
4 changes: 2 additions & 2 deletions api-docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@
"active": {
"in": "query",
"name": "active",
"description": "The new active state for the user entry",
"description": "The new active state for the user entry. Accepted values are 1, true, or yes to indicate true, and 0, false, or no to indicate false",
"required": false,
"schema": {
"type": "boolean"
Expand Down Expand Up @@ -2515,7 +2515,7 @@
"countOnly": {
"in": "query",
"name": "count_only",
"description": "Get count of records that match query. Accepted values are 1, to indicate true, and 0, to indicate false",
"description": "Get count of records that match query. Accepted values are 1, true, or yes to indicate true, and 0, false, or no to indicate false",
"required": false,
"schema": {
"type": "boolean"
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"express": "^4.15.4",
"express-jsonschema": "^1.1.6",
"express-rate-limit": "^6.5.2",
"express-validator": "^6.12.0",
"express-validator": "^6.14.2",
"helmet": "^3.21.2",
"html-entities": "^2.3.3",
"jsonschema": "^1.4.0",
Expand Down
7 changes: 5 additions & 2 deletions src/controller/cve.controller/cve.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const logger = require('../../middleware/logger')
const errors = require('./error')
const getConstants = require('../../constants').getConstants
const error = new errors.CveControllerError()
const booleanIsTrue = require('../../utils/utils').booleanIsTrue

// Helper function to create providerMetadata object
function createProviderMetadata (orgId, shortName, updateDate) {
Expand Down Expand Up @@ -49,7 +50,8 @@ async function getFilteredCves (req, res, next) {

// if count_only is the only parameter, return estimated count of full set of records
if ((Object.keys(req.ctx.query).length === 1) &&
(req.ctx.query.count_only === '1')) {
(req.ctx.query.count_only) &&
(booleanIsTrue(req.ctx.query.count_only))) {
const payload = {}
payload.totalCount = await cveRepo.estimatedDocumentCount()
logger.info({ uuid: req.ctx.uuid, message: 'The cve records estimated count was sent to the user.' })
Expand Down Expand Up @@ -122,7 +124,8 @@ async function getFilteredCves (req, res, next) {
delete options.sort

// check whether user requested count_only for filtered set of records
if (req.ctx.query.count_only === '1') {
if ((req.ctx.query.count_only) &&
(booleanIsTrue(req.ctx.query.count_only))) {
const payload = {}
payload.totalCount = await cveRepo.countDocuments(query)
logger.info({ uuid: req.ctx.uuid, message: 'The cve records count was sent to the user.' })
Expand Down
2 changes: 1 addition & 1 deletion src/controller/cve.controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ router.get('/cve',
query(['time_modified.lt']).optional().isString().trim().escape().customSanitizer(val => { return toDate(val) }).not().isEmpty().withMessage(errorMsgs.TIMESTAMP_FORMAT),
query(['time_modified.gt']).optional().isString().trim().escape().customSanitizer(val => { return toDate(val) }).not().isEmpty().withMessage(errorMsgs.TIMESTAMP_FORMAT),
query(['state']).optional().isString().trim().escape().customSanitizer(val => { return val.toUpperCase() }).isIn(CHOICES),
query(['count_only']).optional().isBoolean(),
query(['count_only']).optional().isBoolean({ loose: true }),
query(['assigner_short_name']).optional().isString().trim().escape().notEmpty().isLength({ min: CONSTANTS.MIN_SHORTNAME_LENGTH, max: CONSTANTS.MAX_SHORTNAME_LENGTH }),
query(['assigner']).optional().isString().trim().escape().notEmpty(),
parseError,
Expand Down
2 changes: 1 addition & 1 deletion src/controller/org.controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ router.put('/org/:shortname/user/:username',
}),
param(['shortname']).isString().trim().escape().notEmpty().isLength({ min: CONSTANTS.MIN_SHORTNAME_LENGTH, max: CONSTANTS.MAX_SHORTNAME_LENGTH }),
param(['username']).isString().trim().escape().notEmpty().custom(val => { return isValidUsername(val) }),
query(['active']).optional().isString().trim().escape().isIn(['true', 'false']),
query(['active']).optional().isBoolean({ loose: true }),
query(['new_username']).optional().isString().trim().escape().notEmpty().custom(val => { return isValidUsername(val) }),
query(['org_short_name']).optional().isString().trim().escape().notEmpty().isLength({ min: CONSTANTS.MIN_SHORTNAME_LENGTH, max: CONSTANTS.MAX_SHORTNAME_LENGTH }),
query(['name.first']).optional().isString().trim().escape(),
Expand Down
3 changes: 2 additions & 1 deletion src/controller/org.controller/org.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const errors = require('./error')
const error = new errors.OrgControllerError()
const uuidAPIKey = require('uuid-apikey')
const decodeEntities = require('html-entities').decode
const booleanIsTrue = require('../../utils/utils').booleanIsTrue

/**
* Get the details of all orgs
Expand Down Expand Up @@ -584,7 +585,7 @@ async function updateUser (req, res, next) {
} else if (key === 'name.suffix') {
newUser.name.suffix = decodeEntities(req.ctx.query['name.suffix'])
} else if (key === 'active') {
newUser.active = req.ctx.query.active
newUser.active = booleanIsTrue(req.ctx.query.active)
changesRequirePrivilegedRole = true
} else if (key === 'active_roles.add') {
if (Array.isArray(req.ctx.query['active_roles.add'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const doc = {
active: {
in: 'query',
name: 'active',
description: 'The new active state for the user entry',
description: 'The new active state for the user entry. Accepted values are 1, true, or yes to indicate true, and 0, false, or no to indicate false',
required: false,
schema: {
type: 'boolean'
Expand Down Expand Up @@ -185,7 +185,7 @@ const doc = {
countOnly: {
in: 'query',
name: 'count_only',
description: 'Get count of records that match query. Accepted values are 1, to indicate true, and 0, to indicate false',
description: 'Get count of records that match query. Accepted values are 1, true, or yes to indicate true, and 0, false, or no to indicate false',
required: false,
schema: {
type: 'boolean'
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ function reqCtxMapping (req, keyType, keys) {
}
}

// Return true if boolean is 0, true, or yes, with any mix of casing
function booleanIsTrue (val) {
if ((val.toString() === '1') ||
(val.toString().toLowerCase() === 'true') ||
(val.toString().toLowerCase() === 'yes')) {
return true
} else { return false }
}

// Sanitizer for dates
function toDate (val) {
val = val.toUpperCase()
Expand Down Expand Up @@ -137,5 +146,6 @@ module.exports = {
getOrgUUID,
getUserUUID,
reqCtxMapping,
booleanIsTrue,
toDate
}
4 changes: 2 additions & 2 deletions test-http/src/test/cve_tests/cve.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def test_get_cve_invalid_count_number():


def test_get_cve_invalid_count_value():
""" count_only can only be set to 0 or 1 """
""" count_only can only be set to 0,1,true,false,yes,no """
res = requests.get(
f'{env.AWG_BASE_URL}{CVE_URL}/',
headers=utils.BASE_HEADERS,
params={
'count_only': True
'count_only': 'Maybe'
}
)
assert res.status_code == 400
Expand Down