diff --git a/api/accounts/add.js b/api/accounts/add.js index 51d17be..a608b06 100644 --- a/api/accounts/add.js +++ b/api/accounts/add.js @@ -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 || []) diff --git a/api/utils/doc-to-account.js b/api/utils/doc-to-account.js index c286461..4dac632 100644 --- a/api/utils/doc-to-account.js +++ b/api/utils/doc-to-account.js @@ -20,7 +20,7 @@ function toAccount (doc, options) { } if (options.includeProfile) { - account.profile = doc.profile + account.profile = doc.profile || {} } return account diff --git a/routes/account.js b/routes/account.js index d716596..245dd96 100644 --- a/routes/account.js +++ b/routes/account.js @@ -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, - include: query.include, + profile: profile, id: id + }, { + include: query.include || null }) .then(serialise) @@ -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) @@ -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 }) diff --git a/tests/integration/routes/account/put-account-test.js b/tests/integration/routes/account/put-account-test.js index ba73446..1f8ecef 100644 --- a/tests/integration/routes/account/put-account-test.js +++ b/tests/integration/routes/account/put-account-test.js @@ -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) @@ -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: 'pat@example.com' + } + } + } + } + }, 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, [{ diff --git a/tests/integration/routes/accounts/post-accounts-test.js b/tests/integration/routes/accounts/post-accounts-test.js index 3c9e4b9..0b10f7a 100644 --- a/tests/integration/routes/accounts/post-accounts-test.js +++ b/tests/integration/routes/accounts/post-accounts-test.js @@ -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)