2017-12-18 21:25:08 +08:00
|
|
|
'use strict';
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
describe('AttachmentViewer', function () {
|
|
|
|
describe('setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo', function () {
|
|
|
|
this.timeout(30000);
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'displays & hides data as requested',
|
|
|
|
common.jscMimeTypes(),
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
2019-06-15 14:56:47 +08:00
|
|
|
'string',
|
|
|
|
function (mimeType, rawdata, filename, prefix, postfix) {
|
2020-01-04 18:34:16 +08:00
|
|
|
let clean = jsdom(),
|
2019-06-15 14:56:47 +08:00
|
|
|
data = 'data:' + mimeType + ';base64,' + btoa(rawdata),
|
2018-04-29 17:57:03 +08:00
|
|
|
previewSupported = (
|
|
|
|
mimeType.substring(0, 6) === 'image/' ||
|
|
|
|
mimeType.substring(0, 6) === 'audio/' ||
|
|
|
|
mimeType.substring(0, 6) === 'video/' ||
|
|
|
|
mimeType.match(/\/pdf/i)
|
|
|
|
),
|
2020-01-04 18:34:16 +08:00
|
|
|
results = [],
|
|
|
|
result = '';
|
2019-12-25 16:14:32 +08:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
2017-12-18 21:25:08 +08:00
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
$('body').html(
|
|
|
|
'<div id="attachment" role="alert" class="hidden alert ' +
|
|
|
|
'alert-info"><span class="glyphicon glyphicon-download-' +
|
|
|
|
'alt" aria-hidden="true"></span> <a class="alert-link">' +
|
|
|
|
'Download attachment</a></div><div id="attachmentPrevie' +
|
|
|
|
'w" class="hidden"></div>'
|
|
|
|
);
|
2019-06-15 14:56:47 +08:00
|
|
|
// mock createObjectURL for jsDOM
|
|
|
|
if (typeof window.URL.createObjectURL === 'undefined') {
|
|
|
|
Object.defineProperty(
|
|
|
|
window.URL,
|
|
|
|
'createObjectURL',
|
|
|
|
{value: function(blob) {
|
|
|
|
return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
|
|
|
|
}}
|
|
|
|
)
|
|
|
|
}
|
2017-12-18 21:25:08 +08:00
|
|
|
$.PrivateBin.AttachmentViewer.init();
|
|
|
|
results.push(
|
|
|
|
!$.PrivateBin.AttachmentViewer.hasAttachment() &&
|
|
|
|
$('#attachment').hasClass('hidden') &&
|
|
|
|
$('#attachmentPreview').hasClass('hidden')
|
|
|
|
);
|
|
|
|
if (filename.length) {
|
|
|
|
$.PrivateBin.AttachmentViewer.setAttachment(data, filename);
|
|
|
|
} else {
|
|
|
|
$.PrivateBin.AttachmentViewer.setAttachment(data);
|
|
|
|
}
|
2019-06-15 14:56:47 +08:00
|
|
|
// beyond this point we will get the blob URL instead of the data
|
|
|
|
data = window.URL.createObjectURL(data);
|
2020-01-04 18:34:16 +08:00
|
|
|
const attachment = $.PrivateBin.AttachmentViewer.getAttachment();
|
2017-12-18 21:25:08 +08:00
|
|
|
results.push(
|
|
|
|
$.PrivateBin.AttachmentViewer.hasAttachment() &&
|
|
|
|
$('#attachment').hasClass('hidden') &&
|
|
|
|
$('#attachmentPreview').hasClass('hidden') &&
|
2018-04-29 17:57:03 +08:00
|
|
|
attachment[0] === data &&
|
|
|
|
attachment[1] === filename
|
2017-12-18 21:25:08 +08:00
|
|
|
);
|
|
|
|
$.PrivateBin.AttachmentViewer.showAttachment();
|
|
|
|
results.push(
|
|
|
|
!$('#attachment').hasClass('hidden') &&
|
2018-04-29 17:57:03 +08:00
|
|
|
(previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
|
2017-12-18 21:25:08 +08:00
|
|
|
);
|
|
|
|
$.PrivateBin.AttachmentViewer.hideAttachment();
|
|
|
|
results.push(
|
|
|
|
$('#attachment').hasClass('hidden') &&
|
2018-04-29 17:57:03 +08:00
|
|
|
(previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
|
2017-12-18 21:25:08 +08:00
|
|
|
);
|
2018-04-29 17:57:03 +08:00
|
|
|
if (previewSupported) {
|
2017-12-18 21:25:08 +08:00
|
|
|
$.PrivateBin.AttachmentViewer.hideAttachmentPreview();
|
|
|
|
results.push($('#attachmentPreview').hasClass('hidden'));
|
|
|
|
}
|
|
|
|
$.PrivateBin.AttachmentViewer.showAttachment();
|
|
|
|
results.push(
|
|
|
|
!$('#attachment').hasClass('hidden') &&
|
2018-04-29 17:57:03 +08:00
|
|
|
(previewSupported ? !$('#attachmentPreview').hasClass('hidden') : $('#attachmentPreview').hasClass('hidden'))
|
2017-12-18 21:25:08 +08:00
|
|
|
);
|
2020-01-04 18:34:16 +08:00
|
|
|
let element = $('<div>');
|
2017-12-18 21:25:08 +08:00
|
|
|
$.PrivateBin.AttachmentViewer.moveAttachmentTo(element, prefix + '%s' + postfix);
|
2020-01-04 18:34:16 +08:00
|
|
|
// messageIDs with links get a relaxed treatment
|
|
|
|
if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
|
2020-02-01 05:42:42 +08:00
|
|
|
result = $('<textarea>').text((prefix + filename + postfix)).text();
|
2020-01-04 18:34:16 +08:00
|
|
|
} else {
|
2020-02-06 02:06:45 +08:00
|
|
|
result = DOMPurify.sanitize(
|
|
|
|
prefix + $.PrivateBin.Helper.htmlEntities(filename) + postfix, {
|
|
|
|
ALLOWED_TAGS: ['a', 'i', 'span'],
|
|
|
|
ALLOWED_ATTR: ['href', 'id']
|
|
|
|
}
|
|
|
|
);
|
2020-01-04 18:34:16 +08:00
|
|
|
}
|
2017-12-18 21:25:08 +08:00
|
|
|
if (filename.length) {
|
|
|
|
results.push(
|
|
|
|
element.children()[0].href === data &&
|
|
|
|
element.children()[0].getAttribute('download') === filename &&
|
2020-01-04 18:34:16 +08:00
|
|
|
element.children()[0].text === result
|
2017-12-18 21:25:08 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
results.push(element.children()[0].href === data);
|
|
|
|
}
|
|
|
|
$.PrivateBin.AttachmentViewer.removeAttachment();
|
|
|
|
results.push(
|
|
|
|
$('#attachment').hasClass('hidden') &&
|
|
|
|
$('#attachmentPreview').hasClass('hidden')
|
|
|
|
);
|
|
|
|
clean();
|
|
|
|
return results.every(element => element);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|