/tests/plugins.js

https://github.com/baudehlo/Haraka · JavaScript · 270 lines · 190 code · 48 blank · 32 comment · 0 complexity · 854650ab697b25eed71f0464d73b664c MD5 · raw file

  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var plugin = require('../plugins');
  5. var piName = 'testPlugin';
  6. /*
  7. From: https://github.com/haraka/Haraka/pull/1278#issuecomment-172134064
  8. * Need to test installed mode + core mode
  9. * Need to test each variation of loading plugins
  10. INSTALLED MODE
  11. * Create tests/installation/ with config/, plugins/, and node_modules
  12. * Plugin in <install_dir>/plugins/<name>.js
  13. * Plugin in <install_dir>/plugins/<name>/ with package.json
  14. * Plugin in <install_dir>/node_modules/<name>/ with package.json
  15. CORE MODE + INSTALLED MODE
  16. * Plugin in <core>/plugins/<name>.js
  17. * Plugin in <core>/plugins/<name>/ with package.json
  18. * Plugin in <core>/node_modules/<name>/ with package.json
  19. * Need to test conflict on name in various forms
  20. * Check plugins/<name>.js loads, not node_modules/<name>/package.json
  21. * Should be enough of a check(?)
  22. * Need to test plugin not existing
  23. * Check <bogus_name_guaranteed_to_not_exist> fails
  24. * Need to test plugin existing and failing to compile
  25. * Create bad plugin in tests/installation/plugins/bad_plugin.js
  26. * Need to test plugin inheritance
  27. * Base plugin in tests/installation/plugins/base_plugin.js
  28. * Real plugin in tests/installation/plugins/inherits.js
  29. * Check base methods work
  30. * Check plugin.base.base_plugin is set/correct
  31. * Plugin timeouts (already tested)
  32. */
  33. exports.plugin = {
  34. 'new Plugin() object': function (test) {
  35. var pi = new plugin.Plugin(piName);
  36. test.expect(1);
  37. test.ok(pi);
  38. test.done();
  39. }
  40. };
  41. var toPath = './config/' + piName + '.timeout';
  42. var toVals = [ '0', '3', '60', 'apple'];
  43. var getVal = function () {
  44. return toVals.shift();
  45. };
  46. exports.get_timeout = {
  47. setUp : function (done) {
  48. process.env.WITHOUT_CONFIG_CACHE=true;
  49. this.to = getVal();
  50. fs.writeFile(toPath, this.to, done);
  51. },
  52. tearDown : function (done) {
  53. fs.unlink(toPath, done);
  54. },
  55. '0s' : function (test) {
  56. var pi = new plugin.Plugin(piName);
  57. test.expect(1);
  58. test.equal( pi.timeout, this.to );
  59. test.done();
  60. },
  61. '3s' : function (test) {
  62. var pi = new plugin.Plugin(piName);
  63. test.expect(1);
  64. test.equal( pi.timeout, this.to );
  65. test.done();
  66. },
  67. '60s' : function (test) {
  68. var pi = new plugin.Plugin(piName);
  69. test.expect(1);
  70. test.equal( pi.timeout, this.to );
  71. test.done();
  72. },
  73. '30s default (overrides NaN apple)' : function (test) {
  74. var pi = new plugin.Plugin(piName);
  75. test.expect(1);
  76. test.equal( pi.timeout, 30 );
  77. test.done();
  78. },
  79. };
  80. exports.plugin_paths = {
  81. /* jshint maxlen: 90 */
  82. 'CORE plugin: (tls)' : function (test) {
  83. delete process.env.HARAKA;
  84. var p = new plugin.Plugin('tls');
  85. test.expect(1);
  86. test.equal(p.plugin_path, path.resolve(__dirname, '..', 'plugins', 'tls.js'));
  87. test.done();
  88. },
  89. 'INSTALLED override: (tls)': function (test) {
  90. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  91. var p = new plugin.Plugin('tls');
  92. test.expect(1);
  93. test.equal(p.plugin_path, path.resolve(__dirname, 'installation', 'plugins', 'tls.js'));
  94. test.done();
  95. },
  96. 'CORE package plugin: (watch)': function (test) {
  97. delete process.env.HARAKA;
  98. var p = new plugin.Plugin('watch');
  99. test.expect(3);
  100. test.equal(p.plugin_path, path.resolve(__dirname, '..', 'plugins', 'watch', 'package.json'));
  101. test.ok(p.hasPackageJson);
  102. try {
  103. p._compile();
  104. test.ok(true, "compiles OK");
  105. }
  106. catch (e) {
  107. console.error(e.stack);
  108. test.ok(false, "compiles OK");
  109. }
  110. test.done();
  111. },
  112. 'INSTALLED node_modules package plugin: (test-plugin)': function (test) {
  113. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  114. var p = new plugin.Plugin('test-plugin');
  115. test.expect(3);
  116. test.equal(p.plugin_path, path.resolve(__dirname, 'installation', 'node_modules', 'test-plugin', 'package.json'));
  117. test.ok(p.hasPackageJson);
  118. try {
  119. p._compile();
  120. test.ok(true, "compiles OK");
  121. }
  122. catch (e) {
  123. console.error(e.stack);
  124. test.ok(false, "compiles OK");
  125. }
  126. test.done();
  127. },
  128. 'CORE package plugin: (faked using address-rfc2822)': function (test) {
  129. var p = new plugin.Plugin('address-rfc2822');
  130. test.expect(2);
  131. test.equal(p.plugin_path, path.resolve(__dirname, '..', 'node_modules', 'address-rfc2822', 'package.json'));
  132. test.ok(p.hasPackageJson);
  133. test.done();
  134. },
  135. 'plugins overrides node_modules': function (test) {
  136. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  137. var p = new plugin.Plugin('load_first');
  138. test.expect(3);
  139. test.equal(p.plugin_path, path.resolve(__dirname, 'installation', 'plugins', 'load_first.js'));
  140. try {
  141. p._compile();
  142. test.ok(true, "compiles OK");
  143. }
  144. catch (e) {
  145. console.error(e.stack);
  146. test.ok(false, "compiles OK");
  147. }
  148. test.ok(p.loaded_first);
  149. test.done();
  150. },
  151. 'INSTALLED plugins folder plugin: (folder_plugin)': function (test) {
  152. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  153. var p = new plugin.Plugin('folder_plugin');
  154. test.expect(3);
  155. test.equal(p.plugin_path, path.resolve(__dirname, 'installation', 'plugins', 'folder_plugin', 'package.json'));
  156. test.ok(p.hasPackageJson);
  157. try {
  158. p._compile();
  159. test.ok(true, "compiles OK");
  160. }
  161. catch (e) {
  162. console.error(e.stack);
  163. test.ok(false, "compiles OK");
  164. }
  165. test.done();
  166. },
  167. 'Inheritance: (inherits)': function (test) {
  168. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  169. var p = new plugin.Plugin('inherits');
  170. test.expect(3);
  171. test.equal(p.plugin_path, path.resolve(__dirname, 'installation', 'plugins', 'inherits.js'));
  172. try {
  173. p._compile();
  174. test.ok(true, "compiles OK");
  175. }
  176. catch (e) {
  177. console.error(e.stack);
  178. test.ok(false, "compiles OK");
  179. }
  180. p.register();
  181. test.ok(p.base.base_plugin);
  182. test.done();
  183. },
  184. };
  185. exports.plugin_config = {
  186. /* jshint maxlen: 90 */
  187. 'CORE plugin: (tls)' : function (test) {
  188. delete process.env.HARAKA;
  189. var p = new plugin.Plugin('tls');
  190. test.expect(2);
  191. test.equal(p.config.root_path, path.resolve(__dirname, '..', 'config'));
  192. test.equal(p.config.overrides_path, undefined);
  193. test.done();
  194. },
  195. 'INSTALLED override: (tls)': function (test) {
  196. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  197. var p = new plugin.Plugin('tls');
  198. test.expect(2);
  199. test.equal(p.config.root_path, path.resolve(__dirname, '..', 'config'));
  200. test.equal(p.config.overrides_path, path.resolve(__dirname, 'installation', 'config'));
  201. test.done();
  202. },
  203. 'CORE package plugin: (watch)': function (test) {
  204. delete process.env.HARAKA;
  205. var p = new plugin.Plugin('watch');
  206. test.expect(2);
  207. test.equal(p.config.root_path, path.resolve(__dirname, '..', 'plugins', 'watch', 'config'));
  208. test.equal(p.config.overrides_path, path.resolve(__dirname, '..', 'config'));
  209. test.done();
  210. },
  211. 'INSTALLED node_modules package plugin: (test-plugin)': function (test) {
  212. process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');
  213. var p = new plugin.Plugin('test-plugin');
  214. test.expect(2);
  215. test.equal(p.config.root_path, path.resolve(__dirname, 'installation', 'node_modules', 'test-plugin', 'config'));
  216. test.equal(p.config.overrides_path, path.resolve(__dirname, 'installation', 'config'));
  217. test.done();
  218. },
  219. }