-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathactions.test.js
More file actions
395 lines (356 loc) · 10.8 KB
/
actions.test.js
File metadata and controls
395 lines (356 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import path from 'path';
import fs from 'fs-extra';
import { expect } from 'chai';
import Context from '../../../src/context/yaml';
import handler from '../../../src/context/yaml/handlers/actions';
import { cleanThenMkdir, testDataDir, mockMgmtClient } from '../../utils';
describe('#YAML context actions', () => {
it('should process actions', async () => {
const dir = path.join(testDataDir, 'yaml', 'action-one');
cleanThenMkdir(dir);
const codeContext =
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log(@@replace@@); return {}; };';
const codeFile = path.join(dir, 'code.js');
fs.writeFileSync(codeFile, codeContext);
const yaml = `
actions:
- name: action-one
code: "${codeFile}"
runtime: node12
dependencies:
- name: lodash
version: 4.17.21
deployed: true
secrets: []
status: built
supported_triggers:
- id: post-login
version: v1
`;
const yamlFile = path.join(dir, 'action-one.yaml');
fs.writeFileSync(yamlFile, yaml);
const target = [
{
name: 'action-one',
code: '/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };',
runtime: 'node12',
status: 'built',
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
dependencies: [
{
name: 'lodash',
version: '4.17.21',
},
],
secrets: [],
deployed: true,
},
];
const config = {
AUTH0_INPUT_FILE: yamlFile,
AUTH0_KEYWORD_REPLACE_MAPPINGS: { replace: 'test-action' },
};
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.actions).to.deep.equal(target);
});
it('should process YAML with includes', async () => {
const dir = path.join(testDataDir, 'yaml', 'includes');
cleanThenMkdir(dir);
const clientsYaml = `
- name: "Test Client"
app_type: "spa"
- name: "Test M2M"
app_type: "non_interactive"
`;
const clientsFile = path.join(dir, 'clients.yaml');
fs.writeFileSync(clientsFile, clientsYaml);
const mainYaml = `
tenant:
friendly_name: 'Test Tenant'
clients: !include clients.yaml
`;
const mainFile = path.join(dir, 'tenant.yaml');
fs.writeFileSync(mainFile, mainYaml);
const config = { AUTH0_INPUT_FILE: mainFile };
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.tenant).to.deep.equal({
friendly_name: 'Test Tenant',
});
expect(context.assets.clients).to.deep.equal([
{
name: 'Test Client',
app_type: 'spa',
},
{
name: 'Test M2M',
app_type: 'non_interactive',
},
]);
});
it('should handle nested includes', async () => {
const dir = path.join(testDataDir, 'yaml', 'nested-includes');
cleanThenMkdir(dir);
const rolesYaml = `
- name: Admin
description: Administrator
- name: User
description: Regular User
`;
fs.writeFileSync(path.join(dir, 'roles.yaml'), rolesYaml);
const mainYaml = `
tenant:
friendly_name: 'Main Tenant'
roles: !include roles.yaml
`;
fs.writeFileSync(path.join(dir, 'tenant.yaml'), mainYaml);
const config = { AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml') };
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.roles).to.deep.equal([
{ name: 'Admin', description: 'Administrator' },
{ name: 'User', description: 'Regular User' },
]);
});
it('should process logStreams with includes', async () => {
const dir = path.join(testDataDir, 'yaml', 'logstreams-includes');
cleanThenMkdir(dir);
const logStreamsYaml = `
- name: LoggingSAAS
isPriority: false
filters:
- type: category
name: auth.login.fail
- type: category
name: auth.login.notification
- type: category
name: auth.login.success
- type: category
name: auth.logout.fail
sink:
httpContentFormat: JSONLINES
httpContentType: application/json
httpEndpoint: "##LOGGING_WEBHOOK_URL##"
type: http
- name: SIEM
isPriority: false
filters:
- type: category
name: auth.login.fail
- type: category
name: auth.login.notification
- type: category
name: auth.login.success
sink:
httpContentFormat: JSONLINES
httpContentType: application/json
httpEndpoint: "##SIEM_WEBHOOK_URL##"
type: http
`;
fs.writeFileSync(path.join(dir, 'logStreams.yaml'), logStreamsYaml);
const mainYaml = `
tenant:
friendly_name: 'Test Tenant'
logStreams: !include logStreams.yaml
`;
fs.writeFileSync(path.join(dir, 'tenant.yaml'), mainYaml);
const config = {
AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml'),
AUTH0_KEYWORD_REPLACE_MAPPINGS: {
LOGGING_WEBHOOK_URL: 'https://logging.com/inputs/test',
SIEM_WEBHOOK_URL: 'https://siem.example.com/webhook'
}
};
const context = new Context(config, mockMgmtClient());
await context.loadAssetsFromLocal();
expect(context.assets.logStreams).to.have.length(2);
expect(context.assets.logStreams[0]).to.deep.include({
name: 'LoggingSAAS',
isPriority: false,
type: 'http'
});
expect(context.assets.logStreams[0].sink.httpEndpoint).to.equal('https://logging.com/inputs/test');
expect(context.assets.logStreams[1]).to.deep.include({
name: 'SIEM',
isPriority: false,
type: 'http'
});
expect(context.assets.logStreams[1].sink.httpEndpoint).to.equal('https://siem.example.com/webhook');
});
it('should error on missing include file', async () => {
const dir = path.join(testDataDir, 'yaml', 'missing-include');
cleanThenMkdir(dir);
const mainYaml = `
clients: !include missing.yaml
`;
fs.writeFileSync(path.join(dir, 'tenant.yaml'), mainYaml);
const config = { AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml') };
const context = new Context(config, mockMgmtClient());
await expect(context.loadAssetsFromLocal()).to.be.eventually.rejectedWith(
Error,
/Include file not found/
);
});
it('should dump actions', async () => {
const dir = path.join(testDataDir, 'yaml', 'actionsDump');
cleanThenMkdir(dir);
const context = new Context(
{ AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml') },
mockMgmtClient()
);
const codeValidation =
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };';
context.assets.actions = [
{
name: 'action-one',
code: codeValidation,
runtime: 'node12',
status: 'built',
dependencies: [
{
name: 'lodash',
version: '4.17.20',
},
],
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
secrets: [],
deployed: true,
},
];
const dumped = await handler.dump(context);
expect(dumped).to.deep.equal({
actions: [
{
name: 'action-one',
code: './actions/action-one/code.js',
runtime: 'node12',
status: 'built',
dependencies: [
{
name: 'lodash',
version: '4.17.20',
},
],
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
secrets: [],
deployed: true,
},
],
});
const actionsFolder = path.join(dir, 'actions', 'action-one');
expect(fs.readFileSync(path.join(actionsFolder, 'code.js'), 'utf8')).to.deep.equal(
codeValidation
);
});
it('should dump actions with identifiers when AUTH0_EXPORT_IDENTIFIERS is true', async () => {
const dir = path.join(testDataDir, 'yaml', 'actionsDumpWithId');
cleanThenMkdir(dir);
const context = new Context(
{ AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml'), AUTH0_EXPORT_IDENTIFIERS: true },
mockMgmtClient()
);
const codeValidation =
'/** @type {PostLoginAction} */ module.exports = async (event, context) => { console.log("test-action"); return {}; };';
context.assets.actions = [
{
id: 'act_123',
name: 'action-one',
code: codeValidation,
runtime: 'node12',
status: 'built',
dependencies: [],
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
secrets: [],
deployed: true,
},
];
const dumped = await handler.dump(context);
expect(dumped).to.deep.equal({
actions: [
{
id: 'act_123',
name: 'action-one',
code: './actions/action-one/code.js',
runtime: 'node12',
status: 'built',
dependencies: [],
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
secrets: [],
deployed: true,
},
],
});
const actionsFolder = path.join(dir, 'actions', 'action-one');
expect(fs.readFileSync(path.join(actionsFolder, 'code.js'), 'utf8')).to.deep.equal(
codeValidation
);
});
it('should exclude marketplace actions during dump', async () => {
const dir = path.join(testDataDir, 'yaml', 'actionsDump');
cleanThenMkdir(dir);
const context = new Context(
{ AUTH0_INPUT_FILE: path.join(dir, 'tenant.yaml') },
mockMgmtClient()
);
const marketplaceAction = {
id: 'D1AF7CCF-7ZAB-417F-81C0-533595A926D8',
name: 'Travel0 Integration',
supported_triggers: [
{
id: 'post-login',
version: 'v1',
},
],
created_at: '2022-08-22T23:57:45.856907897Z',
updated_at: '2022-08-22T23:57:45.856907897Z',
installed_integration_id: '73f156dc-e7aa-47b4-9dda-0ef741205c31',
integration: {
id: '046042e2-5732-48ef-9313-0a93778ea8b1',
catalog_id: 'travel0-action',
url_slug: 'travel0-sms',
partner_id: 'bea44019-d08d-47cd-b4f9-30074ca2ab69',
name: 'Travel0',
logo: 'https://cdn.auth0.com/travel0-logo.png',
updated_at: '2022-05-03T15:05:45.684007768Z',
created_at: '2021-08-24T20:49:30.446854653Z',
feature_type: 'action',
current_release: {
id: '',
semver: {},
},
all_changes_deployed: false,
},
};
context.assets.actions = [marketplaceAction];
const dumped = await handler.dump(context);
expect(dumped).to.deep.equal({
actions: [],
});
});
});