@5minds/node-red-contrib-processcube-tools 1.0.1-feature-24a79f-mfgha47l → 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.
@@ -0,0 +1,621 @@
1
+ const { expect } = require('chai');
2
+ const {
3
+ createMockNodeRED,
4
+ setupModuleMocks,
5
+ emailSenderConfigs,
6
+ createMockNodemailer,
7
+ } = require('../helpers/email-sender.mocks.js');
8
+
9
+ describe('E-Mail Sender Node Unit Tests', function () {
10
+ this.timeout(10000);
11
+
12
+ let emailSenderNode;
13
+ let cleanupMocks;
14
+
15
+ before(function () {
16
+ // Set up module mocks using helper
17
+ cleanupMocks = setupModuleMocks();
18
+
19
+ // Load the node with mocked dependencies
20
+ emailSenderNode = require('../../email-sender/email-sender.js');
21
+ });
22
+
23
+ after(function () {
24
+ // Clean up mocks
25
+ if (cleanupMocks) {
26
+ cleanupMocks();
27
+ }
28
+ });
29
+
30
+ // A separate describe block for module export
31
+ describe('Module Export', function () {
32
+ it('should export a function', function () {
33
+ expect(emailSenderNode).to.be.a('function');
34
+ });
35
+ });
36
+
37
+ describe('Node Registration', function () {
38
+ it('should register node type without errors', function () {
39
+ // ARRANGE: Create mock Node-RED with tracking
40
+ const mockRED = createMockNodeRED();
41
+
42
+ // ACT: Register the node
43
+ emailSenderNode(mockRED);
44
+
45
+ // ASSERT: Verify registration
46
+ expect(mockRED.nodes.lastRegisteredType).to.equal('email-sender');
47
+ expect(mockRED.nodes.lastRegisteredConstructor).to.be.a('function');
48
+ });
49
+ });
50
+
51
+ describe('Node Instantiation', function () {
52
+ it('should handle node instantiation with valid config', function () {
53
+ // ARRANGE: Track node creation
54
+ let createdNode = null;
55
+ const mockRED = createMockNodeRED({
56
+ onHandler: function (event, callback) {
57
+ createdNode = this;
58
+ },
59
+ });
60
+
61
+ // ACT: Register and create node instance
62
+ emailSenderNode(mockRED);
63
+ new mockRED.nodes.lastRegisteredConstructor(emailSenderConfigs.valid);
64
+
65
+ // ASSERT: Verify node was created with correct properties
66
+ expect(createdNode).to.exist;
67
+ expect(createdNode).to.have.property('name', emailSenderConfigs.valid.name);
68
+ expect(createdNode).to.have.property('id', emailSenderConfigs.valid.id);
69
+ });
70
+
71
+ it('should handle minimal config', function () {
72
+ // ARRANGE: Use minimal test config
73
+ let createdNode = null;
74
+ const mockRED = createMockNodeRED({
75
+ onHandler: function (event, callback) {
76
+ createdNode = this;
77
+ },
78
+ });
79
+
80
+ // ACT: Register and create node with minimal config
81
+ emailSenderNode(mockRED);
82
+ new mockRED.nodes.lastRegisteredConstructor(emailSenderConfigs.minimal);
83
+
84
+ // ASSERT: Verify node creation
85
+ expect(createdNode).to.exist;
86
+ expect(createdNode).to.have.property('id', emailSenderConfigs.minimal.id);
87
+ });
88
+ });
89
+
90
+ describe('Node Functionality', function () {
91
+ beforeEach(function () {
92
+ // Clear the module cache BEFORE requiring anything
93
+ delete require.cache[require.resolve('nodemailer')];
94
+ });
95
+
96
+ afterEach(function () {
97
+ delete require.cache[require.resolve('nodemailer')];
98
+ });
99
+
100
+ it('should send email successfully and set status to "sent"', function (done) {
101
+ let statusSet = false;
102
+
103
+ // ARRANGE: Initialisiere mockNodemailer
104
+ const mockNodemailer = createMockNodemailer();
105
+
106
+ // Mock the nodemailer module
107
+ delete require.cache[require.resolve('nodemailer')];
108
+ require.cache[require.resolve('nodemailer')] = {
109
+ exports: mockNodemailer,
110
+ };
111
+
112
+ // ARRANGE: Create mock Node-RED environment
113
+ const mockRED = createMockNodeRED({
114
+ onHandler: function (event, callback) {
115
+ if (event === 'input') {
116
+ this.inputCallback = callback;
117
+ }
118
+ },
119
+ statusHandler: function (status) {
120
+ if (status.fill === 'green') {
121
+ expect(status.text).to.include('sent');
122
+ expect(status.shape).to.equal('dot');
123
+ statusSet = true;
124
+ done();
125
+ }
126
+ },
127
+ errorHandler: function (err) {
128
+ done(err || new Error('Unexpected error handler called'));
129
+ },
130
+ });
131
+
132
+ // ACT: Initialize the email sender node
133
+ const emailSenderNode = require('../../email-sender/email-sender.js');
134
+ emailSenderNode(mockRED);
135
+
136
+ const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
137
+ const nodeInstance = new nodeConstructor(emailSenderConfigs.valid);
138
+
139
+ nodeInstance.inputCallback({
140
+ payload: 'test',
141
+ topic: 'test message',
142
+ });
143
+ });
144
+
145
+ it('should handle sendMail error and set status to "error sending"', function (done) {
146
+ let errorHandlerCalled = false;
147
+ let redStatusSet = false;
148
+
149
+ function checkDone() {
150
+ console.log('Check done - Error:', errorHandlerCalled, 'Status:', redStatusSet);
151
+ if (errorHandlerCalled && redStatusSet) {
152
+ done();
153
+ }
154
+ }
155
+
156
+ // Explizit shouldFail auf true setzen
157
+ const mockOptions = { shouldFail: true };
158
+ console.log('Creating mock with options:', mockOptions); // Debug log
159
+
160
+ const mockNodemailer = createMockNodemailer(mockOptions);
161
+
162
+ // Mock the nodemailer module
163
+ delete require.cache[require.resolve('nodemailer')];
164
+ require.cache[require.resolve('nodemailer')] = {
165
+ exports: mockNodemailer,
166
+ };
167
+
168
+ const mockRED = createMockNodeRED({
169
+ onHandler: function (event, callback) {
170
+ if (event === 'input') {
171
+ this.inputCallback = callback;
172
+ }
173
+ },
174
+ statusHandler: function (status) {
175
+ console.log('Status received:', status);
176
+ if (status.fill === 'red' && status.text === 'error sending') {
177
+ redStatusSet = true;
178
+ checkDone();
179
+ }
180
+ },
181
+ errorHandler: function (err) {
182
+ console.log('Error received:', err);
183
+ expect(err.message).to.equal('Mock sendMail error');
184
+ errorHandlerCalled = true;
185
+ checkDone();
186
+ },
187
+ });
188
+
189
+ const emailSenderNode = require('../../email-sender/email-sender.js');
190
+ emailSenderNode(mockRED);
191
+
192
+ const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
193
+ const nodeInstance = new nodeConstructor(emailSenderConfigs.valid);
194
+
195
+ nodeInstance.inputCallback({
196
+ payload: 'test',
197
+ topic: 'test message',
198
+ });
199
+ });
200
+
201
+ it('should handle an array of attachments correctly', function (done) {
202
+ let attachmentsChecked = false;
203
+ let statusSet = false;
204
+
205
+ function checkDone() {
206
+ console.log('checkDone called - attachmentsChecked:', attachmentsChecked, 'statusSet:', statusSet);
207
+ if (attachmentsChecked && statusSet) {
208
+ console.log('Both conditions met, calling done');
209
+ done();
210
+ }
211
+ }
212
+
213
+ // ARRANGE: Configure test attachments
214
+ const attachments = [
215
+ {
216
+ filename: 'test1.txt',
217
+ content: 'This is the first test file.',
218
+ },
219
+ {
220
+ filename: 'test2.txt',
221
+ content: 'This is the second test file.',
222
+ },
223
+ ];
224
+ console.log('Test attachments configured');
225
+
226
+ const mockNodemailer = createMockNodemailer({
227
+ onSendMail: (mailOptions) => {
228
+ console.log('onSendMail called with attachments:', mailOptions.attachments);
229
+ expect(mailOptions.attachments).to.be.an('array').with.lengthOf(2);
230
+ expect(mailOptions.attachments[0].filename).to.equal('test1.txt');
231
+ expect(mailOptions.attachments[1].content).to.equal('This is the second test file.');
232
+ attachmentsChecked = true;
233
+ console.log('Attachments checked successfully');
234
+ checkDone();
235
+ },
236
+ });
237
+ console.log('Mock nodemailer created');
238
+
239
+ // Mock the nodemailer module
240
+ delete require.cache[require.resolve('nodemailer')];
241
+ require.cache[require.resolve('nodemailer')] = {
242
+ exports: mockNodemailer,
243
+ };
244
+ console.log('Nodemailer mock installed');
245
+
246
+ const mockRED = createMockNodeRED({
247
+ onHandler: function (event, callback) {
248
+ console.log('onHandler called with event:', event);
249
+ if (event === 'input') {
250
+ this.inputCallback = callback;
251
+ }
252
+ },
253
+ statusHandler: function (status) {
254
+ console.log('statusHandler called with status:', status);
255
+ if (status.fill === 'green') {
256
+ expect(status.text).to.include('sent');
257
+ expect(status.shape).to.equal('dot');
258
+ statusSet = true;
259
+ console.log('Status set successfully');
260
+ checkDone();
261
+ }
262
+ },
263
+ errorHandler: function (err) {
264
+ console.log('errorHandler called with:', err);
265
+ done(err || new Error('Unexpected error handler called'));
266
+ },
267
+ });
268
+
269
+ const emailSenderNode = require('../../email-sender/email-sender.js');
270
+ emailSenderNode(mockRED);
271
+ console.log('Email sender node initialized');
272
+
273
+ const config = { ...emailSenderConfigs.valid };
274
+ config.attachments = JSON.stringify(attachments);
275
+ config.attachmentsType = 'json';
276
+
277
+ const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
278
+ const nodeInstance = new nodeConstructor(config);
279
+ console.log('Node instance created');
280
+
281
+ setTimeout(() => {
282
+ nodeInstance.inputCallback({
283
+ payload: 'test',
284
+ topic: 'test message',
285
+ });
286
+ }, 100);
287
+ });
288
+
289
+ it('should throw error for malformed attachments', function (done) {
290
+ let errorHandlerCalled = false;
291
+ let redStatusSet = false;
292
+
293
+ function checkDone() {
294
+ console.log('Check done - Error:', errorHandlerCalled, 'Status:', redStatusSet);
295
+ if (errorHandlerCalled && redStatusSet) {
296
+ done();
297
+ }
298
+ }
299
+
300
+ // ARRANGE: Configure the node with a JSON string containing a malformed attachment
301
+ const malformedAttachments = [
302
+ {
303
+ filename: 'test.txt',
304
+ content: 'This is a test file.',
305
+ },
306
+ {
307
+ // Malformed attachment with missing content
308
+ filename: 'invalid.txt',
309
+ },
310
+ ];
311
+
312
+ const mockNodemailer = createMockNodemailer();
313
+
314
+ // Mock the nodemailer module
315
+ delete require.cache[require.resolve('nodemailer')];
316
+ require.cache[require.resolve('nodemailer')] = {
317
+ exports: mockNodemailer,
318
+ };
319
+
320
+ const mockRED = createMockNodeRED({
321
+ onHandler: function (event, callback) {
322
+ if (event === 'input') {
323
+ this.inputCallback = callback;
324
+ }
325
+ },
326
+ statusHandler: function (status) {
327
+ if (status.fill === 'red') {
328
+ redStatusSet = true;
329
+ checkDone();
330
+ }
331
+ },
332
+ errorHandler: function (err) {
333
+ console.log('Error received:', err);
334
+ expect(err).to.equal("Attachment object is missing 'filename' or 'content' property.");
335
+ errorHandlerCalled = true;
336
+ checkDone();
337
+ },
338
+ });
339
+
340
+ const emailSenderNode = require('../../email-sender/email-sender.js');
341
+ emailSenderNode(mockRED);
342
+
343
+ const config = { ...emailSenderConfigs.valid };
344
+ config.attachments = JSON.stringify(malformedAttachments);
345
+ config.attachmentsType = 'json';
346
+
347
+ const nodeConstructor = mockRED.nodes.lastRegisteredConstructor;
348
+ const nodeInstance = new nodeConstructor(config);
349
+
350
+ nodeInstance.inputCallback({
351
+ payload: 'test',
352
+ topic: 'test message',
353
+ });
354
+ });
355
+ });
356
+
357
+ it('should handle rejected emails and set status to rejected', function (done) {
358
+ let errorHandlerCalled = false;
359
+ let redStatusSet = false;
360
+
361
+ function checkDone() {
362
+ console.log('Check done - Error:', errorHandlerCalled, 'Status:', redStatusSet);
363
+ if (errorHandlerCalled && redStatusSet) {
364
+ done();
365
+ }
366
+ }
367
+
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);
374
+
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
+ });
621
+ });