@5minds/node-red-contrib-processcube-tools 1.0.1-feature-cf0d8b-mfl5sxpl → 1.0.1-feature-258a01-mfm4xbwl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/test/unit/email-sender.unit.test.js +260 -15
package/package.json
CHANGED
|
@@ -355,22 +355,267 @@ describe('E-Mail Sender Node Unit Tests', function () {
|
|
|
355
355
|
});
|
|
356
356
|
|
|
357
357
|
it('should handle rejected emails and set status to rejected', function (done) {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
});
|
|
358
|
+
let errorHandlerCalled = false;
|
|
359
|
+
let redStatusSet = false;
|
|
361
360
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
361
|
+
function checkDone() {
|
|
362
|
+
console.log('Check done - Error:', errorHandlerCalled, 'Status:', redStatusSet);
|
|
363
|
+
if (errorHandlerCalled && redStatusSet) {
|
|
364
|
+
done();
|
|
365
|
+
}
|
|
366
|
+
}
|
|
366
367
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
368
|
+
// ARRANGE: Configure mock to simulate rejected emails
|
|
369
|
+
const mockOptions = {
|
|
370
|
+
rejectedEmails: ['recipient@example.com'],
|
|
371
|
+
acceptedEmails: [] // Ensure no emails are accepted
|
|
372
|
+
};
|
|
373
|
+
console.log('Creating mock with rejected email options:', mockOptions);
|
|
371
374
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
375
|
+
const mockNodemailer = createMockNodemailer(mockOptions);
|
|
376
|
+
|
|
377
|
+
// Mock the nodemailer module
|
|
378
|
+
delete require.cache[require.resolve('nodemailer')];
|
|
379
|
+
require.cache[require.resolve('nodemailer')] = {
|
|
380
|
+
exports: mockNodemailer,
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const mockRED = createMockNodeRED({
|
|
384
|
+
onHandler: function (event, callback) {
|
|
385
|
+
if (event === 'input') {
|
|
386
|
+
this.inputCallback = callback;
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
statusHandler: function (status) {
|
|
390
|
+
console.log('Status received:', status);
|
|
391
|
+
if (status.fill === 'red' && status.text === 'rejected') {
|
|
392
|
+
redStatusSet = true;
|
|
393
|
+
checkDone();
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
errorHandler: function (err) {
|
|
397
|
+
console.log('Error received:', err);
|
|
398
|
+
expect(err.message).to.include('Email rejected: recipient@example.com');
|
|
399
|
+
errorHandlerCalled = true;
|
|
400
|
+
checkDone();
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
const emailSenderNode = require('../../email-sender/email-sender.js');
|
|
405
|
+
emailSenderNode(mockRED);
|
|
406
|
+
|
|
407
|
+
const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
|
|
408
|
+
const nodeInstance = new nodeConstructor(emailSenderConfigs.valid);
|
|
409
|
+
|
|
410
|
+
nodeInstance.inputCallback({
|
|
411
|
+
payload: 'test',
|
|
412
|
+
topic: 'test message',
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('should handle pending emails and set status to pending', function (done) {
|
|
417
|
+
let errorHandlerCalled = false;
|
|
418
|
+
let yellowStatusSet = false;
|
|
419
|
+
|
|
420
|
+
function checkDone() {
|
|
421
|
+
console.log('Check done - Error:', errorHandlerCalled, 'Status:', yellowStatusSet);
|
|
422
|
+
if (errorHandlerCalled && yellowStatusSet) {
|
|
423
|
+
done();
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ARRANGE: Configure mock to simulate pending emails
|
|
428
|
+
const mockOptions = {
|
|
429
|
+
pendingEmails: ['recipient@example.com'],
|
|
430
|
+
acceptedEmails: [] // Ensure no emails are accepted
|
|
431
|
+
};
|
|
432
|
+
console.log('Creating mock with pending email options:', mockOptions);
|
|
433
|
+
|
|
434
|
+
const mockNodemailer = createMockNodemailer(mockOptions);
|
|
435
|
+
|
|
436
|
+
// Mock the nodemailer module
|
|
437
|
+
delete require.cache[require.resolve('nodemailer')];
|
|
438
|
+
require.cache[require.resolve('nodemailer')] = {
|
|
439
|
+
exports: mockNodemailer,
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const mockRED = createMockNodeRED({
|
|
443
|
+
onHandler: function (event, callback) {
|
|
444
|
+
if (event === 'input') {
|
|
445
|
+
this.inputCallback = callback;
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
statusHandler: function (status) {
|
|
449
|
+
console.log('Status received:', status);
|
|
450
|
+
if (status.fill === 'yellow' && status.text === 'pending') {
|
|
451
|
+
yellowStatusSet = true;
|
|
452
|
+
checkDone();
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
errorHandler: function (err) {
|
|
456
|
+
console.log('Error received:', err);
|
|
457
|
+
expect(err.message).to.include('Email pending: recipient@example.com');
|
|
458
|
+
errorHandlerCalled = true;
|
|
459
|
+
checkDone();
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const emailSenderNode = require('../../email-sender/email-sender.js');
|
|
464
|
+
emailSenderNode(mockRED);
|
|
465
|
+
|
|
466
|
+
const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
|
|
467
|
+
const nodeInstance = new nodeConstructor(emailSenderConfigs.valid);
|
|
468
|
+
|
|
469
|
+
nodeInstance.inputCallback({
|
|
470
|
+
payload: 'test',
|
|
471
|
+
topic: 'test message',
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it('should handle a single attachment object correctly', function (done) {
|
|
476
|
+
let attachmentChecked = false;
|
|
477
|
+
let statusSet = false;
|
|
478
|
+
|
|
479
|
+
function checkDone() {
|
|
480
|
+
console.log('checkDone called - attachmentChecked:', attachmentChecked, 'statusSet:', statusSet);
|
|
481
|
+
if (attachmentChecked && statusSet) {
|
|
482
|
+
console.log('Both conditions met, calling done');
|
|
483
|
+
done();
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ARRANGE: Configure test with single attachment object
|
|
488
|
+
const singleAttachment = {
|
|
489
|
+
filename: 'single-test.txt',
|
|
490
|
+
content: 'This is a single test file.',
|
|
491
|
+
};
|
|
492
|
+
console.log('Single attachment configured:', singleAttachment);
|
|
493
|
+
|
|
494
|
+
const mockNodemailer = createMockNodemailer({
|
|
495
|
+
onSendMail: (mailOptions) => {
|
|
496
|
+
console.log('onSendMail called with attachments:', mailOptions.attachments);
|
|
497
|
+
expect(mailOptions.attachments).to.be.an('array').with.lengthOf(1);
|
|
498
|
+
expect(mailOptions.attachments[0].filename).to.equal('single-test.txt');
|
|
499
|
+
expect(mailOptions.attachments[0].content).to.equal('This is a single test file.');
|
|
500
|
+
attachmentChecked = true;
|
|
501
|
+
console.log('Single attachment checked successfully');
|
|
502
|
+
checkDone();
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
console.log('Mock nodemailer created for single attachment');
|
|
506
|
+
|
|
507
|
+
// Mock the nodemailer module
|
|
508
|
+
delete require.cache[require.resolve('nodemailer')];
|
|
509
|
+
require.cache[require.resolve('nodemailer')] = {
|
|
510
|
+
exports: mockNodemailer,
|
|
511
|
+
};
|
|
512
|
+
console.log('Nodemailer mock installed');
|
|
513
|
+
|
|
514
|
+
const mockRED = createMockNodeRED({
|
|
515
|
+
onHandler: function (event, callback) {
|
|
516
|
+
console.log('onHandler called with event:', event);
|
|
517
|
+
if (event === 'input') {
|
|
518
|
+
this.inputCallback = callback;
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
statusHandler: function (status) {
|
|
522
|
+
console.log('statusHandler called with status:', status);
|
|
523
|
+
if (status.fill === 'green') {
|
|
524
|
+
expect(status.text).to.include('sent');
|
|
525
|
+
expect(status.shape).to.equal('dot');
|
|
526
|
+
statusSet = true;
|
|
527
|
+
console.log('Status set successfully for single attachment');
|
|
528
|
+
checkDone();
|
|
529
|
+
}
|
|
530
|
+
},
|
|
531
|
+
errorHandler: function (err) {
|
|
532
|
+
console.log('errorHandler called with:', err);
|
|
533
|
+
done(err || new Error('Unexpected error handler called'));
|
|
534
|
+
},
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const emailSenderNode = require('../../email-sender/email-sender.js');
|
|
538
|
+
emailSenderNode(mockRED);
|
|
539
|
+
console.log('Email sender node initialized for single attachment');
|
|
540
|
+
|
|
541
|
+
const config = { ...emailSenderConfigs.valid };
|
|
542
|
+
config.attachments = JSON.stringify(singleAttachment);
|
|
543
|
+
config.attachmentsType = 'json';
|
|
544
|
+
|
|
545
|
+
const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
|
|
546
|
+
const nodeInstance = new nodeConstructor(config);
|
|
547
|
+
console.log('Node instance created with single attachment');
|
|
548
|
+
|
|
549
|
+
setTimeout(() => {
|
|
550
|
+
nodeInstance.inputCallback({
|
|
551
|
+
payload: 'test',
|
|
552
|
+
topic: 'test message',
|
|
553
|
+
});
|
|
554
|
+
}, 100);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('should handle an empty attachments string without error', function (done) {
|
|
558
|
+
let statusSet = false;
|
|
559
|
+
|
|
560
|
+
// ARRANGE: Create mock nodemailer to verify no attachments are processed
|
|
561
|
+
const mockNodemailer = createMockNodemailer({
|
|
562
|
+
onSendMail: (mailOptions) => {
|
|
563
|
+
console.log('onSendMail called with attachments:', mailOptions.attachments);
|
|
564
|
+
// Should be an empty array when no attachments are provided
|
|
565
|
+
expect(mailOptions.attachments).to.be.an('array').with.lengthOf(0);
|
|
566
|
+
console.log('Empty attachments verified successfully');
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
console.log('Mock nodemailer created for empty attachments test');
|
|
570
|
+
|
|
571
|
+
// Mock the nodemailer module
|
|
572
|
+
delete require.cache[require.resolve('nodemailer')];
|
|
573
|
+
require.cache[require.resolve('nodemailer')] = {
|
|
574
|
+
exports: mockNodemailer,
|
|
575
|
+
};
|
|
576
|
+
console.log('Nodemailer mock installed');
|
|
577
|
+
|
|
578
|
+
const mockRED = createMockNodeRED({
|
|
579
|
+
onHandler: function (event, callback) {
|
|
580
|
+
console.log('onHandler called with event:', event);
|
|
581
|
+
if (event === 'input') {
|
|
582
|
+
this.inputCallback = callback;
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
statusHandler: function (status) {
|
|
586
|
+
console.log('statusHandler called with status:', status);
|
|
587
|
+
if (status.fill === 'green') {
|
|
588
|
+
expect(status.text).to.include('sent');
|
|
589
|
+
expect(status.shape).to.equal('dot');
|
|
590
|
+
statusSet = true;
|
|
591
|
+
console.log('Status set successfully for empty attachments');
|
|
592
|
+
done();
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
errorHandler: function (err) {
|
|
596
|
+
console.log('errorHandler called with:', err);
|
|
597
|
+
done(err || new Error('Unexpected error handler called'));
|
|
598
|
+
},
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
const emailSenderNode = require('../../email-sender/email-sender.js');
|
|
602
|
+
emailSenderNode(mockRED);
|
|
603
|
+
console.log('Email sender node initialized for empty attachments');
|
|
604
|
+
|
|
605
|
+
const config = { ...emailSenderConfigs.valid };
|
|
606
|
+
// Set attachments to empty string to test this scenario
|
|
607
|
+
config.attachments = '';
|
|
608
|
+
config.attachmentsType = 'str';
|
|
609
|
+
|
|
610
|
+
const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
|
|
611
|
+
const nodeInstance = new nodeConstructor(config);
|
|
612
|
+
console.log('Node instance created with empty attachments');
|
|
613
|
+
|
|
614
|
+
setTimeout(() => {
|
|
615
|
+
nodeInstance.inputCallback({
|
|
616
|
+
payload: 'test',
|
|
617
|
+
topic: 'test message',
|
|
618
|
+
});
|
|
619
|
+
}, 100);
|
|
620
|
+
});
|
|
376
621
|
});
|