diff --git a/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.js b/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.js index 95f523d7..31c152b0 100644 --- a/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.js +++ b/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.js @@ -3,21 +3,22 @@ import axios from '../../../../../helpers/axios-with-proxy.js'; import logger from '../../../../../helpers/logger.js'; import appConfig from '../../../../../config/app.js'; -const AUTOMATISCH_NOTIFICATIONS_URL = - 'https://notifications.automatisch.io/notifications.json'; - -const MATION_NOTIFICATIONS_URL = - 'https://notifications.mation.work/notifications.json'; - -const NOTIFICATIONS_URL = appConfig.isMation - ? MATION_NOTIFICATIONS_URL - : AUTOMATISCH_NOTIFICATIONS_URL; - export default async (request, response) => { + const AUTOMATISCH_NOTIFICATIONS_URL = + 'https://notifications.automatisch.io/notifications.json'; + + const MATION_NOTIFICATIONS_URL = + 'https://notifications.mation.work/notifications.json'; + + const NOTIFICATIONS_URL = appConfig.isMation + ? MATION_NOTIFICATIONS_URL + : AUTOMATISCH_NOTIFICATIONS_URL; + let notifications = []; try { const response = await axios.get(NOTIFICATIONS_URL); + notifications = response.data; } catch (error) { logger.error('Error fetching notifications API endpoint!', error); diff --git a/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.test.js b/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.test.js index c5ee8644..616b44db 100644 --- a/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.test.js +++ b/packages/backend/src/controllers/internal/api/v1/automatisch/notifications.test.js @@ -1,6 +1,8 @@ -import { describe, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import request from 'supertest'; import app from '../../../../../app.js'; +import axios from '../../../../../helpers/axios-with-proxy.js'; +import appConfig from '../../../../../config/app.js'; describe('GET /internal/api/v1/automatisch/notifications', () => { it('should return Automatisch notifications', async () => { @@ -8,4 +10,46 @@ describe('GET /internal/api/v1/automatisch/notifications', () => { .get('/internal/api/v1/automatisch/notifications') .expect(200); }); + + it('should use Automatisch notifications URL when Mation is not enabled', async () => { + const AUTOMATISCH_NOTIFICATIONS_URL = + 'https://notifications.automatisch.io/notifications.json'; + + vi.spyOn(axios, 'get').mockResolvedValueOnce({ + data: [], + }); + + await request(app) + .get('/internal/api/v1/automatisch/notifications') + .expect(200); + + expect(axios.get).toHaveBeenCalledWith(AUTOMATISCH_NOTIFICATIONS_URL); + }); + + it('should use Mation notifications URL when Mation is enabled', async () => { + vi.spyOn(appConfig, 'isMation', 'get').mockReturnValue(true); + + const MATION_NOTIFICATIONS_URL = + 'https://notifications.mation.work/notifications.json'; + + vi.spyOn(axios, 'get').mockResolvedValueOnce({ + data: [], + }); + + await request(app) + .get('/internal/api/v1/automatisch/notifications') + .expect(200); + + expect(axios.get).toHaveBeenCalledWith(MATION_NOTIFICATIONS_URL); + }); + + it('should return no notifications when Automatisch notifications are not available', async () => { + vi.spyOn(axios, 'get').mockRejectedValueOnce(); + + const response = await request(app) + .get('/internal/api/v1/automatisch/notifications') + .expect(200); + + expect(response.body.data).toStrictEqual([]); + }); }); diff --git a/packages/backend/vitest.config.js b/packages/backend/vitest.config.js index 2b24eddd..059d3fdf 100644 --- a/packages/backend/vitest.config.js +++ b/packages/backend/vitest.config.js @@ -28,11 +28,11 @@ export default defineConfig({ ], thresholds: { autoUpdate: true, - statements: 99.44, - branches: 98.41, + statements: 99.45, + branches: 98.46, functions: 99.09, - lines: 99.44, + lines: 99.45, }, }, }, -}); +}); \ No newline at end of file