2019-07-16 01:40:01 -05:00
|
|
|
import _ from 'lodash';
|
2020-11-15 05:14:09 -05:00
|
|
|
import { ROLES, Config as AppConfig } from '@verdaccio/config';
|
2020-08-11 00:21:51 -05:00
|
|
|
import { setup } from '@verdaccio/logger';
|
2020-09-16 23:48:16 -05:00
|
|
|
import { IAuth } from '@verdaccio/auth';
|
2020-08-11 00:21:51 -05:00
|
|
|
import { Config } from '@verdaccio/types';
|
2021-09-25 17:08:00 -05:00
|
|
|
import { errorUtils } from '@verdaccio/core';
|
2019-07-16 01:40:01 -05:00
|
|
|
|
2020-08-11 00:21:51 -05:00
|
|
|
import { Auth } from '../src';
|
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
import { authProfileConf, authPluginFailureConf, authPluginPassThrougConf } from './helper/plugin';
|
|
|
|
|
2019-07-16 01:40:01 -05:00
|
|
|
setup([]);
|
|
|
|
|
|
|
|
describe('AuthTest', () => {
|
|
|
|
test('should be defined', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authProfileConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('test authenticate method', () => {
|
|
|
|
describe('test authenticate states', () => {
|
|
|
|
test('should be a success login', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authProfileConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
2020-08-13 16:27:00 -05:00
|
|
|
const groups = ['test'];
|
2019-07-16 01:40:01 -05:00
|
|
|
|
|
|
|
auth.authenticate('foo', 'bar', callback);
|
|
|
|
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(callback).toHaveBeenCalledWith(null, {
|
2020-09-16 23:48:16 -05:00
|
|
|
groups: [
|
|
|
|
'test',
|
|
|
|
ROLES.$ALL,
|
|
|
|
ROLES.$AUTH,
|
|
|
|
ROLES.DEPRECATED_ALL,
|
|
|
|
ROLES.DEPRECATED_AUTH,
|
|
|
|
ROLES.ALL,
|
|
|
|
],
|
2020-08-13 16:27:00 -05:00
|
|
|
name: 'foo',
|
|
|
|
real_groups: groups,
|
|
|
|
});
|
2019-07-16 01:40:01 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be a fail on login', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authPluginFailureConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
|
|
|
auth.authenticate('foo', 'bar', callback);
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
2021-09-25 17:08:00 -05:00
|
|
|
expect(callback).toHaveBeenCalledWith(errorUtils.getInternalError());
|
2019-07-16 01:40:01 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// plugins are free to send whatever they want, so, we need to test some scenarios
|
|
|
|
// that might make break the request
|
|
|
|
// the @ts-ignore below are intended
|
|
|
|
describe('test authenticate out of control inputs from plugins', () => {
|
|
|
|
test('should skip falsy values', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authPluginPassThrougConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
// as defined by https://developer.mozilla.org/en-US/docs/Glossary/Falsy
|
2020-08-13 16:27:00 -05:00
|
|
|
for (const value of [false, 0, '', null, undefined, NaN]) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
const call = callback.mock.calls[index++];
|
|
|
|
expect(call[0]).toBeDefined();
|
|
|
|
expect(call[1]).toBeUndefined();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should error truthy non-array', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authPluginPassThrougConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
for (const value of [true, 1, 'test', {}]) {
|
|
|
|
expect(function () {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
}).toThrow(TypeError);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should skip empty array', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authPluginPassThrougConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
2020-08-13 16:27:00 -05:00
|
|
|
const value = [];
|
2019-07-16 01:40:01 -05:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
expect(callback.mock.calls).toHaveLength(1);
|
|
|
|
expect(callback.mock.calls[0][0]).toBeDefined();
|
|
|
|
expect(callback.mock.calls[0][1]).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should accept valid array', () => {
|
|
|
|
const config: Config = new AppConfig(_.cloneDeep(authPluginPassThrougConf));
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
let index = 0;
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
for (const value of [[''], ['1'], ['0'], ['000']]) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
const call = callback.mock.calls[index++];
|
|
|
|
expect(call[0]).toBeNull();
|
|
|
|
expect(call[1].real_groups).toBe(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-08-13 16:27:00 -05:00
|
|
|
});
|
2019-07-16 01:40:01 -05:00
|
|
|
});
|