mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Adding No Auth support to social email out node (#277)
* Let email out node connect to SMTP without authentication Add the possibility to connect to local SMTP without authentication. * Adapt test to connect to SMTP without credentials Adapt test to connect to SMTP without credentials * Add coverage for social email out node Add coverage for social email out node * Add more coverage to email out node Add more coverage to email out node
This commit is contained in:
		
				
					committed by
					
						 Dave Conway-Jones
						Dave Conway-Jones
					
				
			
			
				
	
			
			
			
						parent
						
							367237d946
						
					
				
				
					commit
					8e001fcd45
				
			| @@ -37,8 +37,6 @@ module.exports = function(RED) { | ||||
|             if (globalkeys) { | ||||
|                 this.userid = globalkeys.user; | ||||
|                 flag = true; | ||||
|             } else { | ||||
|                 this.error(RED._("email.errors.nouserid")); | ||||
|             } | ||||
|         } | ||||
|         if (this.credentials && this.credentials.hasOwnProperty("password")) { | ||||
| @@ -47,8 +45,6 @@ module.exports = function(RED) { | ||||
|             if (globalkeys) { | ||||
|                 this.password = globalkeys.pass; | ||||
|                 flag = true; | ||||
|             } else { | ||||
|                 this.error(RED._("email.errors.nopassword")); | ||||
|             } | ||||
|         } | ||||
|         if (flag) { | ||||
| @@ -59,12 +55,15 @@ module.exports = function(RED) { | ||||
|         var smtpTransport = nodemailer.createTransport({ | ||||
|             host: node.outserver, | ||||
|             port: node.outport, | ||||
|             secure: node.secure, | ||||
|             auth: { | ||||
|             secure: node.secure | ||||
|         }); | ||||
|  | ||||
|         if(this.userid && this.password) { | ||||
|             smtpTransport.auth = { | ||||
|                 user: node.userid, | ||||
|                 pass: node.password | ||||
|             }; | ||||
|         } | ||||
|         }); | ||||
|  | ||||
|         this.on("input", function(msg) { | ||||
|             if (msg.hasOwnProperty("payload")) { | ||||
| @@ -113,7 +112,7 @@ module.exports = function(RED) { | ||||
|                         } | ||||
|                     }); | ||||
|                 } | ||||
|                 else { node.warn(RED._("email.errors.nocredentials")); } | ||||
|                 else { node.warn(RED._("email.errors.nosmtptransport")); } | ||||
|             } | ||||
|             else { node.warn(RED._("email.errors.nopayload")); } | ||||
|         }); | ||||
|   | ||||
| @@ -38,6 +38,7 @@ | ||||
|             "nouserid": "No e-mail userid set", | ||||
|             "nopassword": "No e-mail password set", | ||||
|             "nocredentials": "No Email credentials found. See info panel.", | ||||
|             "nosmtptransport": "No SMTP transport. See info panel.", | ||||
|             "nopayload": "No payload to send",             | ||||
|             "fetchfail": "Failed to fetch folder: __folder__", | ||||
|             "messageerror": "Fetch message error: __error__" | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "node-red-node-email", | ||||
|   "version": "0.1.17", | ||||
|   "version": "0.1.18", | ||||
|   "description": "Node-RED nodes to send and receive simple emails", | ||||
|   "dependencies": { | ||||
|     "nodemailer": "^1.11.0", | ||||
|   | ||||
| @@ -1,4 +1,3 @@ | ||||
|  | ||||
| var should = require("should"); | ||||
| var sinon = require("sinon"); | ||||
| var helper = require('../../../test/helper.js'); | ||||
| @@ -18,7 +17,14 @@ describe('email Node', function() { | ||||
|     describe('email in', function () { | ||||
|  | ||||
|         it('should load with defaults', function (done) { | ||||
|             var flow = [ { id:"n1", type:"e-mail in", name:"emailin", wires:[[]] } ]; | ||||
|             var flow = [{ | ||||
|                 id: "n1", | ||||
|                 type: "e-mail in", | ||||
|                 name: "emailin", | ||||
|                 wires: [ | ||||
|                     [] | ||||
|                 ] | ||||
|             }]; | ||||
|             helper.load(emailNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.should.have.property('name', "emailin"); | ||||
| @@ -28,28 +34,19 @@ describe('email Node', function() { | ||||
|                 done(); | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         //it('should load', function(done) { | ||||
|             //var flow = [ { id:"n1", type:"e-mail in", wires:[["n2"]] }, | ||||
|                     //{id:"n2", type:"helper"} ]; | ||||
|             //helper.load(emailNode, flow, function() { | ||||
|                 //var n1 = helper.getNode("n1"); | ||||
|                 //var n2 = helper.getNode("n2"); | ||||
|                 //n2.on("input", function(msg) { | ||||
|                     //msg.should.have.property('payload',  "hello"); | ||||
|                     //done(); | ||||
|                 //}); | ||||
|                 //var testString = "1,2,3,4"+String.fromCharCode(10); | ||||
|                 //n1.emit("input", {payload:testString}); | ||||
|             //}); | ||||
|         //}); | ||||
|  | ||||
|     }); | ||||
|  | ||||
|     describe('email out', function () { | ||||
|  | ||||
|         it('should load with defaults', function (done) { | ||||
|             var flow = [ { id:"n1", type:"e-mail", name:"emailout", wires:[[]] } ]; | ||||
|             var flow = [{ | ||||
|                 id: "n1", | ||||
|                 type: "e-mail", | ||||
|                 name: "emailout", | ||||
|                 wires: [ | ||||
|                     [] | ||||
|                 ] | ||||
|             }]; | ||||
|             helper.load(emailNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.should.have.property('name', "emailout"); | ||||
| @@ -57,32 +54,134 @@ describe('email Node', function() { | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         it('should fail to send an email (no valid creds)', function(done) { | ||||
|             var smtpTransport = require("nodemailer").createTransport(); | ||||
|             //var spy = sinon.stub(smtpTransport, 'sendMail', function(arg1,arg2,arg3,arg4) { | ||||
|                 //console.log("HELLO"); | ||||
|                 //console.log(arg1,arg2,arg3,arg4); | ||||
|                 //done(); | ||||
|             //}); | ||||
|             var flow = [ { id:"n1", type:"e-mail", name:"emailout", outserver:"smtp.gmail.com", outport:"465", wires:[[]] } ]; | ||||
|         it('should fail with no payload', function (done) { | ||||
|             var flow = [{ | ||||
|                 id: "n1", | ||||
|                 type: "e-mail", | ||||
|                 name: "emailout", | ||||
|                 wires: [ | ||||
|                     [] | ||||
|                 ] | ||||
|             }]; | ||||
|             helper.load(emailNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.should.have.property('name',  "emailout"); | ||||
|                 n1.emit("input", {payload:"Hello World"}); | ||||
|                 n1.credentials = { | ||||
|                     userid: "test", | ||||
|                     password: "test", | ||||
|                 }; | ||||
|                 n1.emit("input", {}); | ||||
|                 //done(); | ||||
|             }); | ||||
|             setTimeout(function () { | ||||
|                 try { | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         //console.log(evt[0].msg); | ||||
|                         return evt[0].type == "e-mail"; | ||||
|                     }); | ||||
|                     //console.log(logEvents); | ||||
|                     //console.log(helper.log().args); | ||||
|                     //console.log(helper.log()); | ||||
|                     //logEvents.should.have.length(3); | ||||
|                     logEvents[0][0].should.have.a.property('msg'); | ||||
|                     logEvents[0][0].msg.toString().should.startWith("email.errors.nouserid"); | ||||
|                     logEvents[0][0].msg.toString().should.startWith("email.errors.nopayload"); | ||||
|                     done(); | ||||
|                 } catch (e) { | ||||
|                     done(e); | ||||
|                 } | ||||
|                 //finally { smtpTransport.sendMail.restore(); } | ||||
|             }, 1000); | ||||
|         }); | ||||
|  | ||||
|         it('should fail to send an email (invalid creds)', function (done) { | ||||
|             //var smtpTransport = require("nodemailer").createTransport(); | ||||
|             //var spy = sinon.stub(smtpTransport, 'sendMail', function(arg1,arg2,arg3,arg4) { | ||||
|             //console.log("HELLO"); | ||||
|             //console.log(arg1,arg2,arg3,arg4); | ||||
|             //done(); | ||||
|             //}); | ||||
|             var flow = [{ | ||||
|                 id: "n1", | ||||
|                 type: "e-mail", | ||||
|                 name: "test@gmail.com", | ||||
|                 server: "smtp.gmail.com", | ||||
|                 secure: true, | ||||
|                 port: "465", | ||||
|                 wires: [ | ||||
|                     [] | ||||
|                 ] | ||||
|             }]; | ||||
|             helper.load(emailNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.credentials = { | ||||
|                     userid: "test", | ||||
|                     password: "test", | ||||
|                 }; | ||||
|                 n1.should.have.property('name', "test@gmail.com"); | ||||
|                 n1.emit("input", { | ||||
|                     payload: "Hello World", | ||||
|                     to: "test@gmail.com" | ||||
|                 }); | ||||
|                 //done(); | ||||
|             }); | ||||
|             setTimeout(function () { | ||||
|                 try { | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         //console.log(evt[0].msg); | ||||
|                         return evt[0].type == "e-mail"; | ||||
|                     }); | ||||
|                     //console.log(helper.log().args); | ||||
|                     //console.log(helper.log()); | ||||
|                     //logEvents.should.have.length(3); | ||||
|                     logEvents[0][0].should.have.a.property('msg'); | ||||
|                     logEvents[0][0].msg.toString().should.startWith("Error:"); | ||||
|                     done(); | ||||
|                 } catch (e) { | ||||
|                     done(e); | ||||
|                 } | ||||
|                 //finally { smtpTransport.sendMail.restore(); } | ||||
|             }, 1000); | ||||
|         }) | ||||
|  | ||||
|         it('should fail to send an email (no creds provided)', function (done) { | ||||
|             //var smtpTransport = require("nodemailer").createTransport(); | ||||
|             //var spy = sinon.stub(smtpTransport, 'sendMail', function(arg1,arg2,arg3,arg4) { | ||||
|             //console.log("HELLO"); | ||||
|             //console.log(arg1,arg2,arg3,arg4); | ||||
|             //done(); | ||||
|             //}); | ||||
|             var flow = [{ | ||||
|                 id: "n1", | ||||
|                 type: "e-mail", | ||||
|                 name: "test@gmail.com", | ||||
|                 server: "smtp.gmail.com", | ||||
|                 secure: true, | ||||
|                 port: "465", | ||||
|                 wires: [ | ||||
|                     [] | ||||
|                 ] | ||||
|             }]; | ||||
|             helper.load(emailNode, flow, function () { | ||||
|                 var n1 = helper.getNode("n1"); | ||||
|                 n1.should.have.property('name', "test@gmail.com"); | ||||
|                 n1.emit("input", { | ||||
|                     payload: "Hello World", | ||||
|                     to: "test@gmail.com" | ||||
|                 }); | ||||
|                 //done(); | ||||
|             }); | ||||
|             setTimeout(function () { | ||||
|                 try { | ||||
|                     var logEvents = helper.log().args.filter(function (evt) { | ||||
|                         //console.log(evt[0].msg); | ||||
|                         return evt[0].type == "e-mail"; | ||||
|                     }); | ||||
|                     //console.log(helper.log().args); | ||||
|                     //logEvents.should.have.length(3); | ||||
|                     logEvents[0][0].should.have.a.property('msg'); | ||||
|                     logEvents[0][0].msg.toString().should.startWith("Error:"); | ||||
|                     done(); | ||||
|                 } catch (e) { | ||||
|                     done(e); | ||||
|                 } | ||||
|                 catch(e) { done(e); } | ||||
|                 //finally { smtpTransport.sendMail.restore(); } | ||||
|             }, 1000); | ||||
|         }) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user