AttachmentViewer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // mock createObjectURL for jsDOM
  34. if (typeof window.URL.createObjectURL === 'undefined') {
  35. Object.defineProperty(
  36. window.URL,
  37. 'createObjectURL',
  38. {value: function(blob) {
  39. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  40. }}
  41. )
  42. }
  43. $.PrivateBin.AttachmentViewer.init();
  44. results.push(
  45. !$.PrivateBin.AttachmentViewer.hasAttachment() &&
  46. $('#attachment').hasClass('hidden') &&
  47. $('#attachmentPreview').hasClass('hidden')
  48. );
  49. if (filename.length) {
  50. $.PrivateBin.AttachmentViewer.setAttachment(data, filename);
  51. } else {
  52. $.PrivateBin.AttachmentViewer.setAttachment(data);
  53. }
  54. // beyond this point we will get the blob URL instead of the data
  55. data = window.URL.createObjectURL(data);
  56. const attachment = $.PrivateBin.AttachmentViewer.getAttachment();
  57. results.push(
  58. $.PrivateBin.AttachmentViewer.hasAttachment() &&
  59. $('#attachment').hasClass('hidden') &&
  60. $('#attachmentPreview').hasClass('hidden') &&
  61. attachment[0] === data &&
  62. attachment[1] === filename
  63. );
  64. $.PrivateBin.AttachmentViewer.showAttachment();
  65. results.push(
  66. !$('#attachment').hasClass('hidden') &&
  67. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  68. );
  69. $.PrivateBin.AttachmentViewer.hideAttachment();
  70. results.push(
  71. $('#attachment').hasClass('hidden') &&
  72. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  73. );
  74. if (previewSupported) {
  75. $.PrivateBin.AttachmentViewer.hideAttachmentPreview();
  76. results.push($('#attachmentPreview').hasClass('hidden'));
  77. }
  78. $.PrivateBin.AttachmentViewer.showAttachment();
  79. results.push(
  80. !$('#attachment').hasClass('hidden') &&
  81. (previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
  82. );
  83. let element = $('<div>');
  84. $.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
  85. // messageIDs with links get a relaxed treatment
  86. if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
  87. result = $.PrivateBin.Helper.htmlEntities(prefix + filename + postfix);
  88. } else {
  89. result = $('<div>').html(prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix).html();
  90. }
  91. if (filename.length) {
  92. results.push(
  93. element.children()[0].href === data &&
  94. element.children()[0].getAttribute('download') === filename &&
  95. element.children()[0].text === result
  96. );
  97. } else {
  98. results.push(element.children()[0].href === data);
  99. }
  100. $.PrivateBin.AttachmentViewer.removeAttachment();
  101. results.push(
  102. $('#attachment').hasClass('hidden') &&
  103. $('#attachmentPreview').hasClass('hidden')
  104. );
  105. clean();
  106. return results.every(element => element);
  107. }
  108. );
  109. });
  110. });