|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { |
| 3 | + PlainWebhookPayloadError, |
| 4 | + PlainWebhookSignatureVerificationError, |
| 5 | +} from '../webhooks/errors'; |
| 6 | +import { verifyPlainWebhook } from '../webhooks/verify'; |
| 7 | +import threadCreatedPayload from './webhook-payloads/thread-created'; |
| 8 | + |
| 9 | +describe('verifyPlainWebhook', () => { |
| 10 | + beforeEach(() => { |
| 11 | + vi.useFakeTimers(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + vi.useRealTimers(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns an error when the payload is empty', () => { |
| 19 | + const result = verifyPlainWebhook('', 'signature', 'secret'); |
| 20 | + |
| 21 | + expect(result.error).instanceOf(PlainWebhookSignatureVerificationError); |
| 22 | + expect(result.error?.message).toBe('No webhook payload was provided.'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns an error when the signature is empty', () => { |
| 26 | + const result = verifyPlainWebhook('payload', '', 'secret'); |
| 27 | + |
| 28 | + expect(result.error).instanceOf(PlainWebhookSignatureVerificationError); |
| 29 | + expect(result.error?.message).toBe( |
| 30 | + 'No signature header value was provided. Please pass the value of the "Plain-Request-Signature" header.' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns an error when the secret is empty', () => { |
| 35 | + const result = verifyPlainWebhook('payload', 'signature', ''); |
| 36 | + |
| 37 | + expect(result.error).instanceOf(PlainWebhookSignatureVerificationError); |
| 38 | + expect(result.error?.message).toBe( |
| 39 | + 'No webhook secret was provided. You can find your webhook secret in your workspace settings.' |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns an error when the signature does not match', () => { |
| 44 | + const result = verifyPlainWebhook('payload', 'signature', 'secret'); |
| 45 | + |
| 46 | + expect(result.error).instanceOf(PlainWebhookSignatureVerificationError); |
| 47 | + expect(result.error?.message).toBe('The signature provided is invalid.'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns an error when the signature matches but the timestamp is too far in the past', () => { |
| 51 | + const result = verifyPlainWebhook( |
| 52 | + JSON.stringify(threadCreatedPayload), |
| 53 | + 'c593af32704c178b404c3de8d32ae54c038f4901349c4c55454f1b7952c25e76', |
| 54 | + 'secret' |
| 55 | + ); |
| 56 | + |
| 57 | + expect(result.error).instanceOf(PlainWebhookSignatureVerificationError); |
| 58 | + expect(result.error?.message).toBe( |
| 59 | + 'The timestamp provided in the webhook payload is too far in the past. The maximum allowed difference is 300 seconds.' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("doesn't return an error when the signature matches and the timestamp is within the tolerance", () => { |
| 64 | + // +5 minutes - 1 second |
| 65 | + vi.setSystemTime(new Date(Date.UTC(2023, 9, 19, 14, 17, 26))); |
| 66 | + |
| 67 | + const result = verifyPlainWebhook( |
| 68 | + JSON.stringify(threadCreatedPayload), |
| 69 | + 'c593af32704c178b404c3de8d32ae54c038f4901349c4c55454f1b7952c25e76', |
| 70 | + 'secret' |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result.error).toBeUndefined(); |
| 74 | + expect(result.data?.type).toBe('thread.thread_created'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns an error when the payload is not a valid JSON object', () => { |
| 78 | + const result = verifyPlainWebhook( |
| 79 | + 'hello-world', |
| 80 | + '1bff4699de4fb5202a4b1e6cefd7b5fdfb02d19a67a1eb371dd417a45b0a47df', |
| 81 | + 'secret' |
| 82 | + ); |
| 83 | + |
| 84 | + expect(result.error).instanceOf(PlainWebhookPayloadError); |
| 85 | + expect(result.error?.message).toBe('The webhook payload is not a valid JSON object.'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns an error when the payload is not a valid webhook payload', () => { |
| 89 | + const invalidPayload = { |
| 90 | + ...threadCreatedPayload, |
| 91 | + payload: { |
| 92 | + ...threadCreatedPayload.payload, |
| 93 | + thread: { |
| 94 | + ...threadCreatedPayload.payload.thread, |
| 95 | + title: undefined, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + const result = verifyPlainWebhook( |
| 101 | + JSON.stringify(invalidPayload), |
| 102 | + '239dbfc186e45ea30ead51cb741a2c5a2997fc0d932b8c85ed1c0ee49132e52f', |
| 103 | + 'secret' |
| 104 | + ); |
| 105 | + |
| 106 | + expect(result.error).instanceOf(PlainWebhookPayloadError); |
| 107 | + expect(result.error?.message).toBe("data/payload/thread must have required property 'title'"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments