2017-12-14 07:31:09 +01:00
|
|
|
'use strict';
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
describe('Model', function () {
|
|
|
|
describe('getExpirationDefault', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.Model.reset();
|
2020-01-04 11:34:16 +01:00
|
|
|
cleanup = jsdom();
|
2017-12-14 07:31:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns the contents of the element with id "pasteExpiration"',
|
2020-01-04 11:34:16 +01:00
|
|
|
'nearray asciinestring',
|
2017-12-14 07:31:09 +01:00
|
|
|
'string',
|
|
|
|
'small nat',
|
|
|
|
function (keys, value, key) {
|
2020-01-04 11:34:16 +01:00
|
|
|
keys = keys.map($.PrivateBin.Helper.htmlEntities);
|
|
|
|
value = $.PrivateBin.Helper.htmlEntities(value);
|
|
|
|
var content = keys.length > key ? keys[key] : keys[0],
|
2017-12-14 07:31:09 +01:00
|
|
|
contents = '<select id="pasteExpiration" name="pasteExpiration">';
|
|
|
|
keys.forEach(function(item) {
|
|
|
|
contents += '<option value="' + item + '"';
|
|
|
|
if (item === content) {
|
|
|
|
contents += ' selected="selected"';
|
|
|
|
}
|
|
|
|
contents += '>' + value + '</option>';
|
|
|
|
});
|
|
|
|
contents += '</select>';
|
|
|
|
$('body').html(contents);
|
2020-01-04 11:34:16 +01:00
|
|
|
var result = $.PrivateBin.Helper.htmlEntities(
|
2017-12-14 07:31:09 +01:00
|
|
|
$.PrivateBin.Model.getExpirationDefault()
|
|
|
|
);
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
return content === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getFormatDefault', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.Model.reset();
|
2020-01-04 11:34:16 +01:00
|
|
|
});
|
|
|
|
after(function () {
|
2017-12-14 07:31:09 +01:00
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns the contents of the element with id "pasteFormatter"',
|
2020-01-04 11:34:16 +01:00
|
|
|
'nearray asciinestring',
|
2017-12-14 07:31:09 +01:00
|
|
|
'string',
|
|
|
|
'small nat',
|
|
|
|
function (keys, value, key) {
|
2020-01-04 11:34:16 +01:00
|
|
|
keys = keys.map($.PrivateBin.Helper.htmlEntities);
|
|
|
|
value = $.PrivateBin.Helper.htmlEntities(value);
|
|
|
|
var content = keys.length > key ? keys[key] : keys[0],
|
2017-12-14 07:31:09 +01:00
|
|
|
contents = '<select id="pasteFormatter" name="pasteFormatter">';
|
|
|
|
keys.forEach(function(item) {
|
|
|
|
contents += '<option value="' + item + '"';
|
|
|
|
if (item === content) {
|
|
|
|
contents += ' selected="selected"';
|
|
|
|
}
|
|
|
|
contents += '>' + value + '</option>';
|
|
|
|
});
|
|
|
|
contents += '</select>';
|
|
|
|
$('body').html(contents);
|
2020-01-04 11:34:16 +01:00
|
|
|
var result = $.PrivateBin.Helper.htmlEntities(
|
2017-12-14 07:31:09 +01:00
|
|
|
$.PrivateBin.Model.getFormatDefault()
|
|
|
|
);
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
return content === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPasteId', function () {
|
|
|
|
this.timeout(30000);
|
2018-12-25 20:16:41 +01:00
|
|
|
beforeEach(function () {
|
2017-12-14 07:31:09 +01:00
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns the query string without separator, if any',
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
2019-01-20 11:05:34 +01:00
|
|
|
jsc.tuple(new Array(16).fill(common.jscHexString)),
|
|
|
|
jsc.array(common.jscQueryString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
2017-12-14 07:31:09 +01:00
|
|
|
'string',
|
2019-01-20 11:05:34 +01:00
|
|
|
function (schema, address, pasteId, queryStart, queryEnd, fragment) {
|
|
|
|
var pasteIdString = pasteId.join(''),
|
|
|
|
queryStartString = queryStart.join('') + (queryStart.length > 0 ? '&' : ''),
|
|
|
|
queryEndString = (queryEnd.length > 0 ? '&' : '') + queryEnd.join(''),
|
|
|
|
queryString = queryStartString + pasteIdString + queryEndString,
|
|
|
|
clean = jsdom('', {
|
2017-12-14 07:31:09 +01:00
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/?' + queryString + '#' + fragment
|
2019-06-20 22:30:49 +02:00
|
|
|
});
|
|
|
|
global.URL = require('jsdom-url').URL;
|
|
|
|
var result = $.PrivateBin.Model.getPasteId();
|
2017-12-14 07:31:09 +01:00
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
2019-01-20 11:05:34 +01:00
|
|
|
return pasteIdString === result;
|
2017-12-14 07:31:09 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'throws exception on empty query string',
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
'string',
|
|
|
|
function (schema, address, fragment) {
|
|
|
|
var clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/#' + fragment
|
|
|
|
}),
|
|
|
|
result = false;
|
2019-06-20 22:30:49 +02:00
|
|
|
global.URL = require('jsdom-url').URL;
|
2017-12-14 07:31:09 +01:00
|
|
|
try {
|
|
|
|
$.PrivateBin.Model.getPasteId();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPasteKey', function () {
|
|
|
|
this.timeout(30000);
|
2018-12-25 20:16:41 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
});
|
|
|
|
|
2017-12-14 07:31:09 +01:00
|
|
|
jsc.property(
|
2019-05-15 21:20:54 +02:00
|
|
|
'returns the fragment of a v1 URL',
|
2017-12-14 07:31:09 +01:00
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
2018-12-29 18:40:59 +01:00
|
|
|
'nestring',
|
2017-12-14 07:31:09 +01:00
|
|
|
function (schema, address, query, fragment) {
|
2019-05-19 09:54:40 +02:00
|
|
|
const fragmentString = common.btoa(fragment.padStart(32, '\u0000'));
|
2019-05-19 08:36:18 +02:00
|
|
|
let clean = jsdom('', {
|
2017-12-14 07:31:09 +01:00
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/?' + query.join('') + '#' + fragmentString
|
|
|
|
}),
|
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
2019-05-19 08:36:18 +02:00
|
|
|
return fragmentString === result;
|
2017-12-14 07:31:09 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
2019-05-15 21:20:54 +02:00
|
|
|
'returns the v1 fragment stripped of trailing query parts',
|
2017-12-14 07:31:09 +01:00
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
2018-12-29 18:40:59 +01:00
|
|
|
'nestring',
|
2018-08-11 07:33:33 +02:00
|
|
|
jsc.array(common.jscHashString()),
|
2017-12-14 07:31:09 +01:00
|
|
|
function (schema, address, query, fragment, trail) {
|
2019-05-19 09:54:40 +02:00
|
|
|
const fragmentString = common.btoa(fragment.padStart(32, '\u0000'));
|
2019-05-19 08:36:18 +02:00
|
|
|
let clean = jsdom('', {
|
2017-12-14 07:31:09 +01:00
|
|
|
url: schema.join('') + '://' + address.join('') + '/?' +
|
|
|
|
query.join('') + '#' + fragmentString + '&' + trail.join('')
|
|
|
|
}),
|
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
2019-05-19 08:36:18 +02:00
|
|
|
return fragmentString === result;
|
2019-05-15 21:20:54 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'returns the fragment of a v2 URL',
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
|
|
|
'nestring',
|
|
|
|
function (schema, address, query, fragment) {
|
2019-05-19 09:54:40 +02:00
|
|
|
// base58 strips leading NULL bytes, so the string is padded with these if not found
|
|
|
|
fragment = fragment.padStart(32, '\u0000');
|
2019-05-15 21:20:54 +02:00
|
|
|
let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
|
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/?' + query.join('') + '#' + fragmentString
|
|
|
|
}),
|
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
|
|
|
return fragment === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'returns the v2 fragment stripped of trailing query parts',
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
|
|
|
'nestring',
|
|
|
|
jsc.array(common.jscHashString()),
|
|
|
|
function (schema, address, query, fragment, trail) {
|
2019-05-19 09:54:40 +02:00
|
|
|
// base58 strips leading NULL bytes, so the string is padded with these if not found
|
|
|
|
fragment = fragment.padStart(32, '\u0000');
|
2019-05-15 21:20:54 +02:00
|
|
|
let fragmentString = $.PrivateBin.CryptTool.base58encode(fragment),
|
2017-12-14 07:31:09 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') + '/?' +
|
|
|
|
query.join('') + '#' + fragmentString + '&' + trail.join('')
|
|
|
|
}),
|
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
2018-12-29 18:40:59 +01:00
|
|
|
return fragment === result;
|
2017-12-14 07:31:09 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'throws exception on empty fragment of the URL',
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.array(common.jscQueryString()),
|
|
|
|
function (schema, address, query) {
|
|
|
|
var clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
|
|
|
'/?' + query.join('')
|
|
|
|
}),
|
|
|
|
result = false;
|
|
|
|
try {
|
|
|
|
$.PrivateBin.Model.getPasteKey();
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
clean();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getTemplate', function () {
|
2018-12-25 20:16:41 +01:00
|
|
|
beforeEach(function () {
|
2017-12-14 07:31:09 +01:00
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns the contents of the element with id "[name]template"',
|
|
|
|
jsc.nearray(common.jscAlnumString()),
|
|
|
|
jsc.nearray(common.jscA2zString()),
|
|
|
|
jsc.nearray(common.jscAlnumString()),
|
|
|
|
function (id, element, value) {
|
|
|
|
id = id.join('');
|
|
|
|
element = element.join('');
|
|
|
|
value = value.join('').trim();
|
|
|
|
|
|
|
|
// <br>, <hr>, <img> and <wbr> tags can't contain strings,
|
|
|
|
// table tags can't be alone, so test with a <p> instead
|
|
|
|
if (['br', 'col', 'hr', 'img', 'tr', 'td', 'th', 'wbr'].indexOf(element) >= 0) {
|
|
|
|
element = 'p';
|
|
|
|
}
|
|
|
|
|
|
|
|
$('body').html(
|
|
|
|
'<div id="templates"><' + element + ' id="' + id +
|
|
|
|
'template">' + value + '</' + element + '></div>'
|
|
|
|
);
|
|
|
|
$.PrivateBin.Model.init();
|
|
|
|
var template = '<' + element + ' id="' + id + '">' + value +
|
|
|
|
'</' + element + '>',
|
|
|
|
result = $.PrivateBin.Model.getTemplate(id).wrap('<p/>').parent().html();
|
|
|
|
$.PrivateBin.Model.reset();
|
|
|
|
return template === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|