Skip to content

Commit c3c02cd

Browse files
authored
Merge branch 'dev' into dr_1545
2 parents 983e182 + 729912b commit c3c02cd

File tree

5 files changed

+332
-3
lines changed

5 files changed

+332
-3
lines changed

src/controller/review-object.controller/review-object.controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ async function getReviewObjectByOrgIdentifier (req, res, next) {
1414
} else {
1515
value = await repo.getOrgReviewObjectByOrgShortname(identifier)
1616
}
17+
if (!value) {
18+
return res.status(404).json({ message: 'Review Object does not exist' })
19+
}
1720
return res.status(200).json(value)
1821
}
1922

src/repositories/reviewObjectRepository.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ReviewObjectRepository extends BaseRepository {
88
const baseOrgRepository = new BaseOrgRepository()
99
const org = await baseOrgRepository.findOneByShortName(orgShortName)
1010
if (!org) {
11-
throw new Error(`No organization found with short name ${orgShortName}`)
11+
return null
1212
}
1313
const reviewObject = await ReviewObjectModel.find({ target_object_uuid: org.UUID }, null, options)
1414

@@ -34,7 +34,7 @@ class ReviewObjectRepository extends BaseRepository {
3434
const baseOrgRepository = new BaseOrgRepository()
3535
const org = await baseOrgRepository.findOneByShortName(orgShortName)
3636
if (!org) {
37-
throw new Error(`No organization found with short name ${orgShortName}`)
37+
return null
3838
}
3939
const reviewObject = await ReviewObjectModel.findOne({ target_object_uuid: org.UUID }, null, options)
4040

@@ -45,7 +45,7 @@ class ReviewObjectRepository extends BaseRepository {
4545
const baseOrgRepository = new BaseOrgRepository()
4646
const org = await baseOrgRepository.findOneByUUID(orgUUID)
4747
if (!org) {
48-
throw new Error(`No organization found with UUID ${orgUUID}`)
48+
return null
4949
}
5050
const reviewObject = await ReviewObjectModel.findOne({ target_object_uuid: org.UUID }, null, options)
5151

@@ -63,6 +63,9 @@ class ReviewObjectRepository extends BaseRepository {
6363
async updateReviewOrgObject (body, UUID, options = {}) {
6464
console.log('Updating review object with UUID:', UUID)
6565
const reviewObject = await this.findOneByUUID(UUID, options)
66+
if (!reviewObject) {
67+
return null
68+
}
6669

6770
// For each item waiting for approval, for testing we are going to just do shortname
6871
reviewObject.new_review_data.short_name = body.new_review_data.short_name || reviewObject.new_review_data.short_name

test/integration-tests/constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,20 @@ const testRegistryOrg = {
377377
hard_quota: 100000
378378
}
379379

380+
const testRegistryOrg2 = {
381+
short_name: 'test_registry_org2',
382+
long_name: 'Test Registry Organization2',
383+
contact_info: {
384+
poc: 'Dave',
385+
poc_email: '[email protected]',
386+
poc_phone: '555-1234',
387+
org_email: '[email protected]',
388+
website: 'https://test.org'
389+
},
390+
authority: 'CNA',
391+
hard_quota: 100000
392+
}
393+
380394
const existingOrg = {
381395

382396
short_name: 'win_5',
@@ -419,6 +433,7 @@ module.exports = {
419433
testAdp2,
420434
testOrg,
421435
testRegistryOrg,
436+
testRegistryOrg2,
422437
existingOrg,
423438
existingRegistryOrg
424439
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* eslint-disable no-unused-expressions */
2+
const chai = require('chai')
3+
const expect = chai.expect
4+
chai.use(require('chai-http'))
5+
6+
const constants = require('../constants.js')
7+
const app = require('../../../src/index.js')
8+
9+
describe('Review Object Controller Integration Tests', () => {
10+
let orgUUID
11+
let reviewUUID
12+
const reviewPayload = {
13+
target_object_uuid: '',
14+
new_review_data: {}
15+
}
16+
17+
context('Positive Tests', () => {
18+
it('Creates an organization to use for review object tests', async () => {
19+
const res = await chai
20+
.request(app)
21+
.post('/api/registry/org')
22+
.set({ ...constants.headers })
23+
.send(constants.testRegistryOrg2)
24+
expect(res).to.have.status(200)
25+
expect(res.body).to.have.property('created')
26+
expect(res.body.created).to.have.property('UUID')
27+
orgUUID = res.body.created.UUID
28+
})
29+
30+
it('Creates a review object for the organization', async () => {
31+
reviewPayload.target_object_uuid = orgUUID
32+
reviewPayload.new_review_data = constants.testRegistryOrg2
33+
const res = await chai
34+
.request(app)
35+
.post('/api/review/org/')
36+
.set({ ...constants.headers })
37+
.send(reviewPayload)
38+
expect(res).to.have.status(200)
39+
expect(res.body).to.have.property('uuid')
40+
expect(res.body).to.have.property('target_object_uuid', orgUUID)
41+
expect(res.body).to.have.property('new_review_data')
42+
reviewUUID = res.body.uuid
43+
})
44+
45+
it('Retrieves the review object by org short_name', async () => {
46+
const res = await chai
47+
.request(app)
48+
.get(`/api/review/org/${constants.testRegistryOrg2.short_name}`)
49+
.set({ ...constants.headers })
50+
expect(res).to.have.status(200)
51+
expect(res.body).to.have.property('uuid', reviewUUID)
52+
})
53+
54+
it('Retrieves the review object by org UUID', async () => {
55+
const res = await chai
56+
.request(app)
57+
.get(`/api/review/org/${orgUUID}`)
58+
.set({ ...constants.headers })
59+
expect(res).to.have.status(200)
60+
expect(res.body).to.have.property('uuid', reviewUUID)
61+
})
62+
63+
it('Retrieves all review objects', async () => {
64+
const res = await chai
65+
.request(app)
66+
.get('/api/review/orgs')
67+
.set({ ...constants.headers })
68+
expect(res).to.have.status(200)
69+
expect(res.body).to.be.an('array')
70+
const found = res.body.find(obj => obj.uuid === reviewUUID)
71+
expect(found).to.exist
72+
})
73+
74+
it('Updates the review object with new short_name', async () => {
75+
const updatePayload = {
76+
new_review_data: constants.testRegistryOrg2
77+
}
78+
79+
updatePayload.new_review_data.short_name = 'updated_org'
80+
const res = await chai
81+
.request(app)
82+
.put(`/api/review/org/${reviewUUID}`)
83+
.set({ ...constants.headers })
84+
.send(updatePayload)
85+
expect(res).to.have.status(200)
86+
expect(res.body).to.have.property('uuid', reviewUUID)
87+
expect(res.body.new_review_data).to.have.property('short_name', 'updated_org')
88+
})
89+
})
90+
91+
context('Negative Tests', () => {
92+
it('Fails when target_object_uuid is missing', async () => {
93+
const res = await chai
94+
.request(app)
95+
.post('/api/review/org/')
96+
.set({ ...constants.headers })
97+
.send({ new_review_data: constants.testOrg })
98+
expect(res).to.have.status(400)
99+
expect(res.body).to.have.property('message', 'Missing required field target_object_uuid')
100+
})
101+
102+
it('Fails when new_review_data is missing', async () => {
103+
const res = await chai
104+
.request(app)
105+
.post('/api/review/org/')
106+
.set({ ...constants.headers })
107+
.send({ target_object_uuid: orgUUID })
108+
expect(res).to.have.status(400)
109+
expect(res.body).to.have.property('message', 'Missing required field new_review_data')
110+
})
111+
112+
it('Fails when uuid is provided in creation payload', async () => {
113+
const res = await chai
114+
.request(app)
115+
.post('/api/review/org/')
116+
.set({ ...constants.headers })
117+
.send({
118+
uuid: 'should-not-be-here',
119+
target_object_uuid: orgUUID,
120+
new_review_data: constants.testOrg
121+
})
122+
expect(res).to.have.status(400)
123+
expect(res.body).to.have.property('message', 'Do not pass in a uuid key when creating a review object')
124+
})
125+
126+
it('Returns 404 for non-existent review object GET', async () => {
127+
const res = await chai
128+
.request(app)
129+
.get('/api/review/org/nonexistent-org')
130+
.set({ ...constants.headers })
131+
expect(res).to.have.status(404)
132+
})
133+
134+
it('Returns 404 for non-existent review object UPDATE', async () => {
135+
const updatePayload = {
136+
new_review_data: constants.testRegistryOrg2
137+
}
138+
139+
updatePayload.new_review_data.short_name = 'updated_org'
140+
const res = await chai
141+
.request(app)
142+
.put('/api/review/org/nonexistent-uuid')
143+
.set({ ...constants.headers })
144+
.send(updatePayload)
145+
expect(res).to.have.status(404)
146+
expect(res.body).to.have.property('message')
147+
})
148+
})
149+
})
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/* eslint-disable no-unused-vars */
2+
/* eslint-disable no-unused-expressions */
3+
const { expect } = require('chai')
4+
const sinon = require('sinon')
5+
const controller = require('../../../src/controller/review-object.controller/review-object.controller.js')
6+
7+
describe('Review Object Controller', function () {
8+
let req, res, next, repoStub, orgRepoStub
9+
10+
beforeEach(() => {
11+
repoStub = { }
12+
orgRepoStub = { }
13+
14+
req = {
15+
params: {},
16+
body: {},
17+
ctx: { repositories: { getReviewObjectRepository: () => repoStub, getBaseOrgRepository: () => orgRepoStub } }
18+
}
19+
20+
res = {
21+
status: sinon.stub().returnsThis(),
22+
json: sinon.stub().returnsThis()
23+
}
24+
25+
next = sinon.stub()
26+
})
27+
28+
describe('getReviewObjectByOrgIdentifier', function () {
29+
it('should return 400 if identifier is missing', async () => {
30+
await controller.getReviewObjectByOrgIdentifier(req, res, next)
31+
expect(res.status.calledWith(400)).to.be.true
32+
expect(res.json.calledWith({ message: 'Missing identifier parameter' })).to.be.true
33+
})
34+
35+
it('should call getOrgReviewObjectByOrgUUID when identifier is a UUID', async () => {
36+
const uuid = '123e4567-e89b-12d3-a456-426614174000'
37+
req.params.identifier = uuid
38+
repoStub.getOrgReviewObjectByOrgUUID = sinon.stub().resolves({ id: uuid })
39+
await controller.getReviewObjectByOrgIdentifier(req, res, next)
40+
expect(repoStub.getOrgReviewObjectByOrgUUID.calledWith(uuid)).to.be.true
41+
expect(res.status.calledWith(200)).to.be.true
42+
expect(res.json.calledWith({ id: uuid })).to.be.true
43+
})
44+
45+
it('should call getOrgReviewObjectByOrgShortname when identifier is not a UUID', async () => {
46+
const short = 'myorg'
47+
req.params.identifier = short
48+
repoStub.getOrgReviewObjectByOrgShortname = sinon.stub().resolves({ name: short })
49+
await controller.getReviewObjectByOrgIdentifier(req, res, next)
50+
expect(repoStub.getOrgReviewObjectByOrgShortname.calledWith(short)).to.be.true
51+
expect(res.status.calledWith(200)).to.be.true
52+
expect(res.json.calledWith({ name: short })).to.be.true
53+
})
54+
})
55+
56+
describe('getAllReviewObjects', function () {
57+
it('should return all review objects', async () => {
58+
const data = [{ id: 1 }, { id: 2 }]
59+
repoStub.getAllReviewObjects = sinon.stub().resolves(data)
60+
await controller.getAllReviewObjects(req, res, next)
61+
expect(repoStub.getAllReviewObjects.calledOnce).to.be.true
62+
expect(res.status.calledWith(200)).to.be.true
63+
expect(res.json.calledWith(data)).to.be.true
64+
})
65+
})
66+
67+
describe('updateReviewObjectByReviewUUID', function () {
68+
it('should return 400 if new_review_data is invalid', async () => {
69+
req.params.uuid = 'some-uuid'
70+
req.body.new_review_data = { invalid: true }
71+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: false, errors: ['bad data'] })
72+
await controller.updateReviewObjectByReviewUUID(req, res, next)
73+
expect(orgRepoStub.validateOrg.calledWith(req.body.new_review_data)).to.be.true
74+
expect(res.status.calledWith(400)).to.be.true
75+
expect(res.json.calledWith({ message: 'Invalid new_review_data', errors: ['bad data'] })).to.be.true
76+
})
77+
78+
it('should return 404 if review object not found', async () => {
79+
const uuid = 'rev-uuid'
80+
req.params.uuid = uuid
81+
req.body.new_review_data = { foo: 'bar' }
82+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: true })
83+
repoStub.updateReviewOrgObject = sinon.stub().resolves(undefined)
84+
await controller.updateReviewObjectByReviewUUID(req, res, next)
85+
expect(repoStub.updateReviewOrgObject.calledWith(req.body, uuid)).to.be.true
86+
expect(res.status.calledWith(404)).to.be.true
87+
expect(res.json.calledWith({ message: `No review object found with UUID ${uuid}` })).to.be.true
88+
})
89+
90+
it('should return 200 with updated value', async () => {
91+
const uuid = 'rev-uuid'
92+
const updated = { uuid }
93+
req.params.uuid = uuid
94+
req.body.new_review_data = { foo: 'bar' }
95+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: true })
96+
repoStub.updateReviewOrgObject = sinon.stub().resolves(updated)
97+
await controller.updateReviewObjectByReviewUUID(req, res, next)
98+
expect(repoStub.updateReviewOrgObject.calledWith(req.body, uuid)).to.be.true
99+
expect(res.status.calledWith(200)).to.be.true
100+
expect(res.json.calledWith(updated)).to.be.true
101+
})
102+
})
103+
104+
describe('createReviewObject', function () {
105+
it('should return 400 if body contains uuid', async () => {
106+
req.body.uuid = 'should-not-be-here'
107+
await controller.createReviewObject(req, res, next)
108+
expect(res.status.calledWith(400)).to.be.true
109+
expect(res.json.calledWith({ message: 'Do not pass in a uuid key when creating a review object' })).to.be.true
110+
})
111+
112+
it('should return 400 if target_object_uuid missing', async () => {
113+
req.body.new_review_data = { foo: 'bar' }
114+
await controller.createReviewObject(req, res, next)
115+
expect(res.status.calledWith(400)).to.be.true
116+
expect(res.json.calledWith({ message: 'Missing required field target_object_uuid' })).to.be.true
117+
})
118+
119+
it('should return 400 if new_review_data missing', async () => {
120+
req.body.target_object_uuid = 'obj-uuid'
121+
await controller.createReviewObject(req, res, next)
122+
expect(res.status.calledWith(400)).to.be.true
123+
expect(res.json.calledWith({ message: 'Missing required field new_review_data' })).to.be.true
124+
})
125+
126+
it('should return 400 if new_review_data is invalid', async () => {
127+
req.body.target_object_uuid = 'obj-uuid'
128+
req.body.new_review_data = { bad: true }
129+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: false, errors: ['err'] })
130+
await controller.createReviewObject(req, res, next)
131+
expect(orgRepoStub.validateOrg.calledWith(req.body.new_review_data)).to.be.true
132+
expect(res.status.calledWith(400)).to.be.true
133+
expect(res.json.calledWith({ message: 'Invalid new_review_data', errors: ['err'] })).to.be.true
134+
})
135+
136+
it('should return 500 if repo create fails', async () => {
137+
req.body.target_object_uuid = 'obj-uuid'
138+
req.body.new_review_data = { foo: 'bar' }
139+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: true })
140+
repoStub.createReviewOrgObject = sinon.stub().resolves(undefined)
141+
await controller.createReviewObject(req, res, next)
142+
expect(repoStub.createReviewOrgObject.calledWith(req.body)).to.be.true
143+
expect(res.status.calledWith(500)).to.be.true
144+
expect(res.json.calledWith({ message: 'Failed to create review object' })).to.be.true
145+
})
146+
147+
it('should return 200 with created object', async () => {
148+
const created = { uuid: 'new-uuid', target_object_uuid: 'obj-uuid', new_review_data: { foo: 'bar' } }
149+
req.body.target_object_uuid = 'obj-uuid'
150+
req.body.new_review_data = { foo: 'bar' }
151+
orgRepoStub.validateOrg = sinon.stub().returns({ isValid: true })
152+
repoStub.createReviewOrgObject = sinon.stub().resolves(created)
153+
await controller.createReviewObject(req, res, next)
154+
expect(repoStub.createReviewOrgObject.calledWith(req.body)).to.be.true
155+
expect(res.status.calledWith(200)).to.be.true
156+
expect(res.json.calledWith(created)).to.be.true
157+
})
158+
})
159+
})

0 commit comments

Comments
 (0)