diff --git a/tests/resources/webhooks.spec.ts b/tests/resources/webhooks.spec.ts new file mode 100644 index 00000000..c2ebe33f --- /dev/null +++ b/tests/resources/webhooks.spec.ts @@ -0,0 +1,210 @@ +import APIClient from '../../src/apiClient'; +import { Webhooks } from '../../src/resources/webhooks'; +import { WebhookTriggers } from '../../src/models/webhooks'; + +jest.mock('../src/apiClient'); + +describe('Webhooks', () => { + let apiClient: jest.Mocked; + let webhooks: Webhooks; + + beforeAll(() => { + apiClient = new APIClient({ + apiKey: 'apiKey', + apiUri: 'https://test.api.nylas.com', + timeout: 30, + }) as jest.Mocked; + + webhooks = new Webhooks(apiClient); + apiClient.request.mockResolvedValue({}); + }); + + describe('list', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.list(); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/webhooks', + }); + }); + + it('should call apiClient.request with overrides', async () => { + await webhooks.list({ + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/webhooks', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('find', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.find({ + webhookId: 'webhook123', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/webhooks/webhook123', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('create', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.create({ + requestBody: { + triggerTypes: [WebhookTriggers.CalendarCreated], + callbackUrl: 'https://test.callback.com', + description: "My Webhook's Description", + notificationEmailAddress: 'notification@example.com', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'POST', + path: '/v3/webhooks', + body: { + triggerTypes: [WebhookTriggers.CalendarCreated], + callbackUrl: 'https://test.callback.com', + description: "My Webhook's Description", + notificationEmailAddress: 'notification@example.com', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('update', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.update({ + webhookId: 'webhook123', + requestBody: { + description: "Updated Calendar's Description", + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'PUT', + path: '/v3/webhooks/webhook123', + body: { + description: "Updated Calendar's Description", + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('destroy', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.destroy({ + webhookId: 'webhook123', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'DELETE', + path: '/v3/webhooks/webhook123', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('rotateSecret', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.rotateSecret({ + webhookId: 'webhook123', + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'PUT', + path: '/v3/webhooks/webhook123/rotate-secret', + body: {}, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('ipAddresses', () => { + it('should call apiClient.request with the correct params', async () => { + await webhooks.ipAddresses(); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/webhooks/ip-addresses', + body: undefined, + }); + }); + + it('should call apiClient.request with overrides', async () => { + await webhooks.ipAddresses({ + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/webhooks/ip-addresses', + body: undefined, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); + + describe('extractChallengeParameter', () => { + it('should extract the challenge parameter from a valid URL', () => { + const url = 'https://example.com?challenge=testValue'; + const result = webhooks.extractChallengeParameter(url); + expect(result).toBe('testValue'); + }); + + it('should throw an error if the challenge parameter is missing', () => { + const url = 'https://example.com?otherParam=value'; + expect(() => webhooks.extractChallengeParameter(url)).toThrow( + 'Invalid URL or no challenge parameter found.' + ); + }); + + it('should throw an error for an invalid URL', () => { + const url = 'not-a-valid-url'; + expect(() => webhooks.extractChallengeParameter(url)).toThrow(); + }); + }); +});