-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisable-import-audit-processor.test.js
More file actions
465 lines (373 loc) · 17.7 KB
/
Copy pathdisable-import-audit-processor.test.js
File metadata and controls
465 lines (373 loc) · 17.7 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
import { expect, use } from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import esmock from 'esmock';
import { MockContextBuilder } from '../../shared.js';
use(sinonChai);
// Dynamic import for ES modules
let runDisableImportAuditProcessor;
let mockSay;
describe('Disable Import Audit Processor', () => {
let context;
let message;
let mockSite;
let mockSiteConfig;
let mockConfiguration;
let toDynamoItemStub;
const serializedConfigFixture = {
slack: {},
handlers: {},
imports: [],
};
beforeEach(async () => {
// Reset all stubs
sinon.restore();
// Create sandbox
const sandbox = sinon.createSandbox();
// Mock the say function
mockSay = sandbox.stub().resolves();
toDynamoItemStub = sandbox.stub().returns(serializedConfigFixture);
// Dynamic import with mocked dependencies
const handlerModule = await esmock('../../../src/tasks/disable-import-audit-processor/handler.js', {
'../../../src/utils/slack-utils.js': {
say: mockSay,
},
'@adobe/spacecat-shared-data-access/src/models/site/config.js': {
Config: { toDynamoItem: toDynamoItemStub },
},
});
runDisableImportAuditProcessor = handlerModule.runDisableImportAuditProcessor;
// Mock site and configuration
mockSiteConfig = {
disableImport: sandbox.stub(),
};
mockSite = {
getConfig: sandbox.stub().returns(mockSiteConfig),
setConfig: sandbox.stub(),
save: sandbox.stub().resolves(),
};
mockConfiguration = {
disableHandlerForSite: sandbox.stub(),
save: sandbox.stub().resolves(),
};
// Mock context
context = new MockContextBuilder()
.withSandbox(sandbox)
.build();
// Override data access with our mocks
context.dataAccess.Site.findByBaseURL = sandbox.stub().resolves(mockSite);
context.dataAccess.Configuration.findLatest = sandbox.stub().resolves(mockConfiguration);
// Mock message
message = {
siteId: 'test-site-id',
siteUrl: 'https://example.com',
organizationId: 'test-org-id',
taskContext: {
importTypes: ['ahrefs', 'screaming-frog'],
auditTypes: ['cwv', 'broken-links'],
slackContext: 'test-slack-context',
},
};
});
afterEach(() => {
sinon.restore();
});
describe('runDisableImportAuditProcessor', () => {
describe('successful processing', () => {
it('should process disable import and audit successfully', async () => {
const result = await runDisableImportAuditProcessor(message, context);
// Verify initial logging
expect(context.log.info).to.have.been.calledWith('Processing disable import and audit request:', {
taskType: 'disable-import-audit-processor',
siteId: 'test-site-id',
organizationId: 'test-org-id',
importTypes: ['ahrefs', 'screaming-frog'],
auditTypes: ['cwv', 'broken-links'],
scheduledRun: false,
});
// Verify site lookup
expect(context.dataAccess.Site.findByBaseURL).to.have.been.calledOnceWith('https://example.com');
expect(mockSite.getConfig).to.have.been.calledOnce;
// Verify import types were disabled
expect(mockSiteConfig.disableImport).to.have.been.calledWith('ahrefs');
expect(mockSiteConfig.disableImport).to.have.been.calledWith('screaming-frog');
expect(mockSiteConfig.disableImport).to.have.callCount(2);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
// Verify audit types were disabled
expect(context.dataAccess.Configuration.findLatest).to.have.been.calledOnce;
expect(mockConfiguration.disableHandlerForSite).to.have.been.calledWith('cwv', mockSite);
expect(mockConfiguration.disableHandlerForSite).to.have.been.calledWith('broken-links', mockSite);
expect(mockConfiguration.disableHandlerForSite).to.have.callCount(2);
// Verify saves were called
expect(mockSite.save).to.have.been.calledOnce;
expect(mockConfiguration.save).to.have.been.calledOnce;
// Verify completion logging
expect(context.log.info).to.have.been.calledWith('For site: https://example.com: Disabled imports and audits');
// Verify Slack messages
expect(mockSay).to.have.been.calledTwice;
expect(mockSay.firstCall).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':broom: *For site: https://example.com: Disabled imports*: ahrefs, screaming-frog *and audits*: cwv, broken-links',
);
expect(mockSay.secondCall).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':information_source: The list of enabled imports and audits may differ from the disabled ones because items that are already enabled are not automatically disabled. When schedule run flag is true then no imports and audits are disabled.',
);
// Verify successful completion
expect(result).to.exist;
});
it('should handle empty import and audit arrays', async () => {
message.taskContext.importTypes = [];
message.taskContext.auditTypes = [];
const result = await runDisableImportAuditProcessor(message, context);
// Verify no disable calls were made
expect(mockSiteConfig.disableImport).to.not.have.been.called;
expect(mockConfiguration.disableHandlerForSite).to.not.have.been.called;
// Verify saves were still called
expect(mockSite.save).to.have.been.calledOnce;
expect(mockConfiguration.save).to.have.been.calledOnce;
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
// Verify Slack messages with "None" text
expect(mockSay.firstCall).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':broom: *For site: https://example.com: Disabled imports*: None *and audits*: None',
);
expect(result).to.exist;
});
it('should handle missing taskContext properties', async () => {
message.taskContext = {
slackContext: 'test-slack-context',
};
const result = await runDisableImportAuditProcessor(message, context);
// Verify no disable calls were made
expect(mockSiteConfig.disableImport).to.not.have.been.called;
expect(mockConfiguration.disableHandlerForSite).to.not.have.been.called;
// Verify Slack messages with "None" text
expect(mockSay.firstCall).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':broom: *For site: https://example.com: Disabled imports*: None *and audits*: None',
);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(result).to.exist;
});
it('should handle missing slackContext gracefully', async () => {
delete message.taskContext.slackContext;
const result = await runDisableImportAuditProcessor(message, context);
// Should complete without error
expect(context.log.info).to.have.been.calledWith('For site: https://example.com: Disabled imports and audits');
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(mockSay).to.have.been.calledTwice;
expect(result).to.exist;
});
});
describe('scheduled run handling', () => {
it('should skip disable operations when scheduledRun is true', async () => {
message.taskContext.scheduledRun = true;
const result = await runDisableImportAuditProcessor(message, context);
// Should log scheduled run detection
expect(context.log.info).to.have.been.calledWith('Processing disable import and audit request:', {
taskType: 'disable-import-audit-processor',
siteId: 'test-site-id',
organizationId: 'test-org-id',
importTypes: ['ahrefs', 'screaming-frog'],
auditTypes: ['cwv', 'broken-links'],
scheduledRun: true,
});
expect(context.log.info).to.have.been.calledWith('Scheduled run detected - skipping disable of imports and audits for site: https://example.com');
// Should not perform any disable operations
expect(context.dataAccess.Site.findByBaseURL).to.not.have.been.called;
expect(mockSiteConfig.disableImport).to.not.have.been.called;
expect(mockConfiguration.disableHandlerForSite).to.not.have.been.called;
expect(mockSite.save).to.not.have.been.called;
expect(mockConfiguration.save).to.not.have.been.called;
// Should send Slack notification about scheduled run
expect(mockSay).to.have.been.calledOnceWith(
context.env,
context.log,
'test-slack-context',
':information_source: Scheduled run detected for site https://example.com - skipping disable of imports and audits',
);
// Should return success message indicating no operations performed
expect(result.status).to.equal(200);
const resultBody = await result.json();
expect(resultBody).to.deep.equal({
message: 'Scheduled run - no disable of imports and audits performed',
});
});
it('should handle scheduled run with empty arrays', async () => {
message.taskContext.scheduledRun = true;
message.taskContext.importTypes = [];
message.taskContext.auditTypes = [];
const result = await runDisableImportAuditProcessor(message, context);
expect(mockSay).to.have.been.calledOnceWith(
context.env,
context.log,
'test-slack-context',
':information_source: Scheduled run detected for site https://example.com - skipping disable of imports and audits',
);
expect(result.status).to.equal(200);
const resultBody = await result.json();
expect(resultBody).to.deep.equal({
message: 'Scheduled run - no disable of imports and audits performed',
});
});
});
describe('error handling', () => {
it('should handle site not found error', async () => {
context.dataAccess.Site.findByBaseURL.resolves(null);
const result = await runDisableImportAuditProcessor(message, context);
expect(context.log.error).to.have.been.calledWith('Error in disable import and audit processor:', sinon.match.instanceOf(Error));
expect(mockSite.setConfig).to.not.have.been.called;
expect(mockSite.save).to.not.have.been.called;
expect(mockConfiguration.save).to.not.have.been.called;
// Should send error Slack message
expect(mockSay).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':x: Error disabling imports and audits: Site not found for siteId: test-site-id',
);
expect(result).to.exist;
});
it('should handle site save errors', async () => {
const saveError = new Error('Database save error');
mockSite.save.rejects(saveError);
const result = await runDisableImportAuditProcessor(message, context);
expect(context.log.error).to.have.been.calledWith('Error in disable import and audit processor:', saveError);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(mockSay).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':x: Error disabling imports and audits: Database save error',
);
expect(result).to.exist;
});
it('should handle configuration save errors', async () => {
const saveError = new Error('Configuration save failed');
mockConfiguration.save.rejects(saveError);
const result = await runDisableImportAuditProcessor(message, context);
expect(context.log.error).to.have.been.calledWith('Error in disable import and audit processor:', saveError);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(mockSay).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':x: Error disabling imports and audits: Configuration save failed',
);
expect(result).to.exist;
});
it('should handle site lookup errors', async () => {
const lookupError = new Error('Database connection failed');
context.dataAccess.Site.findByBaseURL.rejects(lookupError);
const result = await runDisableImportAuditProcessor(message, context);
expect(context.log.error).to.have.been.calledWith('Error in disable import and audit processor:', lookupError);
expect(mockSite.setConfig).to.not.have.been.called;
expect(mockSay).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':x: Error disabling imports and audits: Database connection failed',
);
expect(result).to.exist;
});
it('should handle configuration lookup errors', async () => {
const configError = new Error('Configuration not found');
context.dataAccess.Configuration.findLatest.rejects(configError);
const result = await runDisableImportAuditProcessor(message, context);
expect(context.log.error).to.have.been.calledWith('Error in disable import and audit processor:', configError);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(mockSay).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':x: Error disabling imports and audits: Configuration not found',
);
expect(result).to.exist;
});
it('should handle Slack notification errors gracefully', async () => {
const slackError = new Error('Slack API error');
mockSay.onFirstCall().rejects(slackError);
mockSay.onSecondCall().resolves(); // Second call should succeed
const result = await runDisableImportAuditProcessor(message, context);
// Should still complete successfully despite Slack error
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(mockSite.save).to.have.been.calledOnce;
expect(mockConfiguration.save).to.have.been.calledOnce;
expect(result).to.exist;
});
});
describe('input validation', () => {
it('should handle null taskContext', async () => {
message.taskContext = null;
try {
await runDisableImportAuditProcessor(message, context);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.instanceOf(TypeError);
expect(error.message).to.include('Cannot read properties of null');
}
});
it('should handle undefined taskContext', async () => {
delete message.taskContext;
try {
await runDisableImportAuditProcessor(message, context);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).to.be.instanceOf(TypeError);
expect(error.message).to.include('Cannot read properties of undefined');
}
});
it('should handle missing siteUrl', async () => {
delete message.siteUrl;
const result = await runDisableImportAuditProcessor(message, context);
expect(context.dataAccess.Site.findByBaseURL).to.have.been.calledWith(undefined);
expect(result).to.exist;
});
it('should handle single import and audit types', async () => {
message.taskContext.importTypes = ['single-import'];
message.taskContext.auditTypes = ['single-audit'];
const result = await runDisableImportAuditProcessor(message, context);
expect(mockSiteConfig.disableImport).to.have.been.calledOnceWith('single-import');
expect(mockConfiguration.disableHandlerForSite).to.have.been.calledOnceWith('single-audit', mockSite);
expect(mockSay.firstCall).to.have.been.calledWith(
context.env,
context.log,
'test-slack-context',
':broom: *For site: https://example.com: Disabled imports*: single-import *and audits*: single-audit',
);
expect(toDynamoItemStub).to.have.been.calledOnceWith(mockSiteConfig);
expect(mockSite.setConfig).to.have.been.calledOnceWith(serializedConfigFixture);
expect(result).to.exist;
});
});
});
});