1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/packages/api/test/publish.spec.ts

300 lines
6.6 KiB
TypeScript
Raw Normal View History

2020-03-03 23:59:19 +01:00
import { addVersion, uploadPackageTarball, removeTarball, unPublishPackage, publishPackage } from '../src/publish';
import { HTTP_STATUS, API_ERROR } from '@verdaccio/dev-commons';
const REVISION_MOCK = '15-e53a77096b0ee33e';
2020-03-03 23:59:19 +01:00
require('@verdaccio/logger').setup([
{ type: 'stdout', format: 'pretty', level: 'info' }
]);
describe('Publish endpoints - add a tag', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
params: {
version: '1.0.0',
tag: 'tag',
2020-03-03 23:59:19 +01:00
package: 'verdaccio',
},
2020-03-03 23:59:19 +01:00
body: '',
};
res = {
2020-03-03 23:59:19 +01:00
status: jest.fn(),
};
next = jest.fn();
});
2020-03-03 23:59:19 +01:00
test('should add a version', done => {
const storage = {
addVersion: (packageName, version, body, tag, cb) => {
expect(packageName).toEqual(req.params.package);
expect(version).toEqual(req.params.version);
expect(body).toEqual(req.body);
expect(tag).toEqual(req.params.tag);
cb();
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
addVersion(storage)(req, res, next);
expect(res.status).toHaveBeenLastCalledWith(HTTP_STATUS.CREATED);
expect(next).toHaveBeenLastCalledWith({ ok: 'package published' });
});
2020-03-03 23:59:19 +01:00
test('when failed to add a version', done => {
const storage = {
addVersion: (packageName, version, body, tag, cb) => {
const error = {
2020-03-03 23:59:19 +01:00
message: 'failure',
};
cb(error);
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
addVersion(storage)(req, res, next);
expect(next).toHaveBeenLastCalledWith({ message: 'failure' });
});
});
/**
* upload package: '/:package/-/:filename/*'
*/
describe('Publish endpoints - upload package tarball', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
params: {
filename: 'verdaccio.gzip',
2020-03-03 23:59:19 +01:00
package: 'verdaccio',
},
pipe: jest.fn(),
2020-03-03 23:59:19 +01:00
on: jest.fn(),
};
res = { status: jest.fn(), report_error: jest.fn() };
next = jest.fn();
});
test('should upload package tarball successfully', () => {
const stream = {
done: jest.fn(),
abort: jest.fn(),
2020-03-03 23:59:19 +01:00
on: jest.fn(() => (status, cb) => cb()),
};
const storage = {
addTarball(packageName, filename) {
expect(packageName).toEqual(req.params.package);
expect(filename).toEqual(req.params.filename);
return stream;
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
uploadPackageTarball(storage)(req, res, next);
expect(req.pipe).toHaveBeenCalled();
expect(req.on).toHaveBeenCalled();
});
});
/**
* Delete tarball: '/:package/-/:filename/-rev/:revision'
*/
describe('Publish endpoints - delete tarball', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
params: {
filename: 'verdaccio.gzip',
package: 'verdaccio',
2020-03-03 23:59:19 +01:00
revision: REVISION_MOCK,
},
};
res = { status: jest.fn() };
next = jest.fn();
});
2020-03-03 23:59:19 +01:00
test('should delete tarball successfully', done => {
const storage = {
removeTarball(packageName, filename, revision, cb) {
expect(packageName).toEqual(req.params.package);
expect(filename).toEqual(req.params.filename);
expect(revision).toEqual(req.params.revision);
cb();
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
removeTarball(storage)(req, res, next);
expect(res.status).toHaveBeenCalledWith(HTTP_STATUS.CREATED);
expect(next).toHaveBeenCalledWith({ ok: 'tarball removed' });
});
2020-03-03 23:59:19 +01:00
test('failed while deleting the tarball', done => {
const error = {
2020-03-03 23:59:19 +01:00
message: 'deletion failed',
};
const storage = {
removeTarball(packageName, filename, revision, cb) {
cb(error);
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
removeTarball(storage)(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});
/**
* Un-publish package: '/:package/-rev/*'
*/
describe('Publish endpoints - un-publish package', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
params: {
2020-03-03 23:59:19 +01:00
package: 'verdaccio',
},
};
res = { status: jest.fn() };
next = jest.fn();
});
2020-03-03 23:59:19 +01:00
test('should un-publish package successfully', done => {
const storage = {
removePackage(packageName, cb) {
expect(packageName).toEqual(req.params.package);
cb();
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
unPublishPackage(storage)(req, res, next);
expect(res.status).toHaveBeenCalledWith(HTTP_STATUS.CREATED);
expect(next).toHaveBeenCalledWith({ ok: 'package removed' });
});
2020-03-03 23:59:19 +01:00
test('un-publish failed', done => {
const error = {
2020-03-03 23:59:19 +01:00
message: 'un-publish failed',
};
const storage = {
removePackage(packageName, cb) {
cb(error);
done();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
unPublishPackage(storage)(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});
/**
* Publish package: '/:package/:_rev?/:revision?'
*/
describe('Publish endpoints - publish package', () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
body: {
2020-03-03 23:59:19 +01:00
name: 'verdaccio',
},
params: {
2020-03-03 23:59:19 +01:00
package: 'verdaccio',
},
};
res = { status: jest.fn() };
next = jest.fn();
});
test('should change the existing package', () => {
const storage = {
2020-03-03 23:59:19 +01:00
changePackage: jest.fn(),
};
req.params._rev = REVISION_MOCK;
// @ts-ignore
publishPackage(storage)(req, res, next);
expect(storage.changePackage).toMatchSnapshot();
});
test('should publish a new a new package', () => {
const storage = {
2020-03-03 23:59:19 +01:00
addPackage: jest.fn(),
};
// @ts-ignore
publishPackage(storage)(req, res, next);
expect(storage.addPackage).toMatchSnapshot();
});
test('should throw an error while publishing package', () => {
const storage = {
addPackage() {
throw new Error();
2020-03-03 23:59:19 +01:00
},
};
// @ts-ignore
publishPackage(storage)(req, res, next);
expect(next).toHaveBeenCalledWith(new Error(API_ERROR.BAD_PACKAGE_DATA));
});
describe('test start', () => {
test('should star a package', () => {
const storage = {
changePackage: jest.fn(),
2019-09-26 18:22:14 +02:00
getPackage({ callback }) {
callback(null, {
2020-03-03 23:59:19 +01:00
users: {},
});
2020-03-03 23:59:19 +01:00
},
};
req = {
params: {
2020-03-03 23:59:19 +01:00
package: 'verdaccio',
},
body: {
_rev: REVISION_MOCK,
users: {
2020-03-03 23:59:19 +01:00
verdaccio: true,
},
},
remote_user: {
2020-03-03 23:59:19 +01:00
name: 'verdaccio',
},
};
// @ts-ignore
publishPackage(storage)(req, res, next);
expect(storage.changePackage).toMatchSnapshot();
});
});
});