From d85bda282e2bd0141810191c8d9b54a40cb70dd0 Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Wed, 7 May 2025 12:36:45 +0200 Subject: [PATCH] tests: Add API tests for webhook preferences --- backend/tests/helpers/request-helpers.ts | 11 ++ .../api/global-preferences/webhook.test.ts | 119 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 backend/tests/integration/api/global-preferences/webhook.test.ts diff --git a/backend/tests/helpers/request-helpers.ts b/backend/tests/helpers/request-helpers.ts index e53f32f..e9c09c9 100644 --- a/backend/tests/helpers/request-helpers.ts +++ b/backend/tests/helpers/request-helpers.ts @@ -338,6 +338,17 @@ export const updateWebbhookPreferences = async (preferences: WebhookPreferences) return response; }; +export const getWebbhookPreferences = async () => { + checkAppIsRunning(); + + const userCookie = await loginUserAsRole(UserRole.ADMIN); + const response = await request(app) + .get(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/preferences/webhooks`) + .set('Cookie', userCookie) + .send(); + return response; +}; + /** * Generates a token for retrieving/deleting recordings from a room and returns the cookie containing the token */ diff --git a/backend/tests/integration/api/global-preferences/webhook.test.ts b/backend/tests/integration/api/global-preferences/webhook.test.ts new file mode 100644 index 0000000..087e6f0 --- /dev/null +++ b/backend/tests/integration/api/global-preferences/webhook.test.ts @@ -0,0 +1,119 @@ +import { afterEach, beforeAll, describe, expect, it } from '@jest/globals'; +import { + startTestServer, + updateWebbhookPreferences, + getWebbhookPreferences +} from '../../../helpers/request-helpers.js'; +import { expectValidationError } from '../../../helpers/assertion-helpers.js'; +import { MEET_WEBHOOK_ENABLED, MEET_WEBHOOK_URL } from '../../../../src/environment.js'; + +const restoreDefaultWebhookPreferences = async () => { + const defaultPreferences = { + enabled: MEET_WEBHOOK_ENABLED === 'true', + url: MEET_WEBHOOK_URL + }; + await updateWebbhookPreferences(defaultPreferences); +}; + +describe('Webhook Preferences API Tests', () => { + beforeAll(() => { + startTestServer(); + }); + + afterEach(async () => { + await restoreDefaultWebhookPreferences(); + }); + + describe('Update webhook preferences', () => { + it('should update webhook preferences with valid data', async () => { + const validPreferences = { + enabled: true, + url: 'https://example.com/webhook' + }; + let response = await updateWebbhookPreferences(validPreferences); + + expect(response.status).toBe(200); + expect(response.body.message).toBe('Webhooks preferences updated successfully'); + + response = await getWebbhookPreferences(); + expect(response.status).toBe(200); + expect(response.body.enabled).toBe(true); + expect(response.body.url).toBe(validPreferences.url); + expect(response.body).toEqual(validPreferences); + }); + + it('should allow disabling webhooks', async () => { + const oldWebhookPreferences = await getWebbhookPreferences(); + expect(oldWebhookPreferences.status).toBe(200); + + let response = await updateWebbhookPreferences({ + enabled: false + }); + + expect(response.status).toBe(200); + expect(response.body.message).toBe('Webhooks preferences updated successfully'); + + response = await getWebbhookPreferences(); + expect(response.status).toBe(200); + expect(response.body.enabled).toBe(false); + expect(response.body.url).toBe(oldWebhookPreferences.body.url); + }); + + it('should update URL even when disabling webhooks', async () => { + const preference = { + enabled: false, + url: 'https://newurl.com/webhook' + }; + const response = await updateWebbhookPreferences(preference); + + expect(response.status).toBe(200); + expect(response.body.message).toBe('Webhooks preferences updated successfully'); + + const preferencesResponse = await getWebbhookPreferences(); + expect(preferencesResponse.status).toBe(200); + expect(preferencesResponse.body.enabled).toBe(preference.enabled); + expect(preferencesResponse.body.url).toBe(preference.url); + }); + }); + + describe('Update webhook preferences validation', () => { + it('should reject invalid webhook URL', async () => { + const response = await updateWebbhookPreferences({ + enabled: true, + url: 'invalid-url' + }); + + expect(response.status).toBe(422); + expectValidationError(response, 'url', 'URL must start with http:// or https://'); + }); + + it('should reject missing URL when webhooks are enabled', async () => { + const response = await updateWebbhookPreferences({ enabled: true }); + + expect(response.status).toBe(422); + expectValidationError(response, 'url', 'URL is required when webhooks are enabled'); + }); + + it('should reject non-http(s) URLs', async () => { + const response = await updateWebbhookPreferences({ + enabled: true, + url: 'ftp://example.com/webhook' + }); + + expect(response.status).toBe(422); + expectValidationError(response, 'url', 'URL must start with http:// or https://'); + }); + }); + + describe('Get webhook preferences', () => { + it('should return webhook preferences when authenticated as admin', async () => { + const response = await getWebbhookPreferences(); + + expect(response.status).toBe(200); + expect(response.body).toEqual({ + enabled: MEET_WEBHOOK_ENABLED === 'true', + url: MEET_WEBHOOK_URL + }); + }); + }); +});