2020-08-19 13:27:35 -05:00
|
|
|
/* eslint-disable jest/no-mocks-import */
|
|
|
|
import path from 'path';
|
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
import { fileUtils } from '@verdaccio/core';
|
|
|
|
import { logger, setup } from '@verdaccio/logger';
|
|
|
|
import { IPluginStorage, PluginOptions } from '@verdaccio/types';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
import LocalDatabase, { ERROR_DB_LOCKED } from '../src/local-database';
|
|
|
|
|
|
|
|
const mockWrite = jest.fn(() => Promise.resolve());
|
|
|
|
const mockmkdir = jest.fn(() => Promise.resolve());
|
|
|
|
const mockRead = jest.fn(() => Promise.resolve());
|
|
|
|
|
|
|
|
jest.mock('../src/fs', () => ({
|
|
|
|
mkdirPromise: () => mockRead(),
|
|
|
|
readFilePromise: () => mockmkdir(),
|
|
|
|
writeFilePromise: () => mockWrite(),
|
|
|
|
}));
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
setup();
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
// @ts-expect-error
|
2020-08-19 13:27:35 -05:00
|
|
|
const optionsPlugin: PluginOptions<{}> = {
|
|
|
|
logger,
|
|
|
|
};
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
let locaDatabase: IPluginStorage<{}>;
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
describe('Local Database', () => {
|
2021-09-08 12:06:37 -05:00
|
|
|
let tmpFolder;
|
2021-03-30 07:05:58 -05:00
|
|
|
beforeEach(async () => {
|
2022-07-29 13:51:45 -05:00
|
|
|
tmpFolder = await fileUtils.createTempFolder('local-storage-plugin-');
|
|
|
|
const tempFolder = path.join(tmpFolder, 'verdaccio-test.yaml');
|
2021-09-08 12:06:37 -05:00
|
|
|
// @ts-expect-error
|
|
|
|
locaDatabase = new LocalDatabase(
|
|
|
|
// @ts-expect-error
|
|
|
|
{
|
|
|
|
storage: 'storage',
|
2022-07-29 13:51:45 -05:00
|
|
|
configPath: tempFolder,
|
2021-09-08 12:06:37 -05:00
|
|
|
checkSecretKey: () => 'fooX',
|
|
|
|
},
|
|
|
|
optionsPlugin.logger
|
|
|
|
);
|
|
|
|
await (locaDatabase as any).init();
|
|
|
|
(locaDatabase as any).clean();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2021-09-08 12:06:37 -05:00
|
|
|
jest.resetAllMocks();
|
2020-08-19 13:27:35 -05:00
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create an instance', () => {
|
|
|
|
expect(locaDatabase).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
test('should display log error if fails on load database', async () => {
|
2021-09-08 12:06:37 -05:00
|
|
|
mockmkdir.mockImplementation(() => {
|
2020-08-19 13:27:35 -05:00
|
|
|
throw Error();
|
|
|
|
});
|
2022-07-29 13:51:45 -05:00
|
|
|
const tmpFolder = await fileUtils.createTempFolder('local-storage-plugin-');
|
|
|
|
const tempFolder = path.join(tmpFolder, 'verdaccio-test.yaml');
|
2021-09-08 12:06:37 -05:00
|
|
|
const instance = new LocalDatabase(
|
|
|
|
// @ts-expect-error
|
|
|
|
{
|
|
|
|
storage: 'storage',
|
|
|
|
config_path: tempFolder,
|
|
|
|
},
|
|
|
|
optionsPlugin.logger
|
|
|
|
);
|
|
|
|
|
|
|
|
await expect(instance.init()).rejects.toEqual(new Error(ERROR_DB_LOCKED));
|
2022-07-29 13:51:45 -05:00
|
|
|
// expect(optionsPlugin.logger.error).toHaveBeenCalled();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
describe('should handle secret', () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
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 () => {
|
2021-09-08 12:06:37 -05:00
|
|
|
await locaDatabase.setSecret('foooo');
|
2020-08-19 13:27:35 -05:00
|
|
|
const fetchedSecretKey = await locaDatabase.getSecret();
|
2021-09-08 12:06:37 -05:00
|
|
|
expect('foooo').toBe(fetchedSecretKey);
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
test.todo('write tarball');
|
|
|
|
test.todo('read tarball');
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
// expect(storagePath).toBe(
|
|
|
|
// path
|
|
|
|
// .normalize(
|
|
|
|
// path.join(__dirname, '__fixtures__', optionsPlugin.config.storage || '', pkgName)
|
|
|
|
// )
|
|
|
|
// .toLowerCase()
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
// path
|
|
|
|
// .normalize(
|
|
|
|
// path.join(
|
|
|
|
// __dirname,
|
|
|
|
// '__fixtures__',
|
|
|
|
// optionsPlugin.config.storage || '',
|
|
|
|
// 'private_folder',
|
|
|
|
// pkgName
|
|
|
|
// )
|
|
|
|
// )
|
|
|
|
// .toLowerCase()
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
// 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));
|
|
|
|
// jest
|
|
|
|
// .spyOn(fs, 'readdir')
|
|
|
|
// .mockImplementation((storePath, cb) =>
|
|
|
|
// cb(null, storePath.match('test-storage') ? scopedPackages : [])
|
|
|
|
// );
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
// jest
|
|
|
|
// .spyOn(fs, 'readdir')
|
|
|
|
// .mockImplementation((storePath, cb) =>
|
|
|
|
// cb(null, storePath.match('test-storage') ? nonScopedPackages : [])
|
|
|
|
// );
|
|
|
|
|
|
|
|
// 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) => {
|
|
|
|
// const spyInstance = jest
|
|
|
|
// .spyOn(fs, 'readdir')
|
|
|
|
// .mockImplementation((_, cb) => cb(Error('fails'), null));
|
|
|
|
|
|
|
|
// const db = new LocalDatabase(
|
|
|
|
// assign({}, optionsPlugin.config, {
|
|
|
|
// // clean up this, it creates noise
|
|
|
|
// packages: {},
|
|
|
|
// }),
|
|
|
|
// optionsPlugin.logger
|
|
|
|
// );
|
|
|
|
|
|
|
|
// callSearch(db, 0, done);
|
|
|
|
// spyInstance.mockRestore();
|
|
|
|
// });
|
|
|
|
// });
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2021-03-30 07:05:58 -05:00
|
|
|
|
|
|
|
// NOTE: Crear test para verificar que se crea el storage file
|