0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/middleware/test/params.spec.ts
Juan Picado a1986e098d
feat: expose middleware utils (#3580)
* feat: expose middleware utils

* feat: expose middleware utils

* Update antiLoop.ts

* Update e2e-ci.yml
2023-01-29 15:08:50 +01:00

83 lines
2.3 KiB
TypeScript

import request from 'supertest';
import { HTTP_STATUS } from '@verdaccio/core';
import { match, validateName, validatePackage } from '../src';
import { getApp } from './helper';
describe('validate params', () => {
test('should validate package name', async () => {
const app = getApp([]);
// @ts-ignore
app.param('package', validatePackage);
app.get('/pkg/:package', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/pkg/react').expect(HTTP_STATUS.OK);
});
test('should fails validate package name', async () => {
const app = getApp([]);
// @ts-ignore
app.param('package', validatePackage);
app.get('/pkg/:package', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/pkg/node_modules').expect(HTTP_STATUS.FORBIDDEN);
});
test('should fails file name package name', async () => {
const app = getApp([]);
// @ts-ignore
app.param('filename', validateName);
app.get('/file/:filename', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/file/__proto__').expect(HTTP_STATUS.FORBIDDEN);
});
test('should validate file name package name', async () => {
const app = getApp([]);
// @ts-ignore
app.param('filename', validateName);
app.get('/file/:filename', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/file/react.tar.gz').expect(HTTP_STATUS.OK);
});
});
describe('match', () => {
test('should not match middleware', async () => {
const app = getApp([]);
app.param('_rev', match(/^-rev$/));
app.param('org_couchdb_user', match(/^org\.couchdb\.user:/));
app.get('/-/user/:org_couchdb_user', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
app.use((res: any) => {
res.status(HTTP_STATUS.INTERNAL_ERROR);
});
return request(app).get('/-/user/test').expect(HTTP_STATUS.INTERNAL_ERROR);
});
test('should match middleware', async () => {
const app = getApp([]);
app.param('_rev', match(/^-rev$/));
app.get('/-/user/:_rev?', (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
app.use((res: any) => {
res.status(HTTP_STATUS.INTERNAL_ERROR);
});
return request(app).get('/-/user/-rev').expect(HTTP_STATUS.OK);
});
});