2023-08-06 10:42:20 -05:00
|
|
|
import nock from 'nock';
|
2024-08-03 12:39:22 -05:00
|
|
|
import { beforeEach, describe, expect, test } from 'vitest';
|
2022-03-05 12:44:10 -05:00
|
|
|
|
2021-10-29 10:33:05 -05:00
|
|
|
import { createRemoteUser, parseConfigFile } from '@verdaccio/config';
|
2021-06-12 11:24:54 -05:00
|
|
|
import { setup } from '@verdaccio/logger';
|
2020-09-22 16:43:39 -05:00
|
|
|
import { Config } from '@verdaccio/types';
|
2021-10-29 10:33:05 -05:00
|
|
|
|
2020-09-22 16:43:39 -05:00
|
|
|
import { notify } from '../src/notify';
|
|
|
|
import { parseConfigurationFile } from './__helper';
|
|
|
|
|
|
|
|
const parseConfigurationNotifyFile = (name) => {
|
|
|
|
return parseConfigurationFile(`notify/${name}`);
|
|
|
|
};
|
|
|
|
const singleHeaderNotificationConfig = parseConfigFile(
|
|
|
|
parseConfigurationNotifyFile('single.header.notify')
|
|
|
|
);
|
|
|
|
const multiNotificationConfig = parseConfigFile(parseConfigurationNotifyFile('multiple.notify'));
|
|
|
|
|
2023-02-12 14:26:18 -05:00
|
|
|
setup({});
|
2020-09-22 16:43:39 -05:00
|
|
|
|
|
|
|
const domain = 'http://slack-service';
|
|
|
|
|
2021-06-12 11:24:54 -05:00
|
|
|
const options = {
|
|
|
|
path: '/foo?auth_token=mySecretToken',
|
|
|
|
};
|
2020-09-22 16:43:39 -05:00
|
|
|
|
2021-06-12 11:24:54 -05:00
|
|
|
describe('Notifications:: notifyRequest', () => {
|
2023-08-06 10:42:20 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
2020-09-22 16:43:39 -05:00
|
|
|
test('when sending a empty notification', async () => {
|
2023-08-06 10:42:20 -05:00
|
|
|
nock(domain).post(options.path).reply(200, { body: 'test' });
|
2020-09-22 16:43:39 -05:00
|
|
|
|
|
|
|
const notificationResponse = await notify({}, {}, createRemoteUser('foo', []), 'bar');
|
|
|
|
expect(notificationResponse).toEqual([false]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when sending a single notification', async () => {
|
2023-08-06 10:42:20 -05:00
|
|
|
nock(domain).post(options.path).reply(200, { body: 'test' });
|
2020-09-22 16:43:39 -05:00
|
|
|
const notificationResponse = await notify(
|
|
|
|
{},
|
|
|
|
singleHeaderNotificationConfig,
|
|
|
|
createRemoteUser('foo', []),
|
|
|
|
'bar'
|
|
|
|
);
|
|
|
|
expect(notificationResponse).toEqual([true]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when notification endpoint is missing', async () => {
|
2023-08-06 10:42:20 -05:00
|
|
|
nock(domain).post(options.path).reply(200, { body: 'test' });
|
2020-09-22 16:43:39 -05:00
|
|
|
const name = 'package';
|
|
|
|
const config: Partial<Config> = {
|
|
|
|
// @ts-ignore
|
|
|
|
notify: {
|
|
|
|
method: 'POST',
|
|
|
|
endpoint: undefined,
|
|
|
|
content: '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const notificationResponse = await notify({ name }, config, createRemoteUser('foo', []), 'bar');
|
|
|
|
expect(notificationResponse).toEqual([false]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when multiple notifications', async () => {
|
2023-08-06 10:42:20 -05:00
|
|
|
nock(domain)
|
|
|
|
.post(options.path)
|
|
|
|
.once()
|
|
|
|
.reply(200, { body: 'test' })
|
|
|
|
.post(options.path)
|
|
|
|
.once()
|
|
|
|
.reply(400, {})
|
|
|
|
.post(options.path)
|
|
|
|
.once()
|
|
|
|
.reply(500, { message: 'Something bad happened' });
|
|
|
|
// mockClient.intercept(options).reply(200, { body: 'test' });
|
|
|
|
// mockClient.intercept(options).reply(400, {});
|
|
|
|
// mockClient.intercept(options).reply(500, { message: 'Something bad happened' });
|
2020-09-22 16:43:39 -05:00
|
|
|
|
|
|
|
const name = 'package';
|
|
|
|
const responses = await notify({ name }, multiNotificationConfig, { name: 'foo' }, 'bar');
|
|
|
|
expect(responses).toEqual([true, false, false]);
|
|
|
|
});
|
|
|
|
});
|