AttachmentViewer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
  29. '<div id="attachment" class="hidden"></div>' +
  30. '<div id="templates">' +
  31. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  32. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  33. '<a class="alert-link">Download attachment</a>' +
  34. '</div>' +
  35. '</div>'
  36. );
  37. // mock createObjectURL for jsDOM
  38. if (typeof window.URL.createObjectURL === 'undefined') {
  39. Object.defineProperty(
  40. window.URL,
  41. 'createObjectURL',
  42. {value: function(blob) {
  43. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  44. }}
  45. )
  46. }
  47. $.PrivateBin.AttachmentViewer.init();
  48. $.PrivateBin.Model.init();
  49. results.push(
  50. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  51. $('#attachment').hasClass('hidden') &&
  52. $('#attachment').children().length === 0 &&
  53. $('#attachmenttemplate').hasClass('hidden') &&
  54. $('#attachmentPreview').hasClass('hidden')
  55. );
  56. global.atob = common.atob;
  57. if (filename.length) {
  58. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  59. } else {
  60. $.PrivateBin.AttachmentViewer.setAttachment(data);
  61. }
  62. // // beyond this point we will get the blob URL instead of the data
  63. data = window.URL.createObjectURL(data);
  64. const attachment = $.PrivateBin.AttachmentViewer.getAttachments();
  65. results.push(
  66. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  67. $('#attachment').hasClass('hidden') &&
  68. $('#attachment').children().length > 0 &&
  69. $('#attachmentPreview').hasClass('hidden') &&
  70. attachment[0][0] === data &&
  71. attachment[0][1] === filename
  72. );
  73. $.PrivateBin.AttachmentViewer.showAttachment();
  74. results.push(
  75. !$('#attachment').hasClass('hidden') &&
  76. $('#attachment').children().length > 0 &&
  77. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  78. );
  79. $.PrivateBin.AttachmentViewer.hideAttachment();
  80. results.push(
  81. $('#attachment').hasClass('hidden') &&
  82. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  83. );
  84. if (previewSupported) {
  85. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  86. results.push($('#attachmentPreview').hasClass('hidden'));
  87. }
  88. $.PrivateBin.AttachmentViewer.showAttachment();
  89. results.push(
  90. !$('#attachment').hasClass('hidden') &&
  91. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  92. );
  93. let element = $('<div>');
  94. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, attachment[0], prefix + '%s' + postfix);
  95. // messageIDs with links get a relaxed treatment
  96. if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
  97. result = $('<textarea>').text((prefix + filename + postfix)).text();
  98. } else {
  99. result = DOMPurify.sanitize(
  100. prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix, {
  101. ALLOWED_TAGS: ['a', 'i', 'span'],
  102. ALLOWED_ATTR: ['href', 'id']
  103. }
  104. );
  105. }
  106. if (filename.length) {
  107. results.push(
  108. element.find('a')[0].href === data &&
  109. element.find('a')[0].getAttribute('download') === filename &&
  110. element.find('a')[0].text === result
  111. );
  112. } else {
  113. results.push(element.find('a')[0].href === data);
  114. }
  115. $.PrivateBin.AttachmentViewer.removeAttachment();
  116. results.push(
  117. $('#attachment').hasClass('hidden') &&
  118. $('#attachment').children().length === 0 &&
  119. $('#attachmentPreview').hasClass('hidden')
  120. );
  121. clean();
  122. return results.every(element => element);
  123. }
  124. );
  125. });
  126. });