Skip to content

Commit

Permalink
improve the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Nov 3, 2021
1 parent 5ca5a4d commit 82053bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 81 deletions.
67 changes: 19 additions & 48 deletions x-pack/plugins/reporting/server/config/create_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,71 +147,42 @@ 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',
})
);

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',
});
});
});
}
});
53 changes: 20 additions & 33 deletions x-pack/plugins/reporting/server/config/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
});
}
});

0 comments on commit 82053bc

Please sign in to comment.