From c66692e8f50e4b7512e3e99b2146c273a64e565e Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 19 Feb 2018 10:36:20 -0800 Subject: [PATCH] style: standard --- routes/account.js | 218 +++++++-------- routes/accounts.js | 260 +++++++++--------- routes/profile.js | 56 ++-- routes/requests.js | 56 ++-- routes/session.js | 200 +++++++------- .../routes/account/patch-account-test.js | 34 +-- .../routes/accounts/get-accounts-test.js | 12 +- .../routes/accounts/patch-accounts-test.js | 20 +- .../routes/profile/patch-profile-test.js | 2 +- 9 files changed, 429 insertions(+), 429 deletions(-) diff --git a/routes/account.js b/routes/account.js index d847244..87e2ef2 100644 --- a/routes/account.js +++ b/routes/account.js @@ -49,16 +49,16 @@ function accountRoutes (server, options, next) { id: id }) - .then(serialise) + .then(serialise) - .then(function (json) { - reply(json).code(201) - }) + .then(function (json) { + reply(json).code(201) + }) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status || 400, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status || 400, error.message)) + }) } } @@ -79,39 +79,39 @@ function accountRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - throw errors.NO_ADMIN_ACCOUNT - }, - - // if not admin - function (error) { - if (error.status === 404) { - return sessions.find(sessionId, { - include: request.query.include === 'profile' ? 'account.profile' : undefined - }).catch(function (error) { - if (error.status === 404) { - throw errors.INVALID_SESSION - } - }) - } + function (doc) { + throw errors.NO_ADMIN_ACCOUNT + }, + + // if not admin + function (error) { + if (error.status === 404) { + return sessions.find(sessionId, { + include: request.query.include === 'profile' ? 'account.profile' : undefined + }).catch(function (error) { + if (error.status === 404) { + throw errors.INVALID_SESSION + } + }) + } - throw error - }) + throw error + }) - .then(function (session) { - return session.account - }) + .then(function (session) { + return session.account + }) - .then(serialise) + .then(serialise) - .then(reply) + .then(reply) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } @@ -135,57 +135,57 @@ function accountRoutes (server, options, next) { var id = request.payload.data.id admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - throw errors.FORBIDDEN_ADMIN_ACCOUNT - }, - - // if not admin - function (error) { - if (error.status === 404) { - return sessions.find(sessionId) - .catch(function (error) { - if (error.status === 404) { - throw errors.INVALID_SESSION - } - }) + function (doc) { + throw errors.FORBIDDEN_ADMIN_ACCOUNT + }, + + // if not admin + function (error) { + if (error.status === 404) { + return sessions.find(sessionId) + .catch(function (error) { + if (error.status === 404) { + throw errors.INVALID_SESSION + } + }) + } + throw error + }) + + .then(function (session) { + if (session.account.id !== id) { + throw errors.accountIdConflict(session.account.id) } - throw error + return accounts.update(session.account, { + username: newUsername, + password: newPassword + }, { + include: request.query.include + }) }) - .then(function (session) { - if (session.account.id !== id) { - throw errors.accountIdConflict(session.account.id) - } - return accounts.update(session.account, { - username: newUsername, - password: newPassword - }, { - include: request.query.include - }) - }) - - .then(function (account) { + .then(function (account) { // no auth param, act as 'admin' (we already validated the old session above) - return sessions.add({ - account: { - username: account.username - } + return sessions.add({ + account: { + username: account.username + } + }) }) - }) - .then(function (session) { - reply() - .code(204) - .header('x-set-session', session.id) - }) + .then(function (session) { + reply() + .code(204) + .header('x-set-session', session.id) + }) - .catch(function (error) { - error = errors.parse(error) + .catch(function (error) { + error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + reply(Boom.create(error.status, error.message)) + }) } } @@ -205,45 +205,45 @@ function accountRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - throw errors.FORBIDDEN_ADMIN_ACCOUNT - }, - - // if not admin - function (error) { - if (error.status === 404) { - return sessions.find(sessionId, { - include: request.query.include === 'profile' ? 'account.profile' : undefined - }).catch(function (error) { - if (error.status === 404) { - throw errors.INVALID_SESSION - } - }) - } + function (doc) { + throw errors.FORBIDDEN_ADMIN_ACCOUNT + }, + + // if not admin + function (error) { + if (error.status === 404) { + return sessions.find(sessionId, { + include: request.query.include === 'profile' ? 'account.profile' : undefined + }).catch(function (error) { + if (error.status === 404) { + throw errors.INVALID_SESSION + } + }) + } - throw error - }) + throw error + }) - .then(function (session) { - return accounts.remove(session.account, { - include: request.query.include + .then(function (session) { + return accounts.remove(session.account, { + include: request.query.include + }) }) - }) - .then(function (account) { - if (request.query.include) { - return reply(serialise(account)).code(200) - } + .then(function (account) { + if (request.query.include) { + return reply(serialise(account)).code(200) + } - reply().code(204) - }) + reply().code(204) + }) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } diff --git a/routes/accounts.js b/routes/accounts.js index 8583b17..aa08586 100644 --- a/routes/accounts.js +++ b/routes/accounts.js @@ -37,36 +37,36 @@ function accountRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then(function (doc) { - return accounts.add({ - username: username, - password: password, - include: query.include + .then(function (doc) { + return accounts.add({ + username: username, + password: password, + include: query.include + }) + }) + + .then(function (account) { + return serialise({ + baseUrl: server.info.uri, + include: request.query.include + }, account) + }) + + .then(function (json) { + reply(json).code(201) + }) + + .catch(function (error) { + if (error.status === 401) { + error.message = 'Session invalid' + } + if (error.message === 'missing') { + error = errors.INVALID_SESSION + } + error = errors.parse(error) + + reply(Boom.wrap(error, error.status, error.message)) }) - }) - - .then(function (account) { - return serialise({ - baseUrl: server.info.uri, - include: request.query.include - }, account) - }) - - .then(function (json) { - reply(json).code(201) - }) - - .catch(function (error) { - if (error.status === 401) { - error.message = 'Session invalid' - } - if (error.message === 'missing') { - error = errors.INVALID_SESSION - } - error = errors.parse(error) - - reply(Boom.wrap(error, error.status, error.message)) - }) } } @@ -86,36 +86,36 @@ function accountRoutes (server, options, next) { admins.validateSession(sessionId) - .catch(function (error) { + .catch(function (error) { // pouchdb-admins throws MISSING_DOC with status 404 if the admin doc is not found - if (error.status === 404) { - throw errors.INVALID_SESSION - } + if (error.status === 404) { + throw errors.INVALID_SESSION + } - throw error - }) + throw error + }) - .then(function () { - return accounts.findAll({ - db: options.db, - sessionId: sessionId, - include: request.query.include + .then(function () { + return accounts.findAll({ + db: options.db, + sessionId: sessionId, + include: request.query.include + }) }) - }) - .then(function (accounts) { - return serialise({ - baseUrl: server.info.uri, - include: request.query.include - }, accounts) - }) + .then(function (accounts) { + return serialise({ + baseUrl: server.info.uri, + include: request.query.include + }, accounts) + }) - .then(reply) + .then(reply) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } @@ -135,50 +135,50 @@ function accountRoutes (server, options, next) { admins.validateSession(sessionId) - .catch(function (error) { + .catch(function (error) { // pouchdb-admins throws MISSING_DOC with status 404 if the admin doc is not found - if (error.status === 404) { - throw errors.INVALID_SESSION - } + if (error.status === 404) { + throw errors.INVALID_SESSION + } - throw error - }) + throw error + }) - .catch(function (error) { + .catch(function (error) { // pouchdb-admins throws MISSING_DOC with status 404 if the admin doc is not found - if (error.status === 404) { - throw errors.INVALID_SESSION - } + if (error.status === 404) { + throw errors.INVALID_SESSION + } - throw error - }) + throw error + }) - .then(function () { - return accounts.find(request.params.id, { - sessionId: sessionId, - include: request.query.include + .then(function () { + return accounts.find(request.params.id, { + sessionId: sessionId, + include: request.query.include + }) }) - }) - .then(function (account) { - return serialise({ - baseUrl: server.info.uri, - include: request.query.include, - admin: true - }, account) - }) + .then(function (account) { + return serialise({ + baseUrl: server.info.uri, + include: request.query.include, + admin: true + }, account) + }) - .then(reply) + .then(reply) - .catch(function (error) { - if (error.status === 401) { - error.message = 'Session invalid' - } + .catch(function (error) { + if (error.status === 401) { + error.message = 'Session invalid' + } - error = errors.parse(error) + error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + reply(Boom.create(error.status, error.message)) + }) } } @@ -202,27 +202,27 @@ function accountRoutes (server, options, next) { return admins.validateSession(sessionId) - .catch(function () { - throw errors.INVALID_SESSION - }) - - .then(function () { - return accounts.update(request.params.id, { - username: username, - password: password, - profile: profile - }, { - include: request.query.include + .catch(function () { + throw errors.INVALID_SESSION + }) + + .then(function () { + return accounts.update(request.params.id, { + username: username, + password: password, + profile: profile + }, { + include: request.query.include + }) }) - }) - - .then(function (account) { - return serialise({ - baseUrl: server.info.uri, - include: request.query.include, - admin: true - }, account) - }, + + .then(function (account) { + return serialise({ + baseUrl: server.info.uri, + include: request.query.include, + admin: true + }, account) + }, function (error) { if (error.status === 404) { throw errors.ACCOUNT_ID_NOT_FOUND @@ -231,18 +231,18 @@ function accountRoutes (server, options, next) { throw error }) - .then(function (json) { - if (profile) { - reply(json).code(201) - } else { - reply(json).code(204) - } - }) - - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .then(function (json) { + if (profile) { + reply(json).code(201) + } else { + reply(json).code(204) + } + }) + + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } @@ -261,24 +261,24 @@ function accountRoutes (server, options, next) { return admins.validateSession(sessionId) - .catch(function () { - throw errors.INVALID_SESSION - }) + .catch(function () { + throw errors.INVALID_SESSION + }) - .then(function () { - return accounts.remove(request.params.id, { - sessionId: sessionId + .then(function () { + return accounts.remove(request.params.id, { + sessionId: sessionId + }) }) - }) - .then(function (/* json */) { - reply().code(204) - }) + .then(function (/* json */) { + reply().code(204) + }) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } diff --git a/routes/profile.js b/routes/profile.js index 5fa71d5..5afc87b 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -36,40 +36,40 @@ function profileRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - throw errors.NO_PROFILE_ACCOUNT - }, - - // if not admin - function (error) { - if (error.status === 404) { - return sessions.find(sessionId, { - include: 'account.profile' - }) - .catch(function (error) { - if (error.status === 404) { - throw errors.INVALID_SESSION - } - }) - } + function (doc) { + throw errors.NO_PROFILE_ACCOUNT + }, - throw error - }) + // if not admin + function (error) { + if (error.status === 404) { + return sessions.find(sessionId, { + include: 'account.profile' + }) + .catch(function (error) { + if (error.status === 404) { + throw errors.INVALID_SESSION + } + }) + } - .then(function (session) { - return session.account - }) + throw error + }) - .then(serialise) + .then(function (session) { + return session.account + }) + + .then(serialise) - .then(reply) + .then(reply) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } diff --git a/routes/requests.js b/routes/requests.js index 87ef948..8a44c53 100644 --- a/routes/requests.js +++ b/routes/requests.js @@ -43,44 +43,44 @@ function requestRoutes (server, options, next) { api.accounts.update({username: username}, {password: newPassword}) - .then(function (account) { - var transportConfig = options.notifications.transport - var transporter = nodemailer.createTransport(transportConfig) + .then(function (account) { + var transportConfig = options.notifications.transport + var transporter = nodemailer.createTransport(transportConfig) - return transporter.sendMail({ - from: options.notifications.from, - to: account.username, - subject: 'Password reset', - text: `Hello there, + return transporter.sendMail({ + from: options.notifications.from, + to: account.username, + subject: 'Password reset', + text: `Hello there, you can now sign in with username: ${account.username} password: ${newPassword}` - }) + }) - .then(function (result) { - return { - data: { - type: 'request', - id: requestId, - attributes: { - username: username, - messageId: result.messageId, - createdAt: new Date().toISOString() + .then(function (result) { + return { + data: { + type: 'request', + id: requestId, + attributes: { + username: username, + messageId: result.messageId, + createdAt: new Date().toISOString() + } + } } - } - } + }) }) - }) - .then(function (json) { - reply(json).code(201) - }) + .then(function (json) { + reply(json).code(201) + }) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status || 400, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status || 400, error.message)) + }) } } diff --git a/routes/session.js b/routes/session.js index 1564fe5..ec33fa3 100644 --- a/routes/session.js +++ b/routes/session.js @@ -44,42 +44,42 @@ function sessionRoutes (server, options, next) { // check for admin. If not found, check for user promise = admins.validatePassword(username, password) - .then( + .then( // if admin - function () { - if (query.include) { - throw errors.FORBIDDEN_ADMIN_ACCOUNT - } - - return admins.calculateSessionId(username) - - .then(function (sessionId) { - return { - id: sessionId + function () { + if (query.include) { + throw errors.FORBIDDEN_ADMIN_ACCOUNT } - }) - }, - // if not admin - function (error) { - if (error.status === 404) { - return sessions.add({ - account: { - username: username, - password: password - }, - include: query.include - }) - .catch(function (error) { - if (error.status === 404) { - throw errors.INVALID_CREDENTIALS - } - throw error - }) - } + return admins.calculateSessionId(username) + + .then(function (sessionId) { + return { + id: sessionId + } + }) + }, + + // if not admin + function (error) { + if (error.status === 404) { + return sessions.add({ + account: { + username: username, + password: password + }, + include: query.include + }) + .catch(function (error) { + if (error.status === 404) { + throw errors.INVALID_CREDENTIALS + } + throw error + }) + } - throw error - }) + throw error + }) } else { promise = sessions.add({ account: { @@ -91,14 +91,14 @@ function sessionRoutes (server, options, next) { promise.then(serialise) - .then(function (json) { - reply(json).code(201) - }) + .then(function (json) { + reply(json).code(201) + }) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } @@ -120,43 +120,43 @@ function sessionRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - if (query.include) { - throw errors.FORBIDDEN_ADMIN_ACCOUNT - } + function (doc) { + if (query.include) { + throw errors.FORBIDDEN_ADMIN_ACCOUNT + } - return { - id: sessionId - } - }, + return { + id: sessionId + } + }, - // if not admin - function (error) { - if (error.status === 404) { - return sessions.find(sessionId, { - include: request.query.include - }) - .catch(function (error) { - if (error.status === 401 || error.status === 404) { - throw errors.INVALID_SESSION - } - throw error - }) - } + // if not admin + function (error) { + if (error.status === 404) { + return sessions.find(sessionId, { + include: request.query.include + }) + .catch(function (error) { + if (error.status === 401 || error.status === 404) { + throw errors.INVALID_SESSION + } + throw error + }) + } - throw error - }) + throw error + }) - .then(serialise) + .then(serialise) - .then(reply) + .then(reply) - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } @@ -178,42 +178,42 @@ function sessionRoutes (server, options, next) { // check for admin. If not found, check for user admins.validateSession(sessionId) - .then( + .then( // if admin - function (doc) { - if (query.include) { - throw errors.FORBIDDEN_ADMIN_ACCOUNT - } - }, + function (doc) { + if (query.include) { + throw errors.FORBIDDEN_ADMIN_ACCOUNT + } + }, - // if not admin - function (error) { - if (error.status === 404) { - return sessions.remove(sessionId, { - include: request.query.include - }) - .catch(function (error) { - if (error.status === 404 || error.status === 401) { - throw errors.INVALID_SESSION - } - throw error - }) - } + // if not admin + function (error) { + if (error.status === 404) { + return sessions.remove(sessionId, { + include: request.query.include + }) + .catch(function (error) { + if (error.status === 404 || error.status === 401) { + throw errors.INVALID_SESSION + } + throw error + }) + } + + throw error + }) - throw error + .then(function (session) { + if (!request.query.include) { + return reply().code(204) + } + reply(serialise(session)).code(200) }) - .then(function (session) { - if (!request.query.include) { - return reply().code(204) - } - reply(serialise(session)).code(200) - }) - - .catch(function (error) { - error = errors.parse(error) - reply(Boom.create(error.status, error.message)) - }) + .catch(function (error) { + error = errors.parse(error) + reply(Boom.create(error.status, error.message)) + }) } } diff --git a/tests/integration/routes/account/patch-account-test.js b/tests/integration/routes/account/patch-account-test.js index e1b44a7..c048ec4 100644 --- a/tests/integration/routes/account/patch-account-test.js +++ b/tests/integration/routes/account/patch-account-test.js @@ -91,12 +91,12 @@ function mockPasswordChange () { return error === null }) - .query(true) - .reply(201, { - ok: true, - id: 'org.couchdb.user:pat-doe', - rev: '2-3456' - }) + .query(true) + .reply(201, { + ok: true, + id: 'org.couchdb.user:pat-doe', + rev: '2-3456' + }) return couchdbMock } @@ -125,12 +125,12 @@ function mockUsernameChange () { return error === null }) - .query(true) - .reply(201, { - ok: true, - id: 'org.couchdb.user:newName', - rev: '2-3456' - }) + .query(true) + .reply(201, { + ok: true, + id: 'org.couchdb.user:newName', + rev: '2-3456' + }) // account.update(): deleted old doc couchdbMock.put('/_users/org.couchdb.user%3Apat-doe', function (body) { @@ -153,11 +153,11 @@ function mockUsernameChange () { return error === null }) - .query(true) - .reply(201, { - id: 'org.couchdb.user:pat-doe', - rev: '2-3456' - }) + .query(true) + .reply(201, { + id: 'org.couchdb.user:pat-doe', + rev: '2-3456' + }) return couchdbMock } diff --git a/tests/integration/routes/accounts/get-accounts-test.js b/tests/integration/routes/accounts/get-accounts-test.js index dff6d7f..c818950 100644 --- a/tests/integration/routes/accounts/get-accounts-test.js +++ b/tests/integration/routes/accounts/get-accounts-test.js @@ -21,12 +21,12 @@ function mockCouchDbGetAccounts () { return nock('http://localhost:5984', { encodedQueryParams: true }) - .get('/_users/_all_docs') - .query({ - include_docs: true, - startkey: '%22org.couchdb.user%3A%22', - endkey: '%22org.couchdb.user%3A%EF%BF%B0%22' - }) + .get('/_users/_all_docs') + .query({ + include_docs: true, + startkey: '%22org.couchdb.user%3A%22', + endkey: '%22org.couchdb.user%3A%EF%BF%B0%22' + }) } test('GET /accounts', function (group) { diff --git a/tests/integration/routes/accounts/patch-accounts-test.js b/tests/integration/routes/accounts/patch-accounts-test.js index 421e027..3ebd60f 100644 --- a/tests/integration/routes/accounts/patch-accounts-test.js +++ b/tests/integration/routes/accounts/patch-accounts-test.js @@ -117,16 +117,16 @@ test('PATCH /accounts/abc4567', function (group) { group.test('Not found', function (t) { var couchdb = nock('http://localhost:5984') - .get('/_users/_design/byId/_view/byId') - .query({ - key: '"xyz1234"', - include_docs: true - }) - .reply(200, { - total_rows: 1, - offset: 0, - rows: [] - }) + .get('/_users/_design/byId/_view/byId') + .query({ + key: '"xyz1234"', + include_docs: true + }) + .reply(200, { + total_rows: 1, + offset: 0, + rows: [] + }) this.server.inject({ method: 'PATCH', diff --git a/tests/integration/routes/profile/patch-profile-test.js b/tests/integration/routes/profile/patch-profile-test.js index 20839d5..77c3d00 100644 --- a/tests/integration/routes/profile/patch-profile-test.js +++ b/tests/integration/routes/profile/patch-profile-test.js @@ -76,7 +76,7 @@ test('PATCH /session/account/profile', function (group) { group.test('Session does not exist', function (t) { var couch = mockCouchDbGetUserDoc - .reply(404, {error: 'not_found', reason: 'missing'}) + .reply(404, {error: 'not_found', reason: 'missing'}) this.server.inject(routeOptions, function (response) { t.is(couch.pendingMocks()[0], undefined, 'all mocks satisfied')