-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afa5db0
commit a45d63a
Showing
3 changed files
with
314 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
const chai = require('chai') | ||
const sinon = require('sinon') | ||
const { validateOrg } = require('../../../src/middleware/middleware.js') | ||
const OrgRepository = require('../../../src/repositories/orgRepository.js') | ||
const expect = chai.expect | ||
|
||
const secretariat = { | ||
short_name: 'mitre', | ||
name: 'MITRE Corporation', | ||
authority: { | ||
active_roles: [ | ||
'SECRETARIAT', | ||
'CNA' | ||
] | ||
}, | ||
policies: { | ||
id_quota: 1248 | ||
} | ||
} | ||
|
||
const nonSecretariat = { | ||
short_name: 'win_5', | ||
name: 'test_org', | ||
authority: { | ||
active_roles: [ | ||
'CNA' | ||
] | ||
}, | ||
policies: { | ||
id_quota: 200 | ||
} | ||
} | ||
|
||
const nonSecretariat2 = { | ||
short_name: 'cause_8', | ||
name: 'test_org2', | ||
authority: { | ||
active_roles: [ | ||
'CNA' | ||
] | ||
}, | ||
policies: { | ||
id_quota: 888 | ||
} | ||
} | ||
|
||
describe('Testing the validateOrg function', () => { | ||
let status, json, res, next, getOrgRepository, orgRepo | ||
beforeEach(() => { | ||
status = sinon.stub() | ||
json = sinon.spy() | ||
res = { json, status } | ||
next = sinon.stub() | ||
status.returns(res) | ||
|
||
orgRepo = new OrgRepository() | ||
getOrgRepository = sinon.stub() | ||
getOrgRepository.returns(orgRepo) | ||
}) | ||
context('Positive Tests', () => { | ||
it('Secretariat can update itself', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(true) | ||
|
||
const req = { | ||
ctx: { | ||
org: secretariat.short_name, | ||
repositories: { | ||
getOrgRepository | ||
} | ||
}, | ||
params: { | ||
shortname: secretariat.short_name | ||
}, | ||
query: { | ||
id_quota: 111 | ||
} | ||
} | ||
await validateOrg(req, res, next) | ||
|
||
expect(next.calledOnce).to.be.true | ||
expect(next.firstCall.args).to.be.empty | ||
}) | ||
it('Secretariat can update another org', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(true) | ||
|
||
const req = { | ||
ctx: { | ||
org: secretariat.short_name, | ||
repositories: { | ||
getOrgRepository | ||
} | ||
}, | ||
params: { | ||
shortname: nonSecretariat.short_name | ||
}, | ||
query: { | ||
id_quota: 999 | ||
} | ||
} | ||
await validateOrg(req, res, next) | ||
|
||
expect(next.calledOnce).to.be.true | ||
expect(next.firstCall.args).to.be.empty | ||
}) | ||
it('Non-secretariat can update itself', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(true) | ||
|
||
const req = { | ||
ctx: { | ||
org: nonSecretariat.short_name, | ||
repositories: { | ||
getOrgRepository | ||
} | ||
}, | ||
params: { | ||
shortname: nonSecretariat.short_name | ||
} | ||
} | ||
await validateOrg(req, res, next) | ||
|
||
expect(next.calledOnce).to.be.true | ||
expect(next.firstCall.args).to.be.empty | ||
}) | ||
}) | ||
context('Negative Tests', () => { | ||
it('Non-secretariat cannot update its fields other than last_active', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(false) | ||
|
||
const req = { | ||
ctx: { | ||
org: nonSecretariat.short_name, | ||
repositories: { | ||
getOrgRepository | ||
} | ||
}, | ||
params: { | ||
shortname: nonSecretariat.short_name | ||
}, | ||
query: { | ||
id_quota: 999 | ||
} | ||
} | ||
await validateOrg(req, res, next) | ||
|
||
expect(status.calledWith(403)).to.be.true | ||
expect(next.calledOnce).to.be.false | ||
}) | ||
it('Non-secretariat cannot update another org', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(false) | ||
|
||
const req = { | ||
ctx: { | ||
org: nonSecretariat.short_name, | ||
repositories: { | ||
getOrgRepository | ||
} | ||
}, | ||
params: { | ||
shortname: nonSecretariat2.short_name | ||
}, | ||
query: { | ||
id_quota: 999 | ||
} | ||
} | ||
await validateOrg(req, res, next) | ||
|
||
expect(status.calledWith(403)).to.be.true | ||
expect(next.calledOnce).to.be.false | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
const chai = require('chai') | ||
const sinon = require('sinon') | ||
const { ORG_UPDATE_SINGLE } = require('../../../src/controller/org.controller/org.controller.js') | ||
const OrgRepository = require('../../../src/repositories/orgRepository.js') | ||
const UserRepository = require('../../../src/repositories/userRepository.js') | ||
const expect = chai.expect | ||
|
||
const secretariat = { | ||
short_name: 'mitre', | ||
name: 'MITRE Corporation', | ||
authority: { | ||
active_roles: [ | ||
'SECRETARIAT', | ||
'CNA' | ||
] | ||
}, | ||
policies: { | ||
id_quota: 1248 | ||
} | ||
} | ||
|
||
const nonSecretariat = { | ||
short_name: 'win_5', | ||
name: 'test_org', | ||
authority: { | ||
active_roles: [ | ||
'CNA' | ||
] | ||
}, | ||
policies: { | ||
id_quota: 200 | ||
} | ||
} | ||
|
||
describe('Testing the updateOrg function', () => { | ||
let status, json, res, next, getOrgRepository, orgRepo, getUserRepository, | ||
userRepo, updateOrg | ||
beforeEach(() => { | ||
status = sinon.stub() | ||
json = sinon.spy() | ||
res = { json, status } | ||
next = sinon.spy() | ||
status.returns(res) | ||
|
||
orgRepo = new OrgRepository() | ||
getOrgRepository = sinon.stub() | ||
getOrgRepository.returns(orgRepo) | ||
|
||
userRepo = new UserRepository() | ||
getUserRepository = sinon.stub() | ||
getUserRepository.returns(userRepo) | ||
|
||
updateOrg = sinon.stub(orgRepo, 'updateByOrgUUID').returns(true) | ||
sinon.stub(orgRepo, 'getOrgUUID').returns(true) | ||
sinon.stub(userRepo, 'getUserUUID').returns(true) | ||
}) | ||
context('Positive Tests', () => { | ||
it('Secretariat updates itself', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(true) | ||
sinon.stub(orgRepo, 'findOneByShortName').returns(secretariat) | ||
sinon.stub(orgRepo, 'aggregate').returns([secretariat]) | ||
|
||
const req = { | ||
ctx: { | ||
org: secretariat.short_name, | ||
repositories: { | ||
getOrgRepository, | ||
getUserRepository | ||
}, | ||
params: { | ||
shortname: secretariat.short_name | ||
}, | ||
query: { | ||
id_quota: 111 | ||
} | ||
} | ||
} | ||
await ORG_UPDATE_SINGLE(req, res, next) | ||
|
||
expect(status.args[0][0]).to.equal(200) | ||
expect(updateOrg.args[0][1].policies.id_quota).to.equal(req.ctx.query.id_quota) | ||
}) | ||
it('Secretariat updates a different org', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(true) | ||
sinon.stub(orgRepo, 'findOneByShortName').returns(nonSecretariat) | ||
sinon.stub(orgRepo, 'aggregate').returns([nonSecretariat]) | ||
|
||
const req = { | ||
ctx: { | ||
org: secretariat.short_name, | ||
repositories: { | ||
getOrgRepository, | ||
getUserRepository | ||
}, | ||
params: { | ||
shortname: nonSecretariat.short_name | ||
}, | ||
query: { | ||
id_quota: 999 | ||
} | ||
} | ||
} | ||
await ORG_UPDATE_SINGLE(req, res, next) | ||
|
||
expect(status.args[0][0]).to.equal(200) | ||
expect(updateOrg.args[0][1].policies.id_quota).to.equal(req.ctx.query.id_quota) | ||
}) | ||
it('Non-secretariat no params only updates last_active field', async () => { | ||
sinon.stub(orgRepo, 'isSecretariat').returns(false) | ||
sinon.stub(orgRepo, 'findOneByShortName').returns(nonSecretariat) | ||
sinon.stub(orgRepo, 'aggregate').returns([nonSecretariat]) | ||
|
||
const req = { | ||
ctx: { | ||
org: nonSecretariat.short_name, | ||
repositories: { | ||
getOrgRepository, | ||
getUserRepository | ||
}, | ||
params: { | ||
shortname: nonSecretariat.short_name | ||
} | ||
} | ||
} | ||
await ORG_UPDATE_SINGLE(req, res, next) | ||
|
||
expect(status.args[0][0]).to.equal(200) | ||
const now = Date.now() | ||
const lastActive = updateOrg.args[0][1].last_active | ||
const diff = Math.abs(now - lastActive) | ||
const withinHalfASecond = diff < 500 | ||
expect(withinHalfASecond).to.be.true | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.