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) {
},
_load: { method: 'GET' },
dismiss_sidebar_tutorial: {
method: 'POST',
params: { path: 'dismiss_sidebar_tutorial' },
},
};
Object.keys(actions).forEach(function (action) {
......@@ -89,7 +84,7 @@ function session($http, $resource, $rootScope, annotationUI, auth,
authority = settings.services[0].authority;
}
if (authority) {
return store.profile({authority: authority}).then(update);
return store.profile.read({authority: authority}).then(update);
} else {
return resource._load().$promise;
}
......@@ -104,6 +99,16 @@ function session($http, $resource, $rootScope, annotationUI, auth,
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()
*
......@@ -203,7 +208,7 @@ function session($http, $resource, $rootScope, annotationUI, auth,
}
return {
dismissSidebarTutorial: resource.dismiss_sidebar_tutorial,
dismissSidebarTutorial: dismissSidebarTutorial,
load: resource.load,
login: resource.login,
logout: logout,
......
......@@ -143,7 +143,10 @@ function store($http, $q, auth, settings) {
get: apiCall('annotation.read'),
update: apiCall('annotation.update'),
},
profile: apiCall('profile.read'),
profile: {
read: apiCall('profile.read'),
update: apiCall('profile.update'),
},
};
}
......
......@@ -43,7 +43,10 @@ describe('session', function () {
setUserInfo: sandbox.spy(),
};
fakeStore = {
profile: sandbox.stub(),
profile: {
read: sandbox.stub(),
update: sandbox.stub().returns(Promise.resolve({})),
},
};
fakeSettings = {
serviceUrl: 'https://test.hypothes.is/root/',
......@@ -168,14 +171,14 @@ describe('session', function () {
authority: 'publisher.org',
grantToken: 'a.jwt.token',
}];
fakeStore.profile.returns(Promise.resolve({
fakeStore.profile.read.returns(Promise.resolve({
userid: 'acct:user@publisher.org',
}));
});
it('should fetch profile data from the API', 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 () {
});
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 () {
$httpBackend.expectPOST(url).respond({});
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 () {
method: 'GET',
url: 'http://example.com/api/profile',
},
update: {
method: 'PATCH',
url: 'http://example.com/api/profile',
},
},
},
});
......@@ -150,7 +154,7 @@ describe('store', function () {
it("fetches the user's profile", function (done) {
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);
done();
});
......@@ -158,4 +162,16 @@ describe('store', function () {
.respond(function () { return [200, profile, {}]; });
$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