|
| 1 | +const chai = require('chai') |
| 2 | +const sinon = require('sinon') |
| 3 | +const mongoose = require('mongoose') |
| 4 | +const expect = chai.expect |
| 5 | + |
| 6 | +const registryOrgController = require('../../../src/controller/registry-org.controller/registry-org.controller') |
| 7 | +const logger = require('../../../src/middleware/logger') |
| 8 | + |
| 9 | +describe('Registry org payload logging', () => { |
| 10 | + let mockSession |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + mockSession = { |
| 14 | + startTransaction: sinon.stub(), |
| 15 | + commitTransaction: sinon.stub().resolves(), |
| 16 | + abortTransaction: sinon.stub().resolves(), |
| 17 | + endSession: sinon.stub().resolves() |
| 18 | + } |
| 19 | + sinon.stub(mongoose, 'startSession').resolves(mockSession) |
| 20 | + sinon.stub(logger, 'info') |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sinon.restore() |
| 25 | + }) |
| 26 | + |
| 27 | + function createResponse () { |
| 28 | + return { |
| 29 | + status: sinon.stub().returnsThis(), |
| 30 | + json: sinon.stub().returnsThis() |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + function getLoggedPayload (action) { |
| 35 | + const logCall = logger.info.getCalls().find(call => { |
| 36 | + try { |
| 37 | + return JSON.parse(call.args[0]).action === action |
| 38 | + } catch (err) { |
| 39 | + return false |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + expect(logCall).to.not.equal(undefined) |
| 44 | + return JSON.parse(logCall.args[0]) |
| 45 | + } |
| 46 | + |
| 47 | + async function updateRegistryOrg (updatedOrg) { |
| 48 | + const callerOrgUUID = '54f5fa83-86f6-44fd-b45a-290087680ac0' |
| 49 | + const org = { |
| 50 | + UUID: updatedOrg.UUID, |
| 51 | + short_name: updatedOrg.short_name, |
| 52 | + toObject: () => ({ ...updatedOrg }) |
| 53 | + } |
| 54 | + const repo = { |
| 55 | + isSecretariatByShortName: sinon.stub().resolves(true), |
| 56 | + findOneByShortName: sinon.stub().resolves(org), |
| 57 | + validateOrg: sinon.stub().returns({ isValid: true }), |
| 58 | + checkAliasCollisions: sinon.stub().resolves(null), |
| 59 | + getOrgUUID: sinon.stub().callsFake(shortName => { |
| 60 | + if (shortName === updatedOrg.short_name) return updatedOrg.UUID |
| 61 | + if (shortName === 'mitre') return callerOrgUUID |
| 62 | + return null |
| 63 | + }), |
| 64 | + updateOrgFull: sinon.stub().resolves(updatedOrg) |
| 65 | + } |
| 66 | + const userRepo = { |
| 67 | + isAdmin: sinon.stub().resolves(false), |
| 68 | + findOneByUsernameAndOrgShortname: sinon.stub().resolves({ UUID: 'requesting-user-uuid' }) |
| 69 | + } |
| 70 | + const conversationRepo = { |
| 71 | + getAllByTargetUUID: sinon.stub().resolves([]) |
| 72 | + } |
| 73 | + const reviewRepo = { |
| 74 | + getOrgReviewObjectByOrgShortname: sinon.stub().resolves(null) |
| 75 | + } |
| 76 | + const req = { |
| 77 | + ctx: { |
| 78 | + uuid: 'request-uuid', |
| 79 | + org: 'mitre', |
| 80 | + user: 'test_secretariat_0@mitre.org', |
| 81 | + params: { shortname: updatedOrg.short_name }, |
| 82 | + body: { |
| 83 | + short_name: updatedOrg.short_name, |
| 84 | + long_name: 'Target Org', |
| 85 | + authority: ['CNA'] |
| 86 | + }, |
| 87 | + repositories: { |
| 88 | + getBaseOrgRepository: () => repo, |
| 89 | + getBaseUserRepository: () => userRepo, |
| 90 | + getConversationRepository: () => conversationRepo, |
| 91 | + getReviewObjectRepository: () => reviewRepo |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + const res = createResponse() |
| 96 | + const next = sinon.stub() |
| 97 | + |
| 98 | + await registryOrgController.UPDATE_ORG(req, res, next) |
| 99 | + |
| 100 | + return { payload: getLoggedPayload('update_registry_org'), res, next, callerOrgUUID } |
| 101 | + } |
| 102 | + |
| 103 | + it('logs the updated registry org UUID for update_registry_org', async () => { |
| 104 | + const updatedOrg = { |
| 105 | + UUID: 'cc5f9414-acf2-4e43-8071-67e8ea810113', |
| 106 | + short_name: 'target_org', |
| 107 | + authority: ['CNA'], |
| 108 | + joint_approval_required: false |
| 109 | + } |
| 110 | + |
| 111 | + const { payload, res, next, callerOrgUUID } = await updateRegistryOrg(updatedOrg) |
| 112 | + expect(res.status.calledWith(200)).to.equal(true) |
| 113 | + expect(next.notCalled).to.equal(true) |
| 114 | + expect(payload.org_UUID).to.equal(updatedOrg.UUID) |
| 115 | + expect(payload.org_UUID).to.not.equal(callerOrgUUID) |
| 116 | + expect(payload.org.UUID).to.equal(updatedOrg.UUID) |
| 117 | + }) |
| 118 | + |
| 119 | + it('logs the updated registry org UUID for update_registry_org when joint approval is required', async () => { |
| 120 | + const updatedOrg = { |
| 121 | + UUID: '895cdd1b-9825-4f10-b031-bffad6650c26', |
| 122 | + short_name: 'target_org', |
| 123 | + authority: ['CNA'], |
| 124 | + joint_approval_required: true |
| 125 | + } |
| 126 | + |
| 127 | + const { payload, res, next, callerOrgUUID } = await updateRegistryOrg(updatedOrg) |
| 128 | + expect(res.status.calledWith(200)).to.equal(true) |
| 129 | + expect(next.notCalled).to.equal(true) |
| 130 | + expect(payload.org_UUID).to.equal(updatedOrg.UUID) |
| 131 | + expect(payload.org_UUID).to.not.equal(callerOrgUUID) |
| 132 | + expect(payload.org.UUID).to.equal(updatedOrg.UUID) |
| 133 | + }) |
| 134 | + |
| 135 | + it('logs the deleted registry org UUID for delete_registry_org', async () => { |
| 136 | + const targetOrg = { |
| 137 | + UUID: '8db8e7ed-a43f-40b8-9ebb-d68aa8dc2216', |
| 138 | + short_name: 'target_org' |
| 139 | + } |
| 140 | + const repo = { |
| 141 | + findOneByShortName: sinon.stub().resolves(targetOrg), |
| 142 | + deleteOrg: sinon.stub().resolves(), |
| 143 | + getOrgUUID: sinon.stub().throws(new Error('delete payload should not look up the caller org UUID')) |
| 144 | + } |
| 145 | + const req = { |
| 146 | + ctx: { |
| 147 | + uuid: 'request-uuid', |
| 148 | + org: 'mitre', |
| 149 | + params: { identifier: targetOrg.short_name }, |
| 150 | + repositories: { |
| 151 | + getBaseOrgRepository: () => repo |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + const res = createResponse() |
| 156 | + const next = sinon.stub() |
| 157 | + |
| 158 | + await registryOrgController.DELETE_ORG(req, res, next) |
| 159 | + |
| 160 | + const payload = getLoggedPayload('delete_registry_org') |
| 161 | + expect(res.status.calledWith(200)).to.equal(true) |
| 162 | + expect(next.notCalled).to.equal(true) |
| 163 | + expect(repo.deleteOrg.calledWith(targetOrg.short_name)).to.equal(true) |
| 164 | + expect(repo.getOrgUUID.notCalled).to.equal(true) |
| 165 | + expect(payload.org_UUID).to.equal(targetOrg.UUID) |
| 166 | + }) |
| 167 | +}) |
0 commit comments