Skip to content

Commit

Permalink
#1258 updated swagger documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-flores committed Aug 2, 2024
1 parent 403b3bf commit 03cc618
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/controller/org.controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ router.put('/org/:shortname',
<h2>Access Control</h2>
<p>User must belong to an organization with the <b>Secretariat</b> role</p>
<h2>Expected Behavior</h2>
<p><b>CNA:</b> Updates 'last_active' timestamp to show that a CNA is still active</p>
<p><b>Secretariat:</b> Updates any organization's information</p>"
#swagger.parameters['shortname'] = { description: 'The shortname of the organization' }
#swagger.parameters['$ref'] = [
Expand Down
3 changes: 2 additions & 1 deletion src/controller/org.controller/org.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
96 changes: 93 additions & 3 deletions test/unit-tests/org/orgUpdateTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-expressions */
const express = require('express')
const app = express()
const chai = require('chai')
Expand Down Expand Up @@ -38,7 +39,9 @@ class OrgUpdatedAddingRole {
}

async aggregate () {
return [orgFixtures.owningOrg]
const org = orgFixtures.owningOrg
org.last_active = Date.now()
return [org]
}

async updateByOrgUUID () {
Expand All @@ -60,7 +63,9 @@ class OrgUpdatedRemovingRole {
}

async aggregate () {
return [orgFixtures.owningOrg]
const org = orgFixtures.owningOrg
org.last_active = Date.now()
return [org]
}

async updateByOrgUUID () {
Expand Down Expand Up @@ -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()
})
})
Expand Down Expand Up @@ -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()
})
})
Expand Down Expand Up @@ -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()
})
})
Expand Down Expand Up @@ -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()
})
})
Expand All @@ -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 () {
Expand Down Expand Up @@ -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()
})
})
})
})

0 comments on commit 03cc618

Please sign in to comment.