AttachmentViewer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var common = require('../common');
  3. describe('AttachmentViewer', function () {
  4. describe('setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo', function () {
  5. this.timeout(30000);
  6. jsc.property(
  7. 'displays & hides data as requested',
  8. common.jscMimeTypes(),
  9. jsc.nearray(common.jscBase64String()),
  10. 'string',
  11. 'string',
  12. 'string',
  13. function (mimeType, base64, filename, prefix, postfix) {
  14. var clean = jsdom(),
  15. data = 'data:' + mimeType + ';base64,' + base64.join(''),
  16. previewSupported = (
  17. mimeType.substring(0, 6) === 'image/' ||
  18. mimeType.substring(0, 6) === 'audio/' ||
  19. mimeType.substring(0, 6) === 'video/' ||
  20. mimeType.match(/\/pdf/i)
  21. ),
  22. results = [],
  23. result = '';
  24. prefix = prefix.replace(/%(s|d)/g, '%%');
  25. postfix = postfix.replace(/%(s|d)/g, '%%');
  26. $('body').html(
  27. '<div id="attachment" role="alert" class="hidden alert ' +
  28. 'alert-info"><span class="glyphicon glyphicon-download-' +
  29. 'alt" aria-hidden="true"></span> <a class="alert-link">' +
  30. 'Download attachment</a></div><div id="attachmentPrevie' +
  31. 'w" class="hidden"></div>'
  32. );
  33. $.PrivateBin.AttachmentViewer.init();
  34. results.push(
  35. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  36. $('#attachment').hasClass('hidden') &&
  37. $('#attachmentPreview').hasClass('hidden')
  38. );
  39. if (filename.length) {
  40. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  41. } else {
  42. $.PrivateBin.AttachmentViewer.setAttachment(data);
  43. }
  44. var attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  45. results.push(
  46. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  47. $('#attachment').hasClass('hidden') &&
  48. $('#attachmentPreview').hasClass('hidden') &&
  49. attachment[0] === data &&
  50. attachment[1] === filename
  51. );
  52. $.PrivateBin.AttachmentViewer.showAttachment();
  53. results.push(
  54. !$('#attachment').hasClass('hidden') &&
  55. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  56. );
  57. $.PrivateBin.AttachmentViewer.hideAttachment();
  58. results.push(
  59. $('#attachment').hasClass('hidden') &&
  60. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  61. );
  62. if (previewSupported) {
  63. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  64. results.push($('#attachmentPreview').hasClass('hidden'));
  65. }
  66. $.PrivateBin.AttachmentViewer.showAttachment();
  67. results.push(
  68. !$('#attachment').hasClass('hidden') &&
  69. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  70. );
  71. let element = $('<div>');
  72. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  73. // messageIDs with links get a relaxed treatment
  74. if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
  75. result = $.PrivateBin.Helper.htmlEntities(prefix + filename + postfix);
  76. } else {
  77. result = $('<div>').html(prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix).html();
  78. }
  79. if (filename.length) {
  80. results.push(
  81. element.children()[0].href === data &&
  82. element.children()[0].getAttribute('download') === filename &&
  83. element.children()[0].text === result
  84. );
  85. } else {
  86. results.push(element.children()[0].href === data);
  87. }
  88. $.PrivateBin.AttachmentViewer.removeAttachment();
  89. results.push(
  90. $('#attachment').hasClass('hidden') &&
  91. $('#attachmentPreview').hasClass('hidden')
  92. );
  93. clean();
  94. return results.every(element => element);
  95. }
  96. );
  97. });
  98. });