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

Proof of concept for account profiles. #158

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions api/accounts/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function addAccount (state, properties, options) {
type: 'user',
name: properties.username,
password: properties.password,
profile: properties.profile || {},
roles: [
'id:' + accountId
].concat(properties.roles || [])
Expand Down
2 changes: 1 addition & 1 deletion api/utils/doc-to-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function toAccount (doc, options) {
}

if (options.includeProfile) {
account.profile = doc.profile
account.profile = doc.profile || {}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave that in, but replace account.profile = doc.profile with account.profile = doc.profile || {}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored here: cb223fa


return account
Expand Down
9 changes: 7 additions & 2 deletions routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ function accountRoutes (server, options, next) {
handler: function (request, reply) {
var username = request.payload.data.attributes.username
var password = request.payload.data.attributes.password
var profile = request.payload.data.attributes.profile
var id = request.payload.data.id
var query = request.query
accounts.add({
username: username,
password: password,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gr2m is this correct? Or the below? My understanding is that this should be an option, not a property?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you got it right, good catch

include: query.include,
profile: profile,
id: id
}, {
include: query.include || null
})

.then(serialise)
Expand Down Expand Up @@ -126,6 +129,7 @@ function accountRoutes (server, options, next) {

var newUsername = request.payload.data.attributes.username
var newPassword = request.payload.data.attributes.password
var newProfile = request.payload.data.attributes.profile
var id = request.payload.data.id

admins.validateSession(sessionId)
Expand Down Expand Up @@ -154,7 +158,8 @@ function accountRoutes (server, options, next) {
}
return accounts.update(session.account, {
username: newUsername,
password: newPassword
password: newPassword,
profile: newProfile
}, {
include: request.query.include
})
Expand Down
39 changes: 38 additions & 1 deletion tests/integration/routes/account/put-account-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var mockCouchDbPutUser = nock('http://localhost:5984')
derived_key: Joi.string().required(),
iterations: Joi.any().only(10).required(),
password_scheme: Joi.any().only('pbkdf2').required(),
roles: Joi.array().items(Joi.string().regex(/^id:[0-9a-f]{12}$/)).max(1).min(1)
roles: Joi.array().items(Joi.string().regex(/^id:[0-9a-f]{12}$/)).max(1).min(1),
profile: Joi.object().required()
}).validate(body.docs[0]).error === null
})
.query(true)
Expand Down Expand Up @@ -76,6 +77,42 @@ getServer(function (error, server) {
})
})

group.test('User added with profile', function (t) {
var couchdb = mockCouchDbPutUser
.reply(201, [{
id: 'org.couchdb.user:pat-doe',
rev: '1-234'
}])

var options = _.defaultsDeep({
url: '/session/account?include=profile',
payload: {
data: {
attributes: {
profile: {
fullName: 'pat Doe',
email: '[email protected]'
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}, routeOptions)

var accountWithProfileFixture = require('../../fixtures/account-with-profile.json')

server.inject(options, function (response) {
t.is(couchdb.pendingMocks()[0], undefined, 'CouchDB received request')
delete response.result.meta

t.is(response.statusCode, 201, 'returns 201 status')
response.result.data.id = 'userid123'
response.result.data.relationships.profile.data.id = 'userid123-profile'
response.result.included[0].id = 'userid123-profile'
t.deepEqual(response.result, accountWithProfileFixture, 'returns account in right format')
t.end()
})
})

group.test('CouchDB User already exists', function (t) {
var couchdb = mockCouchDbPutUser
.reply(201, [{
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/routes/accounts/post-accounts-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var mockCouchDbCreateUserDoc = nock('http://localhost:5984')
derived_key: Joi.string().required(),
iterations: Joi.any().only(10).required(),
password_scheme: Joi.any().only('pbkdf2').required(),
roles: Joi.array().items(Joi.string().regex(/^id:[0-9a-f]{12}$/)).max(1).min(1)
roles: Joi.array().items(Joi.string().regex(/^id:[0-9a-f]{12}$/)).max(1).min(1),
profile: Joi.object().required()
}).validate(body.docs[0]).error === null
})
.query(true)
Expand Down