Skip to content

Commit

Permalink
Adding migration and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Oct 21, 2020
1 parent 55e090e commit ee836c0
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ describe('config validation', () => {
};

test('config validation passes when only required fields are provided', () => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -101,9 +102,10 @@ describe('config validation', () => {

test('config validation passes when valid methods are provided', () => {
['post', 'put'].forEach((method) => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
method,
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -127,8 +129,9 @@ describe('config validation', () => {
});

test('config validation passes when a url is specified', () => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -155,6 +158,7 @@ describe('config validation', () => {
headers: {
'Content-Type': 'application/json',
},
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand Down Expand Up @@ -184,6 +188,7 @@ describe('config validation', () => {
headers: {
'Content-Type': 'application/json',
},
hasAuth: true,
};

expect(validateConfig(actionType, config)).toEqual({
Expand Down
57 changes: 57 additions & 0 deletions x-pack/plugins/actions/server/saved_objects/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,63 @@ describe('7.10.0', () => {
});
});

describe('7.11.0', () => {
beforeEach(() => {
jest.resetAllMocks();
encryptedSavedObjectsSetup.createMigration.mockImplementation(
(shouldMigrateWhenPredicate, migration) => migration
);
});

test('add hasAuth = true for .webhook actions with user and password', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const action = getMockDataForWebhook({}, true);
expect(migration711(action, context)).toMatchObject({
...action,
attributes: {
...action.attributes,
config: {
hasAuth: true,
},
},
});
});

test('add hasAuth = false for .webhook actions without user and password', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const action = getMockDataForWebhook({}, false);
expect(migration711(action, context)).toMatchObject({
...action,
attributes: {
...action.attributes,
config: {
hasAuth: false,
},
},
});
});
});

function getMockDataForWebhook(
overwrites: Record<string, unknown> = {},
hasUserAndPassword: boolean
): SavedObjectUnsanitizedDoc<RawAction> {
const secrets = hasUserAndPassword
? { user: 'test', password: '123' }
: { user: '', password: '' };
return {
attributes: {
name: 'abc',
actionTypeId: '.webhook',
config: {},
secrets,
...overwrites,
},
id: uuid.v4(),
type: 'action',
};
}

function getMockDataForEmail(
overwrites: Record<string, unknown> = {}
): SavedObjectUnsanitizedDoc<RawAction> {
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/actions/server/saved_objects/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ export function getMigrations(
pipeMigrations(renameCasesConfigurationObject, addHasAuthConfigurationObject)
);

const migrationWebhookConnectorHasAuth = encryptedSavedObjects.createMigration<
RawAction,
RawAction
>(
(doc): doc is SavedObjectUnsanitizedDoc<RawAction> =>
doc.attributes.actionTypeId === '.webhook',
pipeMigrations(addHasAuthConfigurationObject)
);

return {
'7.10.0': executeMigrationWithErrorHandling(migrationActions, '7.10.0'),
'7.11.0': executeMigrationWithErrorHandling(migrationWebhookConnectorHasAuth, '7.11.0'),
};
}

Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -20250,7 +20250,6 @@
"xpack.triggersActionsUI.sections.actionsConnectorsList.unableToLoadActionTypesMessage": "アクションタイプを読み込めません",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHeaderKeyText": "キーが必要です。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHeaderValueText": "値が必要です。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHostText": "ユーザー名が必要です。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredMethodText": "メソッドが必要です",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredPasswordText": "パスワードが必要です。",
"xpack.triggersActionsUI.sections.addAlert.error.greaterThenThreshold0Text": "しきい値 1 はしきい値 0 よりも大きい値にしてください。",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -20270,7 +20270,6 @@
"xpack.triggersActionsUI.sections.actionsConnectorsList.unableToLoadActionTypesMessage": "无法加载操作类型",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHeaderKeyText": "“键”必填。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHeaderValueText": "“值”必填。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredHostText": "“用户名”必填。",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredMethodText": "“方法”必填",
"xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredPasswordText": "“密码”必填。",
"xpack.triggersActionsUI.sections.addAlert.error.greaterThenThreshold0Text": "阈值 1 应 > 阈值 0。",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('actionTypeRegistry.get() works', () => {
});

describe('webhook connector validation', () => {
test('connector validation succeeds when connector config is valid', () => {
test('connector validation succeeds when hasAuth is true and connector config is valid', () => {
const actionConnector = {
secrets: {
user: 'user',
Expand Down Expand Up @@ -56,6 +56,34 @@ describe('webhook connector validation', () => {
});
});

test('connector validation succeeds when hasAuth is false and connector config is valid', () => {
const actionConnector = {
secrets: {
user: '',
password: '',
},
id: 'test',
actionTypeId: '.webhook',
name: 'webhook',
isPreconfigured: false,
config: {
method: 'PUT',
url: 'http://test.com',
headers: { 'content-type': 'text' },
hasAuth: false,
},
} as WebhookActionConnector;

expect(actionTypeModel.validateConnector(actionConnector)).toEqual({
errors: {
url: [],
method: [],
user: [],
password: [],
},
});
});

test('connector validation fails when connector config is not valid', () => {
const actionConnector = {
secrets: {
Expand All @@ -66,6 +94,7 @@ describe('webhook connector validation', () => {
name: 'webhook',
config: {
method: 'PUT',
hasAuth: true,
},
} as WebhookActionConnector;

Expand All @@ -74,7 +103,7 @@ describe('webhook connector validation', () => {
url: ['URL is required.'],
method: [],
user: [],
password: ['Password is required.'],
password: ['Password is required when username is used.'],
},
});
});
Expand All @@ -91,6 +120,7 @@ describe('webhook connector validation', () => {
config: {
method: 'PUT',
url: 'invalid.url',
hasAuth: true,
},
} as WebhookActionConnector;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function getActionType(): ActionTypeModel<
if (action.config.hasAuth && !action.secrets.user && !action.secrets.password) {
errors.password.push(
i18n.translate(
'xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredPasswordText',
'xpack.triggersActionsUI.sections.addAction.webhookAction.error.requiredAuthPasswordText',
{
defaultMessage: 'Password is required.',
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('WebhookActionConnectorFields renders', () => {
method: 'PUT',
url: 'http:\\test',
headers: { 'content-type': 'text' },
hasAuth: true,
},
} as WebhookActionConnector;
const wrapper = mountWithIntl(
Expand All @@ -50,7 +51,9 @@ describe('WebhookActionConnectorFields renders', () => {
secrets: {},
actionTypeId: '.webhook',
isPreconfigured: false,
config: {},
config: {
hasAuth: true,
},
} as WebhookActionConnector;
const wrapper = mountWithIntl(
<WebhookActionConnectorFields
Expand Down Expand Up @@ -80,6 +83,7 @@ describe('WebhookActionConnectorFields renders', () => {
method: 'PUT',
url: 'http:\\test',
headers: { 'content-type': 'text' },
hasAuth: true,
},
} as WebhookActionConnector;
const wrapper = mountWithIntl(
Expand Down

0 comments on commit ee836c0

Please sign in to comment.