test(notifications): cover conditional server URLs
This commit is contained in:
@@ -3,21 +3,22 @@ import axios from '../../../../../helpers/axios-with-proxy.js';
|
|||||||
import logger from '../../../../../helpers/logger.js';
|
import logger from '../../../../../helpers/logger.js';
|
||||||
import appConfig from '../../../../../config/app.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) => {
|
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 = [];
|
let notifications = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(NOTIFICATIONS_URL);
|
const response = await axios.get(NOTIFICATIONS_URL);
|
||||||
|
|
||||||
notifications = response.data;
|
notifications = response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error fetching notifications API endpoint!', error);
|
logger.error('Error fetching notifications API endpoint!', error);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { describe, it } from 'vitest';
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
import app from '../../../../../app.js';
|
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', () => {
|
describe('GET /internal/api/v1/automatisch/notifications', () => {
|
||||||
it('should return Automatisch notifications', async () => {
|
it('should return Automatisch notifications', async () => {
|
||||||
@@ -8,4 +10,46 @@ describe('GET /internal/api/v1/automatisch/notifications', () => {
|
|||||||
.get('/internal/api/v1/automatisch/notifications')
|
.get('/internal/api/v1/automatisch/notifications')
|
||||||
.expect(200);
|
.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([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -28,11 +28,11 @@ export default defineConfig({
|
|||||||
],
|
],
|
||||||
thresholds: {
|
thresholds: {
|
||||||
autoUpdate: true,
|
autoUpdate: true,
|
||||||
statements: 99.44,
|
statements: 99.45,
|
||||||
branches: 98.41,
|
branches: 98.46,
|
||||||
functions: 99.09,
|
functions: 99.09,
|
||||||
lines: 99.44,
|
lines: 99.45,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user