Skip to content

Commit b94e2c2

Browse files
author
jack-flores
committed
#1258 fixes for pr pipeline
1 parent a660d97 commit b94e2c2

File tree

4 files changed

+134
-122
lines changed

4 files changed

+134
-122
lines changed

src/controller/org.controller/org.controller.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,14 @@ async function updateOrg (req, res, next) {
331331
const org = await orgRepo.findOneByShortName(shortName)
332332
const orgMakingChanges = req.ctx.org
333333
let agt = setAggregateOrgObj({ short_name: shortName })
334-
334+
335335
// org doesn't exist
336336
if (!org) {
337337
logger.info({ uuid: req.ctx.uuid, message: shortName + ' organization could not be updated in MongoDB because it does not exist.' })
338338
return res.status(404).json(error.orgDnePathParam(shortName))
339339
}
340340

341341
newOrg.last_active = Date.now()
342-
343342
const isSec = await orgRepo.isSecretariat(orgMakingChanges)
344343

345344
if (isSec) {
@@ -367,36 +366,36 @@ async function updateOrg (req, res, next) {
367366
}
368367
}
369368
})
369+
}
370370

371-
// updating the org's roles
372-
if (org) {
373-
const roles = org.authority.active_roles
371+
// updating the org's roles
372+
if (org) {
373+
const roles = org.authority.active_roles
374374

375-
// adding roles
376-
addRoles.forEach(role => {
377-
if (!roles.includes(role)) {
378-
roles.push(role)
379-
}
380-
})
375+
// adding roles
376+
addRoles.forEach(role => {
377+
if (!roles.includes(role)) {
378+
roles.push(role)
379+
}
380+
})
381381

382-
// removing roles
383-
removeRoles.forEach(role => {
384-
const index = roles.indexOf(role)
382+
// removing roles
383+
removeRoles.forEach(role => {
384+
const index = roles.indexOf(role)
385385

386-
if (index > -1) {
387-
roles.splice(index, 1)
388-
}
389-
})
386+
if (index > -1) {
387+
roles.splice(index, 1)
388+
}
389+
})
390390

391-
newOrg.authority.active_roles = roles
392-
}
391+
newOrg.authority.active_roles = roles
392+
}
393393

