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 () {
});
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,49 +468,52 @@ describe('sidebar.oauth-auth', function () {
});
});
});
});
context('when another client instance saves new tokens', () => {
function notifyStoredTokenChange() {
// Trigger "storage" event as if another client refreshed the token.
var storageEvent = new Event('storage');
storageEvent.key = TOKEN_KEY;
context('when another client instance saves new tokens', () => {
beforeEach(() => {
fakeSettings.services = [];
});
fakeLocalStorage.getObject.returns({
accessToken: 'storedAccessToken',
refreshToken: 'storedRefreshToken',
expiresAt: Date.now() + 100,
});
function notifyStoredTokenChange() {
// Trigger "storage" event as if another client refreshed the token.
var storageEvent = new Event('storage');
storageEvent.key = TOKEN_KEY;
fakeWindow.trigger(storageEvent);
}
fakeLocalStorage.getObject.returns({
accessToken: 'storedAccessToken',
refreshToken: 'storedRefreshToken',
expiresAt: Date.now() + 100,
});
it('reloads tokens from storage', () => {
return login().then(() => {
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'firstAccessToken');
fakeWindow.trigger(storageEvent);
}
notifyStoredTokenChange();
it('reloads tokens from storage', () => {
return login().then(() => {
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'firstAccessToken');
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'storedAccessToken');
});
notifyStoredTokenChange();
return auth.tokenGetter();
}).then(token => {
assert.equal(token, 'storedAccessToken');
});
});
it('notifies other services about the change', () => {
var onTokenChange = sinon.stub();
$rootScope.$on(events.OAUTH_TOKENS_CHANGED, onTokenChange);
it('notifies other services about the change', () => {
var onTokenChange = sinon.stub();
$rootScope.$on(events.OAUTH_TOKENS_CHANGED, onTokenChange);
notifyStoredTokenChange();
notifyStoredTokenChange();
assert.called(onTokenChange);
});
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