Skip to content

Commit 278065c

Browse files
committed
#1258 secretariat update to another org no longer updates last_active
1 parent a45d63a commit 278065c

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ async function updateOrg (req, res, next) {
364364
})
365365
}
366366
}
367+
if (shortName === orgMakingChanges) {
368+
newOrg.last_active = Date.now()
369+
}
367370
})
371+
} else {
372+
newOrg.last_active = Date.now()
368373
}
369374

370375
// updating the org's roles
@@ -398,8 +403,6 @@ async function updateOrg (req, res, next) {
398403
}
399404
}
400405

401-
newOrg.last_active = Date.now()
402-
403406
// update org
404407
let result = await orgRepo.updateByOrgUUID(org.UUID, newOrg)
405408
if (result.n === 0) {
@@ -411,6 +414,9 @@ async function updateOrg (req, res, next) {
411414
result = result.length > 0 ? result[0] : null
412415

413416
if (!isSec) {
417+
if (!result.last_active) {
418+
return res.status(500).json(error.serverError())
419+
}
414420
result = { last_active: result.last_active }
415421
}
416422

test/integration-tests/org/putOrgTest.js

+79-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,37 @@ describe('Testing org put endpoint', () => {
6060
expect(err).to.be.undefined
6161
})
6262
})
63-
it('Update made by non secretariat org to itself ONLY updates last_active field', async () => {
63+
it('Update made by a secretariat to another org does NOT update last_active field', async () => {
64+
await chai.request(app)
65+
.put('/api/org/win_5')
66+
.set({ ...constants.headers })
67+
.query(params)
68+
.send()
69+
.then((res, err) => {
70+
expect(res.body.updated.last_active).to.be.undefined
71+
expect(res).to.have.status(200)
72+
expect(err).to.be.undefined
73+
})
74+
})
75+
it('Update made by a secretariat to itself DOES update last_active field', async () => {
76+
const now = Date.now()
77+
await chai.request(app)
78+
.put('/api/org/mitre')
79+
.set({ ...constants.headers })
80+
.query(params)
81+
.send()
82+
.then((res, err) => {
83+
expect(res.body.updated.last_active).to.not.be.null
84+
// Assert that that the last_active field was updated under 2 seconds ago
85+
const lastActive = Date.parse(res.body.updated.last_active)
86+
const diff = Math.abs(now - lastActive)
87+
const withinTwoSeconds = diff < 2000
88+
expect(withinTwoSeconds).to.be.true
89+
expect(res).to.have.status(200)
90+
expect(err).to.be.undefined
91+
})
92+
})
93+
it('Update made by non-secretariat org to itself ONLY updates last_active field', async () => {
6494
const now = Date.now()
6595
await chai.request(app)
6696
.put('/api/org/win_5')
@@ -80,6 +110,54 @@ describe('Testing org put endpoint', () => {
80110
expect(err).to.be.undefined
81111
})
82112
})
113+
it('Request body ignored in update made by non-secretariat org to itself', async () => {
114+
const requestBody = {
115+
key1: 'value1',
116+
key2: 'value2',
117+
key3: 'value3',
118+
key4: 'value4',
119+
key5: 'value5',
120+
key6: 'value6',
121+
key7: 'value7',
122+
key8: 'value8'
123+
}
124+
await chai.request(app)
125+
.put('/api/org/win_5')
126+
.set({ ...constants.nonSecretariatUserHeaders })
127+
.send(requestBody)
128+
.then((res, err) => {
129+
expect(res).to.have.status(200)
130+
expect(res.body.updated.last_active).to.not.be.null
131+
expect(res.body.updated.active_roles).to.be.undefined
132+
expect(res.body.updated.name).to.be.undefined
133+
expect(res.body.updated.policies).to.be.undefined
134+
expect(err).to.be.undefined
135+
})
136+
})
137+
it('Request body ignored in update made by secretariat to itself', async () => {
138+
const requestBody = {
139+
key1: 'value1',
140+
key2: 'value2',
141+
key3: 'value3',
142+
key4: 'value4',
143+
key5: 'value5',
144+
key6: 'value6',
145+
key7: 'value7',
146+
key8: 'value8'
147+
}
148+
await chai.request(app)
149+
.put('/api/org/mitre')
150+
.set({ ...constants.headers })
151+
.query(params)
152+
.send(requestBody)
153+
.then((res, err) => {
154+
expect(res).to.have.status(200)
155+
expect(res.body.updated.last_active).to.not.be.null
156+
expect(res.body.updated.name).to.equal(params.name)
157+
expect(res.body.updated.policies.id_quota).to.equal(params.id_quota)
158+
expect(err).to.be.undefined
159+
})
160+
})
83161
})
84162
context('Negative Tests', () => {
85163
it('Fails update made by a non-secretariat org to a different org', async () => {

test/unit-tests/org/orgUpdateLastActiveTest.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ describe('Testing the updateOrg function', () => {
109109
it('Non-secretariat no params only updates last_active field', async () => {
110110
sinon.stub(orgRepo, 'isSecretariat').returns(false)
111111
sinon.stub(orgRepo, 'findOneByShortName').returns(nonSecretariat)
112-
sinon.stub(orgRepo, 'aggregate').returns([nonSecretariat])
112+
const nonSecretariatAgt = nonSecretariat
113+
nonSecretariatAgt.last_active = Date.now()
114+
sinon.stub(orgRepo, 'aggregate').returns([nonSecretariatAgt])
113115

114116
const req = {
115117
ctx: {

0 commit comments

Comments
 (0)