|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +const sinon = require('sinon'); |
| 3 | +const { expect } = require('chai'); |
| 4 | +const { getContext } = require('../common'); |
| 5 | +const { AMQPClient } = require('../../lib/amqp'); |
| 6 | +const { process } = require('../../lib/actions/publish'); |
| 7 | + |
| 8 | +describe('processAction', () => { |
| 9 | + beforeEach(() => { |
| 10 | + }); |
| 11 | + afterEach(() => { |
| 12 | + sinon.restore(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should send unencrypted message if doNotEncrypt is truthy', async () => { |
| 16 | + const message = { |
| 17 | + id: '123', |
| 18 | + body: { |
| 19 | + payload: { |
| 20 | + name: 'John', |
| 21 | + age: 30, |
| 22 | + }, |
| 23 | + routingKey: 'test', |
| 24 | + }, |
| 25 | + }; |
| 26 | + const configuration = { |
| 27 | + doNotEncrypt: true, |
| 28 | + }; |
| 29 | + const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { }); |
| 30 | + const result = await process.call(getContext(), message, configuration); |
| 31 | + |
| 32 | + expect(result).to.deep.equal(message); |
| 33 | + expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey); |
| 34 | + expect(publishStub.getCall(0).args[1]).to.be.deep.equal(Buffer.from(JSON.stringify(message.body.payload))); |
| 35 | + expect(publishStub.getCall(0).args[2]).to.be.deep.equal({ |
| 36 | + contentType: 'application/octet-stream', |
| 37 | + messageId: message.id, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should send encrypted message if doNotEncrypt is falsy', async () => { |
| 42 | + const message = { |
| 43 | + id: '123', |
| 44 | + body: { |
| 45 | + payload: { |
| 46 | + name: 'John', |
| 47 | + age: 30, |
| 48 | + }, |
| 49 | + routingKey: 'test', |
| 50 | + }, |
| 51 | + }; |
| 52 | + const publishStub = sinon.stub(AMQPClient.prototype, 'publish').callsFake(async () => { }); |
| 53 | + const result = await process.call(getContext(), message, {}); |
| 54 | + |
| 55 | + expect(result).to.deep.equal(message); |
| 56 | + expect(publishStub.getCall(0).args[0]).to.be.deep.equal(message.body.routingKey); |
| 57 | + expect(publishStub.getCall(0).args[1].toString('utf8')).to.be.deep.equal('Z�.�g�{.\u0018�Cƚ\n�95�Ì(��6�-rN*��h(]�=qEa���cu�<\u0013'); |
| 58 | + expect(publishStub.getCall(0).args[2]).to.be.deep.equal({ |
| 59 | + contentType: 'application/octet-stream', |
| 60 | + messageId: message.id, |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments