Skip to content

Commit

Permalink
#1258 more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-flores committed Aug 8, 2024
1 parent afa5db0 commit a45d63a
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 94 deletions.
172 changes: 172 additions & 0 deletions test/unit-tests/middleware/validateOrgTest.js
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
})
})
})
136 changes: 136 additions & 0 deletions test/unit-tests/org/orgUpdateLastActiveTest.js
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
})
})
})
Loading

0 comments on commit a45d63a

Please sign in to comment.