diff --git a/src/controllers/json-rpc/about/index.js b/src/controllers/json-rpc/about/index.js index 08e5221..6f3948a 100644 --- a/src/controllers/json-rpc/about/index.js +++ b/src/controllers/json-rpc/about/index.js @@ -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 diff --git a/test/unit/controllers/json-rpc/a12-validators.unit.js b/test/unit/controllers/json-rpc/a12-validators.unit.js index 8aefb7f..5677147 100644 --- a/test/unit/controllers/json-rpc/a12-validators.unit.js +++ b/test/unit/controllers/json-rpc/a12-validators.unit.js @@ -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.') } }) diff --git a/test/unit/controllers/json-rpc/about.json-rpc.controller.unit.js b/test/unit/controllers/json-rpc/about.json-rpc.controller.unit.js new file mode 100644 index 0000000..39cac34 --- /dev/null +++ b/test/unit/controllers/json-rpc/about.json-rpc.controller.unit.js @@ -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') + }) + }) +})