test(notifications): cover conditional server URLs

This commit is contained in:
Ali BARIN
2025-05-09 12:25:08 +00:00
parent 804faeb5ec
commit 374eba2c07
3 changed files with 60 additions and 15 deletions

View File

@@ -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);

View File

@@ -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([]);
});
});

View File

@@ -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,
},
},
},
});
});