Commit dc168217 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #207 from hypothesis/new-update-preferences

Use new profile update endpoint for dismissing sidebar tutorial
parents 97839e63 2450954f
...@@ -20,11 +20,6 @@ function sessionActions(options) { ...@@ -20,11 +20,6 @@ function sessionActions(options) {
}, },
_load: { method: 'GET' }, _load: { method: 'GET' },
dismiss_sidebar_tutorial: {
method: 'POST',
params: { path: 'dismiss_sidebar_tutorial' },
},
}; };
Object.keys(actions).forEach(function (action) { Object.keys(actions).forEach(function (action) {
...@@ -89,7 +84,7 @@ function session($http, $resource, $rootScope, annotationUI, auth, ...@@ -89,7 +84,7 @@ function session($http, $resource, $rootScope, annotationUI, auth,
authority = settings.services[0].authority; authority = settings.services[0].authority;
} }
if (authority) { if (authority) {
return store.profile({authority: authority}).then(update); return store.profile.read({authority: authority}).then(update);
} else { } else {
return resource._load().$promise; return resource._load().$promise;
} }
...@@ -104,6 +99,16 @@ function session($http, $resource, $rootScope, annotationUI, auth, ...@@ -104,6 +99,16 @@ function session($http, $resource, $rootScope, annotationUI, auth,
return lastLoad; return lastLoad;
}; };
/**
* @name session.dismissSidebarTutorial()
*
* @description Stores the preference server-side that the user dismissed
* the sidebar tutorial, and then updates the session state.
*/
function dismissSidebarTutorial() {
return store.profile.update({}, {preferences: {show_sidebar_tutorial: false}}).then(update);
}
/** /**
* @name session.update() * @name session.update()
* *
...@@ -203,7 +208,7 @@ function session($http, $resource, $rootScope, annotationUI, auth, ...@@ -203,7 +208,7 @@ function session($http, $resource, $rootScope, annotationUI, auth,
} }
return { return {
dismissSidebarTutorial: resource.dismiss_sidebar_tutorial, dismissSidebarTutorial: dismissSidebarTutorial,
load: resource.load, load: resource.load,
login: resource.login, login: resource.login,
logout: logout, logout: logout,
......
...@@ -143,7 +143,10 @@ function store($http, $q, auth, settings) { ...@@ -143,7 +143,10 @@ function store($http, $q, auth, settings) {
get: apiCall('annotation.read'), get: apiCall('annotation.read'),
update: apiCall('annotation.update'), update: apiCall('annotation.update'),
}, },
profile: apiCall('profile.read'), profile: {
read: apiCall('profile.read'),
update: apiCall('profile.update'),
},
}; };
} }
......
...@@ -43,7 +43,10 @@ describe('session', function () { ...@@ -43,7 +43,10 @@ describe('session', function () {
setUserInfo: sandbox.spy(), setUserInfo: sandbox.spy(),
}; };
fakeStore = { fakeStore = {
profile: sandbox.stub(), profile: {
read: sandbox.stub(),
update: sandbox.stub().returns(Promise.resolve({})),
},
}; };
fakeSettings = { fakeSettings = {
serviceUrl: 'https://test.hypothes.is/root/', serviceUrl: 'https://test.hypothes.is/root/',
...@@ -168,14 +171,14 @@ describe('session', function () { ...@@ -168,14 +171,14 @@ describe('session', function () {
authority: 'publisher.org', authority: 'publisher.org',
grantToken: 'a.jwt.token', grantToken: 'a.jwt.token',
}]; }];
fakeStore.profile.returns(Promise.resolve({ fakeStore.profile.read.returns(Promise.resolve({
userid: 'acct:user@publisher.org', userid: 'acct:user@publisher.org',
})); }));
}); });
it('should fetch profile data from the API', function () { it('should fetch profile data from the API', function () {
return session.load().then(function () { return session.load().then(function () {
assert.calledWith(fakeStore.profile, {authority: 'publisher.org'}); assert.calledWith(fakeStore.profile.read, {authority: 'publisher.org'});
}); });
}); });
...@@ -290,11 +293,21 @@ describe('session', function () { ...@@ -290,11 +293,21 @@ describe('session', function () {
}); });
describe('#dismissSidebarTutorial()', function () { describe('#dismissSidebarTutorial()', function () {
var url = 'https://test.hypothes.is/root/app/dismiss_sidebar_tutorial'; beforeEach(function () {
fakeStore.profile.update.returns(Promise.resolve({
preferences: {},
}));
});
it('disables the tutorial for the user', function () { it('disables the tutorial for the user', function () {
$httpBackend.expectPOST(url).respond({});
session.dismissSidebarTutorial(); session.dismissSidebarTutorial();
$httpBackend.flush(); assert.calledWith(fakeStore.profile.update, {}, {preferences: {show_sidebar_tutorial: false}});
});
it('should update the session with the response from the API', function () {
return session.dismissSidebarTutorial().then(function () {
assert.isNotOk(session.state.preferences.show_sidebar_tutorial);
});
}); });
}); });
......
...@@ -76,6 +76,10 @@ describe('store', function () { ...@@ -76,6 +76,10 @@ describe('store', function () {
method: 'GET', method: 'GET',
url: 'http://example.com/api/profile', url: 'http://example.com/api/profile',
}, },
update: {
method: 'PATCH',
url: 'http://example.com/api/profile',
},
}, },
}, },
}); });
...@@ -150,7 +154,7 @@ describe('store', function () { ...@@ -150,7 +154,7 @@ describe('store', function () {
it("fetches the user's profile", function (done) { it("fetches the user's profile", function (done) {
var profile = {userid: 'acct:user@publisher.org'}; var profile = {userid: 'acct:user@publisher.org'};
store.profile({authority: 'publisher.org'}).then(function (profile_) { store.profile.read({authority: 'publisher.org'}).then(function (profile_) {
assert.deepEqual(profile_, profile); assert.deepEqual(profile_, profile);
done(); done();
}); });
...@@ -158,4 +162,16 @@ describe('store', function () { ...@@ -158,4 +162,16 @@ describe('store', function () {
.respond(function () { return [200, profile, {}]; }); .respond(function () { return [200, profile, {}]; });
$httpBackend.flush(); $httpBackend.flush();
}); });
it("updates a user's profile", function (done) {
store.profile.update({}, {preferences: {}}).then(function () {
done();
});
$httpBackend.expectPATCH('http://example.com/api/profile')
.respond(function () {
return [200, {}, {}];
});
$httpBackend.flush();
});
}); });
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