feat(JSON RPC): Auth returns error messages. Added unit tests

This commit is contained in:
Chris Troutner
2021-04-06 17:31:02 -07:00
parent 0abe291a1d
commit c5755ef119
7 changed files with 109 additions and 12 deletions
+1 -1
View File
@@ -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'
}
+1 -1
View File
@@ -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'
}
+1 -1
View File
@@ -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'
}
+3 -2
View File
@@ -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",
+16 -3
View File
@@ -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'
}
}
}
}
+83 -3
View File
@@ -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')
})
})
})
+4 -1
View File
@@ -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', () => {