diff --git a/src/controller/org.controller/index.js b/src/controller/org.controller/index.js index abbac143f..598cb0550 100644 --- a/src/controller/org.controller/index.js +++ b/src/controller/org.controller/index.js @@ -247,6 +247,7 @@ router.put('/org/:shortname',
User must belong to an organization with the Secretariat role
CNA: Updates 'last_active' timestamp to show that a CNA is still active
Secretariat: Updates any organization's information
" #swagger.parameters['shortname'] = { description: 'The shortname of the organization' } #swagger.parameters['$ref'] = [ diff --git a/src/controller/org.controller/org.controller.js b/src/controller/org.controller/org.controller.js index b4e1873f4..0e82f640d 100644 --- a/src/controller/org.controller/org.controller.js +++ b/src/controller/org.controller/org.controller.js @@ -338,7 +338,6 @@ async function updateOrg (req, res, next) { return res.status(404).json(error.orgDnePathParam(shortName)) } - newOrg.last_active = Date.now() const isSec = await orgRepo.isSecretariat(orgMakingChanges) if (isSec) { @@ -399,6 +398,8 @@ async function updateOrg (req, res, next) { } } + newOrg.last_active = Date.now() + // update org let result = await orgRepo.updateByOrgUUID(org.UUID, newOrg) if (result.n === 0) { diff --git a/test/unit-tests/org/orgUpdateTest.js b/test/unit-tests/org/orgUpdateTest.js index 05e696f79..61a4502ad 100644 --- a/test/unit-tests/org/orgUpdateTest.js +++ b/test/unit-tests/org/orgUpdateTest.js @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-expressions */ const express = require('express') const app = express() const chai = require('chai') @@ -38,7 +39,9 @@ class OrgUpdatedAddingRole { } async aggregate () { - return [orgFixtures.owningOrg] + const org = orgFixtures.owningOrg + org.last_active = Date.now() + return [org] } async updateByOrgUUID () { @@ -60,7 +63,9 @@ class OrgUpdatedRemovingRole { } async aggregate () { - return [orgFixtures.owningOrg] + const org = orgFixtures.owningOrg + org.last_active = Date.now() + return [org] } async updateByOrgUUID () { @@ -172,6 +177,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { expect(res.body.updated.name).to.equal(orgFixtures.owningOrg.name) expect(res.body.updated.UUID).to.equal(orgFixtures.owningOrg.UUID) expect(res.body.updated.policies.id_quota).to.equal(orgFixtures.owningOrg.policies.id_quota) + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true done() }) }) @@ -207,6 +217,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { expect(res.body.updated.name).to.equal(orgFixtures.owningOrg.name) expect(res.body.updated.UUID).to.equal(orgFixtures.owningOrg.UUID) expect(res.body.updated.policies.id_quota).to.equal(orgFixtures.owningOrg.policies.id_quota) + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true done() }) }) @@ -241,6 +256,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { expect(res.body.updated.name).to.equal(orgFixtures.owningOrg.name) expect(res.body.updated.UUID).to.equal(orgFixtures.owningOrg.UUID) expect(res.body.updated.policies.id_quota).to.equal(orgFixtures.owningOrg.policies.id_quota) + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true done() }) }) @@ -275,6 +295,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { expect(res.body.updated.name).to.equal(orgFixtures.owningOrg.name) expect(res.body.updated.UUID).to.equal(orgFixtures.owningOrg.UUID) expect(res.body.updated.policies.id_quota).to.equal(orgFixtures.owningOrg.policies.id_quota) + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true done() }) }) @@ -295,7 +320,9 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { } async aggregate () { - return [orgFixtures.existentOrg] + const org = orgFixtures.existentOrg + org.last_active = Date.now() + return [org] } async isSecretariat () { @@ -328,6 +355,69 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => { expect(res.body.updated.name).to.equal(orgFixtures.existentOrg.name) expect(res.body.updated.short_name).to.equal(orgFixtures.existentOrg.short_name) expect(res.body.updated.UUID).to.equal(orgFixtures.existentOrg.UUID) + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true + }) + + it('Non-secretariat only last_active field updated', (done) => { + class NonSecretariat { + async findOneByShortName () { + return orgFixtures.owningOrg + } + + async aggregate () { + const org = orgFixtures.owningOrg + org.last_active = Date.now() + return [org] + } + + async updateByOrgUUID () { + return { n: 1 } + } + + async getOrgUUID () { + return null + } + + async isSecretariat () { + return false + } + } + app.route('/org-non-secretariat-updates-last-active-field/:shortname') + .put((req, res, next) => { + const factory = { + getOrgRepository: () => { return new NonSecretariat() }, + getUserRepository: () => { return new NullUserRepo() } + } + req.ctx.repositories = factory + next() + }, orgParams.parsePostParams, orgController.ORG_UPDATE_SINGLE) + + chai.request(app) + .put(`/org-non-secretariat-updates-last-active-field/${orgFixtures.owningOrg.short_name}?name=TestOrg`) + .set(orgFixtures.owningOrgHeader) + .end((err, res) => { + if (err) { + done(err) + } + expect(res).to.have.status(200) + expect(res).to.have.property('body').and.to.be.a('object') + expect(res.body).to.have.property('updated').and.to.be.a('object') + // Assert that that the last_active field was updated under 0.5 seconds ago + const now = Date.now() + const lastActive = res.body.updated.last_active + const diff = Math.abs(now - lastActive) + const withinTwoSeconds = diff < 500 + expect(withinTwoSeconds).to.be.true + // Assert no other fields were changed + expect(res.body.updated.active_roles).to.be.undefined + expect(res.body.updated.name).to.be.undefined + expect(res.body.updated.policies).to.be.undefined + done() + }) }) }) })