2020-10-24 06:17:21 -05:00
|
|
|
import { ROLES } from '@verdaccio/dev-commons';
|
2020-03-08 03:19:12 -05:00
|
|
|
import {
|
2020-08-13 16:27:00 -05:00
|
|
|
createAnonymousRemoteUser,
|
|
|
|
createRemoteUser,
|
|
|
|
validatePassword,
|
|
|
|
createSessionToken,
|
|
|
|
getAuthenticatedMessage,
|
|
|
|
} from '../src';
|
2020-09-25 13:16:16 -05:00
|
|
|
|
2020-03-08 03:19:12 -05:00
|
|
|
jest.mock('@verdaccio/logger', () => ({
|
2020-08-13 16:27:00 -05:00
|
|
|
logger: { trace: jest.fn() },
|
2020-03-08 03:19:12 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
describe('Auth Utilities', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
describe('validatePassword', () => {
|
|
|
|
test('should validate password according the length', () => {
|
|
|
|
expect(validatePassword('12345', 1)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on validate password according the length', () => {
|
|
|
|
expect(validatePassword('12345', 10)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on validate password according the length and default config', () => {
|
|
|
|
expect(validatePassword('12')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should validate password according the length and default config', () => {
|
|
|
|
expect(validatePassword('1235678910')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createRemoteUser and createAnonymousRemoteUser', () => {
|
|
|
|
test('should create a remote user with default groups', () => {
|
|
|
|
expect(createRemoteUser('12345', ['foo', 'bar'])).toEqual({
|
2020-09-16 23:48:16 -05:00
|
|
|
groups: [
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
ROLES.$ALL,
|
|
|
|
ROLES.$AUTH,
|
|
|
|
ROLES.DEPRECATED_ALL,
|
|
|
|
ROLES.DEPRECATED_AUTH,
|
|
|
|
ROLES.ALL,
|
|
|
|
],
|
2020-08-13 16:27:00 -05:00
|
|
|
name: '12345',
|
|
|
|
real_groups: ['foo', 'bar'],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create a anonymous remote user with default groups', () => {
|
|
|
|
expect(createAnonymousRemoteUser()).toEqual({
|
|
|
|
groups: [ROLES.$ALL, ROLES.$ANONYMOUS, ROLES.DEPRECATED_ALL, ROLES.DEPRECATED_ANONYMOUS],
|
|
|
|
name: undefined,
|
|
|
|
real_groups: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createSessionToken', () => {
|
|
|
|
test('should generate session token', () => {
|
|
|
|
expect(createSessionToken()).toHaveProperty('expires');
|
|
|
|
expect(createSessionToken().expires).toBeInstanceOf(Date);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAuthenticatedMessage', () => {
|
|
|
|
test('should generate user message token', () => {
|
|
|
|
expect(getAuthenticatedMessage('foo')).toEqual("you are authenticated as 'foo'");
|
|
|
|
});
|
|
|
|
});
|
2020-03-08 03:19:12 -05:00
|
|
|
});
|