From 82053bc37ffeeb5897ad5f451c496fe88f4ead23 Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Tue, 2 Nov 2021 19:57:04 -0700 Subject: [PATCH] improve the tests --- .../server/config/create_config.test.ts | 67 ++++++------------- .../reporting/server/config/schema.test.ts | 53 ++++++--------- 2 files changed, 39 insertions(+), 81 deletions(-) diff --git a/x-pack/plugins/reporting/server/config/create_config.test.ts b/x-pack/plugins/reporting/server/config/create_config.test.ts index 38795a488a2e9..718638e1ba62b 100644 --- a/x-pack/plugins/reporting/server/config/create_config.test.ts +++ b/x-pack/plugins/reporting/server/config/create_config.test.ts @@ -147,51 +147,30 @@ describe('Reporting server createConfig$', () => { expect(mockLogger.warn.mock.calls.length).toBe(0); }); - describe('prevent invalid server hostnames', () => { - beforeEach(() => { + for (const hostname of [ + '0', + '0.0', + '0.0.0', + '0.0.0.0', + '0000:0000:0000:0000:0000:0000:0000:0000', + '::', + ]) { + it(`apply failover logic when hostname is given as ${hostname}`, async () => { mockInitContext = coreMock.createPluginInitializerContext( createMockConfigSchema({ encryptionKey: 'aaaaaaaaaaaaabbbbbbbbbbbbaaaaaaaaa', + // overwrite settings added by createMockConfigSchema and apply the default settings + // TODO make createMockConfigSchema _not_ default xpack.reporting.kibanaServer.hostname to 'localhost' kibanaServer: { hostname: undefined, port: undefined, - }, // overwrite settings added by createMockConfigSchema and apply the default settings - }) - ); - }); - - it(`apply failover logic to use "localhost" when "server.host" is "0.0.0.0"`, async () => { - mockCoreSetup.http.getServerInfo = jest.fn().mockImplementation( - (): HttpServerInfo => ({ - name: 'cool server', - hostname: '0.0.0.0', - port: 5601, - protocol: 'http', + }, }) ); - - const mockConfig$ = createMockConfig(mockInitContext); - const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); - - expect(result.kibanaServer).toMatchInlineSnapshot(` - Object { - "hostname": "localhost", - "port": 5601, - "protocol": "http", - } - `); - - expect(mockLogger.warn.mock.calls.length).toBe(1); - expect(mockLogger.warn.mock.calls[0]).toMatchObject([ - "Found 'server.host: \"0.0.0.0\"' in Kibana configuration. Reporting is not able to use this as the Kibana server hostname. To enable PNG/PDF Reporting to work, 'xpack.reporting.kibanaServer.hostname: localhost' is automatically set in the configuration. You can prevent this message by adding 'xpack.reporting.kibanaServer.hostname: localhost' in kibana.yml.", - ]); - }); - - it(`apply failover logic when using a variation of the "0" address`, async () => { mockCoreSetup.http.getServerInfo = jest.fn().mockImplementation( (): HttpServerInfo => ({ name: 'cool server', - hostname: '0.0', + hostname, port: 5601, protocol: 'http', }) @@ -199,19 +178,11 @@ describe('Reporting server createConfig$', () => { const mockConfig$ = createMockConfig(mockInitContext); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); - - expect(result.kibanaServer).toMatchInlineSnapshot(` - Object { - "hostname": "localhost", - "port": 5601, - "protocol": "http", - } - `); - - expect(mockLogger.warn.mock.calls.length).toBe(1); - expect(mockLogger.warn.mock.calls[0]).toMatchObject([ - "Found 'server.host: \"0.0.0.0\"' in Kibana configuration. Reporting is not able to use this as the Kibana server hostname. To enable PNG/PDF Reporting to work, 'xpack.reporting.kibanaServer.hostname: localhost' is automatically set in the configuration. You can prevent this message by adding 'xpack.reporting.kibanaServer.hostname: localhost' in kibana.yml.", - ]); + expect(result.kibanaServer).toMatchObject({ + hostname: 'localhost', + port: 5601, + protocol: 'http', + }); }); - }); + } }); diff --git a/x-pack/plugins/reporting/server/config/schema.test.ts b/x-pack/plugins/reporting/server/config/schema.test.ts index e5035f3304dc0..7f1da5b55ccb6 100644 --- a/x-pack/plugins/reporting/server/config/schema.test.ts +++ b/x-pack/plugins/reporting/server/config/schema.test.ts @@ -288,38 +288,25 @@ describe('Reporting Config Schema', () => { `); }); - it(`logs the proper validation messages for invalid hostname`, () => { - // kibanaServer - const throwValidationErr = () => ConfigSchema.validate({ kibanaServer: { hostname: '0' } }); - expect(throwValidationErr).toThrowError( - `[kibanaServer.hostname]: value must be a valid hostname (see RFC 1123).` - ); - }); - - it(`logs the proper validation messages for hostname: 0.0.0.0`, () => { - // kibanaServer - const throwValidationErr = () => - ConfigSchema.validate({ kibanaServer: { hostname: '0.0.0.0' } }); - expect(throwValidationErr).toThrowError( - `[kibanaServer.hostname]: cannot use '0.0.0.0' as Kibana host name, consider using the default (localhost) instead` - ); - }); + for (const address of ['0', '0.0', '0.0.0']) { + it(`fails to validate "kibanaServer.hostname" with an invalid hostname: "${address}"`, () => { + expect(() => + ConfigSchema.validate({ + kibanaServer: { hostname: address }, + }) + ).toThrowError(`[kibanaServer.hostname]: value must be a valid hostname (see RFC 1123).`); + }); + } - it(`logs the proper validation messages for hostname set to a variation of the "0" address`, () => { - expect(() => ConfigSchema.validate({ kibanaServer: { hostname: '0.0.0' } })).toThrowError( - `[kibanaServer.hostname]: value must be a valid hostname (see RFC 1123).` - ); - - expect(() => - ConfigSchema.validate({ - kibanaServer: { hostname: '0000:0000:0000:0000:0000:0000:0000:0000' }, - }) - ).toThrowError( - `[kibanaServer.hostname]: cannot use '0.0.0.0' as Kibana host name, consider using the default (localhost) instead` - ); - - expect(() => ConfigSchema.validate({ kibanaServer: { hostname: '::' } })).toThrowError( - `[kibanaServer.hostname]: cannot use '0.0.0.0' as Kibana host name, consider using the default (localhost) instead` - ); - }); + for (const address of ['0.0.0.0', '0000:0000:0000:0000:0000:0000:0000:0000', '::']) { + it(`fails to validate "kibanaServer.hostname" hostname as zero: "${address}"`, () => { + expect(() => + ConfigSchema.validate({ + kibanaServer: { hostname: address }, + }) + ).toThrowError( + `[kibanaServer.hostname]: cannot use '0.0.0.0' as Kibana host name, consider using the default (localhost) instead` + ); + }); + } });