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

Resolves #818 - Better Errors for Bad dates in time_modified #1099

Merged
merged 2 commits into from
Jul 14, 2023
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
2 changes: 1 addition & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function booleanIsTrue (val) {
// Sanitizer for dates
function toDate (val) {
val = val.toUpperCase()
let value = val.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(|Z|((-|\+)\d{2}:\d{2}))$/)
let value = val.match(/^\d{4}-\d{2}-\d{2}T(?:0?[0-9]|1[0-9]|2[0-3]):(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(\.\d+)?(|Z|((-|\+)\d{2}:\d{2}))$/)
let result = null
if (value) {
const dateStr = value[0]
Expand Down
113 changes: 113 additions & 0 deletions test/integration-tests/cve/getCveDateTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* eslint-disable no-unused-expressions */

const chai = require('chai')
chai.use(require('chai-http'))
const expect = chai.expect

const constants = require('../constants.js')
const app = require('../../../src/index.js')

describe('Test time_modified for get CVE', () => {
context('Negative Tests', () => {
it('Get CVE should fail if time_modified.gt is given a date with an invalid month', async () => {
await chai.request(app)
.get('/api/cve?time_modified.gt=2022-13-01T00:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.lt is given a date with an invalid month', async () => {
await chai.request(app)
.get('/api/cve?time_modified.lt=2022-13-01T00:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.lt is given a date with an invalid day', async () => {
await chai.request(app)
.get('/api/cve?time_modified.lt=2022-01-32T00:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.gt is given a date with an invalid day', async () => {
await chai.request(app)
.get('/api/cve?time_modified.gt=2022-01-32T00:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.gt is given a date with invalid hours', async () => {
await chai.request(app)
.get('/api/cve?time_modified.gt=2022-01-01T25:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.lt is given a date with invalid hours', async () => {
await chai.request(app)
.get('/api/cve?time_modified.lt=2022-01-01T25:00:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.gt is given a date with invalid minutes', async () => {
await chai.request(app)
.get('/api/cve?time_modified.gt=2022-01-01T00:61:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.lt is given a date with invalid minutes', async () => {
await chai.request(app)
.get('/api/cve?time_modified.lt=2022-01-01T00:61:00Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.gt is given a date with invalid seconds', async () => {
await chai.request(app)
.get('/api/cve?time_modified.gt=2022-01-01T00:00:61Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
it('Get CVE should fail if time_modified.lt is given a date with invalid seconds', async () => {
await chai.request(app)
.get('/api/cve?time_modified.lt=2022-01-01T00:00:61Z')
.set(constants.headers)
.then((res, err) => {
expect(err).to.be.undefined
expect(res).to.have.status(400)
expect(res.body.message).to.contain('Parameters were invalid')
})
})
})
})
Loading