|
| 1 | +const nock = require('nock') |
| 2 | +const Hydro = require('../../lib/hydro') |
| 3 | + |
| 4 | +describe('hydro', () => { |
| 5 | + let hydro, params |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + hydro = new Hydro({ secret: '123', endpoint: 'https://real-hydro.com' }) |
| 9 | + |
| 10 | + nock(hydro.endpoint, { |
| 11 | + reqheaders: { |
| 12 | + Authorization: /^Hydro [\d\w]{64}$/, |
| 13 | + 'Content-Type': 'application/json', |
| 14 | + 'X-Hydro-App': 'docs' |
| 15 | + } |
| 16 | + }) |
| 17 | + // Respond with a 201 and store the body we sent |
| 18 | + .post('/').reply(201, (_, body) => { params = body }) |
| 19 | + }) |
| 20 | + |
| 21 | + describe('#publish', () => { |
| 22 | + it('publishes a single event to Hydro', async () => { |
| 23 | + await hydro.publish('event-name', { pizza: true }) |
| 24 | + expect(params).toEqual({ |
| 25 | + events: [{ schema: 'event-name', value: { pizza: true } }] |
| 26 | + }) |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + describe('#publishMany', () => { |
| 31 | + it('publishes multiple events to Hydro', async () => { |
| 32 | + await hydro.publishMany([ |
| 33 | + { schema: 'event-name', value: { pizza: true } }, |
| 34 | + { schema: 'other-name', value: { salad: false } } |
| 35 | + ]) |
| 36 | + |
| 37 | + expect(params).toEqual({ |
| 38 | + events: [ |
| 39 | + { schema: 'event-name', value: { pizza: true } }, |
| 40 | + { schema: 'other-name', value: { salad: false } } |
| 41 | + ] |
| 42 | + }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('#generatePayloadHmac', () => { |
| 47 | + it('returns a SHA256 HMAC string', () => { |
| 48 | + const body = JSON.stringify({ pizza: true }) |
| 49 | + const hash = hydro.generatePayloadHmac(body) |
| 50 | + expect(hash).toEqual(expect.any(String)) |
| 51 | + expect(hash).toHaveLength(64) |
| 52 | + }) |
| 53 | + |
| 54 | + it('generates the same string for the same payload', () => { |
| 55 | + const body = JSON.stringify({ pizza: true }) |
| 56 | + const one = hydro.generatePayloadHmac(body) |
| 57 | + const two = hydro.generatePayloadHmac(body) |
| 58 | + expect(one).toBe(two) |
| 59 | + }) |
| 60 | + }) |
| 61 | +}) |
0 commit comments