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

207 lines
6.5 KiB
TypeScript
Raw Normal View History

2018-06-28 08:26:07 +02:00
import path from 'path';
import fs from 'fs';
2020-03-03 23:59:19 +01:00
import {Writable} from 'stream';
import { Config as AppConfig } from '@verdaccio/config';
import { Storage } from '@verdaccio/store';
import {IStorageHandler} from '@verdaccio/dev-types';
import {Config} from '@verdaccio/types';
import {API_ERROR, HTTP_STATUS} from '@verdaccio/dev-commons';
import {mockServer, configExample, DOMAIN_SERVERS, generateRamdonStorage} from '@verdaccio/mock';
import {setup, logger} from '@verdaccio/logger';
2018-02-19 19:29:14 +01:00
setup([]);
2018-02-19 19:29:14 +01:00
2020-03-03 23:59:19 +01:00
const mockServerPort = 55548;
2020-03-03 23:59:19 +01:00
const generateStorage = async function() {
const storagePath = generateRamdonStorage();
const storageConfig = configExample({
self_path: storagePath,
storage: storagePath,
uplinks: {
npmjs: {
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`
}
2020-03-03 23:59:19 +01:00
}
}, 'store.spec.yaml', __dirname);
2018-06-24 22:39:09 +02:00
const config: Config = new AppConfig(storageConfig);
const store: IStorageHandler = new Storage(config);
await store.init(config, []);
2018-02-19 19:29:14 +01:00
2018-06-24 22:39:09 +02:00
return store;
};
2020-03-03 23:59:19 +01:00
const generateSameUplinkStorage = async function() {
const storagePath = generateRamdonStorage();
console.log("-->storagePath", storagePath);
const storageConfig = configExample({
self_path: storagePath,
storage: storagePath,
packages: {
jquery: {
access: ['$all'],
publish: ['$all'],
proxy: ['cached'],
},
2020-03-03 23:59:19 +01:00
'@jquery/*': {
access: ['$all'],
publish: ['$all'],
proxy: ['notcached'],
}
},
2020-03-03 23:59:19 +01:00
uplinks: {
cached: {
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`,
cache: true,
},
notcached: {
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`,
cache: false,
}
}
}, 'store.spec.yaml', __dirname);
const config: Config = new AppConfig(storageConfig);
const store: IStorageHandler = new Storage(config);
await store.init(config, []);
return store;
};
2020-03-03 23:59:19 +01:00
const createNullStream = () => new Writable({
write: function(chunk, encoding, next) {
next();
}
});
2018-02-19 19:29:14 +01:00
describe('StorageTest', () => {
2018-06-24 22:39:09 +02:00
let mockRegistry;
2018-02-19 19:29:14 +01:00
2020-03-03 23:59:19 +01:00
beforeAll(async done => {
const binPath = require.resolve('verdaccio/bin/verdaccio');
const storePath = path.join(__dirname, '/mock/store');
mockRegistry = await mockServer(mockServerPort, { storePath, silence: true }).init(binPath);
done();
2018-06-24 22:39:09 +02:00
});
2018-02-19 19:29:14 +01:00
2020-03-03 23:59:19 +01:00
afterAll(function(done) {
const [registry, pid] = mockRegistry;
registry.stop();
logger.info(`registry ${pid} has been stopped`);
2018-06-24 22:39:09 +02:00
done();
});
2018-02-19 19:29:14 +01:00
2018-06-24 22:39:09 +02:00
test('should be defined', async () => {
const storage: IStorageHandler = await generateStorage();
2018-02-19 19:29:14 +01:00
expect(storage).toBeDefined();
2018-06-24 22:39:09 +02:00
});
2018-02-19 19:29:14 +01:00
describe('test getTarball', () => {
2020-03-03 23:59:19 +01:00
test.skip('should select right uplink given package.proxy for upstream tarballs', async (done) => {
const storage: IStorageHandler = await generateSameUplinkStorage();
const notcachedSpy = jest.spyOn(storage.uplinks.notcached, 'fetchTarball');
const cachedSpy = jest.spyOn(storage.uplinks.cached, 'fetchTarball');
await new Promise((res, rej) => {
const reader = storage.getTarball('jquery', 'jquery-1.5.1.tgz');
reader.on('end', () => {
expect(notcachedSpy).toHaveBeenCalledTimes(0);
expect(cachedSpy).toHaveBeenCalledTimes(1);
expect(cachedSpy).toHaveBeenCalledWith('http://0.0.0.0:55548/jquery/-/jquery-1.5.1.tgz');
res();
});
reader.on('error', (err) => {
rej(err);
});
reader.pipe(createNullStream());
});
// Reset counters.
cachedSpy.mockClear();
notcachedSpy.mockClear();
await new Promise((res, rej) => {
const reader = storage.getTarball('@jquery/jquery', 'jquery-1.5.1.tgz');
reader.on('end', () => {
expect(cachedSpy).toHaveBeenCalledTimes(0);
expect(notcachedSpy).toHaveBeenCalledTimes(1);
2020-03-03 23:59:19 +01:00
expect(notcachedSpy).toHaveBeenCalledWith('http://0.0.0.0:55548/@jquery%2fjquery/-/jquery-1.5.1.tgz');
res();
});
reader.on('error', (err) => {
rej(err);
});
reader.pipe(createNullStream());
});
done();
});
});
2018-06-27 08:52:52 +02:00
describe('test _syncUplinksMetadata', () => {
test('should fetch from uplink jquery metadata from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
2018-02-19 19:29:14 +01:00
// @ts-ignore
2019-09-26 18:22:14 +02:00
storage._syncUplinksMetadata('jquery', null, {}, (err, metadata) => {
2018-06-27 08:52:52 +02:00
expect(err).toBeNull();
expect(metadata).toBeDefined();
expect(metadata).toBeInstanceOf(Object);
done();
});
2018-06-24 22:39:09 +02:00
});
2018-02-19 19:29:14 +01:00
2018-06-27 08:52:52 +02:00
test('should fails on fetch from uplink non existing from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
// @ts-ignore
2018-06-27 08:52:52 +02:00
storage._syncUplinksMetadata('@verdaccio/404', null, {}, (err, metadata, errors) => {
expect(err).not.toBeNull();
expect(errors).toBeInstanceOf(Array);
expect(errors[0][0].statusCode).toBe(HTTP_STATUS.NOT_FOUND);
expect(errors[0][0].message).toMatch(API_ERROR.NOT_PACKAGE_UPLINK);
done();
});
});
test('should fails on fetch from uplink corrupted pkg from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
2018-02-19 19:29:14 +01:00
// @ts-ignore
2018-06-27 08:52:52 +02:00
storage._syncUplinksMetadata('corrupted-package', null, {}, (err, metadata, errors) => {
expect(err).not.toBeNull();
expect(errors).toBeInstanceOf(Array);
expect(errors[0][0].statusCode).toBe(HTTP_STATUS.INTERNAL_ERROR);
expect(errors[0][0].message).toMatch(API_ERROR.BAD_STATUS_CODE);
done();
});
2018-06-24 22:39:09 +02:00
});
2020-03-03 23:59:19 +01:00
test.skip('should not touch if the package exists and has no uplinks', async (done) => {
const storagePath = generateRamdonStorage();
const storage: IStorageHandler = await generateStorage() as IStorageHandler;
2019-05-19 23:07:17 +02:00
const metadataSource = path.join(__dirname, '../../partials/metadata');
2019-01-30 04:21:36 +01:00
const metadataPath = path.join(storagePath, 'npm_test/package.json');
2019-01-30 04:21:36 +01:00
fs.mkdirSync(path.join(storagePath, 'npm_test'));
2019-01-29 07:03:04 +01:00
fs.writeFileSync(metadataPath, fs.readFileSync(metadataSource));
2019-01-30 04:21:36 +01:00
const metadata = JSON.parse(fs.readFileSync(metadataPath).toString());
// @ts-ignore
2019-01-30 04:21:36 +01:00
storage.localStorage.updateVersions = jest.fn(storage.localStorage.updateVersions);
expect(metadata).toBeDefined();
storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => {
2019-01-30 04:21:36 +01:00
expect(err).toBeNull();
// @ts-ignore
2019-01-30 04:21:36 +01:00
expect(storage.localStorage.updateVersions).not.toHaveBeenCalled();
done();
});
});
2018-02-19 19:29:14 +01:00
});
});