Skip to content

Instantly share code, notes, and snippets.

@i-van
Created March 14, 2016 09:42
Show Gist options
  • Save i-van/c1a7e48f912b61c94746 to your computer and use it in GitHub Desktop.
Save i-van/c1a7e48f912b61c94746 to your computer and use it in GitHub Desktop.
'use strict';
let expect = require('chai').expect
, AuthService = require('../../../app/services/auth')
, policy = require('../../../app/policies/authenticated');
describe('app/policies/authenticated', function() {
it('returns auth info when token is valid', async function() {
let id = 1
, req = { headers: { authentication: await AuthService.generateToken(id) } }
, res = {};
await policy(req, res);
expect(req.auth.id).to.equal(id);
});
it('throws error when authentication header is empty', async function() {
let req = { headers: {} }
, res = {};
try {
await policy(req, res);
expect.fail();
} catch (err) {
expect(err).to.exist;
expect(err.message).to.equal('Access token required');
expect(err.status).to.equal(401);
}
});
it('throws error when token is incorrect', async function() {
let req = { headers: { authentication: 'incorrectToken' } }
, res = {};
try {
await policy(req, res);
expect.fail();
} catch (err) {
expect(err).to.exist;
expect(err.message).to.equal('Invalid token');
expect(err.status).to.equal(401);
}
});
it('throws error when token is expired', async function() {
let req = { headers: { authentication: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiaWF0IjoxNDU3MDIyMzIyLCJleHAiOjE0NTcwMjIzMjN9.IU_8N-I8oPOTqXe_T7Si5Lb8FZg0PRmALX9ZHyM4KZk' } }
, res = {};
try {
await policy(req, res);
expect.fail();
} catch (err) {
expect(err).to.exist;
expect(err.message).to.equal('Token Expired');
expect(err.status).to.equal(401);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment