2017-01-29 14:31:44 +01:00
|
|
|
'use strict';
|
2017-02-05 16:45:11 +01:00
|
|
|
var jsc = require('jsverify'),
|
|
|
|
jsdom = require('jsdom-global'),
|
2017-02-05 16:58:58 +01:00
|
|
|
cleanup = jsdom(),
|
|
|
|
|
|
|
|
a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
|
|
|
|
'n','o','p','q','r','s','t','u','v','w','x','y','z'],
|
|
|
|
alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']),
|
2017-02-05 18:03:42 +01:00
|
|
|
queryString = alnumString.concat(['+','%','&','.','*','-','_']),
|
|
|
|
base64String = alnumString.concat(['+','/','=']).concat(
|
|
|
|
a2zString.map(function(c) {
|
|
|
|
return c.toUpperCase();
|
|
|
|
})
|
|
|
|
);
|
2017-02-05 16:58:58 +01:00
|
|
|
|
2017-02-05 16:45:11 +01:00
|
|
|
global.$ = global.jQuery = require('./jquery-3.1.1');
|
|
|
|
global.sjcl = require('./sjcl-1.0.6');
|
|
|
|
global.Base64 = require('./base64-2.1.9');
|
|
|
|
global.RawDeflate = require('./rawdeflate-0.5');
|
|
|
|
require('./rawinflate-0.3');
|
|
|
|
require('./privatebin');
|
2017-01-29 14:31:44 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('Helper', function () {
|
2017-01-29 14:31:44 +01:00
|
|
|
describe('secondsToHuman', function () {
|
2017-02-05 16:45:11 +01:00
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
2017-01-29 14:31:44 +01:00
|
|
|
jsc.property('returns an array with a number and a word', 'integer', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
var result = $.PrivateBin.Helper.secondsToHuman(number);
|
2017-01-29 14:31:44 +01:00
|
|
|
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) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === number;
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns seconds on the second array position', 'integer 59', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'second';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the first array position', 'integer 60 3599', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / 60);
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns minutes on the second array position', 'integer 60 3599', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'minute';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns hours on the first array position', 'integer 3600 86399', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns hours on the second array position', 'integer 3600 86399', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'hour';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns days on the first array position', 'integer 86400 5184000', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns days on the second array position', 'integer 86400 5184000', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'day';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
// 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) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[0] === Math.floor(number / (60 * 60 * 24 * 30));
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
jsc.property('returns months on the second array position', 'integer 5184000 9007199254740991', function (number) {
|
2017-02-15 22:59:55 +01:00
|
|
|
return $.PrivateBin.Helper.secondsToHuman(number)[1] === 'month';
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|
|
|
|
});
|
2017-02-05 16:45:11 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('baseUri', function () {
|
2017-02-05 16:45:11 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the URL without query & fragment',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
'string',
|
|
|
|
function (schema, address, query, fragment) {
|
|
|
|
var expected = schema.join('') + '://' + address.join('') + '/',
|
|
|
|
clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Helper.baseUri();
|
2017-02-05 16:45:11 +01:00
|
|
|
clean();
|
|
|
|
return expected === result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-02-05 16:58:58 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('htmlEntities', function () {
|
|
|
|
after(function () {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'removes all HTML entities from any given string',
|
|
|
|
'string',
|
|
|
|
function (string) {
|
|
|
|
var result = $.PrivateBin.Helper.htmlEntities(string);
|
|
|
|
return !(/[<>"'`=\/]/.test(result)) && !(string.indexOf('&') > -1 && !(/&/.test(result)));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model', function () {
|
|
|
|
describe('getPasteId', function () {
|
2017-02-05 16:58:58 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the query string without separator, if any',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(queryString)),
|
2017-02-05 16:58:58 +01:00
|
|
|
'string',
|
|
|
|
function (schema, address, query, fragment) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var queryString = query.join(''),
|
2017-02-05 16:58:58 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
2017-02-05 21:22:09 +01:00
|
|
|
'/?' + queryString + '#' + fragment
|
2017-02-05 16:58:58 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteId();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 16:58:58 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return queryString === result;
|
2017-02-05 16:58:58 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-02-05 18:03:42 +01:00
|
|
|
|
2017-02-25 09:35:55 +01:00
|
|
|
describe('getPasteKey', function () {
|
2017-02-05 18:03:42 +01:00
|
|
|
jsc.property(
|
|
|
|
'returns the fragment of the URL',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(base64String)),
|
2017-02-05 18:03:42 +01:00
|
|
|
function (schema, address, query, fragment) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var fragmentString = fragment.join(''),
|
2017-02-05 18:03:42 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') +
|
2017-02-05 21:22:09 +01:00
|
|
|
'/?' + query.join('') + '#' + fragmentString
|
2017-02-05 18:03:42 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 18:03:42 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return fragmentString === result;
|
2017-02-05 18:03:42 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'returns the fragment stripped of trailing query parts',
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.nearray(jsc.elements(a2zString)),
|
|
|
|
jsc.array(jsc.elements(queryString)),
|
2017-02-25 09:35:55 +01:00
|
|
|
jsc.nearray(jsc.elements(base64String)),
|
2017-02-05 18:03:42 +01:00
|
|
|
jsc.array(jsc.elements(queryString)),
|
|
|
|
function (schema, address, query, fragment, trail) {
|
2017-02-05 21:22:09 +01:00
|
|
|
var fragmentString = fragment.join(''),
|
2017-02-05 18:03:42 +01:00
|
|
|
clean = jsdom('', {
|
|
|
|
url: schema.join('') + '://' + address.join('') + '/?' +
|
2017-02-05 21:22:09 +01:00
|
|
|
query.join('') + '#' + fragmentString + '&' + trail.join('')
|
2017-02-05 18:03:42 +01:00
|
|
|
}),
|
2017-02-25 09:35:55 +01:00
|
|
|
result = $.PrivateBin.Model.getPasteKey();
|
|
|
|
$.PrivateBin.Model.reset();
|
2017-02-05 18:03:42 +01:00
|
|
|
clean();
|
2017-02-05 21:22:09 +01:00
|
|
|
return fragmentString === result;
|
2017-02-05 18:03:42 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-01-29 14:31:44 +01:00
|
|
|
});
|