0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/hooks/test/notify-request.spec.ts
Juan Picado 0c7b19b192
chore: migrate vitest packages (#4777)
* chore: migrate core to vitest

* chore: migrate url module

* chore: migrate tarball package

* chore: migrate loader package

* chore: migrate hook package

* chore: migrate signature package

* chore: migrate utils package
2024-08-03 19:39:22 +02:00

83 lines
2.6 KiB
TypeScript

import nock from 'nock';
import { beforeEach, describe, expect, test } from 'vitest';
import { createRemoteUser, parseConfigFile } from '@verdaccio/config';
import { setup } from '@verdaccio/logger';
import { Config } from '@verdaccio/types';
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'));
setup({});
const domain = 'http://slack-service';
const options = {
path: '/foo?auth_token=mySecretToken',
};
describe('Notifications:: notifyRequest', () => {
beforeEach(() => {
nock.cleanAll();
});
test('when sending a empty notification', async () => {
nock(domain).post(options.path).reply(200, { body: 'test' });
const notificationResponse = await notify({}, {}, createRemoteUser('foo', []), 'bar');
expect(notificationResponse).toEqual([false]);
});
test('when sending a single notification', async () => {
nock(domain).post(options.path).reply(200, { body: 'test' });
const notificationResponse = await notify(
{},
singleHeaderNotificationConfig,
createRemoteUser('foo', []),
'bar'
);
expect(notificationResponse).toEqual([true]);
});
test('when notification endpoint is missing', async () => {
nock(domain).post(options.path).reply(200, { body: 'test' });
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 () => {
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' });
const name = 'package';
const responses = await notify({ name }, multiNotificationConfig, { name: 'foo' }, 'bar');
expect(responses).toEqual([true, false, false]);
});
});