Skip to content

Commit

Permalink
Implement and test token authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
jkintscher committed Sep 21, 2015
1 parent 432683e commit 6f6610d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 48 deletions.
25 changes: 15 additions & 10 deletions src/lib/authentication.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import url from 'url';
import location from './location';

var AUTH_TOKEN;
let AUTH_TOKEN;

export function authorize() {
let uri = url.parse(global.location, true);
AUTH_TOKEN = uri.query.auth_token || undefined;
export default {
token() {
return AUTH_TOKEN;
},
authorize() {
let uri = location.parse();
AUTH_TOKEN = uri.query.auth_token || undefined;

if (!AUTH_TOKEN) { return false; }
if (!AUTH_TOKEN) { return false; }

delete uri.query.auth_token;
global.history.replaceState({}, '', url.format(uri));
delete uri.query.auth_token;
location.replace(uri);

return true;
}
return true;
}
};
10 changes: 10 additions & 0 deletions src/lib/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import url from 'url';

export default {
parse() {
return url.parse(global.location.href, true);
},
replace(new_location) {
global.history.replaceState({}, '', url.format(new_location));
}
};
38 changes: 0 additions & 38 deletions test/authentication_test.js

This file was deleted.

51 changes: 51 additions & 0 deletions test/lib/authentication_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from 'chai';
import { test } from 'sinon';

import Authentication from '../../src/lib/authentication';

// Internal dependencies to stub
import location from '../../src/lib/location';

describe('Authentication', () => {
before(function() {
if (!global.location) {
global.location = { href: 'http://ti.test/' };
}
});

describe('#authorize', () => {
it('succeeds when auth_token param exists', test(function() {
this.stub(location, 'parse', str => ({ query: { auth_token: 'secret'} }));
this.stub(location, 'replace');
expect(Authentication.authorize()).to.equal(true);
}));

it('fails when auth_token is missing', test(function() {
this.stub(location, 'parse', str => ({ query: {} }));
expect(Authentication.authorize()).to.equal(false);
}));

it('removes auth_token param from URL without modifying history', test(function() {
let url = { host: 'foo.bar/', query: { auth_token: 'secret'} };
this.stub(location, 'parse').returns(url);
this.mock(location).expects('replace')
.withExactArgs({ host: 'foo.bar/', query: {} });
Authentication.authorize();
}));
});

describe('#token', () => {
it('is empty by default', test(function() {
Authentication.authorize();
expect(Authentication.token()).to.be.undefined
}));

it('stores successfully extracted auth token', test(function() {
let url = { query: { auth_token: 'secret'} };
this.stub(location, 'parse').returns(url);
this.stub(location, 'replace');
Authentication.authorize();
expect(Authentication.token()).to.equal('secret');
}));
});
});
32 changes: 32 additions & 0 deletions test/lib/location_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from 'sinon';

import location from '../../src/lib/location';

// Internal dependencies to stub
import url from 'url';

describe('Location', () => {

before(function() {
if (!global.location) { global.location = { href: 'http://ti.test/' }; }
if (!global.history) { global.history = { replaceState() {} }; }
});

describe('#parse', () => {
it('parses the URL', test(function() {
this.mock(url).expects('parse')
.withExactArgs(global.location.href, true);
location.parse();
}));
});

describe('#replace', () => {
it('changes URL without modifying history', test(function() {
let target = { host: 'foo.bar' };
this.mock(global.history).expects('replaceState')
.withExactArgs({}, '', '//foo.bar');
location.replace(target);
}));
});
});

0 comments on commit 6f6610d

Please sign in to comment.