Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include profile #112

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function AccountAdmin (options) {
return getUsername(state)
},
signIn: function (options) {
// include defaults to account.profile, but admins have neither
options.include = ''

return signIn(state, options)

.then(function (session) {
Expand Down
5 changes: 3 additions & 2 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function fetch (state, path) {
return Promise.reject(error)
}
return internals.fetchProperties({
url: state.url + '/session/account',
url: state.url + '/session/account?include=profile',
sessionId: get(state, 'account.session.id'),
path: path
path: path,
include: 'profile'
})

.then(function (properties) {
Expand Down
20 changes: 18 additions & 2 deletions lib/sign-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function signIn (state, options) {
return Promise.reject(new Error('options.username and options.password is required'))
}

if (!options.hasOwnProperty('include')) {
options.include = 'account.profile'
}

var preHooks = []
// note: the `pre:signin` & `post:signin` events are not considered public
// APIs and might change in future without notice
Expand All @@ -30,15 +34,15 @@ function signIn (state, options) {

.then(function () {
return internals.request({
url: state.url + '/session',
url: sessionUrl(state, options),
method: 'PUT',
body: internals.serialise('session', options)
})
})

.then(function (response) {
var data = internals.deserialise(response.body, {
include: 'account'
include: 'account.profile'
})

// admins don’t have an account
Expand All @@ -65,6 +69,10 @@ function signIn (state, options) {
state.account.id = data.account.id
}

if (data.account.profile) {
state.account.profile = data.account.profile
}

internals.saveAccount({
cacheKey: state.cacheKey,
account: state.account
Expand All @@ -90,3 +98,11 @@ function signIn (state, options) {
})
})
}

function sessionUrl (state, options) {
if (options.include) {
return state.url + '/session?include=' + options.include
}

return state.url + '/session'
}
4 changes: 2 additions & 2 deletions test/fixtures/fetch-profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"id": "abc123-profile",
"type": "profile",
"attributes": {
"fullName": "Docs Chicken",
"favoriteClothing": "Hoodie"
"fullName": "Docs Chicken",
"favoriteClothing": "Hoodie"
}
}
}
12 changes: 11 additions & 1 deletion test/fixtures/fetch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
}
}
}
}
},
"included": [
{
"id": "abc4567-profile",
"type": "profile",
"attributes": {
"fullName": "Docs Chicken",
"favoriteClothing": "Hoodie"
}
}
]
}
8 changes: 8 additions & 0 deletions test/fixtures/signin.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
}
}
}
},
{
"id": "abc4567-profile",
"type": "profile",
"attributes": {
"fullName": "Docs Chicken",
"favoriteClothing": "Hoodie"
}
}
]
}
2 changes: 1 addition & 1 deletion test/integration/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('account.signIn() with invalid credentials', function (t) {
})

nock('http://localhost:3000')
.put('/session')
.put('/session?include=account.profile')
.reply(401, sessionUnauthorizedResponse)

