Skip to content

Commit

Permalink
removed usage of anywhere possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 22, 2020
1 parent 647e9ca commit b8ba1d7
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 84 deletions.
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/task_manager/server/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export function createLegacyApi(legacyTaskManager: Promise<TaskManager>): Legacy
fetch: (opts: SearchOpts) => legacyTaskManager.then((tm: TaskManager) => tm.fetch(opts)),
get: (id: string) => legacyTaskManager.then((tm: TaskManager) => tm.get(id)),
remove: (id: string) => legacyTaskManager.then((tm: TaskManager) => tm.remove(id)),
schedule: (taskInstance: TaskInstanceWithDeprecatedFields, options?: any) =>
schedule: (taskInstance: TaskInstanceWithDeprecatedFields, options?: unknown) =>
legacyTaskManager.then((tm: TaskManager) => tm.schedule(taskInstance, options)),
runNow: (taskId: string) => legacyTaskManager.then((tm: TaskManager) => tm.runNow(taskId)),
ensureScheduled: (taskInstance: TaskInstanceWithId, options?: any) =>
ensureScheduled: (taskInstance: TaskInstanceWithId, options?: unknown) =>
legacyTaskManager.then((tm: TaskManager) => tm.ensureScheduled(taskInstance, options)),
};
}
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/task_manager/server/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SavedObject } from '../../../../../src/core/server';

