Skip to content

Commit

Permalink
Simplify config and fix tests
Browse files Browse the repository at this point in the history
We had some unnecessary complexity in the config to satisfy test
mocks as they were written. Chagned the tests and simplified said
config.
  • Loading branch information
jportner committed Dec 13, 2019
1 parent f3b7df6 commit 595f8af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
5 changes: 4 additions & 1 deletion x-pack/plugins/security/server/authentication/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ describe('setupAuthentication()', () => {
coreMock.createPluginInitializerContext({
encryptionKey: 'ab'.repeat(16),
secureCookies: true,
session: {
idleTimeout: null,
lifespan: null,
},
cookieName: 'my-sid-cookie',
authc: { providers: ['basic'] },
}),
Expand Down Expand Up @@ -87,7 +91,6 @@ describe('setupAuthentication()', () => {
encryptionKey: 'ab'.repeat(16),
secureCookies: true,
cookieName: 'my-sid-cookie',
authc: { providers: ['basic'] },
};

await setupAuthentication(mockSetupAuthenticationParams);
Expand Down
48 changes: 15 additions & 33 deletions x-pack/plugins/security/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,22 @@ describe('config schema', () => {
});

describe('createConfig$()', () => {
const mockAndCreateConfig = async (isTLSEnabled: boolean, value = {}, context?: any) => {
const contextMock = coreMock.createPluginInitializerContext(
// we must use validate to avoid errors in `createConfig$`
ConfigSchema.validate(value, context)
);
return await createConfig$(contextMock, isTLSEnabled)
.pipe(first())
.toPromise()
.then(config => ({ contextMock, config }));
};
it('should log a warning and set xpack.security.encryptionKey if not set', async () => {
const mockRandomBytes = jest.requireMock('crypto').randomBytes;
mockRandomBytes.mockReturnValue('ab'.repeat(16));

const contextMock = coreMock.createPluginInitializerContext({});
const config = await createConfig$(contextMock, true)
.pipe(first())
.toPromise();
expect(config).toEqual({
encryptionKey: 'ab'.repeat(16),
secureCookies: true,
session: { idleTimeout: null, lifespan: null },
});
const { contextMock, config } = await mockAndCreateConfig(true, {}, { dist: true });
expect(config.encryptionKey).toEqual('ab'.repeat(16));

expect(loggingServiceMock.collect(contextMock.logger).warn).toMatchInlineSnapshot(`
Array [
Expand All @@ -278,14 +281,7 @@ describe('createConfig$()', () => {
});

it('should log a warning if SSL is not configured', async () => {
const contextMock = coreMock.createPluginInitializerContext({
encryptionKey: 'a'.repeat(32),
secureCookies: false,
});

const config = await createConfig$(contextMock, false)
.pipe(first())
.toPromise();
const { contextMock, config } = await mockAndCreateConfig(false, {});
expect(config.secureCookies).toEqual(false);

expect(loggingServiceMock.collect(contextMock.logger).warn).toMatchInlineSnapshot(`
Expand All @@ -298,14 +294,7 @@ describe('createConfig$()', () => {
});

it('should log a warning if SSL is not configured yet secure cookies are being used', async () => {
const contextMock = coreMock.createPluginInitializerContext({
encryptionKey: 'a'.repeat(32),
secureCookies: true,
});

const config = await createConfig$(contextMock, false)
.pipe(first())
.toPromise();
const { contextMock, config } = await mockAndCreateConfig(false, { secureCookies: true });
expect(config.secureCookies).toEqual(true);

expect(loggingServiceMock.collect(contextMock.logger).warn).toMatchInlineSnapshot(`
Expand All @@ -318,14 +307,7 @@ describe('createConfig$()', () => {
});

it('should set xpack.security.secureCookies if SSL is configured', async () => {
const contextMock = coreMock.createPluginInitializerContext({
encryptionKey: 'a'.repeat(32),
secureCookies: false,
});

const config = await createConfig$(contextMock, true)
.pipe(first())
.toPromise();
const { contextMock, config } = await mockAndCreateConfig(true, {});
expect(config.secureCookies).toEqual(true);

expect(loggingServiceMock.collect(contextMock.logger).warn).toEqual([]);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/security/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function createConfig$(context: PluginInitializerContext, isTLSEnabled: b
encryptionKey,
secureCookies,
session: {
idleTimeout: config.session?.idleTimeout || deprecatedSessionTimeout,
lifespan: config.session?.lifespan || null,
...config.session,
idleTimeout: config.session.idleTimeout || deprecatedSessionTimeout,
},
};
delete val.sessionTimeout; // DEPRECATED
Expand Down

0 comments on commit 595f8af

Please sign in to comment.