AttachmentViewer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. const 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,' + common.btoa(rawdata),
  16. mimePrefix = mimeType.substring(0, 6),
  17. previewSupported = (
  18. mimePrefix === 'image/' ||
  19. mimePrefix === 'audio/' ||
  20. mimePrefix === 'video/' ||
  21. mimeType.match(/\/pdf/i)
  22. ),
  23. results = [],
  24. result = '';
  25. prefix = prefix.replace(/%(s|d)/g, '%%');
  26. postfix = postfix.replace(/%(s|d)/g, '%%');
  27. $('body').html(
  28. '<div id="attachment" role="alert" class="hidden alert ' +
  29. 'alert-info"><span class="glyphicon glyphicon-download-' +
  30. 'alt" aria-hidden="true"></span> <a class="alert-link">' +
  31. 'Download attachment</a></div><div id="attachmentPrevie' +
  32. 'w" class="hidden"></div>'
  33. );
  34. // mock createObjectURL for jsDOM
  35. if (typeof window.URL.createObjectURL === 'undefined') {
  36. Object.defineProperty(
  37. window.URL,
  38. 'createObjectURL',
  39. {value: function(blob) {
  40. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  41. }}
  42. )
  43. }
  44. $.PrivateBin.AttachmentViewer.init();
  45. results.push(
  46. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  47. $('#attachment').hasClass('hidden') &&
  48. $('#attachmentPreview').hasClass('hidden')
  49. );
  50. global.atob = common.atob;
  51. if (filename.length) {
  52. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  53. } else {
  54. $.PrivateBin.AttachmentViewer.setAttachment(data);
  55. }
  56. // beyond this point we will get the blob URL instead of the data
  57. data = window.URL.createObjectURL(data);
  58. const attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  59. results.push(
  60. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  61. $('#attachment').hasClass('hidden') &&
  62. $('#attachmentPreview').hasClass('hidden') &&
  63. attachment[0] === data &&
  64. attachment[1] === filename
  65. );
  66. $.PrivateBin.AttachmentViewer.showAttachment();
  67. results.push(
  68. !$('#attachment').hasClass('hidden') &&
  69. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  70. );
  71. $.PrivateBin.AttachmentViewer.hideAttachment();
  72. results.push(
  73. $('#attachment').hasClass('hidden') &&
  74. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  75. );
  76. if (previewSupported) {
  77. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  78. results.push($('#attachmentPreview').hasClass('hidden'));
  79. }
  80. $.PrivateBin.AttachmentViewer.showAttachment();
  81. results.push(
  82. !$('#attachment').hasClass('hidden') &&
  83. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  84. );
  85. let element = $('<div>');
  86. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  87. // messageIDs with links get a relaxed treatment
  88. if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
  89. result = $('<textarea>').text((prefix + filename + postfix)).text();
  90. } else {
  91. result = DOMPurify.sanitize(
  92. prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix, {
  93. ALLOWED_TAGS: ['a', 'i', 'span'],
  94. ALLOWED_ATTR: ['href', 'id']
  95. }
  96. );
  97. }
  98. if (filename.length) {
  99. results.push(
  100. element.children()[0].href === data &&
  101. element.children()[0].getAttribute('download') === filename &&
  102. element.children()[0].text === result
  103. );
  104. } else {
  105. results.push(element.children()[0].href === data);
  106. }
  107. $.PrivateBin.AttachmentViewer.removeAttachment();
  108. results.push(
  109. $('#attachment').hasClass('hidden') &&
  110. $('#attachmentPreview').hasClass('hidden')
  111. );
  112. clean();
  113. return results.every(element => element);
  114. }
  115. );
  116. });
  117. });