PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/regression-suite/node_modules/nodemailer/node_modules/simplesmtp/test/client.js

https://gitlab.com/oytunistrator/x3dom
JavaScript | 495 lines | 372 code | 102 blank | 21 comment | 9 complexity | 0585f0d12c15e7d76537dbd50f97877c MD5 | raw file
  1. "use strict";
  2. var simplesmtp = require("../index"),
  3. packageData = require("../package.json"),
  4. fs = require("fs");
  5. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  6. var PORT_NUMBER = 8397;
  7. exports["Version test"] = {
  8. "Should expose version number": function(test){
  9. test.ok(simplesmtp.version);
  10. test.equal(simplesmtp.version, packageData.version);
  11. test.done();
  12. }
  13. };
  14. exports["General tests"] = {
  15. setUp: function (callback) {
  16. this.server = new simplesmtp.createServer();
  17. this.server.listen(PORT_NUMBER, function(err){
  18. if(err){
  19. throw err;
  20. }else{
  21. callback();
  22. }
  23. });
  24. },
  25. tearDown: function (callback) {
  26. this.server.end(callback);
  27. },
  28. "Connect and setup": function(test){
  29. var client = simplesmtp.connect(PORT_NUMBER);
  30. client.once("idle", function(){
  31. // Client is ready to take messages
  32. test.ok(true);
  33. client.close();
  34. });
  35. client.on("error", function(){
  36. test.ok(false);
  37. });
  38. client.on("end", function(){
  39. test.done();
  40. });
  41. },
  42. "socketTimeout": function(test){
  43. var client = simplesmtp.connect(PORT_NUMBER, false, {socketTimeout: 500});
  44. var waitTimeout = setTimeout(function(){
  45. test.ok(false);
  46. test.done();
  47. }, 2000);
  48. client.once("idle", function(){
  49. // Client is ready to take messages
  50. test.ok(true);
  51. });
  52. client.on("error", function(err){
  53. test.ifError(err);
  54. });
  55. client.on("end", function(){
  56. clearTimeout(waitTimeout);
  57. test.done();
  58. });
  59. }
  60. };
  61. exports["Secure server"] = {
  62. setUp: function (callback) {
  63. this.server = new simplesmtp.createServer({
  64. secureConnection: true
  65. });
  66. this.server.listen(PORT_NUMBER, function(err){
  67. if(err){
  68. throw err;
  69. }else{
  70. callback();
  71. }
  72. });
  73. },
  74. tearDown: function (callback) {
  75. this.server.end(callback);
  76. },
  77. "Connect and setup": function(test){
  78. var client = simplesmtp.connect(PORT_NUMBER, false, {
  79. secureConnection: true
  80. });
  81. client.once("idle", function(){
  82. // Client is ready to take messages
  83. test.ok(true);
  84. client.close();
  85. });
  86. client.on("error", function(){
  87. test.ok(false);
  88. });
  89. client.on("end", function(){
  90. test.done();
  91. });
  92. },
  93. "Unsecure client should have timeout": function(test){
  94. var client = simplesmtp.connect(PORT_NUMBER, false, {
  95. secureConnection: false
  96. });
  97. client.once("idle", function(){
  98. test.ok(false);
  99. });
  100. client.on("error", function(err){
  101. test.equal(err.code, "ETIMEDOUT");
  102. client.close();
  103. });
  104. client.on("end", function(){
  105. test.done();
  106. });
  107. }
  108. };
  109. exports["Disabled EHLO"] = {
  110. setUp: function (callback) {
  111. this.server = new simplesmtp.createServer({disableEHLO: true});
  112. this.server.listen(PORT_NUMBER, function(err){
  113. if(err){
  114. throw err;
  115. }else{
  116. callback();
  117. }
  118. });
  119. },
  120. tearDown: function (callback) {
  121. this.server.end(callback);
  122. },
  123. "Connect and setup": function(test){
  124. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  125. client.once("idle", function(){
  126. // Client is ready to take messages
  127. test.ok(true);
  128. client.close();
  129. });
  130. client.on("error", function(){
  131. test.ok(false);
  132. });
  133. client.on("end", function(){
  134. test.done();
  135. });
  136. }
  137. };
  138. exports["Authentication needed"] = {
  139. setUp: function (callback) {
  140. this.server = new simplesmtp.createServer({
  141. requireAuthentication: true
  142. });
  143. this.server.on("authorizeUser", function(envelope, user, pass, callback){
  144. callback(null, user=="test1" && pass == "test2");
  145. });
  146. this.server.listen(PORT_NUMBER, function(err){
  147. if(err){
  148. throw err;
  149. }else{
  150. callback();
  151. }
  152. });
  153. },
  154. tearDown: function (callback) {
  155. this.server.end(callback);
  156. },
  157. "Auth success": function(test){
  158. var client = simplesmtp.connect(PORT_NUMBER, false, {
  159. auth: {
  160. user: "test1",
  161. pass: "test2"
  162. }
  163. });
  164. client.once("idle", function(){
  165. // Client is ready to take messages
  166. test.ok(true);
  167. client.close();
  168. });
  169. client.on("error", function(err){
  170. test.ok(false);
  171. });
  172. client.on("end", function(){
  173. test.done();
  174. });
  175. },
  176. "Auth fails": function(test){
  177. var client = simplesmtp.connect(PORT_NUMBER, false, {
  178. auth: {
  179. user: "test3",
  180. pass: "test4"
  181. }
  182. });
  183. client.once("idle", function(){
  184. // Client is ready to take messages
  185. test.ok(false); // should not occur
  186. client.close();
  187. });
  188. client.on("error", function(err){
  189. test.ok(true); // login failed
  190. });
  191. client.on("end", function(){
  192. test.done();
  193. });
  194. }
  195. };
  196. exports["Message tests"] = {
  197. setUp: function (callback) {
  198. this.server = new simplesmtp.createServer({
  199. validateSender: true,
  200. validateRecipients: true
  201. });
  202. this.server.on("validateSender", function(envelope, email, callback){
  203. callback(email != "test@pangalink.net"?new Error("Failed sender") : null);
  204. });
  205. this.server.on("validateRecipient", function(envelope, email, callback){
  206. callback(email.split("@").pop() != "pangalink.net"?new Error("Failed recipient") : null);
  207. });
  208. this.server.on("dataReady", function(envelope, callback){
  209. callback(null, "ABC1"); // ABC1 is the queue id to be advertised to the client
  210. // callback(new Error("That was clearly a spam!"));
  211. });
  212. this.server.listen(PORT_NUMBER, function(err){
  213. if(err){
  214. throw err;
  215. }else{
  216. callback();
  217. }
  218. });
  219. },
  220. tearDown: function (callback) {
  221. this.server.end(callback);
  222. },
  223. "Set envelope success": function(test){
  224. test.expect(2);
  225. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  226. client.once("idle", function(){
  227. // Client is ready to take messages
  228. test.ok(true); // waiting for envelope
  229. client.useEnvelope({
  230. from: "test@pangalink.net",
  231. to: [
  232. "test1@pangalink.net",
  233. "test2@pangalink.net"
  234. ]
  235. });
  236. });
  237. client.on("message", function(){
  238. // Client is ready to take messages
  239. test.ok(true); // waiting for message
  240. client.close();
  241. });
  242. client.on("error", function(err){
  243. test.ok(false);
  244. });
  245. client.on("end", function(){
  246. test.done();
  247. });
  248. },
  249. "Set envelope fails for sender": function(test){
  250. test.expect(2);
  251. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  252. client.once("idle", function(){
  253. // Client is ready to take messages
  254. test.ok(true); // waiting for envelope
  255. client.useEnvelope({
  256. from: "test3@pangalink.net",
  257. to: [
  258. "test1@pangalink.net",
  259. "test2@pangalink.net"
  260. ]
  261. });
  262. });
  263. client.on("message", function(){
  264. // Client is ready to take messages
  265. test.ok(false); // waiting for message
  266. client.close();
  267. });
  268. client.on("error", function(err){
  269. test.ok(true);
  270. });
  271. client.on("end", function(){
  272. test.done();
  273. });
  274. },
  275. "Set envelope fails for receiver": function(test){
  276. test.expect(2);
  277. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  278. client.once("idle", function(){
  279. // Client is ready to take messages
  280. test.ok(true); // waiting for envelope
  281. client.useEnvelope({
  282. from: "test@pangalink.net",
  283. to: [
  284. "test1@kreata.ee",
  285. "test2@kreata.ee"
  286. ]
  287. });
  288. });
  289. client.on("message", function(){
  290. // Client is ready to take messages
  291. test.ok(false); // waiting for message
  292. client.close();
  293. });
  294. client.on("error", function(err){
  295. test.ok(true);
  296. });
  297. client.on("end", function(){
  298. test.done();
  299. });
  300. },
  301. "Set envelope partly fails": function(test){
  302. test.expect(3);
  303. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  304. client.once("idle", function(){
  305. // Client is ready to take messages
  306. test.ok(true); // waiting for envelope
  307. client.useEnvelope({
  308. from: "test@pangalink.net",
  309. to: [
  310. "test1@pangalink.net",
  311. "test2@kreata.ee"
  312. ]
  313. });
  314. });
  315. client.on("rcptFailed", function(){
  316. // Client is ready to take messages
  317. test.ok(true); // waiting for message
  318. });
  319. client.on("message", function(){
  320. // Client is ready to take messages
  321. test.ok(true); // waiting for message
  322. client.close();
  323. });
  324. client.on("error", function(err){
  325. test.ok(false);
  326. });
  327. client.on("end", function(){
  328. test.done();
  329. });
  330. },
  331. "Send message success": function(test){
  332. test.expect(3);
  333. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  334. client.once("idle", function(){
  335. // Client is ready to take messages
  336. test.ok(true); // waiting for envelope
  337. client.useEnvelope({
  338. from: "test@pangalink.net",
  339. to: [
  340. "test1@pangalink.net",
  341. "test2@pangalink.net"
  342. ]
  343. });
  344. });
  345. client.on("message", function(){
  346. // Client is ready to take messages
  347. test.ok(true); // waiting for message
  348. client.write("From: abc@pangalink.net\r\nTo:cde@pangalink.net\r\nSubject: test\r\n\r\nHello World!");
  349. client.end();
  350. });
  351. client.on("ready", function(success){
  352. test.ok(success);
  353. client.close();
  354. });
  355. client.on("error", function(err){
  356. test.ok(false);
  357. });
  358. client.on("end", function(){
  359. test.done();
  360. });
  361. },
  362. "Stream message": function(test){
  363. test.expect(3);
  364. var client = simplesmtp.connect(PORT_NUMBER, false, {});
  365. client.once("idle", function(){
  366. // Client is ready to take messages
  367. test.ok(true); // waiting for envelope
  368. client.useEnvelope({
  369. from: "test@pangalink.net",
  370. to: [
  371. "test1@pangalink.net",
  372. "test2@pangalink.net"
  373. ]
  374. });
  375. });
  376. client.on("message", function(){
  377. // Client is ready to take messages
  378. test.ok(true); // waiting for message
  379. // pipe file to client
  380. fs.createReadStream(__dirname+"/testmessage.eml").pipe(client);
  381. });
  382. client.on("ready", function(success){
  383. test.ok(success);
  384. client.close();
  385. });
  386. client.on("error", function(err){
  387. test.ok(false);
  388. });
  389. client.on("end", function(){
  390. test.done();
  391. });
  392. }
  393. };