2018-02-24 01:59:36 -05:00
|
|
|
// @flow
|
|
|
|
|
2018-06-17 06:34:59 -05:00
|
|
|
import Auth from '../../../src/lib/auth';
|
2018-02-24 01:59:36 -05:00
|
|
|
// $FlowFixMe
|
2018-06-17 06:34:59 -05:00
|
|
|
import configExample from '../partials/config/index';
|
2018-03-26 08:20:12 -05:00
|
|
|
// $FlowFixMe
|
2018-06-17 06:34:59 -05:00
|
|
|
import configPlugins from '../partials/config/plugin';
|
|
|
|
import AppConfig from '../../../src/lib/config';
|
|
|
|
import {setup} from '../../../src/lib/logger';
|
2018-02-24 01:59:36 -05:00
|
|
|
|
2018-06-17 06:34:59 -05:00
|
|
|
import type {IAuth} from '../../../types/index';
|
2018-02-26 15:28:09 -05:00
|
|
|
import type {Config} from '@verdaccio/types';
|
2018-02-24 01:59:36 -05:00
|
|
|
|
|
|
|
setup(configExample.logs);
|
|
|
|
|
|
|
|
describe('AuthTest', () => {
|
|
|
|
|
|
|
|
test('should be defined', () => {
|
|
|
|
const config: Config = new AppConfig(configExample);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2018-03-26 08:20:12 -05:00
|
|
|
describe('authenticate', () => {
|
|
|
|
test('should utilize plugin', () => {
|
|
|
|
const config: Config = new AppConfig(configPlugins);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
const result = [ "test" ];
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
auth.authenticate(1, null, callback);
|
|
|
|
// $FlowFixMe
|
|
|
|
auth.authenticate(null, result, callback);
|
|
|
|
|
2018-03-26 12:51:14 -05:00
|
|
|
expect(callback.mock.calls).toHaveLength(2);
|
2018-03-26 08:20:12 -05:00
|
|
|
expect(callback.mock.calls[0][0]).toBe(1);
|
|
|
|
expect(callback.mock.calls[0][1]).toBeUndefined();
|
|
|
|
expect(callback.mock.calls[1][0]).toBeNull();
|
|
|
|
expect(callback.mock.calls[1][1].real_groups).toBe(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should skip falsy values', () => {
|
|
|
|
const config: Config = new AppConfig(configPlugins);
|
|
|
|
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
|
|
|
|
for (const value of [ false, 0, "", null, undefined, NaN ]) {
|
|
|
|
// $FlowFixMe
|
|
|
|
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(configPlugins);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
|
|
|
|
for (const value of [ true, 1, "test", { } ]) {
|
|
|
|
expect(function ( ) {
|
|
|
|
// $FlowFixMe
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
}).toThrow(TypeError);
|
2018-03-26 12:51:14 -05:00
|
|
|
expect(callback.mock.calls).toHaveLength(0);
|
2018-03-26 08:20:12 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should skip empty array', () => {
|
|
|
|
const config: Config = new AppConfig(configPlugins);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
const value = [ ];
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
auth.authenticate(null, value, callback);
|
2018-03-26 12:51:14 -05:00
|
|
|
expect(callback.mock.calls).toHaveLength(1);
|
2018-03-26 08:20:12 -05:00
|
|
|
expect(callback.mock.calls[0][0]).toBeDefined();
|
|
|
|
expect(callback.mock.calls[0][1]).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should accept valid array', () => {
|
|
|
|
const config: Config = new AppConfig(configPlugins);
|
|
|
|
const auth: IAuth = new Auth(config);
|
|
|
|
|
|
|
|
expect(auth).toBeDefined();
|
|
|
|
|
|
|
|
const callback = jest.fn();
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
for (const value of [ [ "" ], [ "1" ], [ "0" ], ["000"] ]) {
|
|
|
|
// $FlowFixMe
|
|
|
|
auth.authenticate(null, value, callback);
|
|
|
|
const call = callback.mock.calls[index++];
|
|
|
|
expect(call[0]).toBeNull();
|
|
|
|
expect(call[1].real_groups).toBe(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|