| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 'use strict';
- var common = require('../common');
- describe('Model', function () {
- describe('getExpirationDefault', function () {
- before(function () {
- $.PrivateBin.Model.reset();
- cleanup = jsdom();
- });
- jsc.property(
- 'returns the contents of the element with id "pasteExpiration"',
- 'nearray asciinestring',
- 'string',
- 'small nat',
- function (keys, value, key) {
- keys = keys.map($.PrivateBin.Helper.htmlEntities);
- value = $.PrivateBin.Helper.htmlEntities(value);
- var content = keys.length > key ? keys[key] : keys[0],
- 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);
- var result = $.PrivateBin.Helper.htmlEntities(
- $.PrivateBin.Model.getExpirationDefault()
- );
- $.PrivateBin.Model.reset();
- return content === result;
- }
- );
- });
- describe('getFormatDefault', function () {
- before(function () {
- $.PrivateBin.Model.reset();
- });
- after(function () {
- cleanup();
- });
- jsc.property(
- 'returns the contents of the element with id "pasteFormatter"',
- 'nearray asciinestring',
- 'string',
- 'small nat',
- function (keys, value, key) {
- keys = keys.map($.PrivateBin.Helper.htmlEntities);
- value = $.PrivateBin.Helper.htmlEntities(value);
- var content = keys.length > key ? keys[key] : keys[0],
- 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);
- var result = $.PrivateBin.Helper.htmlEntities(
- $.PrivateBin.Model.getFormatDefault()
- );
- $.PrivateBin.Model.reset();
- return content === result;
- }
- );
- });
- describe('getPasteId', function () {
- this.timeout(30000);
- beforeEach(function () {
- $.PrivateBin.Model.reset();
- });
- jsc.property(
- 'returns the query string without separator, if any',
- common.jscUrl(true, false),
- jsc.tuple(new Array(16).fill(common.jscHexString)),
- jsc.array(common.jscQueryString()),
- jsc.array(common.jscQueryString()),
- function (url, pasteId, queryStart, queryEnd) {
- if (queryStart.length > 0) {
- queryStart.push('&');
- }
- if (queryEnd.length > 0) {
- queryEnd.unshift('&');
- }
- url.query = queryStart.concat(pasteId, queryEnd);
- const pasteIdString = pasteId.join(''),
- clean = jsdom('', {url: common.urlToString(url)});
- const result = $.PrivateBin.Model.getPasteId();
- $.PrivateBin.Model.reset();
- clean();
- return pasteIdString === result;
- }
- );
- jsc.property(
- 'throws exception on empty query string',
- common.jscUrl(true, false),
- function (url) {
- let clean = jsdom('', {url: common.urlToString(url)}),
- result = false;
- try {
- $.PrivateBin.Model.getPasteId();
- }
- catch(err) {
- result = true;
- }
- $.PrivateBin.Model.reset();
- clean();
- return result;
- }
- );
- });
- describe('getPasteKey', function () {
- this.timeout(30000);
- beforeEach(function () {
- $.PrivateBin.Model.reset();
- });
- jsc.property(
- 'returns the fragment of a v1 URL',
- common.jscUrl(),
- function (url) {
- url.fragment = '0OIl'; // any non-base58 string
- const clean = jsdom('', {url: common.urlToString(url)}),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return url.fragment === result;
- }
- );
- jsc.property(
- 'returns the fragment stripped of trailing query parts',
- common.jscUrl(),
- jsc.array(common.jscHashString()),
- function (url, trail) {
- const fragment = url.fragment.padStart(32, '\u0000');
- url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
- const clean = jsdom('', {url: common.urlToString(url)}),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return fragment === result;
- }
- );
- jsc.property(
- 'returns the fragment of a v2 URL',
- common.jscUrl(),
- function (url) {
- // base58 strips leading NULL bytes, so the string is padded with these if not found
- const fragment = url.fragment.padStart(32, '\u0000');
- url.fragment = $.PrivateBin.CryptTool.base58encode(fragment);
- const clean = jsdom('', {url: common.urlToString(url)}),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return fragment === result;
- }
- );
- jsc.property(
- 'returns the v2 fragment stripped of trailing query parts',
- common.jscUrl(),
- jsc.array(common.jscHashString()),
- function (url, trail) {
- // base58 strips leading NULL bytes, so the string is padded with these if not found
- const fragment = url.fragment.padStart(32, '\u0000');
- url.fragment = $.PrivateBin.CryptTool.base58encode(fragment) + '&' + trail.join('');
- const clean = jsdom('', {url: common.urlToString(url)}),
- result = $.PrivateBin.Model.getPasteKey();
- $.PrivateBin.Model.reset();
- clean();
- return fragment === result;
- }
- );
- jsc.property(
- 'throws exception on empty fragment of the URL',
- common.jscUrl(false),
- function (url) {
- let clean = jsdom('', {url: common.urlToString(url)}),
- result = false;
- try {
- $.PrivateBin.Model.getPasteKey();
- }
- catch(err) {
- result = true;
- }
- $.PrivateBin.Model.reset();
- clean();
- return result;
- }
- );
- });
- describe('getTemplate', function () {
- beforeEach(function () {
- $.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;
- }
- );
- });
- });
|