0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/unit/modules/storage/store.spec.ts

117 lines
3.9 KiB
TypeScript
Raw Normal View History

2018-06-28 01:26:07 -05:00
import path from 'path';
import fs from 'fs';
import rimraf from 'rimraf';
2019-05-19 16:07:17 -05:00
import configExample from '../../partials/config';
import AppConfig from '../../../../src/lib/config';
import Storage from '../../../../src/lib/storage';
import {setup} from '../../../../src/lib/logger';
2018-02-19 13:29:14 -05:00
import {Config} from '@verdaccio/types';
import {IStorageHandler} from '../../../../types';
2019-05-19 16:07:17 -05:00
import {API_ERROR, HTTP_STATUS} from '../../../../src/lib/constants';
import {mockServer} from '../../__helper/mock';
import {DOMAIN_SERVERS} from '../../../functional/config.functional';
2018-02-19 13:29:14 -05:00
setup([]);
2018-02-19 13:29:14 -05:00
2019-05-19 16:07:17 -05:00
const storagePath = path.join(__dirname, '../../partials/store/test-storage-store.spec');
const mockServerPort = 55548;
const generateStorage = async function(port = mockServerPort) {
const storageConfig = configExample({
self_path: __dirname,
storage: storagePath,
uplinks: {
npmjs: {
url: `http://${DOMAIN_SERVERS}:${port}`
}
2018-06-24 15:39:09 -05:00
}
}, 'store.spec.yaml');
2018-06-24 15:39:09 -05:00
const config: Config = new AppConfig(storageConfig);
const store: IStorageHandler = new Storage(config);
await store.init(config, []);
2018-02-19 13:29:14 -05:00
2018-06-24 15:39:09 -05:00
return store;
2018-02-19 13:29:14 -05:00
}
describe('StorageTest', () => {
2018-06-24 15:39:09 -05:00
let mockRegistry;
2018-02-19 13:29:14 -05:00
beforeAll(done => {
rimraf(storagePath, async () => {
mockRegistry = await mockServer(mockServerPort).init();
done()
})
2018-06-24 15:39:09 -05:00
});
2018-02-19 13:29:14 -05:00
2018-06-24 15:39:09 -05:00
afterAll(function(done) {
mockRegistry[0].stop();
done();
});
2018-02-19 13:29:14 -05:00
2018-06-24 15:39:09 -05:00
test('should be defined', async () => {
const storage: IStorageHandler = await generateStorage();
2018-02-19 13:29:14 -05:00
expect(storage).toBeDefined();
2018-06-24 15:39:09 -05:00
});
2018-02-19 13:29:14 -05:00
2018-06-27 01:52:52 -05:00
describe('test _syncUplinksMetadata', () => {
test('should fetch from uplink jquery metadata from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
2018-02-19 13:29:14 -05:00
// @ts-ignore
2019-09-26 11:22:14 -05:00
storage._syncUplinksMetadata('jquery', null, {}, (err, metadata) => {
2018-06-27 01:52:52 -05:00
expect(err).toBeNull();
expect(metadata).toBeDefined();
expect(metadata).toBeInstanceOf(Object);
done();
});
2018-06-24 15:39:09 -05:00
});
2018-02-19 13:29:14 -05:00
2018-06-27 01:52:52 -05:00
test('should fails on fetch from uplink non existing from registry', async (done) => {
const storage: IStorageHandler = await generateStorage();
// @ts-ignore
2018-06-27 01:52:52 -05: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 13:29:14 -05:00
// @ts-ignore
2018-06-27 01:52:52 -05: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 15:39:09 -05:00
});
test('should not touch if the package exists and has no uplinks', async (done) => {
const storage: IStorageHandler = await generateStorage() as IStorageHandler;
2019-05-19 16:07:17 -05:00
const metadataSource = path.join(__dirname, '../../partials/metadata');
2019-01-29 22:21:36 -05:00
const metadataPath = path.join(storagePath, 'npm_test/package.json');
2019-01-29 22:21:36 -05:00
fs.mkdirSync(path.join(storagePath, 'npm_test'));
2019-01-29 01:03:04 -05:00
fs.writeFileSync(metadataPath, fs.readFileSync(metadataSource));
2019-01-29 22:21:36 -05:00
const metadata = JSON.parse(fs.readFileSync(metadataPath).toString());
// @ts-ignore
2019-01-29 22:21:36 -05:00
storage.localStorage.updateVersions = jest.fn(storage.localStorage.updateVersions);
expect(metadata).toBeDefined();
storage._syncUplinksMetadata('npm_test', metadata, {}, (err) => {
2019-01-29 22:21:36 -05:00
expect(err).toBeNull();
// @ts-ignore
2019-01-29 22:21:36 -05:00
expect(storage.localStorage.updateVersions).not.toHaveBeenCalled();
done();
});
});
2018-02-19 13:29:14 -05:00
});
});