diff --git a/config/env/development.js b/config/env/development.js index 77ba40a..436c710 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -7,6 +7,6 @@ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/koa-server-dev', + database: 'mongodb://localhost:27017/ipfs-service-dev', env: 'dev' } diff --git a/config/env/production.js b/config/env/production.js index 16901ad..b4b2f1c 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -10,6 +10,6 @@ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://172.17.0.1:5555/koa-server-prod', + database: 'mongodb://172.17.0.1:5555/ipfs-service-prod', env: 'prod' } diff --git a/config/env/test.js b/config/env/test.js index e999166..acbe8a5 100644 --- a/config/env/test.js +++ b/config/env/test.js @@ -7,6 +7,6 @@ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/koa-server-test', + database: 'mongodb://localhost:27017/ipfs-service-test', env: 'test' } diff --git a/package.json b/package.json index e676a5d..aed451e 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "scripts": { "start": "node index.js", "test": "npm run test:all", - "test:all": "export SVC_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/", + "test:all": "export SVC_ENV=test && nyc --reporter=text mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/json-rpc/ test/unit/rest-api/ test/e2e/automated/", "test:unit:lib": "export SVC_ENV=test && mocha --exit --timeout 15000 test/unit/biz-logic/", "test:unit:rest": "export SVC_ENV=test && mocha --exit --timeout 15000 test/unit/rest-api/", + "test:unit:jsonrpc": "export SVC_ENV=test && mocha --exit --timeout 15000 test/unit/json-rpc/", "test:e2e:auto": "export SVC_ENV=test && mocha --exit --timeout 15000 test/e2e/automated/", "lint": "standard --env mocha --fix", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", "coverage": "nyc report --reporter=text-lcov | coveralls", - "coverage:report": "export KOA_ENV=test && nyc --reporter=html mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/" + "coverage:report": "export SVC_ENV=test && nyc --reporter=html mocha --exit --timeout 15000 test/unit/biz-logic/ test/unit/rest-api/ test/e2e/automated/" }, "keywords": [ "koa-api-boilerplate", diff --git a/src/rpc/auth/index.js b/src/rpc/auth/index.js index e9bfaa8..7b66c84 100644 --- a/src/rpc/auth/index.js +++ b/src/rpc/auth/index.js @@ -8,6 +8,7 @@ const jsonrpc = require('jsonrpc-lite') // Local libraries // const AuthLib = require('../../lib/auth') const UserLib = require('../../lib/users') +const wlogger = require('../../lib/wlogger') class AuthRPC { constructor (localConfig) { @@ -62,13 +63,25 @@ class AuthRPC { userType: user.type, userName: user.name, userEmail: user.email, - apiToken: token + apiToken: token, + status: 200, + success: true, + message: '' } return response } catch (err) { - console.error('Error in authUser()') - throw err + // console.error('Error in authUser()') + wlogger.error('Error in authUser(): ', err) + // throw err + + // Return an error response + return { + success: false, + status: 422, + message: err.message, + endpoint: 'authUser' + } } } } diff --git a/test/unit/json-rpc/a11-auth.unit.js b/test/unit/json-rpc/a11-auth.unit.js index 795f811..89050c3 100644 --- a/test/unit/json-rpc/a11-auth.unit.js +++ b/test/unit/json-rpc/a11-auth.unit.js @@ -9,13 +9,19 @@ const sinon = require('sinon') const assert = require('chai').assert const { v4: uid } = require('uuid') -const config = require('../../../config') +// Set the environment variable to signal this is a test. +process.env.SVC_ENV = 'test' +// Local libraries +const config = require('../../../config') const AuthRPC = require('../../../src/rpc/auth') +const UserLib = require('../../../src/lib/users') +const userLib = new UserLib() describe('#AuthRPC', () => { let uut let sandbox + let testUser before(async () => { // Connect to the Mongo Database. @@ -29,6 +35,13 @@ describe('#AuthRPC', () => { useNewUrlParser: true } ) + + // Create a test user. + testUser = await userLib.createUser({ + email: 'test543@test.com', + name: 'tester543', + password: 'password' + }) }) beforeEach(() => { @@ -39,7 +52,11 @@ describe('#AuthRPC', () => { afterEach(() => sandbox.restore()) - after(() => { + after(async () => { + // Delete the test user. + testUser = await userLib.getUser({ id: testUser.userData._id }) + await userLib.deleteUser(testUser) + mongoose.connection.close() }) @@ -68,7 +85,7 @@ describe('#AuthRPC', () => { const id = uid() const authCall = jsonrpc.request(id, 'auth', { endpoint: 'authUser', - login: 'test@test.com', + login: 'test543@test.com', password: 'password' }) const jsonStr = JSON.stringify(authCall, null, 2) @@ -83,6 +100,69 @@ describe('#AuthRPC', () => { assert.property(response, 'userName') assert.property(response, 'userEmail') assert.property(response, 'apiToken') + assert.equal(response.status, 200) + assert.equal(response.success, true) + assert.property(response, 'message') + }) + + it('should return an error for invalid credentials', async () => { + // Generate the parsed data that the main router would pass to this + // endpoint. + const id = uid() + const authCall = jsonrpc.request(id, 'auth', { + endpoint: 'authUser', + login: 'test543@test.com', + password: 'badpassword' + }) + const jsonStr = JSON.stringify(authCall, null, 2) + const rpcData = jsonrpc.parse(jsonStr) + + const response = await uut.authUser(rpcData) + // console.log('response: ', response) + + assert.equal(response.success, false) + assert.equal(response.status, 422) + assert.equal(response.message, 'Login credential do not match') + assert.equal(response.endpoint, 'authUser') + }) + + it('should throw an error if login is not provided', async () => { + // Generate the parsed data that the main router would pass to this + // endpoint. + const id = uid() + const authCall = jsonrpc.request(id, 'auth', { + endpoint: 'authUser' + }) + const jsonStr = JSON.stringify(authCall, null, 2) + const rpcData = jsonrpc.parse(jsonStr) + + const response = await uut.authUser(rpcData) + // console.log('response: ', response) + + assert.equal(response.success, false) + assert.equal(response.status, 422) + assert.equal(response.message, 'login must be specified') + assert.equal(response.endpoint, 'authUser') + }) + + it('should throw an error if password is not provided', async () => { + // Generate the parsed data that the main router would pass to this + // endpoint. + const id = uid() + const authCall = jsonrpc.request(id, 'auth', { + endpoint: 'authUser', + login: 'test543@test.com' + }) + const jsonStr = JSON.stringify(authCall, null, 2) + const rpcData = jsonrpc.parse(jsonStr) + + const response = await uut.authUser(rpcData) + // console.log('response: ', response) + + assert.equal(response.success, false) + assert.equal(response.status, 422) + assert.equal(response.message, 'password must be specified') + assert.equal(response.endpoint, 'authUser') }) }) }) diff --git a/test/unit/json-rpc/a12-users.unit.js b/test/unit/json-rpc/a12-users.unit.js index 5ea6aa1..14ff75a 100644 --- a/test/unit/json-rpc/a12-users.unit.js +++ b/test/unit/json-rpc/a12-users.unit.js @@ -9,8 +9,11 @@ const sinon = require('sinon') const assert = require('chai').assert const { v4: uid } = require('uuid') -const config = require('../../../config') +// Set the environment variable to signal this is a test. +process.env.SVC_ENV = 'test' +// Local libraries +const config = require('../../../config') const UserRPC = require('../../../src/rpc/users') describe('#UserRPC', () => {