From 8d99fcafcb0c9c6f870df5d95c57b8bcf2a6dcae Mon Sep 17 00:00:00 2001 From: Ernesto Rocha Date: Tue, 19 Apr 2016 19:39:36 -0300 Subject: [PATCH] Adds linter and basic tests --- .babelrc | 3 +++ .eslintignore | 1 + .eslintrc | 4 ++++ package.json | 14 +++++++++++--- tests/encode_decode.spec.js | 33 +++++++++++++++++++++++++++++++++ webpack.config.js | 6 ++++++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 .babelrc create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 tests/encode_decode.spec.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..a0765e1 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..849ddff --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +dist/ diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..fc5b237 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,4 @@ +{ + "extends": "airbnb/base", + "parser": "babel-eslint" +} diff --git a/package.json b/package.json index a72e14f..c472c67 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,24 @@ "description": "Algorithm used on TornadoWeb to encode/decode session.", "main": "dist/tornado-auth.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "./node_modules/.bin/mocha --compilers js:babel-core/register ./tests/**/*.spec.js", "build": "webpack", - "watch": "webpack --watch" + "watch": "webpack --watch", + "lint": "./node_modules/.bin/eslint **/*.js" }, "author": "", "license": "ISC", "devDependencies": { - "babel-core": "^6.4.5", + "babel-core": "^6.7.6", + "babel-eslint": "^6.0.2", "babel-loader": "^6.1.0", "babel-preset-es2015": "^6.3.13", + "chai": "^3.5.0", + "eslint": "^2.8.0", + "eslint-config-airbnb": "^7.0.0", + "eslint-loader": "^1.3.0", + "mocha": "^2.4.5", + "sinon": "^1.17.3", "webpack": "^1.12.11" }, "dependencies": { diff --git a/tests/encode_decode.spec.js b/tests/encode_decode.spec.js new file mode 100644 index 0000000..ff34a73 --- /dev/null +++ b/tests/encode_decode.spec.js @@ -0,0 +1,33 @@ +/* globals it, describe */ +import { assert } from 'chai'; +import { configure, decode_signed_value, create_signed_value } from '../index'; +import sinon from 'sinon'; + +const secret = 'MY-SUPER-SECRET-KEY'; +const cookieName = 'cookie-name'; + +configure({ + secret_key: secret, + user_cookie: cookieName, +}); + +const timestamp = 1461104071; // const date = new Date('19 Apr 2016 19:14:31'); +const encodedValue = 'eyJwaWQiOjMwMTE3LCJ1aWQiOjMwMTE4fQ==|1461104071|e08a24c780b74a7d6722d2e467abd4a8dc38b48b'; +const value = { pid: 30117, uid: 30118 }; + +describe('Basic test for encoding and decoding any value', () => { + it('should encode any value', () => { + const clock = sinon.useFakeTimers(timestamp * 1000); + const signedValue = create_signed_value(value); + + assert.equal(encodedValue, signedValue); + + clock.restore(); + }); + + it('should decode to the right value', () => { + const decodedValue = decode_signed_value(secret, cookieName, encodedValue); + + assert.deepEqual(JSON.parse(decodedValue), value); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index 04a5e00..ef92fd8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,4 +1,5 @@ var webpack = require('webpack'); +var path = require('path'); var baseConfig = { entry: { @@ -24,6 +25,11 @@ var baseConfig = { presets: ['es2015'] }, }, + { + test: /\.js$/, + loader: 'eslint-loader', + exclude: /node_modules/ + } ], },