From 5b5e37c71c68b2f34671c463e222df23c476d4d6 Mon Sep 17 00:00:00 2001 From: James West Date: Thu, 2 Jun 2016 15:34:50 +1200 Subject: [PATCH 1/6] Proof of concept for account profiles. --- index.js | 2 +- lib/sign-in.js | 3 ++- lib/sign-up.js | 4 ---- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index b78bcb1..340f707 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ function Account (options) { update: require('./lib/update').bind(null, state), profile: { get: require('./lib/profile-get').bind(null, state), - fetch: require('./lib/profile-fetch').bind(null, state, 'account.profile'), + fetch: require('./lib/profile-fetch').bind(null, state), update: require('./lib/profile-update').bind(null, state) }, request: require('./lib/request').bind(null, state), diff --git a/lib/sign-in.js b/lib/sign-in.js index d9816f5..6f75704 100644 --- a/lib/sign-in.js +++ b/lib/sign-in.js @@ -38,7 +38,7 @@ function signIn (state, options) { .then(function (response) { var data = internals.deserialise(response.body, { - include: 'account' + include: 'account.profile' }) // admins don’t have an account @@ -56,6 +56,7 @@ function signIn (state, options) { state.account = { username: data.account.username, + profile: data.account.profile || {}, session: { id: data.id } diff --git a/lib/sign-up.js b/lib/sign-up.js index 8842249..ceb64d1 100644 --- a/lib/sign-up.js +++ b/lib/sign-up.js @@ -19,10 +19,6 @@ function signUp (state, options) { return Promise.reject(error) } - if (options.profile) { - return Promise.reject(new Error('SignUp with profile data not yet implemented. Please see https://github.com/hoodiehq/hoodie-account-client/issues/11.')) - } - return internals.request({ url: state.url + '/session/account', method: 'PUT', From c7ecde088b26c8a0c5368030eeac3abc5582c20f Mon Sep 17 00:00:00 2001 From: James West Date: Wed, 15 Jun 2016 15:45:39 +1200 Subject: [PATCH 2/6] Profile is now only included when requested --- lib/sign-in.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/sign-in.js b/lib/sign-in.js index 6f75704..9991e48 100644 --- a/lib/sign-in.js +++ b/lib/sign-in.js @@ -56,7 +56,6 @@ function signIn (state, options) { state.account = { username: data.account.username, - profile: data.account.profile || {}, session: { id: data.id } @@ -66,6 +65,10 @@ function signIn (state, options) { state.account.id = data.account.id } + if (options.include === 'account.profile') { + state.account.profile = data.account.profile || {} + } + internals.saveAccount({ cacheKey: state.cacheKey, account: state.account From c278f6f049a2346ce08bd7d0a7c5b739b0c92f83 Mon Sep 17 00:00:00 2001 From: James West Date: Wed, 15 Jun 2016 15:45:51 +1200 Subject: [PATCH 3/6] Tests now pass --- test/integration/events-test.js | 12 ++++++++---- test/integration/sign-in-test.js | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/test/integration/events-test.js b/test/integration/events-test.js index 2207fbf..327d0ca 100644 --- a/test/integration/events-test.js +++ b/test/integration/events-test.js @@ -11,7 +11,8 @@ var signInResponse = require('../fixtures/signin.json') var updateResponse = require('../fixtures/update.json') var options = { username: signUpResponse.data.attributes.username, - password: 'secret' + password: 'secret', + include: 'account.profile' } test('events', function (t) { @@ -71,7 +72,8 @@ test('events', function (t) { t.deepEqual(signInHandler.lastCall.arg, { id: 'abc4567', username: 'chicken@docs.com', - session: { id: 'sessionid123' } + session: { id: 'sessionid123' }, + profile: {} }, '"signin" event emitted with account object') return account.update({username: updateResponse.data.attributes.username}) @@ -86,7 +88,8 @@ test('events', function (t) { t.deepEqual(updateHandler.lastCall.arg, { id: 'abc4567', username: 'newchicken@docs.com', - session: { id: 'sessionid123' } + session: { id: 'sessionid123' }, + profile: {} }, '"update" event emitted with account object') return account.signOut() @@ -96,7 +99,8 @@ test('events', function (t) { t.deepEqual(signOutHandler.lastCall.arg, { id: 'abc4567', username: 'newchicken@docs.com', - session: { id: 'sessionid123' } + session: { id: 'sessionid123' }, + profile: {} }, '"signout" event emitted with account object') t.is(signUpHandler.callCount, 1, '"signup" event emitted once') diff --git a/test/integration/sign-in-test.js b/test/integration/sign-in-test.js index 109cadb..de36bda 100644 --- a/test/integration/sign-in-test.js +++ b/test/integration/sign-in-test.js @@ -11,7 +11,8 @@ var signInResponse = clone(require('../fixtures/signin.json')) var options = { username: 'chicken@docs.com', - password: 'secret' + password: 'secret', + include: 'account.profile' } test('sign in', function (t) { @@ -43,14 +44,16 @@ test('sign in', function (t) { username: 'chicken@docs.com', session: { id: 'sessionid123' - } + }, + profile: {} }, 'stores account with id with session') t.deepEqual(account.get(), { id: 'abc4567', username: 'chicken@docs.com', session: { id: 'sessionid123' - } + }, + profile: {} }, '.get() returns account with session') return account.signOut() From 7ecd2ff42b55995a4a37301d4f59e1897ea1ba39 Mon Sep 17 00:00:00 2001 From: James West Date: Wed, 15 Jun 2016 16:07:58 +1200 Subject: [PATCH 4/6] Fixed data not being included on login --- lib/sign-in.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/sign-in.js b/lib/sign-in.js index 9991e48..c4407ac 100644 --- a/lib/sign-in.js +++ b/lib/sign-in.js @@ -30,7 +30,7 @@ function signIn (state, options) { .then(function () { return internals.request({ - url: state.url + '/session', + url: state.url + '/session' + query(options), method: 'PUT', body: internals.serialise('session', options) }) @@ -94,3 +94,11 @@ function signIn (state, options) { }) }) } + +function query (options) { + if (!options || options.include !== 'account.profile') { + return '' + } + + return '?include=account.profile' +} From 910014002622178c20e99f8438c48770133cf325 Mon Sep 17 00:00:00 2001 From: James West Date: Wed, 15 Jun 2016 16:18:15 +1200 Subject: [PATCH 5/6] Added profile include to events tests --- test/integration/events-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/events-test.js b/test/integration/events-test.js index 327d0ca..6bbf682 100644 --- a/test/integration/events-test.js +++ b/test/integration/events-test.js @@ -25,7 +25,7 @@ test('events', function (t) { nock(baseURL) .put('/session/account') .reply(201, signUpResponse) - .put('/session').thrice() + .put('/session?include=account.profile').thrice() .reply(201, signInResponse) .patch('/session/account') .reply(201, updateResponse) From a938cdc8c26bb8b166f2fff06171d10596c22f38 Mon Sep 17 00:00:00 2001 From: James West Date: Wed, 15 Jun 2016 17:11:51 +1200 Subject: [PATCH 6/6] Fixed PATCH /session/account/profile --- lib/profile-update.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/profile-update.js b/lib/profile-update.js index 46b990c..3618308 100644 --- a/lib/profile-update.js +++ b/lib/profile-update.js @@ -20,7 +20,7 @@ function updateProfile (state, options) { headers: { authorization: 'Bearer ' + state.account.session.id }, - body: internals.serialise('profile', options, state.account.profile.id) + body: internals.serialise('profile', options, state.account.id + '-profile') }) .then(function () {