394-
if (newOrg.short_name) {
395-
const result = await orgRepo.findOneByShortName(newOrg.short_name)
394+
if (newOrg.short_name) {
395+
const result = await orgRepo.findOneByShortName(newOrg.short_name)
396396

397-
if (result) {
398-
return res.status(403).json(error.duplicateShortname(newOrg.short_name))
399-
}
397+
if (result) {
398+
return res.status(403).json(error.duplicateShortname(newOrg.short_name))
400399
}
401400
}
402401

src/middleware/middleware.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ async function validateOrg (req, res, next) {
142142
const CONSTANTS = getConstants()
143143

144144
try {
145-
logger.info({ uuid: req.ctx.uuid, message: 'Authenticating org: ' + org })
145+
logger.info({ uuid: req.ctx.uuid, message: 'Authenticating org: ' + org })
146146

147147
const isSec = await orgRepo.isSecretariat(org)
148148
if (!isSec) {
149-
if (!(org == reqOrg)) {
149+
if (!(org === reqOrg)) {
150150
logger.info({ uuid: req.ctx.uuid, message: org + ' is not a ' + CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT + ' or the same as ' + reqOrg + ' and is not allowed to make these changes.' })
151151
return res.status(401).json(error.unauthorized())
152152
}

test/integration-tests/org/putOrgTest.js

+92-92
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,104 @@ const constants = require('../constants.js')
66
const app = require('../../../src/index.js')
77

88
const params = { name: 'Test Organization', id_quota: 100 }
9-
const secretariat_params = { name: 'MITRE Corporation', id_quota: 100000 }
10-
const cna_params = { name: 'Adams, Nielsen and Hensley', id_quota: 1309 }
9+
const secretariatParams = { name: 'MITRE Corporation', id_quota: 100000 }
10+
const cnaParams = { name: 'Adams, Nielsen and Hensley', id_quota: 1309 }
1111

1212
describe('Testing org put endpoint', () => {
13-
context('Positive Tests', () => {
14-
it('Allows update made by a secretariat to itself', async () => {
15-
await chai.request(app)
16-
.put('/api/org/mitre')
17-
.set({ ...constants.headers })
18-
.query(params)
19-
.send()
20-
.then((res, err) => {
21-
expect(res).to.have.status(200)
22-
expect(res.body.updated.name).to.equal(params.name)
23-
expect(res.body.updated.policies.id_quota).to.equal(params.id_quota)
24-
expect(err).to.be.undefined
25-
})
26-
await chai.request(app)
27-
.put(`/api/org/mitre`)
28-
.set({ ...constants.headers })
29-
.query(secretariat_params)
30-
.send()
31-
.then((res, err) => {
32-
expect(res).to.have.status(200)
33-
expect(res.body.updated.name).to.equal(secretariat_params.name)
34-
expect(res.body.updated.policies.id_quota).to.equal(secretariat_params.id_quota)
35-
expect(err).to.be.undefined
36-
})
13+
context('Positive Tests', () => {
14+
it('Allows update made by a secretariat to itself', async () => {
15+
await chai.request(app)
16+
.put('/api/org/mitre')
17+
.set({ ...constants.headers })
18+
.query(params)
19+
.send()
20+
.then((res, err) => {
21+
expect(res).to.have.status(200)
22+
expect(res.body.updated.name).to.equal(params.name)
23+
expect(res.body.updated.policies.id_quota).to.equal(params.id_quota)
24+
expect(err).to.be.undefined
3725
})
38-
it('Allows update made by a secretariat to another org', async () => {
39-
await chai.request(app)
40-
.put('/api/org/win_5')
41-
.set({ ...constants.headers })
42-
.query(params)
43-
.send()
44-
.then((res, err) => {
45-
expect(res).to.have.status(200)
46-
expect(res.body.updated.name).to.equal(params.name)
47-
expect(res.body.updated.policies.id_quota).to.equal(params.id_quota)
48-
expect(err).to.be.undefined
49-
})
50-
await chai.request(app)
51-
.put('/api/org/win_5')
52-
.set({ ...constants.headers })
53-
.query(cna_params)
54-
.send()
55-
.then((res, err) => {
56-
expect(res).to.have.status(200)
57-
expect(res.body.updated.name).to.equal(cna_params.name)
58-
expect(res.body.updated.policies.id_quota).to.equal(cna_params.id_quota)
59-
expect(err).to.be.undefined
60-
})
26+
await chai.request(app)
27+
.put('/api/org/mitre')
28+
.set({ ...constants.headers })
29+
.query(secretariatParams)
30+
.send()
31+
.then((res, err) => {
32+
expect(res).to.have.status(200)
33+
expect(res.body.updated.name).to.equal(secretariatParams.name)
34+
expect(res.body.updated.policies.id_quota).to.equal(secretariatParams.id_quota)
35+
expect(err).to.be.undefined
6136
})
62-
it('Update made by non secretariat org to itself ONLY updates last_active field', async () => {
63-
let now = Date.now()
64-
await chai.request(app)
65-
.put('/api/org/win_5')
66-
.set({ ...constants.nonSecretariatUserHeaders })
67-
.query(params)
68-
.send()
69-
.then((res, err) => {
70-
// Assert that that the last_active field was updated under 2 seconds ago
71-
let last_active = Date.parse(res.body.updated.last_active)
72-
let diff = Math.abs(now - last_active)
73-
let within_two_seconds = diff < 2000
74-
expect(within_two_seconds).to.be.true
75-
// Assert no other fields were changed
76-
expect(res).to.have.status(200)
77-
expect(res.body.updated.name).to.equal(cna_params.name)
78-
expect(res.body.updated.policies.id_quota).to.equal(cna_params.id_quota)
79-
expect(err).to.be.undefined
80-
})
37+
})
38+
it('Allows update made by a secretariat to another org', async () => {
39+
await chai.request(app)
40+
.put('/api/org/win_5')
41+
.set({ ...constants.headers })
42+
.query(params)
43+
.send()
44+
.then((res, err) => {
45+
expect(res).to.have.status(200)
46+
expect(res.body.updated.name).to.equal(params.name)
47+
expect(res.body.updated.policies.id_quota).to.equal(params.id_quota)
48+
expect(err).to.be.undefined
49+
})
50+
await chai.request(app)
51+
.put('/api/org/win_5')
52+
.set({ ...constants.headers })
53+
.query(cnaParams)
54+
.send()
55+
.then((res, err) => {
56+
expect(res).to.have.status(200)
57+
expect(res.body.updated.name).to.equal(cnaParams.name)
58+
expect(res.body.updated.policies.id_quota).to.equal(cnaParams.id_quota)
59+
expect(err).to.be.undefined
8160
})
8261
})
83-
context('Negative Tests', () => {
84-
it('Fails update made by a non-secretariat org to a different org', async () => {
85-
await chai.request(app)
86-
.put('/api/org/cause_8')
87-
.set({ ...constants.nonSecretariatUserHeaders })
88-
.send()
89-
.then((res, err) => {
90-
expect(res).to.have.status(401)
91-
expect(err).to.be.undefined
92-
expect(res.body).to.haveOwnProperty('error')
93-
expect(res.body.error).to.equal('UNAUTHORIZED')
94-
})
62+
it('Update made by non secretariat org to itself ONLY updates last_active field', async () => {
63+
const now = Date.now()
64+
await chai.request(app)
65+
.put('/api/org/win_5')
66+
.set({ ...constants.nonSecretariatUserHeaders })
67+
.query(params)
68+
.send()
69+
.then((res, err) => {
70+
// Assert that that the last_active field was updated under 2 seconds ago
71+
const lastActive = Date.parse(res.body.updated.last_active)
72+
const diff = Math.abs(now - lastActive)
73+
const withinTwoSeconds = diff < 2000
74+
expect(withinTwoSeconds).to.be.true
75+
// Assert no other fields were changed
76+
expect(res).to.have.status(200)
77+
expect(res.body.updated.name).to.equal(cnaParams.name)
78+
expect(res.body.updated.policies.id_quota).to.equal(cnaParams.id_quota)
79+
expect(err).to.be.undefined
9580
})
96-
it('Fails update made by a non-secretariat org to a secretariat', async () => {
97-
await chai.request(app)
98-
.put('/api/org/mitre')
99-
.set({ ...constants.nonSecretariatUserHeaders })
100-
.send()
101-
.then((res, err) => {
102-
expect(res).to.have.status(401)
103-
expect(err).to.be.undefined
104-
expect(res.body).to.haveOwnProperty('error')
105-
expect(res.body.error).to.equal('UNAUTHORIZED')
106-
})
81+
})
82+
})
83+
context('Negative Tests', () => {
84+
it('Fails update made by a non-secretariat org to a different org', async () => {
85+
await chai.request(app)
86+
.put('/api/org/cause_8')
87+
.set({ ...constants.nonSecretariatUserHeaders })
88+
.send()
89+
.then((res, err) => {
90+
expect(res).to.have.status(401)
91+
expect(err).to.be.undefined
92+
expect(res.body).to.haveOwnProperty('error')
93+
expect(res.body.error).to.equal('UNAUTHORIZED')
94+
})
95+
})
96+
it('Fails update made by a non-secretariat org to a secretariat', async () => {
97+
await chai.request(app)
98+
.put('/api/org/mitre')
99+
.set({ ...constants.nonSecretariatUserHeaders })
100+
.send()
101+
.then((res, err) => {
102+
expect(res).to.have.status(401)
103+
expect(err).to.be.undefined
104+
expect(res.body).to.haveOwnProperty('error')
105+
expect(res.body.error).to.equal('UNAUTHORIZED')
107106
})
108107
})
109-
})
108+
})
109+
})

test/unit-tests/org/orgUpdateTest.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class OrgUpdatedAddingRole {
4848
async getOrgUUID () {
4949
return null
5050
}
51+
52+
async isSecretariat () {
53+
return true
54+
}
5155
}
5256

5357
class OrgUpdatedRemovingRole {
@@ -66,6 +70,10 @@ class OrgUpdatedRemovingRole {
6670
async getOrgUUID () {
6771
return null
6872
}
73+
74+
async isSecretariat () {
75+
return true
76+
}
6977
}
7078

7179
describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
@@ -102,8 +110,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
102110
async findOneByShortName () {
103111
return orgFixtures.existentOrg
104112
}
105-
}
106113

114+
async isSecretariat () {
115+
return true
116+
}
117+
}
107118
app.route('/org-not-updated-shortname-exists/:shortname')
108119
.put((req, res, next) => {
109120
const factory = {
@@ -112,15 +123,13 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
112123
req.ctx.repositories = factory
113124
next()
114125
}, orgParams.parsePostParams, orgController.ORG_UPDATE_SINGLE)
115-
116126
chai.request(app)
117127
.put(`/org-not-updated-shortname-exists/${orgFixtures.existentOrg.short_name}?new_short_name=cisco`)
118128
.set(orgFixtures.secretariatHeader)
119129
.end((err, res) => {
120130
if (err) {
121131
done(err)
122132
}
123-
124133
expect(res).to.have.status(403)
125134
expect(res).to.have.property('body').and.to.be.a('object')
126135
const errObj = error.duplicateShortname('cisco')
@@ -288,6 +297,10 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
288297
async aggregate () {
289298
return [orgFixtures.existentOrg]
290299
}
300+
301+
async isSecretariat () {
302+
return true
303+
}
291304
}
292305

293306
app.route('/org-not-updated-no-query-parameters/:shortname')

0 commit comments

Comments
 (0)