Increased unit test coverage of controllers

This commit is contained in:
Chris Troutner
2021-07-12 15:38:05 -07:00
parent 43b2b9dfdf
commit 508369be01
3 changed files with 67 additions and 5 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ const jsonrpc = require('jsonrpc-lite')
// Local libraries
const aboutStr = require('../../../../config/about')
class AuthRPC {
class AboutRPC {
constructor (localConfig) {
// Encapsulate dependencies
this.jsonrpc = jsonrpc
@@ -42,4 +42,4 @@ class AuthRPC {
}
}
module.exports = AuthRPC
module.exports = AboutRPC
@@ -229,8 +229,9 @@ describe('#validators', () => {
it('should throw an error if user can not be found', async () => {
try {
// Force an error
sandbox.stub(uut.UserModel, 'findById').rejects(new Error('test error'))
// Mock external dependencies.
sandbox.stub(uut.jwt, 'verify').returns(true)
sandbox.stub(uut.UserModel, 'findById').resolves(null)
// Generate the parsed data that the main router would pass to this
// endpoint.
@@ -243,15 +244,38 @@ describe('#validators', () => {
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
await uut.ensureTargetUserOrAdmin(rpcData)
assert.fail('Unexpected code path')
} catch (err) {
// console.log(err)
assert.include(err.message, 'User not found!')
}
})
it('should throw an error if JWT is from a different user', async () => {
try {
// Mock external dependencies.
sandbox.stub(uut.jwt, 'verify').returns(true)
sandbox.stub(uut.UserModel, 'findById').resolves({ _id: 'badId' })
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', {
endpoint: 'deleteUser',
apiToken: 'fakeJWTToken',
userId: 'abc123'
})
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
await uut.ensureTargetUserOrAdmin(rpcData)
assert.fail('Unexpected code path')
} catch (err) {
// console.log(err)
assert.include(err.message, 'test error')
assert.include(err.message, 'User is neither admin nor target user.')
}
})
@@ -0,0 +1,38 @@
/*
Unit tests for the json-rpc/about/index.js file.
*/
// Public npm libraries
const sinon = require('sinon')
const assert = require('chai').assert
// Local libraries
const AboutRPC = require('../../../../src/controllers/json-rpc/about')
describe('#AboutRPC', () => {
let uut
let sandbox
beforeEach(() => {
sandbox = sinon.createSandbox()
uut = new AboutRPC()
})
afterEach(() => sandbox.restore())
describe('#aboutRouter', () => {
it('should return information about the service', async () => {
const result = await uut.aboutRouter()
// console.log('result: ', result)
assert.property(result, 'success')
assert.equal(result.success, true)
assert.property(result, 'status')
assert.equal(result.status, 200)
assert.property(result, 'message')
assert.property(result, 'endpoint')
assert.equal(result.endpoint, 'about')
})
})
})