utilities.spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. 'use strict';
  2. const assert = require('assert');
  3. const path = require('path');
  4. const fs = require('fs');
  5. const md5 = require('md5');
  6. const server = require('./server');
  7. const fileDir = server.fileDir;
  8. const uploadDir = server.uploadDir;
  9. const {
  10. debugLog,
  11. isFunc,
  12. errorFunc,
  13. getTempFilename,
  14. buildOptions,
  15. buildFields,
  16. checkAndMakeDir,
  17. deleteFile,
  18. copyFile,
  19. saveBufferToFile,
  20. parseFileName
  21. } = require('../lib/utilities');
  22. const mockFile = 'basketball.png';
  23. const mockBuffer = fs.readFileSync(path.join(fileDir, mockFile));
  24. const mockHash = md5(mockBuffer);
  25. describe('Test of the utilities functions', function() {
  26. beforeEach(function() {
  27. server.clearUploadsDir();
  28. });
  29. //debugLog tests
  30. describe('Test debugLog function', () => {
  31. let testMessage = 'Test message';
  32. it('debugLog returns false if no options passed', () => {
  33. assert.equal(debugLog(null, testMessage), false);
  34. });
  35. it('debugLog returns false if option debug is false', () => {
  36. assert.equal(debugLog({debug: false}, testMessage), false);
  37. });
  38. it('debugLog returns true if option debug is true', () => {
  39. assert.equal(debugLog({debug: true}, testMessage), true);
  40. });
  41. });
  42. //isFunc tests
  43. describe('Test isFunc function', () => {
  44. it('isFunc returns true if function passed', () => assert.equal(isFunc(()=>{}), true));
  45. it('isFunc returns false if null passed', function() {
  46. assert.equal(isFunc(null), false);
  47. });
  48. it('isFunc returns false if undefined passed', function() {
  49. assert.equal(isFunc(undefined), false);
  50. });
  51. it('isFunc returns false if object passed', function() {
  52. assert.equal(isFunc({}), false);
  53. });
  54. it('isFunc returns false if array passed', function() {
  55. assert.equal(isFunc([]), false);
  56. });
  57. });
  58. //errorFunc tests
  59. describe('Test errorFunc function', () => {
  60. const resolve = () => 'success';
  61. const reject = () => 'error';
  62. it('errorFunc returns resolve if reject function has not been passed', () => {
  63. let result = errorFunc(resolve);
  64. assert.equal(result(), 'success');
  65. });
  66. it('errorFunc returns reject if reject function has been passed', () => {
  67. let result = errorFunc(resolve, reject);
  68. assert.equal(result(), 'error');
  69. });
  70. });
  71. //getTempFilename tests
  72. describe('Test getTempFilename function', () => {
  73. const nameRegexp = /tmp-\d{1,5}-\d{1,}/;
  74. it('getTempFilename result matches regexp /tmp-d{1,5}-d{1,}/', () => {
  75. let errCounter = 0;
  76. let tempName = '';
  77. for (var i = 0; i < 65537; i++) {
  78. tempName = getTempFilename();
  79. if (!nameRegexp.test(tempName)) errCounter ++;
  80. }
  81. assert.equal(errCounter, 0);
  82. });
  83. it('getTempFilename current and previous results are not equal', () => {
  84. let errCounter = 0;
  85. let tempName = '';
  86. let previousName = '';
  87. for (var i = 0; i < 65537; i++) {
  88. previousName = tempName;
  89. tempName = getTempFilename();
  90. if (previousName === tempName) errCounter ++;
  91. }
  92. assert.equal(errCounter, 0);
  93. });
  94. });
  95. //parseFileName
  96. describe('Test parseFileName function', () => {
  97. it('Does nothing to your filename when disabled.', () => {
  98. const opts = {safeFileNames: false};
  99. const name = 'my$Invalid#fileName.png123';
  100. const expected = 'my$Invalid#fileName.png123';
  101. let result = parseFileName(opts, name);
  102. assert.equal(result, expected);
  103. });
  104. it(
  105. 'Strips away all non-alphanumeric characters (excluding hyphens/underscores) when enabled.',
  106. () => {
  107. const opts = {safeFileNames: true};
  108. const name = 'my$Invalid#fileName.png123';
  109. const expected = 'myInvalidfileNamepng123';
  110. let result = parseFileName(opts, name);
  111. assert.equal(result, expected);
  112. });
  113. it(
  114. 'Strips away all non-alphanumeric chars when preserveExtension: true for a name without dots',
  115. () => {
  116. const opts = {safeFileNames: true, preserveExtension: true};
  117. const name = 'my$Invalid#fileName';
  118. const expected = 'myInvalidfileName';
  119. let result = parseFileName(opts, name);
  120. assert.equal(result, expected);
  121. });
  122. it('Accepts a regex for stripping (decidedly) "invalid" characters from filename.', () => {
  123. const opts = {safeFileNames: /[$#]/g};
  124. const name = 'my$Invalid#fileName.png123';
  125. const expected = 'myInvalidfileName.png123';
  126. let result = parseFileName(opts, name);
  127. assert.equal(result, expected);
  128. });
  129. it(
  130. 'Returns correct filename if name contains dots characters and preserveExtension: true.',
  131. () => {
  132. const opts = {safeFileNames: true, preserveExtension: true};
  133. const name = 'basket.ball.png';
  134. const expected = 'basketball.png';
  135. let result = parseFileName(opts, name);
  136. assert.equal(result, expected);
  137. });
  138. });
  139. //buildOptions tests
  140. describe('Test buildOptions function', () => {
  141. const source = { option1: '1', option2: '2' };
  142. const sourceAddon = { option3: '3'};
  143. const expected = { option1: '1', option2: '2' };
  144. const expectedAddon = { option1: '1', option2: '2', option3: '3'};
  145. it('buildOptions returns and equal object to the object which was paased', () => {
  146. let result = buildOptions(source);
  147. assert.deepStrictEqual(result, source);
  148. });
  149. it('buildOptions doesnt add non object or null arguments to the result', () => {
  150. let result = buildOptions(source, 2, '3', null);
  151. assert.deepStrictEqual(result, expected);
  152. });
  153. it('buildOptions adds value to the result from the several source argumets', () => {
  154. let result = buildOptions(source, sourceAddon);
  155. assert.deepStrictEqual(result, expectedAddon);
  156. });
  157. });
  158. //buildFields tests
  159. describe('Test buildOptions function', () => {
  160. it('buildFields does nothing if null value has been passed', () => {
  161. let fields = null;
  162. fields = buildFields(fields, 'test', null);
  163. assert.equal(fields, null);
  164. });
  165. });
  166. //checkAndMakeDir tests
  167. describe('Test checkAndMakeDir function', () => {
  168. //
  169. it('checkAndMakeDir returns false if upload options object was not set', () => {
  170. assert.equal(checkAndMakeDir(), false);
  171. });
  172. //
  173. it('checkAndMakeDir returns false if upload option createParentPath was not set', () => {
  174. assert.equal(checkAndMakeDir({}), false);
  175. });
  176. //
  177. it('checkAndMakeDir returns false if filePath was not set', () => {
  178. assert.equal(checkAndMakeDir({createParentPath: true}), false);
  179. });
  180. //
  181. it('checkAndMakeDir return true if path to the file already exists', ()=>{
  182. let dir = path.join(uploadDir, 'testfile');
  183. assert.equal(checkAndMakeDir({createParentPath: true}, dir), true);
  184. });
  185. //
  186. it('checkAndMakeDir creates a dir if path to the file not exists', ()=>{
  187. let dir = path.join(uploadDir, 'testfolder', 'testfile');
  188. assert.equal(checkAndMakeDir({createParentPath: true}, dir), true);
  189. });
  190. });
  191. //saveBufferToFile tests
  192. describe('Test saveBufferToFile function', function(){
  193. beforeEach(function() {
  194. server.clearUploadsDir();
  195. });
  196. it('Save buffer to a file', function(done) {
  197. let filePath = path.join(uploadDir, mockFile);
  198. saveBufferToFile(mockBuffer, filePath, function(err){
  199. if (err) {
  200. return done(err);
  201. }
  202. fs.stat(filePath, done);
  203. });
  204. });
  205. it('Failed if not a buffer passed', function(done) {
  206. let filePath = path.join(uploadDir, mockFile);
  207. saveBufferToFile(undefined, filePath, function(err){
  208. if (err) {
  209. return done();
  210. }
  211. });
  212. });
  213. it('Failed if wrong path passed', function(done) {
  214. let filePath = '';
  215. saveBufferToFile(mockFile, filePath, function(err){
  216. if (err) {
  217. return done();
  218. }
  219. });
  220. });
  221. });
  222. describe('Test deleteFile function', function(){
  223. beforeEach(function() {
  224. server.clearUploadsDir();
  225. });
  226. it('Failed if nonexistent file passed', function(done){
  227. let filePath = path.join(uploadDir, getTempFilename());
  228. deleteFile(filePath, function(err){
  229. if (err) {
  230. return done();
  231. }
  232. });
  233. });
  234. it('Delete a file', function(done){
  235. let srcPath = path.join(fileDir, mockFile);
  236. let dstPath = path.join(uploadDir, getTempFilename());
  237. //copy a file
  238. copyFile(srcPath, dstPath, function(err){
  239. if (err) {
  240. return done(err);
  241. }
  242. fs.stat(dstPath, (err)=>{
  243. if (err){
  244. return done(err);
  245. }
  246. // delete a file
  247. deleteFile(dstPath, function(err){
  248. if (err) {
  249. return done(err);
  250. }
  251. fs.stat(dstPath, (err)=>{
  252. if (err){
  253. return done();
  254. }
  255. //error if a file still exist
  256. done(err);
  257. });
  258. });
  259. });
  260. });
  261. });
  262. });
  263. describe('Test copyFile function', function(){
  264. beforeEach(function() {
  265. server.clearUploadsDir();
  266. });
  267. it('Copy a file and check a hash', function(done) {
  268. let srcPath = path.join(fileDir, mockFile);
  269. let dstPath = path.join(uploadDir, mockFile);
  270. copyFile(srcPath, dstPath, function(err){
  271. if (err) {
  272. return done(err);
  273. }
  274. fs.stat(dstPath, (err)=>{
  275. if (err){
  276. return done(err);
  277. }
  278. //Match source and destination files hash.
  279. let fileBuffer = fs.readFileSync(dstPath);
  280. let fileHash = md5(fileBuffer);
  281. return (fileHash === mockHash) ? done() : done(err);
  282. });
  283. });
  284. });
  285. it('Failed if wrong source file path passed', function(done){
  286. let srcPath = path.join(fileDir, 'unknown');
  287. let dstPath = path.join(uploadDir, mockFile);
  288. copyFile(srcPath, dstPath, function(err){
  289. if (err) {
  290. return done();
  291. }
  292. });
  293. });
  294. it('Failed if wrong destination file path passed', function(done){
  295. let srcPath = path.join(fileDir, 'unknown');
  296. let dstPath = path.join('unknown', 'unknown');
  297. copyFile(srcPath, dstPath, function(err){
  298. if (err) {
  299. return done();
  300. }
  301. });
  302. });
  303. });
  304. });