Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
*/

import type { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import type {
AuthenticationServiceSetup,
AuthenticationServiceStart,
} from './authentication_service';
import type { AuthenticationServiceStart } from './authentication_service';

import { apiKeysMock } from './api_keys/api_keys.mock';

export const authenticationServiceMock = {
createSetup: (): jest.Mocked<AuthenticationServiceSetup> => ({
getCurrentUser: jest.fn(),
}),
createStart: (): DeeplyMockedKeys<AuthenticationServiceStart> => ({
apiKeys: apiKeysMock.create(),
login: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ import { sessionMock } from '../session_management/session.mock';
import type {
AuthenticationHandler,
AuthToolkit,
ILegacyClusterClient,
KibanaRequest,
Logger,
LoggerFactory,
LegacyScopedClusterClient,
HttpServiceSetup,
HttpServiceStart,
} from '../../../../../src/core/server';
Expand All @@ -46,47 +44,17 @@ describe('AuthenticationService', () => {
let service: AuthenticationService;
let logger: jest.Mocked<Logger>;
let mockSetupAuthenticationParams: {
legacyAuditLogger: jest.Mocked<SecurityAuditLogger>;
audit: jest.Mocked<AuditServiceSetup>;
config: ConfigType;
loggers: LoggerFactory;
http: jest.Mocked<HttpServiceSetup>;
clusterClient: jest.Mocked<ILegacyClusterClient>;
license: jest.Mocked<SecurityLicense>;
getFeatureUsageService: () => jest.Mocked<SecurityFeatureUsageServiceStart>;
session: jest.Mocked<PublicMethodsOf<Session>>;
};
let mockScopedClusterClient: jest.Mocked<PublicMethodsOf<LegacyScopedClusterClient>>;
beforeEach(() => {
logger = loggingSystemMock.createLogger();

mockSetupAuthenticationParams = {
legacyAuditLogger: securityAuditLoggerMock.create(),
audit: auditServiceMock.create(),
http: coreMock.createSetup().http,
config: createConfig(
ConfigSchema.validate({
encryptionKey: 'ab'.repeat(16),
secureCookies: true,
cookieName: 'my-sid-cookie',
}),
loggingSystemMock.create().get(),
{ isTLSEnabled: false }
),
clusterClient: elasticsearchServiceMock.createLegacyClusterClient(),
license: licenseMock.create(),
loggers: loggingSystemMock.create(),
getFeatureUsageService: jest
.fn()
.mockReturnValue(securityFeatureUsageServiceMock.createStartContract()),
session: sessionMock.create(),
};

mockScopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
mockSetupAuthenticationParams.clusterClient.asScoped.mockReturnValue(
(mockScopedClusterClient as unknown) as jest.Mocked<LegacyScopedClusterClient>
);

service = new AuthenticationService(logger);
});

Expand All @@ -101,6 +69,42 @@ describe('AuthenticationService', () => {
expect.any(Function)
);
});
});

describe('#start()', () => {
let mockStartAuthenticationParams: {
legacyAuditLogger: jest.Mocked<SecurityAuditLogger>;
audit: jest.Mocked<AuditServiceSetup>;
config: ConfigType;
loggers: LoggerFactory;
http: jest.Mocked<HttpServiceStart>;
clusterClient: ReturnType<typeof elasticsearchServiceMock.createClusterClient>;
featureUsageService: jest.Mocked<SecurityFeatureUsageServiceStart>;
session: jest.Mocked<PublicMethodsOf<Session>>;
};
beforeEach(() => {
const coreStart = coreMock.createStart();
mockStartAuthenticationParams = {
legacyAuditLogger: securityAuditLoggerMock.create(),
audit: auditServiceMock.create(),
config: createConfig(
ConfigSchema.validate({
encryptionKey: 'ab'.repeat(16),
secureCookies: true,
cookieName: 'my-sid-cookie',
}),
loggingSystemMock.create().get(),
{ isTLSEnabled: false }
),
http: coreStart.http,
clusterClient: elasticsearchServiceMock.createClusterClient(),
loggers: loggingSystemMock.create(),
featureUsageService: securityFeatureUsageServiceMock.createStartContract(),
session: sessionMock.create(),
};

service.setup(mockSetupAuthenticationParams);
});

describe('authentication handler', () => {
let authHandler: AuthenticationHandler;
Expand All @@ -109,12 +113,7 @@ describe('AuthenticationService', () => {
beforeEach(() => {
mockAuthToolkit = httpServiceMock.createAuthToolkit();

service.setup(mockSetupAuthenticationParams);

expect(mockSetupAuthenticationParams.http.registerAuth).toHaveBeenCalledTimes(1);
expect(mockSetupAuthenticationParams.http.registerAuth).toHaveBeenCalledWith(
expect.any(Function)
);
service.start(mockStartAuthenticationParams);

authHandler = mockSetupAuthenticationParams.http.registerAuth.mock.calls[0][0];
authenticate = jest.requireMock('./authenticator').Authenticator.mock.instances[0]
Expand Down Expand Up @@ -298,14 +297,15 @@ describe('AuthenticationService', () => {
describe('getServerBaseURL()', () => {
let getServerBaseURL: () => string;
beforeEach(() => {
mockSetupAuthenticationParams.http.getServerInfo.mockReturnValue({
mockStartAuthenticationParams.http.getServerInfo.mockReturnValue({
name: 'some-name',
protocol: 'socket',
hostname: 'test-hostname',
port: 1234,
});

service.setup(mockSetupAuthenticationParams);
service.start(mockStartAuthenticationParams);

getServerBaseURL = jest.requireMock('./authenticator').Authenticator.mock.calls[0][0]
.getServerBaseURL;
Expand All @@ -316,22 +316,22 @@ describe('AuthenticationService', () => {
});

it('respects `public` config if it is specified', async () => {
mockSetupAuthenticationParams.config.public = {
mockStartAuthenticationParams.config.public = {
protocol: 'https',
} as ConfigType['public'];
expect(getServerBaseURL()).toBe('https://test-hostname:1234');

mockSetupAuthenticationParams.config.public = {
mockStartAuthenticationParams.config.public = {
hostname: 'elastic.co',
} as ConfigType['public'];
expect(getServerBaseURL()).toBe('socket://elastic.co:1234');

mockSetupAuthenticationParams.config.public = {
mockStartAuthenticationParams.config.public = {
port: 4321,
} as ConfigType['public'];
expect(getServerBaseURL()).toBe('socket://test-hostname:4321');

mockSetupAuthenticationParams.config.public = {
mockStartAuthenticationParams.config.public = {
protocol: 'https',
hostname: 'elastic.co',
port: 4321,
Expand All @@ -343,63 +343,7 @@ describe('AuthenticationService', () => {
describe('getCurrentUser()', () => {
let getCurrentUser: (r: KibanaRequest) => AuthenticatedUser | null;
beforeEach(async () => {
getCurrentUser = service.setup(mockSetupAuthenticationParams).getCurrentUser;
});

it('returns `null` if Security is disabled', () => {
mockSetupAuthenticationParams.license.isEnabled.mockReturnValue(false);

expect(getCurrentUser(httpServerMock.createKibanaRequest())).toBe(null);
});

it('returns user from the auth state.', () => {
const mockUser = mockAuthenticatedUser();

const mockAuthGet = mockSetupAuthenticationParams.http.auth.get as jest.Mock;
mockAuthGet.mockReturnValue({ state: mockUser });

const mockRequest = httpServerMock.createKibanaRequest();
expect(getCurrentUser(mockRequest)).toBe(mockUser);
expect(mockAuthGet).toHaveBeenCalledTimes(1);
expect(mockAuthGet).toHaveBeenCalledWith(mockRequest);
});

it('returns null if auth state is not available.', () => {
const mockAuthGet = mockSetupAuthenticationParams.http.auth.get as jest.Mock;
mockAuthGet.mockReturnValue({});

const mockRequest = httpServerMock.createKibanaRequest();
expect(getCurrentUser(mockRequest)).toBeNull();
expect(mockAuthGet).toHaveBeenCalledTimes(1);
expect(mockAuthGet).toHaveBeenCalledWith(mockRequest);
});
});
});

describe('#start()', () => {
let mockStartAuthenticationParams: {
http: jest.Mocked<HttpServiceStart>;
clusterClient: ReturnType<typeof elasticsearchServiceMock.createClusterClient>;
};
beforeEach(() => {
const coreStart = coreMock.createStart();
mockStartAuthenticationParams = {
http: coreStart.http,
clusterClient: elasticsearchServiceMock.createClusterClient(),
};
service.setup(mockSetupAuthenticationParams);
});

describe('getCurrentUser()', () => {
let getCurrentUser: (r: KibanaRequest) => AuthenticatedUser | null;
beforeEach(async () => {
getCurrentUser = (await service.start(mockStartAuthenticationParams)).getCurrentUser;
});

it('returns `null` if Security is disabled', () => {
mockSetupAuthenticationParams.license.isEnabled.mockReturnValue(false);

expect(getCurrentUser(httpServerMock.createKibanaRequest())).toBe(null);
getCurrentUser = service.start(mockStartAuthenticationParams).getCurrentUser;
});

it('returns user from the auth state.', () => {
Expand Down
Loading