2021-01-02 02:11:32 -05:00
|
|
|
/* eslint-disable jest/no-mocks-import */
|
|
|
|
import fs from 'fs';
|
2021-09-08 12:06:37 -05:00
|
|
|
import path from 'path';
|
2024-08-04 13:17:02 -05:00
|
|
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
2021-10-29 10:33:05 -05:00
|
|
|
|
2022-07-29 13:51:45 -05:00
|
|
|
import { fileUtils } from '@verdaccio/core';
|
2021-10-03 07:21:01 -05:00
|
|
|
import { Logger, Token } from '@verdaccio/types';
|
2021-01-02 02:11:32 -05:00
|
|
|
|
|
|
|
import LocalDatabase from '../src/local-database';
|
|
|
|
|
2021-10-03 07:21:01 -05:00
|
|
|
const logger: Logger = {
|
2024-08-04 13:17:02 -05:00
|
|
|
error: vi.fn(),
|
|
|
|
info: vi.fn(),
|
|
|
|
debug: vi.fn(),
|
|
|
|
child: vi.fn(),
|
|
|
|
warn: vi.fn(),
|
|
|
|
http: vi.fn(),
|
|
|
|
trace: vi.fn(),
|
2021-10-03 07:21:01 -05:00
|
|
|
};
|
2021-01-02 02:11:32 -05:00
|
|
|
|
|
|
|
describe('Local Database', () => {
|
2021-09-08 12:06:37 -05:00
|
|
|
let tmpFolder;
|
|
|
|
let locaDatabase;
|
|
|
|
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');
|
2024-08-04 13:17:02 -05:00
|
|
|
const writeMock = vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
|
2021-09-08 12:06:37 -05:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
logger
|
|
|
|
);
|
|
|
|
await (locaDatabase as any).init();
|
|
|
|
(locaDatabase as any).clean();
|
2021-01-02 02:11:32 -05:00
|
|
|
writeMock.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2024-08-04 13:17:02 -05:00
|
|
|
vi.clearAllMocks();
|
2021-01-02 02:11:32 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('token', () => {
|
|
|
|
let token: Token = {
|
|
|
|
user: 'someUser',
|
2021-09-08 12:06:37 -05:00
|
|
|
token: 'foo..foo',
|
2021-01-02 02:11:32 -05:00
|
|
|
key: 'someHash',
|
|
|
|
readonly: true,
|
2021-09-08 12:06:37 -05:00
|
|
|
created: 1234,
|
2021-01-02 02:11:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
test('should save and get token', async () => {
|
|
|
|
await locaDatabase.saveToken(token);
|
|
|
|
const tokens = await locaDatabase.readTokens({ user: token.user });
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
expect(tokens[0]).toEqual(token);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should revoke and get token', async () => {
|
|
|
|
await locaDatabase.saveToken(token);
|
|
|
|
const tokens = await locaDatabase.readTokens({ user: token.user });
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
expect(tokens[0]).toEqual(token);
|
|
|
|
await locaDatabase.deleteToken(token.user, token.key);
|
|
|
|
const tokens2 = await locaDatabase.readTokens({ user: token.user });
|
|
|
|
expect(tokens2).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail on revoke', async () => {
|
|
|
|
await expect(locaDatabase.deleteToken({ user: 'foo', key: 'bar' })).rejects.toThrow(
|
|
|
|
'user not found'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should verify save more than one token', async () => {
|
|
|
|
await locaDatabase.saveToken(token);
|
|
|
|
const tokens = await locaDatabase.readTokens({ user: token.user });
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
expect(tokens[0]).toEqual(token);
|
|
|
|
await locaDatabase.saveToken({ ...token, key: 'foo' });
|
|
|
|
expect(tokens).toHaveLength(2);
|
|
|
|
expect(tokens[1].key).toEqual('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|