2020-08-19 13:27:35 -05:00
|
|
|
/* eslint-disable jest/no-mocks-import */
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { assign } from 'lodash';
|
2021-03-30 07:05:58 -05:00
|
|
|
import { IPluginStorage, PluginOptions } from '@verdaccio/types';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
import LocalDatabase from '../src/local-database';
|
|
|
|
import { ILocalFSPackageManager } from '../src/local-fs';
|
|
|
|
import * as pkgUtils from '../src/pkg-utils';
|
|
|
|
|
|
|
|
// FIXME: remove this mocks imports
|
|
|
|
import Config from './__mocks__/Config';
|
|
|
|
import logger from './__mocks__/Logger';
|
|
|
|
|
|
|
|
const optionsPlugin: PluginOptions<{}> = {
|
|
|
|
logger,
|
|
|
|
config: new Config(),
|
|
|
|
};
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
let locaDatabase: IPluginStorage<{}>;
|
2020-08-19 13:27:35 -05:00
|
|
|
let loadPrivatePackages;
|
|
|
|
|
|
|
|
describe('Local Database', () => {
|
2021-03-30 07:05:58 -05:00
|
|
|
beforeEach(async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const writeMock = jest.spyOn(fs, 'writeFileSync').mockImplementation();
|
2020-09-16 23:48:16 -05:00
|
|
|
loadPrivatePackages = jest
|
|
|
|
.spyOn(pkgUtils, 'loadPrivatePackages')
|
2021-03-30 07:05:58 -05:00
|
|
|
.mockResolvedValue({ list: [], secret: '' });
|
2020-08-19 13:27:35 -05:00
|
|
|
locaDatabase = new LocalDatabase(optionsPlugin.config, optionsPlugin.logger);
|
2021-03-30 07:05:58 -05:00
|
|
|
await (locaDatabase as LocalDatabase).init();
|
2020-08-19 13:27:35 -05:00
|
|
|
(locaDatabase as LocalDatabase).clean();
|
|
|
|
writeMock.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create an instance', () => {
|
|
|
|
expect(optionsPlugin.logger.error).not.toHaveBeenCalled();
|
|
|
|
expect(locaDatabase).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
test('should display log error if fails on load database', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
loadPrivatePackages.mockImplementation(() => {
|
|
|
|
throw Error();
|
|
|
|
});
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
const instance = new LocalDatabase(optionsPlugin.config, optionsPlugin.logger);
|
|
|
|
await instance.init();
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
expect(optionsPlugin.logger.error).toHaveBeenCalled();
|
|
|
|
expect(optionsPlugin.logger.error).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('should create set secret', () => {
|
|
|
|
test('should create get secret', async () => {
|
|
|
|
const secretKey = await locaDatabase.getSecret();
|
|
|
|
|
|
|
|
expect(secretKey).toBeDefined();
|
|
|
|
expect(typeof secretKey === 'string').toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create set secret', async () => {
|
|
|
|
await locaDatabase.setSecret(optionsPlugin.config.checkSecretKey(''));
|
|
|
|
|
|
|
|
expect(optionsPlugin.config.secret).toBeDefined();
|
|
|
|
expect(typeof optionsPlugin.config.secret === 'string').toBeTruthy();
|
|
|
|
|
|
|
|
const fetchedSecretKey = await locaDatabase.getSecret();
|
|
|
|
expect(optionsPlugin.config.secret).toBe(fetchedSecretKey);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPackageStorage', () => {
|
|
|
|
test('should get default storage', () => {
|
|
|
|
const pkgName = 'someRandomePackage';
|
|
|
|
const storage = locaDatabase.getPackageStorage(pkgName);
|
|
|
|
expect(storage).toBeDefined();
|
|
|
|
|
|
|
|
if (storage) {
|
|
|
|
const storagePath = path.normalize((storage as ILocalFSPackageManager).path).toLowerCase();
|
2020-09-16 23:48:16 -05:00
|
|
|
expect(storagePath).toBe(
|
|
|
|
path
|
|
|
|
.normalize(
|
|
|
|
path.join(__dirname, '__fixtures__', optionsPlugin.config.storage || '', pkgName)
|
|
|
|
)
|
|
|
|
.toLowerCase()
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use custom storage', () => {
|
|
|
|
const pkgName = 'local-private-custom-storage';
|
|
|
|
const storage = locaDatabase.getPackageStorage(pkgName);
|
|
|
|
|
|
|
|
expect(storage).toBeDefined();
|
|
|
|
|
|
|
|
if (storage) {
|
|
|
|
const storagePath = path.normalize((storage as ILocalFSPackageManager).path).toLowerCase();
|
|
|
|
expect(storagePath).toBe(
|
2020-09-16 23:48:16 -05:00
|
|
|
path
|
|
|
|
.normalize(
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
'__fixtures__',
|
|
|
|
optionsPlugin.config.storage || '',
|
|
|
|
'private_folder',
|
|
|
|
pkgName
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.toLowerCase()
|
2020-08-19 13:27:35 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Database CRUD', () => {
|
|
|
|
test('should add an item to database', (done) => {
|
|
|
|
const pgkName = 'jquery';
|
|
|
|
locaDatabase.get((err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toHaveLength(0);
|
|
|
|
|
|
|
|
locaDatabase.add(pgkName, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
locaDatabase.get((err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toHaveLength(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should remove an item to database', (done) => {
|
|
|
|
const pgkName = 'jquery';
|
|
|
|
locaDatabase.get((err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toHaveLength(0);
|
|
|
|
locaDatabase.add(pgkName, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
locaDatabase.remove(pgkName, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
locaDatabase.get((err, data) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(data).toHaveLength(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('search', () => {
|
|
|
|
const onPackageMock = jest.fn((item, cb) => cb());
|
|
|
|
const validatorMock = jest.fn(() => true);
|
|
|
|
const callSearch = (db, numberTimesCalled, cb): void => {
|
|
|
|
db.search(
|
|
|
|
onPackageMock,
|
|
|
|
function onEnd() {
|
|
|
|
expect(onPackageMock).toHaveBeenCalledTimes(numberTimesCalled);
|
|
|
|
expect(validatorMock).toHaveBeenCalledTimes(numberTimesCalled);
|
|
|
|
cb();
|
|
|
|
},
|
|
|
|
validatorMock
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should find scoped packages', (done) => {
|
|
|
|
const scopedPackages = ['@pkg1/test'];
|
|
|
|
const stats = { mtime: new Date() };
|
|
|
|
jest.spyOn(fs, 'stat').mockImplementation((_, cb) => cb(null, stats as fs.Stats));
|
2020-09-16 23:48:16 -05:00
|
|
|
jest
|
|
|
|
.spyOn(fs, 'readdir')
|
|
|
|
.mockImplementation((storePath, cb) =>
|
|
|
|
cb(null, storePath.match('test-storage') ? scopedPackages : [])
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
callSearch(locaDatabase, 1, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should find non scoped packages', (done) => {
|
|
|
|
const nonScopedPackages = ['pkg1', 'pkg2'];
|
|
|
|
const stats = { mtime: new Date() };
|
|
|
|
jest.spyOn(fs, 'stat').mockImplementation((_, cb) => cb(null, stats as fs.Stats));
|
2020-09-16 23:48:16 -05:00
|
|
|
jest
|
|
|
|
.spyOn(fs, 'readdir')
|
|
|
|
.mockImplementation((storePath, cb) =>
|
|
|
|
cb(null, storePath.match('test-storage') ? nonScopedPackages : [])
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
const db = new LocalDatabase(
|
|
|
|
assign({}, optionsPlugin.config, {
|
|
|
|
// clean up this, it creates noise
|
|
|
|
packages: {},
|
|
|
|
}),
|
|
|
|
optionsPlugin.logger
|
|
|
|
);
|
|
|
|
|
|
|
|
callSearch(db, 2, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on read the storage', (done) => {
|
2020-09-16 23:48:16 -05:00
|
|
|
const spyInstance = jest
|
|
|
|
.spyOn(fs, 'readdir')
|
|
|
|
.mockImplementation((_, cb) => cb(Error('fails'), null));
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
const db = new LocalDatabase(
|
|
|
|
assign({}, optionsPlugin.config, {
|
|
|
|
// clean up this, it creates noise
|
|
|
|
packages: {},
|
|
|
|
}),
|
|
|
|
optionsPlugin.logger
|
|
|
|
);
|
|
|
|
|
|
|
|
callSearch(db, 0, done);
|
|
|
|
spyInstance.mockRestore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-03-30 07:05:58 -05:00
|
|
|
|
|
|
|
// NOTE: Crear test para verificar que se crea el storage file
|