Commit 8666f000 authored by Robert Knight's avatar Robert Knight

Re-arrange OAuth service tests

Move the tests for fetching and persistence of tokens from/to storage
into the `describe('#tokenGetter', ...` block since this is the method
called from outside the service that initiates the fetching /
persistence.

Also reword a couple of the test cases to be clearer now that the OAuth
service now handles fetching tokens using methods other than exchanging
grant tokens.
parent a27d83b9
......@@ -169,7 +169,7 @@ describe('sidebar.oauth-auth', function () {
});
describe('#tokenGetter', function () {
it('should request an access token if a grant token was provided', function () {
it('exchanges the grant token for an access token if provided', function () {
return auth.tokenGetter().then(function (token) {
var expectedBody =
'assertion=a.jwt.token' +
......@@ -256,7 +256,7 @@ describe('sidebar.oauth-auth', function () {
return Promise.all(tokens);
});
it('should return null if no grant token was provided', function () {
it('should not attempt to exchange a grant token if none was provided', function () {
fakeSettings.services = [{ authority: 'publisher.org' }];
return auth.tokenGetter().then(function (token) {
assert.notCalled(fakeHttp.post);
......@@ -352,14 +352,10 @@ describe('sidebar.oauth-auth', function () {
});
});
});
});
describe('persistence of tokens to storage', () => {
beforeEach(() => {
it('persists tokens retrieved via auth code exchanges to storage', () => {
fakeSettings.services = [];
});
it('persists tokens retrieved via auth code exchanges to storage', () => {
return login().then(() => {
return auth.tokenGetter();
}).then(() => {
......@@ -372,6 +368,8 @@ describe('sidebar.oauth-auth', function () {
});
it('persists refreshed tokens to storage', () => {
fakeSettings.services = [];
// 1. Perform initial token exchange.
return login().then(() => {
return auth.tokenGetter();
......@@ -398,7 +396,9 @@ describe('sidebar.oauth-auth', function () {
});
});
it('loads and uses tokens from storage', () => {
it('fetches and returns tokens from storage', () => {
fakeSettings.services = [];
fakeLocalStorage.getObject.withArgs(TOKEN_KEY).returns({
accessToken: 'foo',
refreshToken: 'bar',
......@@ -410,7 +410,9 @@ describe('sidebar.oauth-auth', function () {
});
});
it('refreshes the token if it expired after loading from storage', () => {
it('refreshes expired tokens loaded from storage', () => {
fakeSettings.services = [];
// Store an expired access token.
clock.tick(200);
fakeLocalStorage.getObject.withArgs(TOKEN_KEY).returns({
......@@ -458,6 +460,7 @@ describe('sidebar.oauth-auth', function () {
}].forEach(({ when, data }) => {
context(when, () => {
it('ignores invalid tokens in storage', () => {
fakeSettings.services = [];
fakeLocalStorage.getObject.withArgs('foo').returns(data);
return auth.tokenGetter().then((token) => {
assert.equal(token, null);
......@@ -465,8 +468,13 @@ describe('sidebar.oauth-auth', function () {
});
});
});
});
context('when another client instance saves new tokens', () => {
beforeEach(() => {
fakeSettings.services = [];
});
function notifyStoredTokenChange() {
// Trigger "storage" event as if another client refreshed the token.
var storageEvent = new Event('storage');
......@@ -504,10 +512,8 @@ describe('sidebar.oauth-auth', function () {
assert.called(onTokenChange);
});
});
});
describe('#login', () => {
beforeEach(() => {
// login() is only currently used when using the public
// Hypothesis service.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment