Skip to content

Commit 1f5248e

Browse files
committed
add integration test and update swagger docs
1 parent 595bb33 commit 1f5248e

File tree

7 files changed

+99
-16
lines changed

7 files changed

+99
-16
lines changed

api-docs/openapi.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,11 +1209,7 @@
12091209
"content": {
12101210
"application/json": {
12111211
"schema": {
1212-
"oneOf": [
1213-
{
1214-
"$ref": "../schemas/cve/get-cve-record-count.json"
1215-
}
1216-
]
1212+
"$ref": "../schemas/cve/get-cve-record-count.json"
12171213
}
12181214
}
12191215
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
"totalCount": 0
2+
"$schema": "http://json-schema.org/draft-04/schema",
3+
"type": "object",
4+
"properties": {
5+
"totalCount": {
6+
"type": "integer",
7+
"format": "int32"
8+
}
9+
}
310
}

src/controller/cve.controller/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,12 @@ router.get('/cve_count',
286286
<p>Retrieves the count of all CVE records for all organizations</p>"
287287
#swagger.parameters['$ref'] = [
288288
'#/components/parameters/cveState',
289-
'#/components/parameters/countOnly',
290289
]
291290
#swagger.responses[200] = {
292291
description: 'A count of the total number of filtered CVE records',
293292
content: {
294293
"application/json": {
295-
schema: {
296-
oneOf: [
297-
{ $ref: '../schemas/cve/get-cve-record-count.json' }
298-
]
299-
},
294+
schema: { $ref: '../schemas/cve/get-cve-record-count.json' }
300295
}
301296
}
302297
}
@@ -326,7 +321,7 @@ router.get('/cve_count',
326321
}
327322
328323
*/
329-
query().custom((query) => { return mw.validateQueryParameterNames(query, ['state']) }),
324+
query().custom((query) => { return mw.validateQueryParameterNames(query, ['state', 'count_only']) }),
330325
query(['state']).optional().isString().trim().customSanitizer(val => { return val.toUpperCase() }).isIn(CHOICES).withMessage(errorMsgs.CVE_FILTERED_STATES),
331326
parseError,
332327
parseGetParams,

src/controller/schemas.controller/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ router.get('/cve/create-cve-record-cna-request.json', controller.getCnaFullSchem
2020
router.get('/cve/create-adp-record-adp-request.json', controller.getAdpFullSchema)
2121
router.get('/cve/create-cve-record-secretariat-request.json', controller.getCnaSecretariatFullSchema)
2222
router.get('/cve/cna-minimum-request.json', controller.getCnaMinSchema)
23+
router.get('/cve/get-cve-record-count.json', controller.getCveCountResponseSchema)
2324

2425
// Schemas relating to CVE IDs
2526
router.get('/cve-id/create-cve-ids-response.json', controller.getCreateCveIdsResponseSchema)

src/controller/schemas.controller/schemas.controller.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ async function getUpdateUserResponseSchema (req, res) {
222222
res.status(200)
223223
}
224224

225+
async function getCveCountResponseSchema (req, res) {
226+
const cveCountResponseSchema = require('../../../schemas/cve/get-cve-record-count.json')
227+
res.json(cveCountResponseSchema)
228+
res.status(200)
229+
}
230+
225231
module.exports = {
226232
getBadRequestSchema: getBadRequestSchema,
227233
getCreateCveRecordResponseSchema: getCreateCveRecordResponseSchema,
@@ -258,5 +264,6 @@ module.exports = {
258264
getCnaFullSchema: getCnaFullSchema,
259265
getAdpFullSchema: getAdpFullSchema,
260266
getCnaSecretariatFullSchema: getCnaSecretariatFullSchema,
261-
getCnaMinSchema: getCnaMinSchema
267+
getCnaMinSchema: getCnaMinSchema,
268+
getCveCountResponseSchema: getCveCountResponseSchema
262269
}

test/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
# CVE-API-Unit-Tests
4-
In order to Run Tests, make sure you configure a DB connection in the config/config.json under the `test` environment.
4+
In order to Run Tests, make sure you configure a DB connection in the config/default.json under the `test` environment.
55

66
## Dependencies
77

@@ -14,11 +14,16 @@ This project uses or depends on software from
1414
- Mocha https://mochajs.org/
1515
- Chai https://www.chaijs.com/
1616

17+
In order to pre-populate a new database for testing, run the following command:
18+
19+
```sh
20+
npm run populate:test
21+
```
1722

1823
In order to run unit tests, use the following command:
1924

2025
```sh
21-
npm run start:test
26+
npm run test
2227
```
2328

2429
## Notes
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-unused-expressions */
2+
3+
const chai = require('chai')
4+
chai.use(require('chai-http'))
5+
const expect = chai.expect
6+
7+
const constants = require('../constants.js')
8+
const app = require('../../../src/index.js')
9+
10+
describe('Test get /cve_count for CVE records', () => {
11+
context('Positive Tests', () => {
12+
it('Get /cve_count should allow any user to get count', async () => {
13+
await chai.request(app)
14+
.get('/api/cve_count')
15+
.then((res, err) => {
16+
expect(err).to.be.undefined
17+
expect(res).to.have.status(200)
18+
expect(res.body).to.have.property('totalCount').that.is.a('number');
19+
})
20+
})
21+
it('Get /cve_count should allow privledged user to get count also', async () => {
22+
await chai.request(app)
23+
.get('/api/cve_count')
24+
.set(constants.headers)
25+
.then((res, err) => {
26+
expect(err).to.be.undefined
27+
expect(res).to.have.status(200)
28+
expect(res.body).to.have.property('totalCount').that.is.a('number');
29+
})
30+
})
31+
it('Get /cve_count should return count with valid parameters', async () => {
32+
await chai.request(app)
33+
.get('/api/cve_count?state=PUBLISHED')
34+
.set(constants.headers)
35+
.then((res, err) => {
36+
expect(err).to.be.undefined
37+
expect(res).to.have.status(200)
38+
expect(res.body).to.have.property('totalCount').that.is.a('number');
39+
})
40+
})
41+
})
42+
context('Negative Tests', () => {
43+
it('Get /cve should NOT allow any user to get count', async () => {
44+
await chai.request(app)
45+
.get('/api/cve?count_only=1')
46+
.then((res, err) => {
47+
expect(err).to.be.undefined
48+
expect(res).to.have.status(400)
49+
})
50+
})
51+
it('Get /cve_count should fail if it is passed invalid parameters', async () => {
52+
await chai.request(app)
53+
.get('/api/cve_count?time_modified.gt=2022-13-01T00:00:00Z')
54+
.set(constants.headers)
55+
.then((res, err) => {
56+
expect(err).to.be.undefined
57+
expect(res).to.have.status(400)
58+
expect(res.body.message).to.contain('Parameters were invalid')
59+
})
60+
})
61+
it('Get /cve_count should fail if it is passed invalid parameters', async () => {
62+
await chai.request(app)
63+
.get('/api/cve_count?state=SUCESS')
64+
.set(constants.headers)
65+
.then((res, err) => {
66+
expect(err).to.be.undefined
67+
expect(res).to.have.status(400)
68+
expect(res.body.message).to.contain('Parameters were invalid')
69+
})
70+
})
71+
})
72+
})

0 commit comments

Comments
 (0)