This is a reworking of #3410 based on some feedback @calebcartwright left on that PR. The goals of injecting the secrets are threefold: 1. Simplify testing 2. Be consistent with all of the other config (which is injected) 3. Encapsulate the sensitive auth-related code in one place so it can be studied and tested thoroughly - Rather than add more code to BaseService to handle authorization logic, it delegates that to an AuthHelper class. - When the server starts, it fetches the credentials from `config` and injects them into `BaseService.register()` which passes them to `invoke()`. - In `invoke()` the service's auth configuration is checked (`static get auth()`, much like `static get route()`). - If the auth config is present, an AuthHelper instance is created and attached to the new instance. - Then within the service, the password, basic auth config, or bearer authentication can be accessed via e.g. `this.authHelper.basicAuth` and passed to `this._requestJson()` and friends. - Everything is being done very explicitly, so it should be very clear where and how the configured secrets are being used. - Testing different configurations of services can now be done by injecting the config into `invoke()` in `.spec` files instead of mocking global state in the service tests as was done before. See the new Jira spec files for a good example of this. Ref #3393
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
'use strict'
|
|
|
|
const { expect } = require('chai')
|
|
const nock = require('nock')
|
|
const { cleanUpNockAfterEach, defaultContext } = require('../test-helpers')
|
|
const Wheelmap = require('./wheelmap.service')
|
|
|
|
describe('Wheelmap', function() {
|
|
cleanUpNockAfterEach()
|
|
|
|
const token = 'abc123'
|
|
const config = { private: { wheelmap_token: token } }
|
|
|
|
function createMock({ nodeId, wheelchair }) {
|
|
const scope = nock('https://wheelmap.org')
|
|
.get(`/api/nodes/${nodeId}`)
|
|
.query({ api_key: token })
|
|
|
|
if (wheelchair) {
|
|
return scope.reply(200, { node: { wheelchair } })
|
|
} else {
|
|
return scope.reply(404)
|
|
}
|
|
}
|
|
|
|
it('node with accessibility', async function() {
|
|
const nodeId = '26699541'
|
|
const scope = createMock({ nodeId, wheelchair: 'yes' })
|
|
expect(
|
|
await Wheelmap.invoke(defaultContext, config, { nodeId })
|
|
).to.deep.equal({ message: 'yes', color: 'brightgreen' })
|
|
scope.done()
|
|
})
|
|
|
|
it('node with limited accessibility', async function() {
|
|
const nodeId = '2034868974'
|
|
const scope = createMock({ nodeId, wheelchair: 'limited' })
|
|
expect(
|
|
await Wheelmap.invoke(defaultContext, config, { nodeId })
|
|
).to.deep.equal({ message: 'limited', color: 'yellow' })
|
|
scope.done()
|
|
})
|
|
|
|
it('node without accessibility', async function() {
|
|
const nodeId = '-147495158'
|
|
const scope = createMock({ nodeId, wheelchair: 'no' })
|
|
expect(
|
|
await Wheelmap.invoke(defaultContext, config, { nodeId })
|
|
).to.deep.equal({ message: 'no', color: 'red' })
|
|
scope.done()
|
|
})
|
|
|
|
it('node not found', async function() {
|
|
const nodeId = '0'
|
|
const scope = createMock({ nodeId })
|
|
expect(
|
|
await Wheelmap.invoke(defaultContext, config, { nodeId })
|
|
).to.deep.equal({ message: 'node not found', color: 'red', isError: true })
|
|
scope.done()
|
|
})
|
|
})
|