export const migrations = {
task: {
'7.4.0': (doc: SavedObject<Record<any, any>>) => ({
'7.4.0': (doc: SavedObject<Record<string, unknown>>) => ({
...doc,
updated_at: new Date().toISOString(),
}),
Expand All @@ -18,7 +18,7 @@ export const migrations = {
function moveIntervalIntoSchedule({
attributes: { interval, ...attributes },
...doc
}: SavedObject<Record<any, any>>) {
}: SavedObject<Record<string, unknown>>) {
return {
...doc,
attributes: {
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/actions/server/builtin_action_types/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const NO_OP_FN = () => {};

const services = {
log: NO_OP_FN,
callCluster: async (path: string, opts: any) => {},
callCluster: async (path: string, opts: unknown) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};

Expand All @@ -52,7 +52,7 @@ describe('actionTypeRegistry.get() works', () => {

describe('config validation', () => {
test('config validation succeeds when config is valid', () => {
const config: Record<string, any> = {
const config: Record<string, unknown> = {
service: 'gmail',
from: 'bob@example.com',
};
Expand All @@ -74,7 +74,7 @@ describe('config validation', () => {
});

test('config validation fails when config is not valid', () => {
const baseConfig: Record<string, any> = {
const baseConfig: Record<string, unknown> = {
from: 'bob@example.com',
};

Expand Down Expand Up @@ -177,15 +177,15 @@ describe('config validation', () => {

describe('secrets validation', () => {
test('secrets validation succeeds when secrets is valid', () => {
const secrets: Record<string, any> = {
const secrets: Record<string, unknown> = {
user: 'bob',
password: 'supersecret',
};
expect(validateSecrets(actionType, secrets)).toEqual(secrets);
});

test('secrets validation succeeds when secrets props are null/undefined', () => {
const secrets: Record<string, any> = {
const secrets: Record<string, unknown> = {
user: null,
password: null,
};
Expand All @@ -197,7 +197,7 @@ describe('secrets validation', () => {

describe('params validation', () => {
test('params validation succeeds when params is valid', () => {
const params: Record<string, any> = {
const params: Record<string, unknown> = {
to: ['bob@example.com'],
subject: 'this is a test',
message: 'this is the message',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('actionTypeRegistry.get() works', () => {

describe('config validation', () => {
test('config validation succeeds when config is valid', () => {
const config: Record<string, any> = {
const config: Record<string, unknown> = {
index: 'testing-123',
refresh: false,
};
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('config validation', () => {
});

test('config validation fails when config is not valid', () => {
const baseConfig: Record<string, any> = {
const baseConfig: Record<string, unknown> = {
indeX: 'bob',
};

Expand All @@ -111,7 +111,7 @@ describe('config validation', () => {

describe('params validation', () => {
test('params validation succeeds when params is valid', () => {
const params: Record<string, any> = {
const params: Record<string, unknown> = {
documents: [{ rando: 'thing' }],
};
expect(validateParams(actionType, params)).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ describe('send_email module', () => {
});

test('handles unauthenticated email using not secure host/port', async () => {
const sendEmailOptions = getSendEmailOptions();
const sendEmailOptions = getSendEmailOptions({
transport: {
host: 'example.com',
port: 1025,
},
});
delete sendEmailOptions.transport.service;
delete sendEmailOptions.transport.user;
delete sendEmailOptions.transport.password;
sendEmailOptions.transport.host = 'example.com';
sendEmailOptions.transport.port = 1025;
const result = await sendEmail(mockLogger, sendEmailOptions);
expect(result).toBe(sendMailMockResult);
expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -105,13 +108,17 @@ describe('send_email module', () => {
});

test('handles unauthenticated email using secure host/port', async () => {
const sendEmailOptions = getSendEmailOptions();
const sendEmailOptions = getSendEmailOptions({
transport: {
host: 'example.com',
port: 1025,
secure: true,
},
});
delete sendEmailOptions.transport.service;
delete sendEmailOptions.transport.user;
delete sendEmailOptions.transport.password;
sendEmailOptions.transport.host = 'example.com';
sendEmailOptions.transport.port = 1025;
sendEmailOptions.transport.secure = true;

const result = await sendEmail(mockLogger, sendEmailOptions);
expect(result).toBe(sendMailMockResult);
expect(createTransportMock.mock.calls[0]).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -154,19 +161,22 @@ describe('send_email module', () => {
});
});

function getSendEmailOptions(): any {
function getSendEmailOptions({ content = {}, routing = {}, transport = {} } = {}) {
return {
content: {
...content,
message: 'a message',
subject: 'a subject',
},
routing: {
...routing,
from: 'fred@example.com',
to: ['jim@example.com'],
cc: ['bob@example.com', 'robert@example.com'],
bcc: [],
},
transport: {
...transport,
service: 'whatever',
user: 'elastic',
password: 'changeme',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('execute()', () => {
await actionType.executor({
actionId,
services: {
callCluster: async (path: string, opts: any) => {},
callCluster: async (path: string, opts: unknown) => {},
savedObjectsClient: savedObjectsClientMock.create(),
},
params: { message: 'message text here', level: 'info' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jest.mock('./action_handlers');
const handleIncidentMock = handleIncident as jest.Mock;

const services: Services = {
callCluster: async (path: string, opts: any) => {},
callCluster: async (path: string, opts: unknown) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};

Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/actions/server/builtin_action_types/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionType, Services, ActionTypeExecutorOptions } from '../types';
import {
ActionType,
Services,
ActionTypeExecutorOptions,
ActionTypeExecutorResult,
} from '../types';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { validateParams, validateSecrets } from '../lib';
import { getActionType } from './slack';
Expand All @@ -13,15 +18,15 @@ import { actionsConfigMock } from '../actions_config.mock';
const ACTION_TYPE_ID = '.slack';

const services: Services = {
callCluster: async (path: string, opts: any) => {},
callCluster: async (path: string, opts: unknown) => {},
savedObjectsClient: savedObjectsClientMock.create(),
};

let actionType: ActionType;

beforeAll(() => {
actionType = getActionType({
async executor(options: ActionTypeExecutorOptions): Promise<any> {},
async executor() {},
configurationUtilities: actionsConfigMock.create(),
});
});
Expand Down Expand Up @@ -117,7 +122,7 @@ describe('validateActionTypeSecrets()', () => {

describe('execute()', () => {
beforeAll(() => {
async function mockSlackExecutor(options: ActionTypeExecutorOptions): Promise<any> {
async function mockSlackExecutor(options: ActionTypeExecutorOptions) {
const { params } = options;
const { message } = params;
if (message == null) throw new Error('message property required in parameter');
Expand All @@ -130,7 +135,9 @@ describe('execute()', () => {

return {
text: `slack mockExecutor success: ${message}`,
};
actionId: '',
status: 'ok',
} as ActionTypeExecutorResult;
}

actionType = getActionType({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async function slackExecutor(
return successResult(actionId, result);
}

function successResult(actionId: string, data: any): ActionTypeExecutorResult {
function successResult(actionId: string, data: unknown): ActionTypeExecutorResult {
return { status: 'ok', data, actionId };
}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { configSchema } from './config';

describe('config validation', () => {
test('action defaults', () => {
const config: Record<string, any> = {};
const config: Record<string, unknown> = {};
expect(configSchema.validate(config)).toMatchInlineSnapshot(`
Object {
"enabled": true,
Expand All @@ -23,7 +23,7 @@ describe('config validation', () => {
});

test('action with preconfigured actions', () => {
const config: Record<string, any> = {
const config: Record<string, unknown> = {
preconfigured: [
{
id: 'my-slack1',
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/alert_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function alertTypeWithVariables(id: string, context: string, state: string): Ale
name: `${id}-name`,
actionGroups: [],
defaultActionGroupId: id,
executor: (params: any): any => {},
async executor() {},
};

if (!context && !state) {
Expand Down
24 changes: 12 additions & 12 deletions x-pack/plugins/alerting/server/alerts_client_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import { taskManagerMock } from '../../../plugins/task_manager/server/task_manag
import { KibanaRequest } from '../../../../src/core/server';
import { loggingServiceMock, savedObjectsClientMock } from '../../../../src/core/server/mocks';
import { encryptedSavedObjectsMock } from '../../../plugins/encrypted_saved_objects/server/mocks';
import { AuthenticatedUser } from '../../../plugins/security/public';
import { securityMock } from '../../../plugins/security/server/mocks';

jest.mock('./alerts_client');

const savedObjectsClient = savedObjectsClientMock.create();
const securityPluginSetup = {
authc: {
grantAPIKeyAsInternalUser: jest.fn(),
getCurrentUser: jest.fn(),
},
};
const securityPluginSetup = securityMock.createSetup();
const alertsClientFactoryParams: jest.Mocked<AlertsClientFactoryOpts> = {
logger: loggingServiceMock.create().get(),
taskManager: taskManagerMock.start(),
Expand All @@ -30,7 +27,7 @@ const alertsClientFactoryParams: jest.Mocked<AlertsClientFactoryOpts> = {
encryptedSavedObjectsPlugin: encryptedSavedObjectsMock.createStart(),
preconfiguredActions: [],
};
const fakeRequest: Request = {
const fakeRequest = ({
headers: {},
getBasePath: () => '',
path: '/',
Expand All @@ -44,7 +41,7 @@ const fakeRequest: Request = {
},
},
getSavedObjectsClient: () => savedObjectsClient,
} as any;
} as unknown) as Request;

beforeEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -86,12 +83,14 @@ test('getUserName() returns a name when security is enabled', async () => {
const factory = new AlertsClientFactory();
factory.initialize({
...alertsClientFactoryParams,
securityPluginSetup: securityPluginSetup as any,
securityPluginSetup,
});
factory.create(KibanaRequest.from(fakeRequest), savedObjectsClient);
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.getCurrentUser.mockReturnValueOnce({ username: 'bob' });
securityPluginSetup.authc.getCurrentUser.mockReturnValueOnce(({
username: 'bob',
} as unknown) as AuthenticatedUser);
const userNameResult = await constructorCall.getUserName();
expect(userNameResult).toEqual('bob');
});
Expand Down Expand Up @@ -121,14 +120,15 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
const factory = new AlertsClientFactory();
factory.initialize({
...alertsClientFactoryParams,
securityPluginSetup: securityPluginSetup as any,
securityPluginSetup,
});
factory.create(KibanaRequest.from(fakeRequest), savedObjectsClient);
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.grantAPIKeyAsInternalUser.mockResolvedValueOnce({
api_key: '123',
id: 'abc',
name: '',
});
const createAPIKeyResult = await constructorCall.createAPIKey();
expect(createAPIKeyResult).toEqual({
Expand All @@ -141,7 +141,7 @@ test('createAPIKey() throws when security plugin createAPIKey throws an error',
const factory = new AlertsClientFactory();
factory.initialize({
...alertsClientFactoryParams,
securityPluginSetup: securityPluginSetup as any,
securityPluginSetup,
});
factory.create(KibanaRequest.from(fakeRequest), savedObjectsClient);
const constructorCall = jest.requireMock('./alerts_client').AlertsClient.mock.calls[0][0];
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/lib/license_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { LicenseState } from './license_state';
import { licensingMock } from '../../../../plugins/licensing/server/mocks';

describe('license_state', () => {
let getRawLicense: any;
const getRawLicense = jest.fn();

beforeEach(() => {
getRawLicense = jest.fn();
jest.resetAllMocks();
});

describe('status is LICENSE_STATUS_INVALID', () => {
Expand Down
Loading

0 comments on commit b8ba1d7

Please sign in to comment.