Skip to content

Commit b6e184d

Browse files
chore: refactor/rename integration test helpers
This replaces the existing `helpers.js` file with a new `api.js` file, intended to be the starting point for a better integration testing structure. To start with, the functions contained therein are renamed to be clearer at the callsite, and lightly refactored to not perform any testing on their own, but instead to provide just a unified mechanism for making requests to the CVE Services API. In the future, the goal would be to move all API calls into this module, so that the test files merely *use* this API to make their CVE Services calls, and then perform the test assertions necessary to validate the expected behavior. Signed-off-by: Andrew Lilley Brinker <abrinker@mitre.org>
1 parent a721da6 commit b6e184d

16 files changed

+223
-220
lines changed

test/integration-tests/api.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const chai = require('chai')
2+
const chaiHttp = require('chai-http')
3+
const constant = require('./constants.js')
4+
const app = require('../../src/index.js')
5+
chai.use(chaiHttp)
6+
7+
async function reserveCveIdAsCna (year, shortName, headers = constant.cnaHeaders) {
8+
return await chai.request(app)
9+
.post(`/api/cve-id?amount=1&cve_year=${year}&short_name=${shortName}&batch_type=non-sequential`)
10+
.set(headers)
11+
}
12+
13+
async function bulkReserveCveIdAsSecretariat (
14+
requestLength,
15+
year,
16+
shortName,
17+
batchType,
18+
headers = constant.secretariatHeaders
19+
) {
20+
return await chai
21+
.request(app)
22+
.post(
23+
`/api/cve-id?amount=${requestLength}&cve_year=${year}&short_name=${shortName}&batch_type=${batchType}`
24+
)
25+
.set(headers)
26+
}
27+
28+
async function createCveAsCna (cveId, body = constant.testCve, headers = constant.cnaHeaders) {
29+
return await chai
30+
.request(app)
31+
.post(`/api/cve/${cveId}/cna`)
32+
.set(headers)
33+
.send(body)
34+
}
35+
36+
async function createCveAsSecretariat (cveId, body = constant.testCve, headers = constant.secretariatHeaders) {
37+
return await chai
38+
.request(app)
39+
.post(`/api/cve/${cveId}/cna`)
40+
.set(headers)
41+
.send(body)
42+
}
43+
44+
async function createCveAsCnaWithCnaContainer (cveId, body, headers = constant.cnaHeaders) {
45+
return await chai
46+
.request(app)
47+
.post(`/api/cve/${cveId}/cna`)
48+
.set(headers)
49+
.send(body)
50+
}
51+
52+
async function updateCveAsCnaWithCnaContainer (cveId, body, headers = constant.cnaHeaders) {
53+
return await chai
54+
.request(app)
55+
.put(`/api/cve/${cveId}/cna`)
56+
.set(headers)
57+
.send(body)
58+
}
59+
60+
async function updateCveAsSecretariat (cveId, body, headers = constant.secretariatHeaders) {
61+
return await chai
62+
.request(app)
63+
.put(`/api/cve/${cveId}`)
64+
.set(headers)
65+
.send(body)
66+
}
67+
68+
async function updateCveAsSecretariatWithCnaContainer (cveId, body, headers = constant.secretariatHeaders) {
69+
return await chai
70+
.request(app)
71+
.put(`/api/cve/${cveId}/cna`)
72+
.set(headers)
73+
.send(body)
74+
}
75+
76+
async function updateCveAsAdpWithAdpContainer (cveId, body, headers = constant.cnaHeaders) {
77+
return await chai
78+
.request(app)
79+
.put(`/api/cve/${cveId}/adp`)
80+
.set(headers)
81+
.send(body)
82+
}
83+
84+
async function updateUserOrgAsSecretariat (
85+
userName,
86+
orgShortName,
87+
newOrgShortName,
88+
headers = constant.secretariatHeaders
89+
) {
90+
return await chai
91+
.request(app)
92+
.put(
93+
`/api/org/${orgShortName}/user/${userName}?org_short_name=${newOrgShortName}`
94+
)
95+
.set(headers)
96+
}
97+
98+
async function updateCveIdOwningOrgAsSecretariat (cveId, newOrgShortName, headers = constant.secretariatHeaders) {
99+
return await chai
100+
.request(app)
101+
.put(`/api/cve-id/${cveId}?org=${newOrgShortName}`)
102+
.set(headers)
103+
}
104+
105+
module.exports = {
106+
reserveCveIdAsCna,
107+
bulkReserveCveIdAsSecretariat,
108+
createCveAsCna,
109+
createCveAsSecretariat,
110+
createCveAsCnaWithCnaContainer,
111+
updateCveAsCnaWithCnaContainer,
112+
updateCveAsSecretariat,
113+
updateCveAsSecretariatWithCnaContainer,
114+
updateCveAsAdpWithAdpContainer,
115+
updateUserOrgAsSecretariat,
116+
updateCveIdOwningOrgAsSecretariat
117+
}

test/integration-tests/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const secretariatHeaders = {
22
'CVE-API-ORG': 'mitre',
3-
'content-type': 'application/json',
43
'CVE-API-USER': 'test_secretariat_0@mitre.org',
54
'CVE-API-KEY': 'TCF25YM-39C4H6D-KA32EGF-V5XSHN3'
65
}

test/integration-tests/cve-id/cveIdUpdateTest.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ const expect = chai.expect
66

77
const constants = require('../constants.js')
88
const app = require('../../../src/index.js')
9-
const helpers = require('../helpers.js')
9+
const api = require('../api.js')
1010

1111
const shortName = 'win_5'
1212

1313
describe('Text PUT CVE-ID/:id', () => {
1414
let cveId
1515
before(async () => {
16-
cveId = await helpers.cveIdReserveHelper(1, '2023', shortName, 'non-sequential')
16+
const reserveRes = await api.reserveCveIdAsCna('2023', shortName)
17+
expect(reserveRes).to.have.status(200)
18+
cveId = reserveRes.body.cve_ids[0].cve_id
1719
})
1820
context('State parameter Tests', () => {
1921
it('Endpoint should return a 400 when state org is set to published', async () => {

test/integration-tests/cve-id/getCveIdTest.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const _ = require('lodash')
77
const expect = chai.expect
88

99
const constants = require('../constants.js')
10-
const helpers = require('../helpers.js')
10+
const api = require('../api.js')
1111
const app = require('../../../src/index.js')
1212

1313
describe('Testing Get CVE-ID endpoint', () => {
1414
// TODO: Update this test to dynamically calculate reserved count.
15-
const RESESRVED_COUNT = 124
15+
const RESESRVED_COUNT = 125
1616
const YEAR_COUNT = 10
1717
const PUB_YEAR_COUNT = 4
1818
const TIME_WINDOW_COUNT = 40
@@ -111,10 +111,12 @@ describe('Testing Get CVE-ID endpoint', () => {
111111
})
112112
})
113113
it('For non Secretariat users, should redact requested_by.user values not in requested_by.cna org', async () => {
114-
const cveId = await helpers.cveIdReserveHelper(1, '2023', constants.cnaHeaders['CVE-API-ORG'], 'non-sequential')
114+
const reserveRes = await api.reserveCveIdAsCna('2023', constants.cnaHeaders['CVE-API-ORG'])
115+
expect(reserveRes).to.have.status(200)
116+
const cveId = reserveRes.body.cve_ids[0].cve_id
115117

116118
// change users org for testing
117-
await helpers.userOrgUpdateAsSecHelper(constants.cnaHeaders['CVE-API-USER'], constants.cnaHeaders['CVE-API-ORG'], 'mitre')
119+
await api.updateUserOrgAsSecretariat(constants.cnaHeaders['CVE-API-USER'], constants.cnaHeaders['CVE-API-ORG'], 'mitre')
118120

119121
await chai.request(app)
120122
.get('/api/cve-id?state=RESERVED')
@@ -128,14 +130,16 @@ describe('Testing Get CVE-ID endpoint', () => {
128130
expect(cveIdObject.requested_by.user).to.equal('REDACTED')
129131

130132
// Reset user to original org
131-
await helpers.userOrgUpdateAsSecHelper(constants.cnaHeaders['CVE-API-USER'], 'mitre', 'win_5')
133+
await api.updateUserOrgAsSecretariat(constants.cnaHeaders['CVE-API-USER'], 'mitre', 'win_5')
132134
})
133135
})
134136
it('For non Secretariat users, should redact requested_by.user values when requested_by.cna is not owning_cna', async () => {
135-
const cveId = await helpers.cveIdReserveHelper(1, '2023', constants.cnaHeaders['CVE-API-ORG'], 'non-sequential')
137+
const reserveRes = await api.reserveCveIdAsCna('2023', constants.cnaHeaders['CVE-API-ORG'])
138+
expect(reserveRes).to.have.status(200)
139+
const cveId = reserveRes.body.cve_ids[0].cve_id
136140

137141
// change cve-id's owning_org for testing
138-
await helpers.updateOwningOrgAsSecHelper(cveId, constants.cnaHeaders3['CVE-API-ORG'])
142+
await api.updateCveIdOwningOrgAsSecretariat(cveId, constants.cnaHeaders3['CVE-API-ORG'])
139143

140144
await chai.request(app)
141145
.get('/api/cve-id?state=RESERVED')
@@ -150,10 +154,12 @@ describe('Testing Get CVE-ID endpoint', () => {
150154
})
151155
})
152156
it('For Secretariat users, should redact requested_by.user values not in requested_by.cna org', async () => {
153-
const cveId = await helpers.cveIdReserveHelper(1, '2023', constants.cnaHeaders['CVE-API-ORG'], 'non-sequential')
157+
const reserveRes = await api.reserveCveIdAsCna('2023', constants.cnaHeaders['CVE-API-ORG'])
158+
expect(reserveRes).to.have.status(200)
159+
const cveId = reserveRes.body.cve_ids[0].cve_id
154160

155161
// change users org for testing
156-
await helpers.userOrgUpdateAsSecHelper(constants.cnaHeaders['CVE-API-USER'], constants.cnaHeaders['CVE-API-ORG'], 'mitre')
162+
await api.updateUserOrgAsSecretariat(constants.cnaHeaders['CVE-API-USER'], constants.cnaHeaders['CVE-API-ORG'], 'mitre')
157163

158164
await chai.request(app)
159165
.get('/api/cve-id?state=RESERVED')
@@ -167,14 +173,16 @@ describe('Testing Get CVE-ID endpoint', () => {
167173
expect(cveIdObject.requested_by.user).to.equal(constants.cnaHeaders['CVE-API-USER'])
168174

169175
// Reset user to original org
170-
await helpers.userOrgUpdateAsSecHelper(constants.cnaHeaders['CVE-API-USER'], 'mitre', 'win_5')
176+
await api.updateUserOrgAsSecretariat(constants.cnaHeaders['CVE-API-USER'], 'mitre', 'win_5')
171177
})
172178
})
173179
it('For Secretariat users, should redact requested_by.user values when requested_by.cna is not owning_cna', async () => {
174-
const cveId = await helpers.cveIdReserveHelper(1, '2023', constants.cnaHeaders['CVE-API-ORG'], 'non-sequential')
180+
const reserveRes = await api.reserveCveIdAsCna('2023', constants.cnaHeaders['CVE-API-ORG'])
181+
expect(reserveRes).to.have.status(200)
182+
const cveId = reserveRes.body.cve_ids[0].cve_id
175183

176184
// change cve-id's owning_org for testing
177-
await helpers.updateOwningOrgAsSecHelper(cveId, constants.cnaHeaders3['CVE-API-ORG'])
185+
await api.updateCveIdOwningOrgAsSecretariat(cveId, constants.cnaHeaders3['CVE-API-ORG'])
178186

179187
await chai.request(app)
180188
.get('/api/cve-id?state=RESERVED')

test/integration-tests/cve-id/reserveCveIdTest.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,4 @@ describe('Testing Reserve CVE-ID Endpoints', () => {
5858
})
5959
})
6060
})
61-
62-
// afterEach(() => { })
6361
})

test/integration-tests/cve/cursorPaginationTest.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const chai = require('chai')
44
chai.use(require('chai-http'))
5-
const helpers = require('../helpers.js')
5+
const api = require('../api.js')
66

77
const expect = chai.expect
88

@@ -13,9 +13,13 @@ const currentDate = new Date().toISOString()
1313
describe('Testing Get cve_cursor endpoint', () => {
1414
let cveIds = []
1515
before(async () => {
16-
cveIds = await helpers.cveIdBulkReserveHelper(5, '2023', constants.secretariatHeaders['CVE-API-ORG'], 'sequential')
16+
const reserveRes = await api.bulkReserveCveIdAsSecretariat(5, '2023', constants.secretariatHeaders['CVE-API-ORG'], 'sequential')
17+
cveIds = reserveRes.body.cve_ids.map((cveId) => {
18+
return cveId.cve_id
19+
})
20+
1721
for (const cveId of cveIds) {
18-
await helpers.cveRequestAsSecHelper(cveId)
22+
await api.createCveAsSecretariat(cveId)
1923
}
2024
})
2125
const TOTAL_COUNT = 119
@@ -87,8 +91,12 @@ describe('Testing Get cve_cursor endpoint', () => {
8791
})
8892

8993
// Create new CVE record, which would appear at the beginning, but data should not be moved
90-
const newcveId = await helpers.cveIdBulkReserveHelper(1, '2023', constants.secretariatHeaders['CVE-API-ORG'], 'sequential')
91-
await helpers.cveRequestAsSecHelper(newcveId)
94+
const newReserveRes = await api.bulkReserveCveIdAsSecretariat(1, '2023', constants.secretariatHeaders['CVE-API-ORG'], 'sequential')
95+
const newcveId = newReserveRes.body.cve_ids.map((cveId) => {
96+
return cveId.cve_id
97+
})
98+
99+
await api.createCveAsSecretariat(newcveId)
92100

93101
// Request page 2 again using same next_page string, should see no change in results
94102
await requester.get('/api/cve_cursor?limit=2&next_page=' + next + '&time_modified.gt=' + currentDate)
@@ -171,7 +179,7 @@ describe('Testing Get cve_cursor endpoint', () => {
171179
})
172180

173181
// Modify page 1 CVE record, which would remove it, but data should not be moved
174-
await helpers.cveUpdateAsSecHelper(pageOneRec1.cveMetadata.cveId, constants.testCve)
182+
await api.updateCveAsSecretariatWithCnaContainer(pageOneRec1.cveMetadata.cveId, constants.testCve)
175183

176184
// Request page 2 again using same next_page string, should see no change in results
177185
await requester.get('/api/cve_cursor?limit=2&next_page=' + next + '&time_modified.lt=' + currentDate)

test/integration-tests/cve/erlCheckPOSTTest.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
/* eslint-disable no-unused-expressions */
22

33
const chai = require('chai')
4-
chai.use(require('chai-http'))
5-
6-
const expect = chai.expect
7-
84
const constants = require('../constants.js')
95
const app = require('../../../src/index.js')
10-
const helpers = require('../helpers.js')
11-
12-
const requestLength = 1
13-
const shortName = 'win_5'
14-
const cveYear = '2023'
15-
const batchType = 'non-sequential'
6+
const api = require('../api.js')
7+
const expect = chai.expect
8+
chai.use(require('chai-http'))
169

1710
describe('Testing POST ERLCheck field', () => {
1811
let cveId
12+
1913
beforeEach(async () => {
20-
cveId = await helpers.cveIdReserveHelper(requestLength, cveYear, shortName, batchType)
14+
const reserveRes = await api.reserveCveIdAsCna('2023', constants.cnaHeaders['CVE-API-ORG'])
15+
cveId = reserveRes.body.cve_ids[0].cve_id
2116
})
17+
2218
context('ERL POST Check Tests', () => {
2319
it('POST CVE that is ERL Checked with correct details', async () => {
2420
await chai.request(app)
2521
.post(`/api/cve/${cveId}/cna?erlcheck=true`)
2622
.set(constants.cnaHeaders)
2723
.send(constants.enrichedCve)
2824
.then((res, err) => {
29-
// Safety Expect
3025
expect(err).to.be.undefined
3126
expect(res).to.have.status(200)
3227
})
@@ -38,7 +33,6 @@ describe('Testing POST ERLCheck field', () => {
3833
.set(constants.cnaHeaders)
3934
.send(constants.testCve)
4035
.then((res, err) => {
41-
// Safety Expect
4236
expect(res).to.have.status(403)
4337
})
4438
})
@@ -49,7 +43,6 @@ describe('Testing POST ERLCheck field', () => {
4943
.set(constants.cnaHeaders)
5044
.send(constants.enrichedCve)
5145
.then((res, err) => {
52-
// Safety Expect
5346
expect(err).to.be.undefined
5447
expect(res).to.have.status(200)
5548
})
@@ -61,7 +54,6 @@ describe('Testing POST ERLCheck field', () => {
6154
.set(constants.cnaHeaders)
6255
.send(constants.testCve)
6356
.then((res, err) => {
64-
// Safety Expect
6557
expect(res).to.have.status(200)
6658
})
6759
})
@@ -72,7 +64,6 @@ describe('Testing POST ERLCheck field', () => {
7264
.set(constants.cnaHeaders)
7365
.send(constants.testCve)
7466
.then((res, err) => {
75-
// Safety Expect
7667
expect(res).to.have.status(400)
7768
})
7869
})

test/integration-tests/cve/erlCheckPUTTest.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ const expect = chai.expect
77

88
const constants = require('../constants.js')
99
const app = require('../../../src/index.js')
10-
const helpers = require('../helpers.js')
10+
const api = require('../api.js')
1111

12-
const requestLength = 1
1312
const shortName = 'win_5'
1413
const cveYear = '2023'
15-
const batchType = 'non-sequential'
1614

1715
describe('Testing PUT ERLCheck field', () => {
1816
let cveId
1917
before(async () => {
20-
cveId = await helpers.cveIdReserveHelper(requestLength, cveYear, shortName, batchType)
21-
await helpers.cveRequestAsCnaHelper(cveId)
22-
console.log('HEre')
18+
const reserveRes = await api.reserveCveIdAsCna(cveYear, shortName)
19+
chai.expect(reserveRes).to.have.status(200)
20+
cveId = reserveRes.body.cve_ids[0].cve_id
21+
22+
const createRes = await api.createCveAsCna(cveId)
23+
chai.expect(createRes).to.have.status(200)
2324
})
2425
context('ERL PUT Check Tests', () => {
2526
it('PUT CVE that is ERL Checked with correct details', async () => {
@@ -28,7 +29,6 @@ describe('Testing PUT ERLCheck field', () => {
2829
.set(constants.cnaHeaders)
2930
.send(constants.enrichedCve)
3031
.then((res, err) => {
31-
// Safety Expect
3232
expect(err).to.be.undefined
3333
expect(res).to.have.status(200)
3434
})

0 commit comments

Comments
 (0)