2017-12-13 06:40:48 +00:00
|
|
|
'use strict';
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
describe('Helper', function () {
|
|
|
|
describe('secondsToHuman', function () {
|
|
|
|
jsc.property('returns an array with a number and a word', 'integer', function (number) {
|
|
|
|
var result = $.PrivateBin.Helper.secondsToHuman(number);
|
|
|
|
return Array.isArray(result) &&
|
|
|
|
result.length === 2 &&
|
|
|
|
result[0] === parseInt(result[0], 10) &&
|
|
|
|
typeof result[1] === 'string';
|
|
|
|
});
|
|
|
|
jsc.property('returns seconds on the first array position', 'integer 59', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
|
|
|
|
});
|
|
|
|
jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
|
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
|
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
|
|
|
|
});
|
|
|
|
jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
|
|
|
|
});
|
|
|
|
jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
|
|
|
|
});
|
|
|
|
jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
|
|
|
|
});
|
|
|
|
jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
|
|
|
|
});
|
|
|
|
// max safe integer as per http://ecma262-5.com/ELS5_HTML.htm#Section_8.5
|
|
|
|
jsc.property('returns months on the first array position', 'integer 5184000 9007199254740991', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
|
|
|
|
});
|
|
|
|
jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
|
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// this test is not yet meaningful using jsdom, as it does not contain getSelection support.
|
|
|
|
// TODO: This needs to be tested using a browser.
|
|
|
|
describe('selectText', function () {
|
|
|
|
this.timeout(30000);
|
|
|
|
jsc.property(
|
|
|
|
'selection contains content of given ID',
|
2017-12-14 06:19:05 +00:00
|
|
|
jsc.nearray(jsc.nearray(common.jscAlnumString())),
|
2017-12-13 06:40:48 +00:00
|
|
|
'nearray string',
|
|
|
|
function (ids, contents) {
|
|
|
|
var html = '',
|
2020-01-04 10:34:16 +00:00
|
|
|
result = true,
|
|
|
|
clean = jsdom(html);
|
2017-12-13 06:40:48 +00:00
|
|
|
ids.forEach(function(item, i) {
|
2020-01-04 10:34:16 +00:00
|
|
|
html += '<div id="' + item.join('') + '">' + $.PrivateBin.Helper.htmlEntities(contents[i] || contents[0]) + '</div>';
|
2017-12-13 06:40:48 +00:00
|
|
|
});
|
2018-01-06 08:26:10 +00:00
|
|
|
// TODO: As per https://github.com/tmpvar/jsdom/issues/321 there is no getSelection in jsdom, yet.
|
|
|
|
// Once there is one, uncomment the block below to actually check the result.
|
|
|
|
/*
|
2017-12-13 06:40:48 +00:00
|
|
|
ids.forEach(function(item, i) {
|
|
|
|
$.PrivateBin.Helper.selectText(item.join(''));
|
2018-01-06 08:26:10 +00:00
|
|
|
result *= (contents[i] || contents[0]) === window.getSelection().toString();
|
2017-12-13 06:40:48 +00:00
|
|
|
});
|
2018-01-06 08:26:10 +00:00
|
|
|
*/
|
2017-12-13 06:40:48 +00:00
|
|
|
clean();
|
|
|
|
return Boolean(result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('urls2links', function () {
|
2020-03-22 05:56:18 +00:00
|
|
|
this.timeout(30000);
|
2020-01-04 10:34:16 +00:00
|
|
|
before(function () {
|
|
|
|
cleanup = jsdom();
|
2017-12-13 06:40:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'ignores non-URL content',
|
|
|
|
'string',
|
|
|
|
function (content) {
|
2020-10-04 12:08:27 +00:00
|
|
|
content = content.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
|
2020-03-06 21:18:38 +00:00
|
|
|
let clean = jsdom();
|
|
|
|
$('body').html('<div id="foo"></div>');
|
|
|
|
let e = $('#foo');
|
|
|
|
e.text(content);
|
|
|
|
$.PrivateBin.Helper.urls2links(e);
|
|
|
|
let result = e.text();
|
|
|
|
clean();
|
|
|
|
return content === result;
|
2017-12-13 06:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces URLs with anchors',
|
|
|
|
'string',
|
2024-01-07 14:47:29 +00:00
|
|
|
common.jscUrl(),
|
2018-08-11 05:33:33 +00:00
|
|
|
jsc.array(common.jscHashString()),
|
2017-12-13 06:40:48 +00:00
|
|
|
'string',
|
2024-01-07 14:47:29 +00:00
|
|
|
function (prefix, url, fragment, postfix) {
|
2020-10-04 12:08:27 +00:00
|
|
|
prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
|
2020-03-06 22:00:48 +00:00
|
|
|
postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
|
2024-01-07 14:47:29 +00:00
|
|
|
url.fragment = fragment.join('');
|
|
|
|
let urlString = common.urlToString(url),
|
2020-03-06 21:18:38 +00:00
|
|
|
clean = jsdom();
|
|
|
|
$('body').html('<div id="foo"></div>');
|
|
|
|
let e = $('#foo');
|
2017-12-13 06:40:48 +00:00
|
|
|
|
|
|
|
// special cases: When the query string and fragment imply the beginning of an HTML entity, eg. � or &#x
|
|
|
|
if (
|
2024-01-07 14:47:29 +00:00
|
|
|
url.query[-1] === '&' &&
|
|
|
|
(parseInt(url.fragment.charAt(0), 10) >= 0 || url.fragment.charAt(0) === 'x')
|
|
|
|
) {
|
|
|
|
url.query.pop();
|
|
|
|
urlString = common.urlToString(url);
|
2017-12-13 06:40:48 +00:00
|
|
|
postfix = '';
|
|
|
|
}
|
2024-01-07 14:47:29 +00:00
|
|
|
e.text(prefix + urlString + postfix);
|
2020-03-06 21:18:38 +00:00
|
|
|
$.PrivateBin.Helper.urls2links(e);
|
|
|
|
let result = e.html();
|
|
|
|
clean();
|
2024-01-07 14:47:29 +00:00
|
|
|
urlString = $('<div />').text(urlString).html();
|
|
|
|
const expected = $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html();
|
|
|
|
return $('<div />').text(prefix).html() + '<a href="' + urlString + '" target="_blank" rel="nofollow noopener noreferrer">' + urlString + '</a>' + $('<div />').text(postfix).html() === result;
|
2017-12-13 06:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces magnet links with anchors',
|
|
|
|
'string',
|
2017-12-14 06:19:05 +00:00
|
|
|
jsc.array(common.jscQueryString()),
|
2017-12-13 06:40:48 +00:00
|
|
|
'string',
|
|
|
|
function (prefix, query, postfix) {
|
2020-10-04 12:08:27 +00:00
|
|
|
prefix = prefix.replace(/\r|\f/g, '\n').replace(/\u0000/g, '').replace(/\u000b/g, '');
|
2020-03-06 22:00:48 +00:00
|
|
|
postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, '');
|
2020-03-06 21:18:38 +00:00
|
|
|
let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''),
|
|
|
|
clean = jsdom();
|
|
|
|
$('body').html('<div id="foo"></div>');
|
|
|
|
let e = $('#foo');
|
|
|
|
e.text(prefix + url + postfix);
|
|
|
|
$.PrivateBin.Helper.urls2links(e);
|
|
|
|
let result = e.html();
|
|
|
|
clean();
|
|
|
|
url = $('<div />').text(url).html();
|
2020-06-07 05:47:28 +00:00
|
|
|
return $('<div />').text(prefix).html() + '<a href="' + url + '" target="_blank" rel="nofollow noopener noreferrer">' + url + '</a>' + $('<div />').text(postfix).html() === result;
|
2017-12-13 06:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sprintf', function () {
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + params[0] + postfix;
|
|
|
|
params.unshift(prefix + '%s' + postfix);
|
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d in strings with first given parameter',
|
|
|
|
'string',
|
|
|
|
'(small nearray) nat',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + params[0] + postfix;
|
|
|
|
params.unshift(prefix + '%d' + postfix);
|
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d in strings with 0 if first parameter is not a number',
|
|
|
|
'string',
|
|
|
|
'(small nearray) falsy',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
var result = prefix + '0' + postfix;
|
|
|
|
params.unshift(prefix + '%d' + postfix);
|
2018-01-06 09:57:54 +00:00
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
2017-12-13 06:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d and %s in strings in order',
|
|
|
|
'string',
|
|
|
|
'nat',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
function (prefix, uint, middle, string, postfix) {
|
2019-09-19 18:48:05 +00:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '');
|
|
|
|
middle = middle.replace(/%(s|d)/g, '');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '');
|
2017-12-13 06:40:48 +00:00
|
|
|
var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
|
|
|
|
result = prefix + uint + middle + string + postfix;
|
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %d and %s in strings in reverse order',
|
|
|
|
'string',
|
|
|
|
'nat',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
'string',
|
|
|
|
function (prefix, uint, middle, string, postfix) {
|
2019-09-19 18:48:05 +00:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '');
|
|
|
|
middle = middle.replace(/%(s|d)/g, '');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '');
|
2017-12-13 06:40:48 +00:00
|
|
|
var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
|
|
|
|
result = prefix + string + middle + uint + postfix;
|
|
|
|
return result === $.PrivateBin.Helper.sprintf.apply(this, params);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCookie', function () {
|
|
|
|
this.timeout(30000);
|
2020-01-04 10:34:16 +00:00
|
|
|
before(function () {
|
2018-12-25 19:16:41 +00:00
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
2017-12-13 06:40:48 +00:00
|
|
|
jsc.property(
|
|
|
|
'returns the requested cookie',
|
2018-09-01 17:42:22 +00:00
|
|
|
jsc.nearray(jsc.nearray(common.jscAlnumString())),
|
|
|
|
jsc.nearray(jsc.nearray(common.jscAlnumString())),
|
2017-12-13 06:40:48 +00:00
|
|
|
function (labels, values) {
|
|
|
|
var selectedKey = '', selectedValue = '',
|
2018-01-06 08:26:10 +00:00
|
|
|
cookieArray = [];
|
2017-12-13 06:40:48 +00:00
|
|
|
labels.forEach(function(item, i) {
|
2018-09-01 17:42:22 +00:00
|
|
|
var key = item.join(''),
|
|
|
|
value = (values[i] || values[0]).join('');
|
2017-12-13 06:40:48 +00:00
|
|
|
cookieArray.push(key + '=' + value);
|
|
|
|
if (Math.random() < 1 / i || selectedKey === key)
|
|
|
|
{
|
|
|
|
selectedKey = key;
|
|
|
|
selectedValue = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var clean = jsdom('', {cookie: cookieArray}),
|
|
|
|
result = $.PrivateBin.Helper.getCookie(selectedKey);
|
2018-09-02 09:33:27 +00:00
|
|
|
$.PrivateBin.Helper.reset();
|
2017-12-13 06:40:48 +00:00
|
|
|
clean();
|
|
|
|
return result === selectedValue;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('baseUri', function () {
|
|
|
|
this.timeout(30000);
|
|
|
|
jsc.property(
|
|
|
|
'returns the URL without query & fragment',
|
2024-01-07 16:45:01 +00:00
|
|
|
common.jscSchemas(false),
|
|
|
|
common.jscUrl(),
|
|
|
|
function (schema, url) {
|
|
|
|
url.schema = schema;
|
|
|
|
const fullUrl = common.urlToString(url);
|
|
|
|
delete(url.query);
|
|
|
|
delete(url.fragment);
|
2018-09-02 09:33:27 +00:00
|
|
|
$.PrivateBin.Helper.reset();
|
2024-01-07 16:45:01 +00:00
|
|
|
const expected = common.urlToString(url),
|
|
|
|
clean = jsdom('', {url: fullUrl}),
|
2017-12-13 06:40:48 +00:00
|
|
|
result = $.PrivateBin.Helper.baseUri();
|
|
|
|
clean();
|
|
|
|
return expected === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('htmlEntities', function () {
|
2020-01-04 10:34:16 +00:00
|
|
|
before(function () {
|
|
|
|
cleanup = jsdom();
|
2017-12-13 06:40:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'removes all HTML entities from any given string',
|
|
|
|
'string',
|
|
|
|
function (string) {
|
2020-01-04 10:34:16 +00:00
|
|
|
var result = $.PrivateBin.Helper.htmlEntities(string);
|
|
|
|
return !(/[<>]/.test(result)) && !(string.indexOf('&') > -1 && !(/&/.test(result)));
|
2017-12-13 06:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|