Skip to content

Commit ee0917e

Browse files
authored
Cleanup actions plugin (#37250)
* Cleanup actions, move code from alerting plugin PR * Rename service terminology to registry * Use static encryption key for encrypted attributes plugin inside of tests * Empty data after create test is done running * Fix type checks * Fix inconsistent naming
1 parent 9ea8594 commit ee0917e

File tree

28 files changed

+230
-246
lines changed

28 files changed

+230
-246
lines changed

x-pack/plugins/actions/server/__jest__/action_type_service.test.ts renamed to x-pack/plugins/actions/server/__jest__/action_type_registry.test.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
*/
66

77
import Joi from 'joi';
8-
import { ActionTypeService } from '../action_type_service';
8+
import { ActionTypeRegistry } from '../action_type_registry';
99

1010
const services = {
1111
log: jest.fn(),
1212
};
13-
const actionTypeServiceParams = {
13+
const actionTypeRegistryParams = {
1414
services,
1515
};
1616

1717
describe('register()', () => {
1818
test('able to register action types', () => {
1919
const executor = jest.fn();
20-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
21-
actionTypeService.register({
20+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
21+
actionTypeRegistry.register({
2222
id: 'my-action-type',
2323
name: 'My action type',
2424
executor,
2525
});
26-
expect(actionTypeService.has('my-action-type')).toEqual(true);
26+
expect(actionTypeRegistry.has('my-action-type')).toEqual(true);
2727
});
2828

2929
test('throws error if action type already registered', () => {
3030
const executor = jest.fn();
31-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
32-
actionTypeService.register({
31+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
32+
actionTypeRegistry.register({
3333
id: 'my-action-type',
3434
name: 'My action type',
3535
executor,
3636
});
3737
expect(() =>
38-
actionTypeService.register({
38+
actionTypeRegistry.register({
3939
id: 'my-action-type',
4040
name: 'My action type',
4141
executor,
@@ -48,13 +48,13 @@ describe('register()', () => {
4848

4949
describe('get()', () => {
5050
test('returns action type', () => {
51-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
52-
actionTypeService.register({
51+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
52+
actionTypeRegistry.register({
5353
id: 'my-action-type',
5454
name: 'My action type',
5555
async executor() {},
5656
});
57-
const actionType = actionTypeService.get('my-action-type');
57+
const actionType = actionTypeRegistry.get('my-action-type');
5858
expect(actionType).toMatchInlineSnapshot(`
5959
Object {
6060
"executor": [Function],
@@ -65,47 +65,47 @@ Object {
6565
});
6666

6767
test(`throws an error when action type doesn't exist`, () => {
68-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
69-
expect(() => actionTypeService.get('my-action-type')).toThrowErrorMatchingInlineSnapshot(
68+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
69+
expect(() => actionTypeRegistry.get('my-action-type')).toThrowErrorMatchingInlineSnapshot(
7070
`"Action type \\"my-action-type\\" is not registered."`
7171
);
7272
});
7373
});
7474

7575
describe('getUnencryptedAttributes()', () => {
7676
test('returns empty array when unencryptedAttributes is undefined', () => {
77-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
78-
actionTypeService.register({
77+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
78+
actionTypeRegistry.register({
7979
id: 'my-action-type',
8080
name: 'My action type',
8181
async executor() {},
8282
});
83-
const result = actionTypeService.getUnencryptedAttributes('my-action-type');
83+
const result = actionTypeRegistry.getUnencryptedAttributes('my-action-type');
8484
expect(result).toEqual([]);
8585
});
8686

8787
test('returns values inside unencryptedAttributes array when it exists', () => {
88-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
89-
actionTypeService.register({
88+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
89+
actionTypeRegistry.register({
9090
id: 'my-action-type',
9191
name: 'My action type',
9292
unencryptedAttributes: ['a', 'b', 'c'],
9393
async executor() {},
9494
});
95-
const result = actionTypeService.getUnencryptedAttributes('my-action-type');
95+
const result = actionTypeRegistry.getUnencryptedAttributes('my-action-type');
9696
expect(result).toEqual(['a', 'b', 'c']);
9797
});
9898
});
9999

100100
describe('list()', () => {
101101
test('returns list of action types', () => {
102-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
103-
actionTypeService.register({
102+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
103+
actionTypeRegistry.register({
104104
id: 'my-action-type',
105105
name: 'My action type',
106106
async executor() {},
107107
});
108-
const actionTypes = actionTypeService.list();
108+
const actionTypes = actionTypeRegistry.list();
109109
expect(actionTypes).toEqual([
110110
{
111111
id: 'my-action-type',
@@ -117,18 +117,18 @@ describe('list()', () => {
117117

118118
describe('validateParams()', () => {
119119
test('should pass when validation not defined', () => {
120-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
121-
actionTypeService.register({
120+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
121+
actionTypeRegistry.register({
122122
id: 'my-action-type',
123123
name: 'My action type',
124124
async executor() {},
125125
});
126-
actionTypeService.validateParams('my-action-type', {});
126+
actionTypeRegistry.validateParams('my-action-type', {});
127127
});
128128

129129
test('should validate and pass when params is valid', () => {
130-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
131-
actionTypeService.register({
130+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
131+
actionTypeRegistry.register({
132132
id: 'my-action-type',
133133
name: 'My action type',
134134
validate: {
@@ -140,12 +140,12 @@ describe('validateParams()', () => {
140140
},
141141
async executor() {},
142142
});
143-
actionTypeService.validateParams('my-action-type', { param1: 'value' });
143+
actionTypeRegistry.validateParams('my-action-type', { param1: 'value' });
144144
});
145145

146146
test('should validate and throw error when params is invalid', () => {
147-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
148-
actionTypeService.register({
147+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
148+
actionTypeRegistry.register({
149149
id: 'my-action-type',
150150
name: 'My action type',
151151
validate: {
@@ -158,7 +158,7 @@ describe('validateParams()', () => {
158158
async executor() {},
159159
});
160160
expect(() =>
161-
actionTypeService.validateParams('my-action-type', {})
161+
actionTypeRegistry.validateParams('my-action-type', {})
162162
).toThrowErrorMatchingInlineSnapshot(
163163
`"child \\"param1\\" fails because [\\"param1\\" is required]"`
164164
);
@@ -167,18 +167,18 @@ describe('validateParams()', () => {
167167

168168
describe('validateActionTypeConfig()', () => {
169169
test('should pass when validation not defined', () => {
170-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
171-
actionTypeService.register({
170+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
171+
actionTypeRegistry.register({
172172
id: 'my-action-type',
173173
name: 'My action type',
174174
async executor() {},
175175
});
176-
actionTypeService.validateActionTypeConfig('my-action-type', {});
176+
actionTypeRegistry.validateActionTypeConfig('my-action-type', {});
177177
});
178178

179179
test('should validate and pass when actionTypeConfig is valid', () => {
180-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
181-
actionTypeService.register({
180+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
181+
actionTypeRegistry.register({
182182
id: 'my-action-type',
183183
name: 'My action type',
184184
validate: {
@@ -190,12 +190,12 @@ describe('validateActionTypeConfig()', () => {
190190
},
191191
async executor() {},
192192
});
193-
actionTypeService.validateActionTypeConfig('my-action-type', { param1: 'value' });
193+
actionTypeRegistry.validateActionTypeConfig('my-action-type', { param1: 'value' });
194194
});
195195

196196
test('should validate and throw error when actionTypeConfig is invalid', () => {
197-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
198-
actionTypeService.register({
197+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
198+
actionTypeRegistry.register({
199199
id: 'my-action-type',
200200
name: 'My action type',
201201
validate: {
@@ -208,7 +208,7 @@ describe('validateActionTypeConfig()', () => {
208208
async executor() {},
209209
});
210210
expect(() =>
211-
actionTypeService.validateActionTypeConfig('my-action-type', {})
211+
actionTypeRegistry.validateActionTypeConfig('my-action-type', {})
212212
).toThrowErrorMatchingInlineSnapshot(
213213
`"child \\"param1\\" fails because [\\"param1\\" is required]"`
214214
);
@@ -217,32 +217,32 @@ describe('validateActionTypeConfig()', () => {
217217

218218
describe('has()', () => {
219219
test('returns false for unregistered action types', () => {
220-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
221-
expect(actionTypeService.has('my-action-type')).toEqual(false);
220+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
221+
expect(actionTypeRegistry.has('my-action-type')).toEqual(false);
222222
});
223223

224224
test('returns true after registering an action type', () => {
225225
const executor = jest.fn();
226-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
227-
actionTypeService.register({
226+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
227+
actionTypeRegistry.register({
228228
id: 'my-action-type',
229229
name: 'My action type',
230230
executor,
231231
});
232-
expect(actionTypeService.has('my-action-type'));
232+
expect(actionTypeRegistry.has('my-action-type'));
233233
});
234234
});
235235

236236
describe('execute()', () => {
237237
test('calls the executor with proper params', async () => {
238238
const executor = jest.fn().mockResolvedValueOnce({ success: true });
239-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
240-
actionTypeService.register({
239+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
240+
actionTypeRegistry.register({
241241
id: 'my-action-type',
242242
name: 'My action type',
243243
executor,
244244
});
245-
await actionTypeService.execute({
245+
await actionTypeRegistry.execute({
246246
id: 'my-action-type',
247247
actionTypeConfig: { foo: true },
248248
params: { bar: false },
@@ -276,8 +276,8 @@ describe('execute()', () => {
276276

277277
test('validates params', async () => {
278278
const executor = jest.fn().mockResolvedValueOnce({ success: true });
279-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
280-
actionTypeService.register({
279+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
280+
actionTypeRegistry.register({
281281
id: 'my-action-type',
282282
name: 'My action type',
283283
executor,
@@ -290,7 +290,7 @@ describe('execute()', () => {
290290
},
291291
});
292292
await expect(
293-
actionTypeService.execute({
293+
actionTypeRegistry.execute({
294294
id: 'my-action-type',
295295
actionTypeConfig: {},
296296
params: {},
@@ -302,8 +302,8 @@ describe('execute()', () => {
302302

303303
test('validates actionTypeConfig', async () => {
304304
const executor = jest.fn().mockResolvedValueOnce({ success: true });
305-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
306-
actionTypeService.register({
305+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
306+
actionTypeRegistry.register({
307307
id: 'my-action-type',
308308
name: 'My action type',
309309
executor,
@@ -316,7 +316,7 @@ describe('execute()', () => {
316316
},
317317
});
318318
await expect(
319-
actionTypeService.execute({
319+
actionTypeRegistry.execute({
320320
id: 'my-action-type',
321321
actionTypeConfig: {},
322322
params: {},
@@ -327,9 +327,9 @@ describe('execute()', () => {
327327
});
328328

329329
test('throws error if action type not registered', async () => {
330-
const actionTypeService = new ActionTypeService(actionTypeServiceParams);
330+
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
331331
await expect(
332-
actionTypeService.execute({
332+
actionTypeRegistry.execute({
333333
id: 'my-action-type',
334334
actionTypeConfig: { foo: true },
335335
params: { bar: false },

0 commit comments

Comments
 (0)