Unverified Commit ed148af4 authored by Sean Hammond's avatar Sean Hammond Committed by GitHub

Merge pull request #573 from hypothesis/sort-oauth-tests

Re-arrange OAuth service tests
parents a27d83b9 8666f000
...@@ -169,7 +169,7 @@ describe('sidebar.oauth-auth', function () { ...@@ -169,7 +169,7 @@ describe('sidebar.oauth-auth', function () {
}); });
describe('#tokenGetter', 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) { return auth.tokenGetter().then(function (token) {
var expectedBody = var expectedBody =
'assertion=a.jwt.token' + 'assertion=a.jwt.token' +
...@@ -256,7 +256,7 @@ describe('sidebar.oauth-auth', function () { ...@@ -256,7 +256,7 @@ describe('sidebar.oauth-auth', function () {
return Promise.all(tokens); 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' }]; fakeSettings.services = [{ authority: 'publisher.org' }];
return auth.tokenGetter().then(function (token) { return auth.tokenGetter().then(function (token) {
assert.notCalled(fakeHttp.post); assert.notCalled(fakeHttp.post);
...@@ -352,14 +352,10 @@ describe('sidebar.oauth-auth', function () { ...@@ -352,14 +352,10 @@ describe('sidebar.oauth-auth', function () {
}); });
}); });
}); });
});
describe('persistence of tokens to storage', () => { it('persists tokens retrieved via auth code exchanges to storage', () => {
beforeEach(() => {
fakeSettings.services = []; fakeSettings.services = [];
});
it('persists tokens retrieved via auth code exchanges to storage', () => {
return login().then(() => { return login().then(() => {
return auth.tokenGetter(); return auth.tokenGetter();
}).then(() => { }).then(() => {
...@@ -372,6 +368,8 @@ describe('sidebar.oauth-auth', function () { ...@@ -372,6 +368,8 @@ describe('sidebar.oauth-auth', function () {
}); });
it('persists refreshed tokens to storage', () => { it('persists refreshed tokens to storage', () => {
fakeSettings.services = [];
// 1. Perform initial token exchange. // 1. Perform initial token exchange.
return login().then(() => { return login().then(() => {
return auth.tokenGetter(); return auth.tokenGetter();
...@@ -398,7 +396,9 @@ describe('sidebar.oauth-auth', function () { ...@@ -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({ fakeLocalStorage.getObject.withArgs(TOKEN_KEY).returns({
accessToken: 'foo', accessToken: 'foo',
refreshToken: 'bar', refreshToken: 'bar',
...@@ -410,7 +410,9 @@ describe('sidebar.oauth-auth', function () { ...@@ -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. // Store an expired access token.
clock.tick(200); clock.tick(200);
fakeLocalStorage.getObject.withArgs(TOKEN_KEY).returns({ fakeLocalStorage.getObject.withArgs(TOKEN_KEY).returns({
...@@ -458,6 +460,7 @@ describe('sidebar.oauth-auth', function () { ...@@ -458,6 +460,7 @@ describe('sidebar.oauth-auth', function () {
}].forEach(({ when, data }) => { }].forEach(({ when, data }) => {
context(when, () => { context(when, () => {
it('ignores invalid tokens in storage', () => { it('ignores invalid tokens in storage', () => {
fakeSettings.services = [];
fakeLocalStorage.getObject.withArgs('foo').returns(data); fakeLocalStorage.getObject.withArgs('foo').returns(data);
return auth.tokenGetter().then((token) => { return auth.tokenGetter().then((token) => {
assert.equal(token, null); assert.equal(token, null);
...@@ -465,49 +468,52 @@ describe('sidebar.oauth-auth', function () { ...@@ -465,49 +468,52 @@ describe('sidebar.oauth-auth', function () {
}); });
}); });
}); });
});
context('when another client instance saves new tokens', () => { context('when another client instance saves new tokens', () => {
function notifyStoredTokenChange() { beforeEach(() => {
// Trigger "storage" event as if another client refreshed the token. fakeSettings.services = [];
var storageEvent = new Event('storage'); });
storageEvent.key = TOKEN_KEY;
fakeLocalStorage.getObject.returns({ function notifyStoredTokenChange() {
accessToken: 'storedAccessToken', // Trigger "storage" event as if another client refreshed the token.
refreshToken: 'storedRefreshToken', var storageEvent = new Event('storage');
expiresAt: Date.now() + 100, storageEvent.key = TOKEN_KEY;
});
fakeWindow.trigger(storageEvent); fakeLocalStorage.getObject.returns({
} accessToken: 'storedAccessToken',
refreshToken: 'storedRefreshToken',
expiresAt: Date.now() + 100,
});
it('reloads tokens from storage', () => { fakeWindow.trigger(storageEvent);
return login().then(() => { }
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'firstAccessToken');
notifyStoredTokenChange(); it('reloads tokens from storage', () => {
return login().then(() => {
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'firstAccessToken');
return auth.tokenGetter(); notifyStoredTokenChange();
}).then(token => {
assert.equal(token, 'storedAccessToken'); return auth.tokenGetter();
}); }).then(token => {
assert.equal(token, 'storedAccessToken');
}); });
});
it('notifies other services about the change', () => { it('notifies other services about the change', () => {
var onTokenChange = sinon.stub(); var onTokenChange = sinon.stub();
$rootScope.$on(events.OAUTH_TOKENS_CHANGED, onTokenChange); $rootScope.$on(events.OAUTH_TOKENS_CHANGED, onTokenChange);
notifyStoredTokenChange(); notifyStoredTokenChange();
assert.called(onTokenChange); assert.called(onTokenChange);
});
}); });
}); });
describe('#login', () => { describe('#login', () => {
beforeEach(() => { beforeEach(() => {
// login() is only currently used when using the public // login() is only currently used when using the public
// Hypothesis service. // 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