Skip to content

Commit f5230d7

Browse files
committed
Fixed unit tests
1 parent b3a708c commit f5230d7

25 files changed

+228
-272
lines changed

src/controller/cve-id.controller/cve-id.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ async function priorityReservation (year, amount, shortName, orgShortName, reque
497497
const id = generateSequentialIds(year, result.ranges.priority.top_id, amount)
498498
const owningOrgUUID = await orgRepo.getOrgUUID(shortName)
499499
const orgUUID = await orgRepo.getOrgUUID(orgShortName)
500-
const requesterUUID = (await userRepo.findOneByUsernameAndOrgUUID(requester, orgUUID)).UUID
500+
const requesterUUID = (await userRepo.findOneByUserNameAndOrgUUID(requester, orgUUID)).UUID
501501

502502
let cveIdDocuments = []
503503
const cveIdDocumentsUUID = []
@@ -594,7 +594,7 @@ async function sequentialReservation (year, amount, shortName, orgShortName, req
594594
const ids = generateSequentialIds(year, result.ranges.general.top_id, amount)
595595
const owningOrgUUID = await orgRepo.getOrgUUID(shortName)
596596
const orgUUID = await orgRepo.getOrgUUID(orgShortName)
597-
const requesterUUID = (await userRepo.findOneByUsernameAndOrgUUID(requester, orgUUID)).UUID
597+
const requesterUUID = (await userRepo.findOneByUserNameAndOrgUUID(requester, orgUUID)).UUID
598598
let cveIdDocuments = []
599599
const cveIdDocumentsUUID = []
600600
let cveIdUUID
@@ -706,7 +706,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
706706
const cveIdDocumentsUUID = []
707707
const owningOrgUUID = await orgRepo.getOrgUUID(shortName)
708708
const orgUUID = await orgRepo.getOrgUUID(orgShortName)
709-
const requesterUUID = (await userRepo.findOneByUsernameAndOrgUUID(requester, orgUUID)).UUID
709+
const requesterUUID = (await userRepo.findOneByUserNameAndOrgUUID(requester, orgUUID)).UUID
710710
const q = availableIds - amount
711711

712712
while ((counter < amount) && !isFull) {

src/middleware/middleware.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,18 @@ async function onlySecretariatOrAdmin (req, res, next) {
231231
// Checks that the requester belongs to an org that has the 'CNA' role
232232
async function onlyCnas (req, res, next) {
233233
const shortName = req.ctx.org
234-
const orgRepo = req.ctx.repositories.getOrgRepository()
234+
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
235235
const CONSTANTS = getConstants()
236236

237237
try {
238238
const org = await orgRepo.findOneByShortName(shortName) // org exists
239239
if (org === null) {
240240
logger.info({ uuid: req.ctx.uuid, message: shortName + ' is NOT a ' + CONSTANTS.AUTH_ROLE_ENUM.CNA })
241241
return res.status(404).json(error.cnaDoesNotExist(shortName))
242-
} else if (org.authority.active_roles.includes(CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT)) {
242+
} else if (org.authority.includes(CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT)) {
243243
logger.info({ uuid: req.ctx.uuid, message: org.short_name + ' is a ' + CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT + ' so until Root organizations are implemented this role is allowed.' })
244244
next()
245-
} else if (org.authority.active_roles.includes(CONSTANTS.AUTH_ROLE_ENUM.CNA)) { // the org is a CNA
245+
} else if (org.authority.includes(CONSTANTS.AUTH_ROLE_ENUM.CNA)) { // the org is a CNA
246246
logger.info({ uuid: req.ctx.uuid, message: 'Confirmed ' + org.short_name + ' as a ' + CONSTANTS.AUTH_ROLE_ENUM.CNA })
247247
next()
248248
} else {
@@ -257,18 +257,18 @@ async function onlyCnas (req, res, next) {
257257
// Checks that the requester belongs to an org that has the 'ADP' role
258258
async function onlyAdps (req, res, next) {
259259
const shortName = req.ctx.org
260-
const orgRepo = req.ctx.repositories.getOrgRepository()
260+
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
261261
const CONSTANTS = getConstants()
262262

263263
try {
264264
const org = await orgRepo.findOneByShortName(shortName) // org exists
265265
if (org === null) {
266266
logger.info({ uuid: req.ctx.uuid, message: shortName + ' is NOT an ' + CONSTANTS.AUTH_ROLE_ENUM.ADP })
267267
return res.status(404).json(error.adpDoesNotExist(shortName))
268-
} else if (org.authority.active_roles.includes(CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT)) {
268+
} else if (org.authority.includes(CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT)) {
269269
logger.info({ uuid: req.ctx.uuid, message: org.short_name + ' is a ' + CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT + ' so until Root organizations are implemented this role is allowed.' })
270270
next()
271-
} else if (org.authority.active_roles.includes(CONSTANTS.AUTH_ROLE_ENUM.ADP)) { // the org is an ADP
271+
} else if (org.authority.includes(CONSTANTS.AUTH_ROLE_ENUM.ADP)) { // the org is an ADP
272272
logger.info({ uuid: req.ctx.uuid, message: 'Confirmed ' + org.short_name + ' as an ' + CONSTANTS.AUTH_ROLE_ENUM.ADP })
273273
next()
274274
} else {
@@ -289,10 +289,10 @@ async function onlyOrgWithPartnerRole (req, res, next) {
289289
if (org === null) {
290290
logger.info({ uuid: req.ctx.uuid, message: shortName + ' does NOT exist ' })
291291
return res.status(404).json(error.orgDoesNotExist(shortName))
292-
} else if ((org.authority.length === 1 && org.authority[0] === 'BULK_DOWNLOAD') || (org.authority?.active_roles?.length === 1 && org.authority.active_roles[0] === 'BULK_DOWNLOAD')) {
292+
} else if ((org.authority.length === 1 && org.authority[0] === 'BULK_DOWNLOAD') || (org.authority?.length === 1 && org.authority[0] === 'BULK_DOWNLOAD')) {
293293
logger.info({ uuid: req.ctx.uuid, message: org.short_name + 'only has BULK_DOWNLOAD role ' })
294294
return res.status(403).json(error.orgHasNoPartnerRole(shortName))
295-
} else if (org.authority.length > 0 || org.authority?.active_roles.length > 0) {
295+
} else if (org.authority.length > 0 || org.authority?.length > 0) {
296296
logger.info({ uuid: req.ctx.uuid, message: org.short_name + ' has a role ' })
297297
next()
298298
} else {
@@ -317,8 +317,8 @@ function validateQueryParameterNames (queryParamNames, validNames) {
317317
async function cnaMustOwnID (req, res, next) {
318318
try {
319319
const requestingOrg = req.ctx.org
320-
const orgRepo = req.ctx.repositories.getOrgRepository()
321-
const isSecretariat = await orgRepo.isSecretariat(requestingOrg)
320+
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
321+
const isSecretariat = await orgRepo.isSecretariatByShortName(requestingOrg)
322322
const requestingOrgInfo = await orgRepo.findOneByShortName(requestingOrg)
323323
const id = req.ctx.params.id
324324
const cveIdRepo = req.ctx.repositories.getCveIdRepository()

test/unit-tests/cve-id/cveIdGetAllTest.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const expect = chai.expect
55
const _ = require('lodash')
66

77
const cveIdController = require('../../../src/controller/cve-id.controller/cve-id.controller.js')
8-
const OrgRepository = require('../../../src/repositories/orgRepository.js')
8+
const OrgRepository = require('../../../src/repositories/baseOrgRepository.js')
99
const CveIdRepository = require('../../../src/repositories/cveIdRepository.js')
10-
const UserRepository = require('../../../src/repositories/userRepository.js')
10+
const UserRepository = require('../../../src/repositories/baseUserRepository.js')
1111

1212
const orgUUID = faker.datatype.uuid()
1313
const orgUUID2 = faker.datatype.uuid()
@@ -22,7 +22,13 @@ const stubOrg = {
2222
'CNA',
2323
'Secretariat'
2424
]
25-
}
25+
},
26+
users: [
27+
{
28+
UUID: userUUID,
29+
username: 'testUser'
30+
}
31+
]
2632
}
2733

2834
const stubOrg2 = {
@@ -33,7 +39,13 @@ const stubOrg2 = {
3339
active_roles: [
3440
'CNA'
3541
]
36-
}
42+
},
43+
users: [
44+
{
45+
UUID: userUUID,
46+
username: 'testUser'
47+
}
48+
]
3749
}
3850

3951
const stubUser = {
@@ -63,9 +75,9 @@ const builtQuery = {
6375
}
6476
}
6577
describe('Testing getFilteredCveId function', () => {
66-
let sandbox, status, json, res, next, getOrgRepository,
78+
let sandbox, status, json, res, next, getBaseOrgRepository,
6779
orgRepo, getCveIdRepository, cveIdRepo,
68-
getUserRepository, userRepo, req, cveIdCopy
80+
getBaseUserRepository, userRepo, req, cveIdCopy
6981

7082
// Stub out functions called in insertAdp and reset them for each test
7183
beforeEach(() => {
@@ -91,12 +103,12 @@ describe('Testing getFilteredCveId function', () => {
91103
}
92104

93105
orgRepo = new OrgRepository()
94-
getOrgRepository = sandbox.stub()
95-
getOrgRepository.returns(orgRepo)
106+
getBaseOrgRepository = sandbox.stub()
107+
getBaseOrgRepository.returns(orgRepo)
96108

97109
userRepo = new UserRepository()
98-
getUserRepository = sandbox.stub()
99-
getUserRepository.returns(userRepo)
110+
getBaseUserRepository = sandbox.stub()
111+
getBaseUserRepository.returns(userRepo)
100112

101113
cveIdRepo = new CveIdRepository()
102114
getCveIdRepository = sandbox.stub()
@@ -106,12 +118,12 @@ describe('Testing getFilteredCveId function', () => {
106118
sandbox.stub(cveIdRepo, 'aggregatePaginate').returns(aggPagResp)
107119

108120
sandbox.stub(orgRepo, 'getOrgUUID').returns(stubOrg.UUID)
109-
sandbox.stub(orgRepo, 'isSecretariat').returns(true)
121+
sandbox.stub(orgRepo, 'isSecretariatByShortName').returns(true)
110122
sandbox.stub(orgRepo, 'isBulkDownload').returns(false)
111-
sandbox.stub(orgRepo, 'getAllOrgs').returns([stubOrg, stubOrg2])
123+
sandbox.stub(orgRepo, 'findAll').returns([stubOrg, stubOrg2])
112124

113125
sandbox.stub(userRepo, 'getUserUUID').returns(stubUser.UUID)
114-
sandbox.stub(userRepo, 'getAllUsers').returns([stubUser])
126+
sandbox.stub(userRepo, 'findAll').returns([stubUser])
115127

116128
sandbox.spy(cveIdController, 'CVEID_GET_FILTER')
117129

@@ -126,8 +138,8 @@ describe('Testing getFilteredCveId function', () => {
126138
state: 'RESERVED'
127139
},
128140
repositories: {
129-
getOrgRepository,
130-
getUserRepository,
141+
getBaseOrgRepository,
142+
getBaseUserRepository,
131143
getCveIdRepository
132144
}
133145
}

test/unit-tests/cve-id/cveIdGetSingleTest.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ class NullOrgRepo {
4545
return null
4646
}
4747

48-
async isSecretariat () {
49-
return null
50-
}
51-
52-
async isSecretariatUUID () {
48+
async isSecretariatByShortName () {
5349
return null
5450
}
5551
}
@@ -67,8 +63,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
6763
.get((req, res, next) => {
6864
const factory = {
6965
getCveIdRepository: () => { return new CveIdGetCveIdDoesntExist() },
70-
getUserRepository: () => { return new NullUserRepo() },
71-
getOrgRepository: () => { return new NullOrgRepo() }
66+
getBaseUserRepository: () => { return new NullUserRepo() },
67+
getBaseOrgRepository: () => { return new NullOrgRepo() }
7268
}
7369
req.ctx.repositories = factory
7470
next()
@@ -102,8 +98,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
10298
.get((req, res, next) => {
10399
const factory = {
104100
getCveIdRepository: () => { return new CveIdGetCveIdAvailable() },
105-
getUserRepository: () => { return new NullUserRepo() },
106-
getOrgRepository: () => { return new NullOrgRepo() }
101+
getBaseUserRepository: () => { return new NullUserRepo() },
102+
getBaseOrgRepository: () => { return new NullOrgRepo() }
107103
}
108104
req.ctx.repositories = factory
109105
next()
@@ -138,7 +134,7 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
138134
return cveIdFixtures.org.UUID
139135
}
140136

141-
async isSecretariatUUID () {
137+
async isSecretariatByShortName () {
142138
return false
143139
}
144140
}
@@ -147,8 +143,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
147143
.get((req, res, next) => {
148144
const factory = {
149145
getCveIdRepository: () => { return new CveIdGetCveIdNotOwningOrg() },
150-
getUserRepository: () => { return new NullUserRepo() },
151-
getOrgRepository: () => { return new OrgGetCveIdNotOwnerOrgReserved() }
146+
getBaseUserRepository: () => { return new NullUserRepo() },
147+
getBaseOrgRepository: () => { return new OrgGetCveIdNotOwnerOrgReserved() }
152148
}
153149
req.ctx.repositories = factory
154150
next()
@@ -192,7 +188,7 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
192188
return cveIdFixtures.org.UUID
193189
}
194190

195-
async isSecretariatUUID () {
191+
async isSecretariatByShortName () {
196192
return false
197193
}
198194

@@ -205,8 +201,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
205201
.get((req, res, next) => {
206202
const factory = {
207203
getCveIdRepository: () => { return new CveIdGetCveIdNotOwningOrgNotReserved() },
208-
getUserRepository: () => { return new NullUserRepo() },
209-
getOrgRepository: () => { return new OrgGetCveIdNotOwnerOrgNotReserved() }
204+
getBaseUserRepository: () => { return new NullUserRepo() },
205+
getBaseOrgRepository: () => { return new OrgGetCveIdNotOwnerOrgNotReserved() }
210206
}
211207
req.ctx.repositories = factory
212208
next()
@@ -248,7 +244,7 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
248244
return cveIdFixtures.owningOrg.UUID
249245
}
250246

251-
async isSecretariatUUID () {
247+
async isSecretariatByShortName () {
252248
return false
253249
}
254250

@@ -261,8 +257,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
261257
.get((req, res, next) => {
262258
const factory = {
263259
getCveIdRepository: () => { return new CveIdGetCveIdOwningOrg() },
264-
getUserRepository: () => { return new UserGetCveIdOwningOrg() },
265-
getOrgRepository: () => { return new OrgGetCveIdOwningOrg() }
260+
getBaseUserRepository: () => { return new UserGetCveIdOwningOrg() },
261+
getBaseOrgRepository: () => { return new OrgGetCveIdOwningOrg() }
266262
}
267263
req.ctx.repositories = factory
268264
req.ctx.authenticated = true
@@ -311,7 +307,7 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
311307
return cveIdFixtures.secretariatOrg.UUID
312308
}
313309

314-
async isSecretariatUUID () {
310+
async isSecretariatByShortName () {
315311
return true
316312
}
317313

@@ -328,8 +324,8 @@ describe('Testing the GET /cve-id/:id endpoint in CveId Controller', () => {
328324
.get((req, res, next) => {
329325
const factory = {
330326
getCveIdRepository: () => { return new CveIdGetCveIdOwningOrg() },
331-
getUserRepository: () => { return new UserGetCveIdRequestorSecretariat() },
332-
getOrgRepository: () => { return new OrgGetCveIdRequestorSecretariat() }
327+
getBaseUserRepository: () => { return new UserGetCveIdRequestorSecretariat() },
328+
getBaseOrgRepository: () => { return new OrgGetCveIdRequestorSecretariat() }
333329
}
334330
req.ctx.repositories = factory
335331
req.ctx.authenticated = true

test/unit-tests/cve-id/cveIdRangeCreateTest.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('Testing the POST /cve-id-range/:year endpoint in CveId Controller', ()
7474
.post((req, res, next) => {
7575
const factory = {
7676
getCveIdRangeRepository: () => { return new CveIdRange2022Exists() },
77-
getUserRepository: () => { return new NullUserRepo() },
78-
getOrgRepository: () => { return new NullOrgRepo() }
77+
getBaseUserRepository: () => { return new NullUserRepo() },
78+
getBaseOrgRepository: () => { return new NullOrgRepo() }
7979
}
8080
req.ctx.repositories = factory
8181
next()
@@ -115,8 +115,8 @@ describe('Testing the POST /cve-id-range/:year endpoint in CveId Controller', ()
115115
.post((req, res, next) => {
116116
const factory = {
117117
getCveIdRangeRepository: () => { return new CveIdRange2022NotExists() },
118-
getUserRepository: () => { return new NullUserRepo() },
119-
getOrgRepository: () => { return new NullOrgRepo() }
118+
getBaseUserRepository: () => { return new NullUserRepo() },
119+
getBaseOrgRepository: () => { return new NullOrgRepo() }
120120
}
121121
req.ctx.repositories = factory
122122
next()

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ describe('Testing the PUT /cve-id/:id endpoint in CveId Controller', () => {
108108
.put((req, res, next) => {
109109
const factory = {
110110
getCveIdRepository: () => { return new CveIdModifyCveIdDoesntExist() },
111-
getOrgRepository: () => { return new OrgModifyCveIdDoesntExist() },
112-
getUserRepository: () => { return new NullUserRepo() },
111+
getBaseOrgRepository: () => { return new OrgModifyCveIdDoesntExist() },
112+
getBaseUserRepository: () => { return new NullUserRepo() },
113113
getCveRepository: () => { return new NullCveRepo() }
114114
}
115115
req.ctx.repositories = factory
@@ -148,8 +148,8 @@ describe('Testing the PUT /cve-id/:id endpoint in CveId Controller', () => {
148148
.put((req, res, next) => {
149149
const factory = {
150150
getCveIdRepository: () => { return new CveIdModifyCveIdOrgAndStateModified() },
151-
getOrgRepository: () => { return new OrgModifyCveIdOrgDoesntExist() },
152-
getUserRepository: () => { return new NullUserRepo() },
151+
getBaseOrgRepository: () => { return new OrgModifyCveIdOrgDoesntExist() },
152+
getBaseUserRepository: () => { return new NullUserRepo() },
153153
getCveRepository: () => { return new NullCveRepo() }
154154
}
155155
req.ctx.repositories = factory
@@ -183,8 +183,8 @@ describe('Testing the PUT /cve-id/:id endpoint in CveId Controller', () => {
183183
// .put((req, res, next) => {
184184
// const factory = {
185185
// getCveIdRepository: () => { return new CveIdModifyCveIdOrgAndStateModified() },
186-
// getOrgRepository: () => { return new NullOrgRepo() },
187-
// getUserRepository: () => { return new NullUserRepo() },
186+
// getBaseOrgRepository: () => { return new NullOrgRepo() },
187+
// getBaseUserRepository: () => { return new NullUserRepo() },
188188
// getCveRepository: () => { return new NullCveRepo() }
189189
// }
190190
// req.ctx.repositories = factory
@@ -215,8 +215,8 @@ describe('Testing the PUT /cve-id/:id endpoint in CveId Controller', () => {
215215
.put((req, res, next) => {
216216
const factory = {
217217
getCveIdRepository: () => { return new CveIdModifyCveIdOrgAndStateModified() },
218-
getOrgRepository: () => { return new OrgModifyCveIdOrgAndStateModified() },
219-
getUserRepository: () => { return new UserModifyCveIdOrgAndStateModified() },
218+
getBaseOrgRepository: () => { return new OrgModifyCveIdOrgAndStateModified() },
219+
getBaseUserRepository: () => { return new UserModifyCveIdOrgAndStateModified() },
220220
getCveRepository: () => { return new NullCveRepo() }
221221
}
222222
req.ctx.repositories = factory
@@ -275,8 +275,8 @@ describe('Testing the PUT /cve-id/:id endpoint in CveId Controller', () => {
275275
.put((req, res, next) => {
276276
const factory = {
277277
getCveIdRepository: () => { return new CveIdModifyCveIdNoQuery() },
278-
getOrgRepository: () => { return new OrgModifyCveIdOrgAndStateModified() },
279-
getUserRepository: () => { return new UserModifyCveIdOrgAndStateModified() },
278+
getBaseOrgRepository: () => { return new OrgModifyCveIdOrgAndStateModified() },
279+
getBaseUserRepository: () => { return new UserModifyCveIdOrgAndStateModified() },
280280
getCveRepository: () => { return new NullCveRepo() }
281281
}
282282
req.ctx.repositories = factory

0 commit comments

Comments
 (0)