AttachmentViewer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'string',
  10. 'string',
  11. 'string',
  12. 'string',
  13. function (mimeType, rawdata, filename, prefix, postfix) {
  14. let clean = jsdom(),
  15. data = 'data:' + mimeType + ';base64,' + btoa(rawdata),
  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. const 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 = $('<textarea>').text((prefix + filename + postfix)).text();
  76. } else {
  77. result = DOMPurify.sanitize(
  78. prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix, {
  79. ALLOWED_TAGS: ['a', 'i', 'span'],
  80. ALLOWED_ATTR: ['href', 'id']
  81. }
  82. );
  83. }
  84. if (filename.length) {
  85. results.push(
  86. element.children()[0].href === data &&
  87. element.children()[0].getAttribute('download') === filename &&
  88. element.children()[0].text === result
  89. );
  90. } else {
  91. results.push(element.children()[0].href === data);
  92. }
  93. $.PrivateBin.AttachmentViewer.removeAttachment();
  94. results.push(
  95. $('#attachment').hasClass('hidden') &&
  96. $('#attachmentPreview').hasClass('hidden')
  97. );
  98. clean();
  99. return results.every(element => element);
  100. }
  101. );
  102. });
  103. });