Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds linter and basic tests #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "airbnb/base",
"parser": "babel-eslint"
}
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
33 changes: 33 additions & 0 deletions tests/encode_decode.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 6 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var webpack = require('webpack');
var path = require('path');

var baseConfig = {
entry: {
Expand All @@ -24,6 +25,11 @@ var baseConfig = {
presets: ['es2015']
},
},
{
test: /\.js$/,
loader: 'eslint-loader',
exclude: /node_modules/
}
],
},

Expand Down