From fdef8bc5be89bbd82ad00af9794748893301c290 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 29 Jan 2017 14:31:44 +0100 Subject: [PATCH] starting to work on JSVerify & Mocha based unit tests for our JS code base --- .gitattributes | 4 +++ .gitignore | 1 + js/.istanbul.yml | 7 +++++ js/test.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ tst/README.md | 48 +++++++++++++++++++++++++---- tst/phpunit.xml | 2 +- 6 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 js/.istanbul.yml create mode 100644 js/test.js diff --git a/.gitattributes b/.gitattributes index e26be35..daef8b1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,9 @@ doc/ export-ignore tst/ export-ignore +js/.istanbul.yml export-ignore +js/test.js export-ignore +js/mocha-3.2.0.js export-ignore +css/mocha-3.2.0.css export-ignore .codeclimate.yml export-ignore .csslintrc export-ignore .dockerignore export-ignore diff --git a/.gitignore b/.gitignore index dfecbe3..9bfc991 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ vendor/**/build_phar.php composer.lock # Ignore unit testing logs, api docs and eclipse project files +js/node_modules/ tst/log/ .settings .buildpath diff --git a/js/.istanbul.yml b/js/.istanbul.yml new file mode 100644 index 0000000..bf6e067 --- /dev/null +++ b/js/.istanbul.yml @@ -0,0 +1,7 @@ +--- +instrumentation: + excludes: + - jquery-3.1.1.js + baseline-file: ../tst/log/js-coverage-baseline.json +reporting: + dir: ../tst/log/js-coverage-report diff --git a/js/test.js b/js/test.js new file mode 100644 index 0000000..399a856 --- /dev/null +++ b/js/test.js @@ -0,0 +1,78 @@ +'use strict'; +var jsc = require('jsverify'); + +before(function () { + this.jsdom = require('jsdom-global')(); + global.$ = global.jQuery = require('./jquery-3.1.1'); + global.sjcl = require('./sjcl-1.0.4'); + global.Base64 = require('./base64-2.1.9'); + global.RawDeflate = require('./rawdeflate-0.5'); + require('./rawinflate-0.3'); + require('./privatebin'); +}) + +after(function () { + this.jsdom(); +}) + +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'; + }); + }); + + describe('hashToParameterString', function () { + jsc.property('returns strings', 'dict nestring', function (dict) { + return typeof $.PrivateBin.helper.hashToParameterString(dict) === 'string'; + }); + }); + + describe('parameterStringToHash', function () { + jsc.property('returns objects', 'string', function (string) { + return typeof $.PrivateBin.helper.parameterStringToHash(string) === 'object'; + }); + jsc.property('decodes hashes generated with hashToParameterString', 'dict nestring', function (dict) { + var result = $.PrivateBin.helper.parameterStringToHash($.PrivateBin.helper.hashToParameterString(dict)); + // padding for URL shorteners + dict.p = 'p'; + return JSON.stringify(result) === JSON.stringify(dict); + }); + }); +}); + diff --git a/tst/README.md b/tst/README.md index 07564fc..76e69ee 100644 --- a/tst/README.md +++ b/tst/README.md @@ -1,20 +1,56 @@ -Running unit tests -================== +Running PHP unit tests +====================== In order to run these tests, you will need to install the following packages and its dependencies: * phpunit * php-gd * php-sqlite3 -* php-xdebug +* php-xdebug (for code coverage reports) Example for Debian and Ubuntu: -```sh -$ sudo aptitude install phpunit php-gd php-sqlite php-xdebug +```console +$ sudo apt install phpunit php-gd php-sqlite php-xdebug ``` To run the tests, just change into this directory and run phpunit: -```sh +```console $ cd PrivateBin/tst $ phpunit ``` + +Running JavaScript unit tests +============================= + +In order to run these tests, you will need to install the following packages +and its dependencies: +* npm + +Then you can use the node package manager to install the latest stable release +of mocha and istanbul (for code coverage reports) globally and jsVerify, jsdom +and jsdom-global locally: + +```console +$ npm install -g mocha istanbul +$ cd PrivateBin/js +$ npm install jsverify jsdom jsdom-global +``` + +Example for Debian and Ubuntu, including steps to allow current user to install +node modules globally: +```console +$ sudo apt install npm +$ sudo mkdir /usr/local/lib/node_modules +$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share} +$ ln -s /usr/bin/nodejs /usr/local/bin/node +$ npm install -g mocha istanbul +$ cd PrivateBin/js +$ npm install jsverify jsdom jsdom-global +``` + +To run the tests, just change into the `js` directory and run istanbul: +```console +$ cd PrivateBin/js +$ istanbul cover _mocha +``` + diff --git a/tst/phpunit.xml b/tst/phpunit.xml index 32cb66e..cb5606b 100644 --- a/tst/phpunit.xml +++ b/tst/phpunit.xml @@ -13,7 +13,7 @@ - +