1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/packages/middleware/test/allow.spec.ts
Juan Picado 631abe1ac1
refactor logger module (#3592)
* refactor logger module

* Update index.ts
2023-02-05 21:00:32 +01:00

98 lines
2.1 KiB
TypeScript

import request from 'supertest';
import { HTTP_STATUS } from '@verdaccio/core';
import { logger, setup } from '@verdaccio/logger';
import { allow } from '../src';
import { getApp } from './helper';
setup({});
test('should allow request', async () => {
const can = allow(
{
allow_publish: (params, remove, cb) => {
return cb(null, true);
},
},
logger
);
const app = getApp([]);
// @ts-ignore
app.get('/:package', can('publish'), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/react').expect(HTTP_STATUS.OK);
});
test('should allow scope request', async () => {
const can = allow(
{
allow_publish: (params, remove, cb) => {
return cb(null, true);
},
},
logger
);
const app = getApp([]);
// @ts-ignore
app.get('/:package/:scope', can('publish'), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/@verdaccio/core').expect(HTTP_STATUS.OK);
});
test('should allow filename request', async () => {
const can = allow(
{
allow_publish: (params, remove, cb) => {
return cb(null, true);
},
},
logger
);
const app = getApp([]);
// @ts-ignore
app.get('/:filename', can('publish'), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/aaa-0.0.1.tgz').expect(HTTP_STATUS.OK);
});
test('should not allow request', async () => {
const can = allow(
{
allow_publish: (params, remove, cb) => {
return cb(null, false);
},
},
logger
);
const app = getApp([]);
// @ts-ignore
app.get('/sec', can('publish'), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/sec').expect(HTTP_STATUS.INTERNAL_ERROR);
});
test('should handle error request', async () => {
const can = allow(
{
allow_publish: (params, remove, cb) => {
return cb(Error('foo error'));
},
},
logger
);
const app = getApp([]);
// @ts-ignore
app.get('/err', can('publish'));
return request(app).get('/err').expect(HTTP_STATUS.INTERNAL_ERROR);
});