Unverified Commit cc89f24c authored by Robert Knight's avatar Robert Knight Committed by Sean Hammond

Re-implement several tests related to fetching of profile data

These tests previously existed for the cookie-based auth but needed to
be re-implemented following the switch-over to OAuth.
parent 6cc127f1
...@@ -32,6 +32,9 @@ function session($q, $rootScope, analytics, annotationUI, auth, ...@@ -32,6 +32,9 @@ function session($q, $rootScope, analytics, annotationUI, auth,
return service.authority; return service.authority;
} }
// Options to pass to `retry.operation` when fetching the user's profile.
var profileFetchRetryOpts = {};
/** /**
* @name session.load() * @name session.load()
* @description Fetches the session data from the server. * @description Fetches the session data from the server.
...@@ -58,7 +61,7 @@ function session($q, $rootScope, analytics, annotationUI, auth, ...@@ -58,7 +61,7 @@ function session($q, $rootScope, analytics, annotationUI, auth,
opts.authority = authority; opts.authority = authority;
} }
return store.profile.read(opts); return store.profile.read(opts);
}).then(function (session) { }, profileFetchRetryOpts).then(function (session) {
update(session); update(session);
lastLoadTime = Date.now(); lastLoadTime = Date.now();
return session; return session;
...@@ -154,6 +157,9 @@ function session($q, $rootScope, analytics, annotationUI, auth, ...@@ -154,6 +157,9 @@ function session($q, $rootScope, analytics, annotationUI, auth,
logout, logout,
reload, reload,
// Exposed for use in tests
profileFetchRetryOpts,
// For the moment, we continue to expose the session state as a property on // For the moment, we continue to expose the session state as a property on
// this service. In future, other services which access the session state // this service. In future, other services which access the session state
// will do so directly from annotationUI or via selector functions // will do so directly from annotationUI or via selector functions
......
...@@ -92,7 +92,7 @@ describe('sidebar.session', function () { ...@@ -92,7 +92,7 @@ describe('sidebar.session', function () {
})); }));
}); });
it('should fetch profile data from the API', function () { it('should pass the "authority" param when fetching the profile', function () {
return session.load().then(function () { return session.load().then(function () {
assert.calledWith(fakeStore.profile.read, {authority: 'publisher.org'}); assert.calledWith(fakeStore.profile.read, {authority: 'publisher.org'});
}); });
...@@ -105,7 +105,7 @@ describe('sidebar.session', function () { ...@@ -105,7 +105,7 @@ describe('sidebar.session', function () {
}); });
}); });
context('when using OAuth for first-party accounts', () => { context('when using a first party account', () => {
beforeEach(() => { beforeEach(() => {
fakeStore.profile.read.returns(Promise.resolve({ fakeStore.profile.read.returns(Promise.resolve({
userid: 'acct:user@hypothes.is', userid: 'acct:user@hypothes.is',
...@@ -118,11 +118,47 @@ describe('sidebar.session', function () { ...@@ -118,11 +118,47 @@ describe('sidebar.session', function () {
}); });
}); });
it('should retry the profile fetch if it fails', () => {
fakeStore.profile.read.onCall(0).returns(
Promise.reject(new Error('Server error'))
);
fakeStore.profile.read.onCall(1).returns(
Promise.resolve({userid: 'acct:user@hypothes.is', groups: []})
);
// Shorten the delay before retrying the fetch.
session.profileFetchRetryOpts.minTimeout = 50;
return session.load().then(() => {
assert.equal(session.state.userid, 'acct:user@hypothes.is');
});
});
it('should update the session with the profile data from the API', () => { it('should update the session with the profile data from the API', () => {
return session.load().then(function () { return session.load().then(function () {
assert.equal(session.state.userid, 'acct:user@hypothes.is'); assert.equal(session.state.userid, 'acct:user@hypothes.is');
}); });
}); });
it('should cache the returned profile data', () => {
return session.load().then(() => {
return session.load();
}).then(() => {
assert.calledOnce(fakeStore.profile.read);
});
});
it('should eventually expire the cache', () => {
var clock = sinon.useFakeTimers();
var CACHE_TTL = 5 * 60 * 1000; // 5 minutes
return session.load().then(() => {
clock.tick(CACHE_TTL * 2);
return session.load();
}).then(() => {
assert.calledTwice(fakeStore.profile.read);
});
});
}); });
}); });
......
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