account.signIn({
Expand Down
2 changes: 1 addition & 1 deletion test/integration/cachekey-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('new Account(options) defaults to "account" cacheKey', function (t) {
})

nock('http://localhost:3000')
.put('/session')
.put('/session?include=account.profile')
.reply(201, JSON.stringify(signInResponse))

return account.signIn({
Expand Down
8 changes: 5 additions & 3 deletions test/integration/destroy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('destroy account', function (t) {
})

nock(baseURL)
.put('/session')
.put('/session?include=account.profile')
.reply(201, signInResponse)
.delete('/session/account')
.reply(204)
Expand All @@ -46,13 +46,15 @@ test('destroy account', function (t) {
t.deepEqual(signOutHandler.lastCall.arg, {
id: 'abc4567',
username: '[email protected]',
session: { id: 'sessionid123' }
session: { id: 'sessionid123' },
profile: { favoriteClothing: 'Hoodie', fullName: 'Docs Chicken' }
}, '"signout" event emitted with account object')

t.deepEqual(destroyHandler.lastCall.arg, {
id: 'abc4567',
username: '[email protected]',
session: { id: 'sessionid123' }
session: { id: 'sessionid123' },
profile: { favoriteClothing: 'Hoodie', fullName: 'Docs Chicken' }
}, '"destroy" event emitted with account object')

t.is(signOutHandler.callCount, 1, '"signout" event emitted once')
Expand Down
11 changes: 7 additions & 4 deletions test/integration/events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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)
Expand Down Expand Up @@ -71,7 +71,8 @@ test('events', function (t) {
t.deepEqual(signInHandler.lastCall.arg, {
id: 'abc4567',
username: '[email protected]',
session: { id: 'sessionid123' }
session: { id: 'sessionid123' },
profile: { favoriteClothing: 'Hoodie', fullName: 'Docs Chicken' }
}, '"signin" event emitted with account object')

return account.update({username: updateResponse.data.attributes.username})
Expand All @@ -86,7 +87,8 @@ test('events', function (t) {
t.deepEqual(updateHandler.lastCall.arg, {
id: 'abc4567',
username: '[email protected]',
session: { id: 'sessionid123' }
session: { id: 'sessionid123' },
profile: { favoriteClothing: 'Hoodie', fullName: 'Docs Chicken' }
}, '"update" event emitted with account object')

return account.signOut()
Expand All @@ -96,7 +98,8 @@ test('events', function (t) {
t.deepEqual(signOutHandler.lastCall.arg, {
id: 'abc4567',
username: '[email protected]',
session: { id: 'sessionid123' }
session: { id: 'sessionid123' },
profile: { favoriteClothing: 'Hoodie', fullName: 'Docs Chicken' }
}, '"signout" event emitted with account object')

t.is(signUpHandler.callCount, 1, '"signup" event emitted once')
Expand Down
11 changes: 9 additions & 2 deletions test/integration/fetch-url-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ test('account.fetch() and account.profil.fetch()', function (t) {
})

var apiMock = nock('http://localhost:3000')
.get('/session/account')
.get('/session/account?include=profile')
.reply(200, require('../fixtures/fetch.json'))
.get('/session/account/profile')
.reply(200, require('../fixtures/fetch-profile.json'))

account.fetch()

.then(function (accountProperties) {
t.same(accountProperties, {id: 'abc4567', username: 'john-doe'})
t.same(accountProperties, {
id: 'abc4567',
username: 'john-doe',
profile: {
fullName: 'Docs Chicken',
favoriteClothing: 'Hoodie'
}
})

return account.profile.fetch()
})
Expand Down
4 changes: 2 additions & 2 deletions test/integration/hooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('sign in with pre & post hooks', function (t) {
t.plan(2)

nock(baseURL)
.put('/session')
.put('/session?include=account.profile')
.reply(201, signInResponse)

var callOrder = []
Expand Down Expand Up @@ -94,7 +94,7 @@ test('sign in with throw in post hook', function (t) {
t.plan(2)

nock(baseURL)
.put('/session')
.put('/session?include=account.profile')
.reply(201, signInResponse)

var account = new Account({
Expand Down
13 changes: 11 additions & 2 deletions test/integration/sign-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ var options = {

test('sign in', function (t) {
store.clear()
t.plan(11)
t.plan(12)

var account = new Account({
url: baseURL,
id: 'abc4567'
})

nock(baseURL)
.put('/session')
.put('/session?include=account.profile')
.reply(201, signInResponse)

.delete('/session')
Expand All @@ -43,13 +43,21 @@ test('sign in', function (t) {
username: '[email protected]',
session: {
id: 'sessionid123'
},
profile: {
fullName: 'Docs Chicken',
favoriteClothing: 'Hoodie'
}
}, 'stores account with id with session')
t.deepEqual(account.get(), {
id: 'abc4567',
username: '[email protected]',
session: {
id: 'sessionid123'
},
profile: {
fullName: 'Docs Chicken',
favoriteClothing: 'Hoodie'
}
}, '.get() returns account with session')

Expand All @@ -60,6 +68,7 @@ test('sign in', function (t) {
t.pass('signes out')

t.is(signOutResult.username, '[email protected]')
t.is(signOutResult.profile.fullName, 'Docs Chicken')

var storeAccount = store.getObject('account')

Expand Down
2 changes: 1 addition & 1 deletion test/integration/unauthenticate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('unauthenticate state within account.fetch()', function (t) {
})

nock('http://example.de')
.get('/session/account')
.get('/session/account?include=profile')
.reply(401, errorMessage)

var account = new Account({url: 'http://example.de'})
Expand Down
4 changes: 2 additions & 2 deletions test/integration/update-account-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('sign in and change username', function (t) {
})

nock(baseURL)
.put('/session', function (body) {
.put('/session?include=account.profile', function (body) {
return body.data.attributes.password === 'secret'
})
.reply(201, signInResponse)
Expand All @@ -40,7 +40,7 @@ test('sign in and change username', function (t) {
return true
})
.reply(200, updateResponse)
.put('/session', function (body) {
.put('/session?include=account.profile', function (body) {
return body.data.attributes.password === 'newsecret'
})
.thrice()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/update-profile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('sign in and change username', function (t) {
})

nock(baseURL)
.put('/session', function (body) {
.put('/session?include=account.profile', function (body) {
return body.data.attributes.password === 'secret'
})
.reply(201, signInResponse)
Expand Down
10 changes: 6 additions & 4 deletions test/unit/fetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ test('fetch', function (t) {
}, 'path')

t.deepEqual(internals.fetchProperties.lastCall.arg, {
url: 'http://example.com/session/account',
url: 'http://example.com/session/account?include=profile',
sessionId: 'abc4567',
path: 'path'
path: 'path',
include: 'profile'
}, 'calls fetchProperties with account url')

simple.restore()
Expand Down Expand Up @@ -52,9 +53,10 @@ test('fetch with undefined path', function (t) {
})

t.deepEqual(internals.fetchProperties.lastCall.arg, {
url: 'http://example.com/session/account',
url: 'http://example.com/session/account?include=profile',
sessionId: 'abc45678',
path: undefined
path: undefined,
include: 'profile'
}, 'calls fetchProperties with account url and non-string path')

simple.restore()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/sign-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('successful account.signIn(options)', function (t) {
.then(function (accountProperties) {
t.deepEqual(signIn.internals.request.lastCall.arg, {
method: 'PUT',
url: 'http://example.com/session',
url: 'http://example.com/session?include=account.profile',
body: 'serialised'
})
t.deepEqual(signIn.internals.deserialise.lastCall.arg, 'response body')
Expand Down
8 changes: 6 additions & 2 deletions test/unit/sign-out-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var test = require('tape')
var signOut = require('../../lib/sign-out')

test('signOut()', function (t) {
t.plan(3)
t.plan(4)

simple.mock(signOut.internals, 'request').resolveWith({
statusCode: 204,
Expand All @@ -19,7 +19,10 @@ test('signOut()', function (t) {
session: {
id: 'abc4567'
},
username: 'pat'
username: 'pat',
profile: {
foo: 'bar'
}
},
emitter: {
emit: simple.stub()
Expand All @@ -41,6 +44,7 @@ test('signOut()', function (t) {
})

t.is(state.account, undefined, 'unsets account')
t.is(result.profile.foo, 'bar', 'resolves with .profile')

simple.restore()
})
Expand Down
Loading