2017-12-17 16:04:26 -05:00
|
|
|
import path from 'path';
|
2020-08-11 00:21:51 -05:00
|
|
|
import rimRaf from 'rimraf';
|
2019-02-24 17:20:25 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
import { Config as AppConfig } from '@verdaccio/config';
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
import { logger, setup } from '@verdaccio/logger';
|
2020-03-03 17:59:19 -05:00
|
|
|
import { configExample, generateNewVersion } from '@verdaccio/mock';
|
2020-08-13 16:27:00 -05:00
|
|
|
import { IStorage } from '@verdaccio/dev-types';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
|
|
|
const readMetadata = (fileName = 'metadata') => readFile(`../fixtures/${fileName}`).toString();
|
2020-08-13 16:27:00 -05:00
|
|
|
import { Config, MergeTags, Package } from '@verdaccio/types';
|
|
|
|
import { API_ERROR, HTTP_STATUS, DIST_TAGS } from '@verdaccio/dev-commons';
|
2019-07-16 01:40:01 -05:00
|
|
|
import { VerdaccioError } from '@verdaccio/commons-api';
|
2020-08-13 16:27:00 -05:00
|
|
|
import { LocalStorage } from '../src/local-storage';
|
|
|
|
import { generatePackageTemplate } from '../src/storage-utils';
|
|
|
|
import { readFile } from './fixtures/test.utils';
|
2017-12-17 12:33:09 -05:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
|
|
|
describe('LocalStorage', () => {
|
|
|
|
let storage: IStorage;
|
2019-07-16 01:40:01 -05:00
|
|
|
const pkgName = 'npm_test';
|
2017-12-17 16:04:26 -05:00
|
|
|
const pkgNameScoped = `@scope/${pkgName}-scope`;
|
2019-07-16 01:40:01 -05:00
|
|
|
const tarballName = `${pkgName}-add-tarball-1.0.4.tgz`;
|
|
|
|
const tarballName2 = `${pkgName}-add-tarball-1.0.5.tgz`;
|
2019-01-29 22:10:10 -05:00
|
|
|
|
|
|
|
const getStorage = (LocalStorageClass = LocalStorage) => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const config: Config = new AppConfig(
|
|
|
|
configExample({
|
|
|
|
self_path: path.join('../partials/store'),
|
|
|
|
})
|
|
|
|
);
|
2019-07-16 01:40:01 -05:00
|
|
|
|
|
|
|
return new LocalStorageClass(config, logger);
|
2020-08-13 16:27:00 -05:00
|
|
|
};
|
2019-01-29 22:10:10 -05:00
|
|
|
|
2019-08-16 14:20:18 -05:00
|
|
|
const getPackageMetadataFromStore = (pkgName: string): Promise<Package> => {
|
2018-07-29 08:26:22 -05:00
|
|
|
return new Promise((resolve) => {
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.getPackageMetadata(pkgName, (err, data) => {
|
2018-07-29 08:26:22 -05:00
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-29 03:28:28 -05:00
|
|
|
const addNewVersion = (pkgName: string, version: string) => {
|
|
|
|
return new Promise((resolve) => {
|
2020-03-03 17:59:19 -05:00
|
|
|
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version), '', (err, data) => {
|
|
|
|
resolve(data);
|
|
|
|
});
|
2018-07-29 03:28:28 -05:00
|
|
|
});
|
|
|
|
};
|
2018-07-29 06:57:32 -05:00
|
|
|
const addTarballToStore = (pkgName: string, tarballName) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
const tarballData = JSON.parse(readMetadata('addTarball').toString());
|
2018-07-29 06:57:32 -05:00
|
|
|
const stream = storage.addTarball(pkgName, tarballName);
|
|
|
|
|
2018-07-29 09:02:08 -05:00
|
|
|
stream.on('error', (err) => {
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err).toBeNull();
|
|
|
|
reject();
|
|
|
|
});
|
2018-07-29 09:02:08 -05:00
|
|
|
stream.on('success', () => {
|
2018-07-29 06:57:32 -05:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
2020-11-03 01:55:08 -05:00
|
|
|
stream.end(Buffer.from(tarballData.data, 'base64'));
|
2018-07-29 06:57:32 -05:00
|
|
|
stream.done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-29 03:28:28 -05:00
|
|
|
const addPackageToStore = (pkgName, metadata) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-29 03:28:28 -05:00
|
|
|
const pkgStoragePath = storage._getLocalStorage(pkgName);
|
|
|
|
rimRaf(pkgStoragePath.path, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
storage.addPackage(pkgName, metadata, async (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-07-29 09:02:08 -05:00
|
|
|
beforeAll(() => {
|
2019-01-29 22:10:10 -05:00
|
|
|
storage = getStorage();
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be defined', () => {
|
|
|
|
expect(storage).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
describe('LocalStorage::preparePackage', () => {
|
2017-12-17 12:33:09 -05:00
|
|
|
test('should add a package', (done) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
const metadata = JSON.parse(readMetadata().toString());
|
|
|
|
// @ts-ignore
|
2017-12-17 16:50:09 -05:00
|
|
|
const pkgStoragePath = storage._getLocalStorage(pkgName);
|
2017-12-17 12:33:09 -05:00
|
|
|
rimRaf(pkgStoragePath.path, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
storage.addPackage(pkgName, metadata, (err, data) => {
|
|
|
|
expect(data.version).toMatch(/1.0.0/);
|
|
|
|
expect(data.dist.tarball).toMatch(/npm_test-1.0.0.tgz/);
|
2017-12-17 16:50:09 -05:00
|
|
|
expect(data.name).toEqual(pkgName);
|
2017-12-17 12:33:09 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-17 16:04:26 -05:00
|
|
|
test('should add a @scope package', (done) => {
|
|
|
|
const metadata = JSON.parse(readMetadata());
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2017-12-17 16:50:09 -05:00
|
|
|
const pkgStoragePath = storage._getLocalStorage(pkgNameScoped);
|
2017-12-17 16:04:26 -05:00
|
|
|
|
|
|
|
rimRaf(pkgStoragePath.path, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
storage.addPackage(pkgNameScoped, metadata, (err, data) => {
|
|
|
|
expect(data.version).toMatch(/1.0.0/);
|
|
|
|
expect(data.dist.tarball).toMatch(/npm_test-1.0.0.tgz/);
|
2017-12-17 16:50:09 -05:00
|
|
|
expect(data.name).toEqual(pkgName);
|
2017-12-17 16:04:26 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-17 12:33:09 -05:00
|
|
|
test('should fails on add a package', (done) => {
|
|
|
|
const metadata = JSON.parse(readMetadata());
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
storage.addPackage(pkgName, metadata, (err) => {
|
2017-12-17 12:33:09 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.CONFLICT);
|
|
|
|
expect(err.message).toMatch(API_ERROR.PACKAGE_EXIST);
|
2017-12-17 12:33:09 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-29 08:26:22 -05:00
|
|
|
describe('LocalStorage::mergeTags', () => {
|
|
|
|
test('should mergeTags', async (done) => {
|
|
|
|
const pkgName = 'merge-tags-test-1';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
await addNewVersion(pkgName, '1.0.0');
|
|
|
|
await addNewVersion(pkgName, '2.0.0');
|
|
|
|
await addNewVersion(pkgName, '3.0.0');
|
|
|
|
const tags: MergeTags = {
|
|
|
|
beta: '3.0.0',
|
2020-08-13 16:27:00 -05:00
|
|
|
latest: '2.0.0',
|
2018-07-29 08:26:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
storage.mergeTags(pkgName, tags, async (err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toBeUndefined();
|
2019-08-16 14:20:18 -05:00
|
|
|
const metadata: Package = await getPackageMetadataFromStore(pkgName);
|
2018-07-29 08:26:22 -05:00
|
|
|
expect(metadata[DIST_TAGS]).toBeDefined();
|
|
|
|
expect(metadata[DIST_TAGS]['beta']).toBeDefined();
|
|
|
|
expect(metadata[DIST_TAGS]['beta']).toBe('3.0.0');
|
|
|
|
expect(metadata[DIST_TAGS]['latest']).toBe('2.0.0');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails mergeTags version not found', async (done) => {
|
|
|
|
const pkgName = 'merge-tags-test-1';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
// const tarballName: string = `${pkgName}-${version}.tgz`;
|
|
|
|
await addNewVersion(pkgName, '1.0.0');
|
|
|
|
await addNewVersion(pkgName, '2.0.0');
|
|
|
|
await addNewVersion(pkgName, '3.0.0');
|
|
|
|
const tags: MergeTags = {
|
2020-08-13 16:27:00 -05:00
|
|
|
beta: '9999.0.0',
|
2018-07-29 08:26:22 -05:00
|
|
|
};
|
|
|
|
|
2019-08-16 14:20:18 -05:00
|
|
|
storage.mergeTags(pkgName, tags, async (err) => {
|
2018-07-29 08:26:22 -05:00
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.NOT_FOUND);
|
|
|
|
expect(err.message).toMatch(API_ERROR.VERSION_NOT_EXIST);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on mergeTags', async (done) => {
|
|
|
|
const tags: MergeTags = {
|
|
|
|
beta: '3.0.0',
|
2020-08-13 16:27:00 -05:00
|
|
|
latest: '2.0.0',
|
2018-07-29 08:26:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
storage.mergeTags('not-found', tags, async (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.NOT_FOUND);
|
|
|
|
expect(err.message).toMatch(API_ERROR.NO_PACKAGE);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
describe('LocalStorage::addVersion', () => {
|
2018-07-29 06:57:32 -05:00
|
|
|
test('should add new version without tag', async (done) => {
|
|
|
|
const pkgName = 'add-version-test-1';
|
|
|
|
const version = '1.0.1';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
2019-07-16 01:40:01 -05:00
|
|
|
const tarballName = `${pkgName}-${version}.tgz`;
|
2018-07-29 06:57:32 -05:00
|
|
|
await addNewVersion(pkgName, '9.0.0');
|
|
|
|
await addTarballToStore(pkgName, `${pkgName}-9.0.0.tgz`);
|
|
|
|
await addTarballToStore(pkgName, tarballName);
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version), '', (err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toBeUndefined();
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-29 06:57:32 -05:00
|
|
|
test('should fails on add a duplicated version without tag', async (done) => {
|
|
|
|
const pkgName = 'add-version-test-2';
|
|
|
|
const version = '1.0.1';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
await addNewVersion(pkgName, version);
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version), '', (err) => {
|
2018-04-20 12:40:48 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.CONFLICT);
|
|
|
|
expect(err.message).toMatch(API_ERROR.PACKAGE_EXIST);
|
2018-04-20 12:40:48 -05:00
|
|
|
done();
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-29 06:57:32 -05:00
|
|
|
test('should fails add new version wrong shasum', async (done) => {
|
|
|
|
const pkgName = 'add-version-test-4';
|
|
|
|
const version = '4.0.0';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
2019-07-16 01:40:01 -05:00
|
|
|
const tarballName = `${pkgName}-${version}.tgz`;
|
2018-07-29 06:57:32 -05:00
|
|
|
await addTarballToStore(pkgName, tarballName);
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version, 'fake'), '', (err) => {
|
2020-03-03 17:59:19 -05:00
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.BAD_REQUEST);
|
|
|
|
expect(err.message).toMatch(/shasum error/);
|
|
|
|
done();
|
|
|
|
});
|
2018-07-29 06:57:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should add new second version without tag', async (done) => {
|
|
|
|
const pkgName = 'add-version-test-3';
|
|
|
|
const version = '1.0.2';
|
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
await addNewVersion(pkgName, '1.0.1');
|
|
|
|
await addNewVersion(pkgName, '1.0.3');
|
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
storage.addVersion(pkgName, version, generateNewVersion(pkgName, version), 'beta', (err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toBeUndefined();
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
describe('LocalStorage::updateVersions', () => {
|
2019-01-28 22:59:31 -05:00
|
|
|
const metadata = JSON.parse(readMetadata('metadata-update-versions-tags'));
|
|
|
|
const pkgName = 'add-update-versions-test-1';
|
|
|
|
const version = '1.0.2';
|
2019-01-29 22:10:10 -05:00
|
|
|
let _storage;
|
2020-08-13 16:27:00 -05:00
|
|
|
beforeEach((done) => {
|
2019-01-29 22:10:10 -05:00
|
|
|
class MockLocalStorage extends LocalStorage {}
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
MockLocalStorage.prototype._writePackage = jest.fn(LocalStorage.prototype._writePackage);
|
2019-01-29 22:10:10 -05:00
|
|
|
_storage = getStorage(MockLocalStorage);
|
2019-02-24 17:20:25 -05:00
|
|
|
rimRaf(path.join(configExample().storage, pkgName), async () => {
|
2019-01-29 22:10:10 -05:00
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
await addNewVersion(pkgName, '1.0.1');
|
|
|
|
await addNewVersion(pkgName, version);
|
|
|
|
done();
|
2020-08-13 16:27:00 -05:00
|
|
|
});
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2019-01-28 22:59:31 -05:00
|
|
|
test('should update versions from external source', async (done) => {
|
2019-01-29 22:10:10 -05:00
|
|
|
_storage.updateVersions(pkgName, metadata, (err, data) => {
|
2018-04-20 12:40:48 -05:00
|
|
|
expect(err).toBeNull();
|
2019-01-29 22:10:10 -05:00
|
|
|
expect(_storage._writePackage).toHaveBeenCalledTimes(1);
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(data.versions['1.0.1']).toBeDefined();
|
|
|
|
expect(data.versions[version]).toBeDefined();
|
|
|
|
expect(data.versions['1.0.4']).toBeDefined();
|
|
|
|
expect(data[DIST_TAGS]['latest']).toBeDefined();
|
|
|
|
expect(data[DIST_TAGS]['latest']).toBe('1.0.1');
|
|
|
|
expect(data[DIST_TAGS]['beta']).toBeDefined();
|
|
|
|
expect(data[DIST_TAGS]['beta']).toBe('1.0.2');
|
|
|
|
expect(data[DIST_TAGS]['next']).toBeDefined();
|
|
|
|
expect(data[DIST_TAGS]['next']).toBe('1.0.4');
|
|
|
|
expect(data['_rev'] === metadata['_rev']).toBeFalsy();
|
|
|
|
expect(data.readme).toBe('readme 1.0.4');
|
2018-04-20 12:40:48 -05:00
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
2019-01-28 22:59:31 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should not update if the metadata match', (done) => {
|
|
|
|
_storage.updateVersions(pkgName, metadata, (e) => {
|
|
|
|
expect(e).toBeNull();
|
|
|
|
_storage.updateVersions(pkgName, metadata, (err) => {
|
|
|
|
expect(err).toBeNull();
|
2019-01-29 22:10:10 -05:00
|
|
|
expect(_storage._writePackage).toHaveBeenCalledTimes(1);
|
2020-08-13 16:27:00 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
describe('LocalStorage::changePackage', () => {
|
2019-07-16 01:40:01 -05:00
|
|
|
const pkgName = 'change-package';
|
2018-07-29 03:28:28 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should unpublish a version', async (done) => {
|
2018-07-29 03:28:28 -05:00
|
|
|
await addPackageToStore(pkgName, generatePackageTemplate(pkgName));
|
|
|
|
await addNewVersion(pkgName, '1.0.1');
|
|
|
|
await addNewVersion(pkgName, '1.0.2');
|
|
|
|
await addNewVersion(pkgName, '1.0.3');
|
|
|
|
const metadata = JSON.parse(readMetadata('changePackage/metadata-change'));
|
2018-04-20 12:40:48 -05:00
|
|
|
const rev: string = metadata['_rev'];
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
storage.changePackage(pkgName, metadata, rev, (err) => {
|
|
|
|
expect(err).toBeUndefined();
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.getPackageMetadata(pkgName, (err, data) => {
|
2018-07-29 03:28:28 -05:00
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data.versions['1.0.1']).toBeDefined();
|
|
|
|
expect(data.versions['1.0.2']).toBeUndefined();
|
|
|
|
expect(data.versions['1.0.3']).toBeUndefined();
|
|
|
|
done();
|
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
describe('LocalStorage::tarball operations', () => {
|
|
|
|
describe('LocalStorage::addTarball', () => {
|
|
|
|
test('should add a new tarball', (done) => {
|
|
|
|
const tarballData = JSON.parse(readMetadata('addTarball'));
|
|
|
|
const stream = storage.addTarball(pkgName, tarballName);
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-07-29 09:02:08 -05:00
|
|
|
stream.on('error', (err) => {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('success', function () {
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2020-11-03 01:55:08 -05:00
|
|
|
stream.end(Buffer.from(tarballData.data, 'base64'));
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
test('should add a new second tarball', (done) => {
|
|
|
|
const tarballData = JSON.parse(readMetadata('addTarball'));
|
|
|
|
const stream = storage.addTarball(pkgName, tarballName2);
|
2018-07-29 09:02:08 -05:00
|
|
|
stream.on('error', (err) => {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('success', function () {
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-03 01:55:08 -05:00
|
|
|
stream.end(Buffer.from(tarballData.data, 'base64'));
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
test('should fails on add a duplicated new tarball ', (done) => {
|
2018-05-30 14:48:33 -05:00
|
|
|
const tarballData = JSON.parse(readMetadata('addTarball'));
|
|
|
|
const stream = storage.addTarball(pkgName, tarballName);
|
2019-07-16 01:40:01 -05:00
|
|
|
stream.on('error', (err: VerdaccioError) => {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.CONFLICT);
|
2018-12-06 02:34:42 -05:00
|
|
|
expect(err.message).toMatch(/this package is already present/);
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
2020-11-03 01:55:08 -05:00
|
|
|
stream.end(Buffer.from(tarballData.data, 'base64'));
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
test('should fails on add a new tarball on missing package', (done) => {
|
|
|
|
const tarballData = JSON.parse(readMetadata('addTarball'));
|
|
|
|
const stream = storage.addTarball('unexsiting-package', tarballName);
|
2019-07-16 01:40:01 -05:00
|
|
|
stream.on('error', (err: VerdaccioError) => {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-10-05 01:20:43 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.NOT_FOUND);
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err.message).toMatch(/no such package available/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-07-29 09:02:08 -05:00
|
|
|
stream.on('success', () => {
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-11-03 01:55:08 -05:00
|
|
|
stream.end(Buffer.from(tarballData.data, 'base64'));
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
test('should fails on use invalid package name on add a new tarball', (done) => {
|
|
|
|
const stream = storage.addTarball(pkgName, `${pkgName}-fails-add-tarball-1.0.4.tgz`);
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('error', function (err: VerdaccioError) {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.BAD_DATA);
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err.message).toMatch(/refusing to accept zero-length file/);
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
test('should fails on abort on add a new tarball', (done) => {
|
2018-12-06 02:34:42 -05:00
|
|
|
const stream = storage.addTarball('__proto__', `${pkgName}-fails-add-tarball-1.0.4.tgz`);
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.abort();
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('error', function (err: VerdaccioError) {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.FORBIDDEN);
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err.message).toMatch(/can't use this filename/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.done();
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2018-08-02 14:44:59 -05:00
|
|
|
});
|
|
|
|
describe('LocalStorage::removeTarball', () => {
|
|
|
|
test('should remove a tarball', (done) => {
|
|
|
|
storage.removeTarball(pkgName, tarballName2, 'rev', (err, pkg) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(pkg).toBeUndefined();
|
|
|
|
done();
|
2018-05-30 14:48:33 -05:00
|
|
|
});
|
2018-08-02 14:44:59 -05:00
|
|
|
});
|
2018-05-30 14:48:33 -05:00
|
|
|
|
2018-08-02 14:44:59 -05:00
|
|
|
test('should remove a tarball that does not exist', (done) => {
|
|
|
|
storage.removeTarball(pkgName, tarballName2, 'rev', (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.NOT_FOUND);
|
|
|
|
expect(err.message).toMatch(/no such file available/);
|
|
|
|
done();
|
2018-05-30 14:48:33 -05:00
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
describe('LocalStorage::getTarball', () => {
|
|
|
|
test('should get a existing tarball', (done) => {
|
|
|
|
const stream = storage.getTarball(pkgName, tarballName);
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('content-length', function (contentLength) {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(contentLength).toBe(279);
|
|
|
|
done();
|
|
|
|
});
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('open', function () {
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
test('should fails on get a tarball that does not exist', (done) => {
|
|
|
|
const stream = storage.getTarball('fake', tarballName);
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('error', function (err: VerdaccioError) {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.statusCode).toEqual(HTTP_STATUS.NOT_FOUND);
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err.message).toMatch(/no such file available/);
|
|
|
|
done();
|
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
describe('LocalStorage::search', () => {
|
|
|
|
test('should find a tarball', (done) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-05-30 14:48:33 -05:00
|
|
|
const stream = storage.search('99999');
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-05-30 14:48:33 -05:00
|
|
|
stream.on('data', function each(pkg) {
|
|
|
|
expect(pkg.name).toEqual(pkgName);
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('error', function (err) {
|
2018-05-30 14:48:33 -05:00
|
|
|
expect(err).not.toBeNull();
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
stream.on('end', function () {
|
2018-05-30 14:48:33 -05:00
|
|
|
done();
|
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2018-05-30 14:48:33 -05:00
|
|
|
});
|
2018-04-20 12:40:48 -05:00
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
describe('LocalStorage::removePackage', () => {
|
2018-05-31 02:50:16 -05:00
|
|
|
test.skip('should remove completely package', (done) => {
|
2018-04-20 12:40:48 -05:00
|
|
|
storage.removePackage(pkgName, (err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toBeUndefined();
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
test('should remove completely @scoped package', (done) => {
|
|
|
|
storage.removePackage(pkgNameScoped, (err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toBeUndefined();
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
test('should fails with package not found', (done) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
const pkgName = 'npm_test_fake';
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.removePackage(pkgName, (err) => {
|
2018-04-20 12:40:48 -05:00
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.message).toMatch(/no such package available/);
|
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-04-20 12:40:48 -05:00
|
|
|
test('should fails with @scoped package not found', (done) => {
|
2020-08-13 16:27:00 -05:00
|
|
|
storage.removePackage(pkgNameScoped, (err) => {
|
2018-04-20 12:40:48 -05:00
|
|
|
expect(err).not.toBeNull();
|
2018-07-29 06:57:32 -05:00
|
|
|
expect(err.message).toMatch(API_ERROR.NO_PACKAGE);
|
2018-04-20 12:40:48 -05:00
|
|
|
done();
|
|
|
|
});
|
2017-12-17 16:04:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-17 12:33:09 -05:00
|
|
|
});
|