2019-05-19 23:07:17 +02:00
|
|
|
import { addVersion, uploadPackageTarball, removeTarball, unPublishPackage, publishPackage } from '../../../../src/api/endpoint/api/publish';
|
|
|
|
import { HTTP_STATUS, API_ERROR } from '../../../../src/lib/constants';
|
2018-10-18 15:44:58 +02:00
|
|
|
|
|
|
|
const REVISION_MOCK = '15-e53a77096b0ee33e';
|
|
|
|
|
|
|
|
describe('Publish endpoints - add a tag', () => {
|
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let next;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
req = {
|
|
|
|
params: {
|
|
|
|
version: '1.0.0',
|
|
|
|
tag: 'tag',
|
|
|
|
package: 'verdaccio',
|
|
|
|
},
|
|
|
|
body: '',
|
|
|
|
};
|
|
|
|
res = {
|
|
|
|
status: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
next = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
addVersion(storage)(req, res, next);
|
|
|
|
|
|
|
|
expect(res.status).toHaveBeenLastCalledWith(HTTP_STATUS.CREATED);
|
|
|
|
expect(next).toHaveBeenLastCalledWith({ ok: 'package published' });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when failed to add a version', done => {
|
|
|
|
const storage = {
|
|
|
|
addVersion: (packageName, version, body, tag, cb) => {
|
|
|
|
const error = {
|
|
|
|
message: 'failure',
|
|
|
|
};
|
|
|
|
cb(error);
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
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',
|
|
|
|
package: 'verdaccio',
|
|
|
|
},
|
|
|
|
pipe: jest.fn(),
|
|
|
|
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(),
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
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',
|
|
|
|
revision: REVISION_MOCK,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
res = { status: jest.fn() };
|
|
|
|
next = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
removeTarball(storage)(req, res, next);
|
|
|
|
expect(res.status).toHaveBeenCalledWith(HTTP_STATUS.CREATED);
|
|
|
|
expect(next).toHaveBeenCalledWith({ ok: 'tarball removed' });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('failed while deleting the tarball', done => {
|
|
|
|
const error = {
|
|
|
|
message: 'deletion failed',
|
|
|
|
};
|
|
|
|
const storage = {
|
|
|
|
removeTarball(packageName, filename, revision, cb) {
|
|
|
|
cb(error);
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
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: {
|
|
|
|
package: 'verdaccio',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
res = { status: jest.fn() };
|
|
|
|
next = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should un-publish package successfully', done => {
|
|
|
|
const storage = {
|
|
|
|
removePackage(packageName, cb) {
|
|
|
|
expect(packageName).toEqual(req.params.package);
|
|
|
|
cb();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
unPublishPackage(storage)(req, res, next);
|
|
|
|
expect(res.status).toHaveBeenCalledWith(HTTP_STATUS.CREATED);
|
|
|
|
expect(next).toHaveBeenCalledWith({ ok: 'package removed' });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('un-publish failed', done => {
|
|
|
|
const error = {
|
|
|
|
message: 'un-publish failed',
|
|
|
|
};
|
|
|
|
const storage = {
|
|
|
|
removePackage(packageName, cb) {
|
|
|
|
cb(error);
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
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: {
|
|
|
|
name: 'verdaccio',
|
|
|
|
},
|
|
|
|
params: {
|
|
|
|
package: 'verdaccio',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
res = { status: jest.fn() };
|
|
|
|
next = jest.fn();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should change the existing package', () => {
|
|
|
|
const storage = {
|
|
|
|
changePackage: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
req.params._rev = REVISION_MOCK;
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
publishPackage(storage)(req, res, next);
|
|
|
|
expect(storage.changePackage).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should add a new package', () => {
|
|
|
|
const storage = {
|
|
|
|
addPackage: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
publishPackage(storage)(req, res, next);
|
|
|
|
expect(storage.addPackage).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw an error while publishing package', () => {
|
|
|
|
const storage = {
|
|
|
|
addPackage() {
|
|
|
|
throw new Error();
|
|
|
|
},
|
|
|
|
};
|
2019-07-16 08:40:01 +02:00
|
|
|
|
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
publishPackage(storage)(req, res, next);
|
|
|
|
expect(next).toHaveBeenCalledWith(new Error(API_ERROR.BAD_PACKAGE_DATA));
|
|
|
|
});
|
|
|
|
|
2019-03-09 14:00:47 +01:00
|
|
|
test('should star a package', () => {
|
|
|
|
const storage = {
|
|
|
|
changePackage: jest.fn(),
|
|
|
|
getPackage({ name, req, callback }) {
|
|
|
|
callback(null, {
|
|
|
|
users: {},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
req = {
|
|
|
|
params: {
|
|
|
|
package: 'verdaccio',
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
_rev: REVISION_MOCK,
|
|
|
|
users: {
|
|
|
|
verdaccio: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remote_user: {
|
|
|
|
name: 'verdaccio',
|
|
|
|
},
|
|
|
|
};
|
2019-07-16 08:40:01 +02:00
|
|
|
|
|
|
|
// @ts-ignore
|
2018-10-18 15:44:58 +02:00
|
|
|
publishPackage(storage)(req, res, next);
|
2019-03-09 14:00:47 +01:00
|
|
|
expect(storage.changePackage).toMatchSnapshot();
|
2018-10-18 15:44:58 +02:00
|
|
|
});
|
|
|
|
});
|