-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement and test token authorization
- Loading branch information
1 parent
432683e
commit 6f6610d
Showing
5 changed files
with
108 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})); | ||
}); | ||
}); | ||
|