|
| 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