Files
shields/services/drone/drone-build.tester.js
Paul Melnikow ce0ddf93fc Inject secrets into the services (#3652)
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
2019-07-09 23:14:36 -04:00

54 lines
1.5 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { isBuildStatus } = require('../build-status')
const t = (module.exports = require('../tester').createServiceTester())
t.create('cloud-hosted build status on default branch')
.get('/drone/drone.json')
.expectBadge({
label: 'build',
message: Joi.alternatives().try(isBuildStatus, Joi.equal('none')),
})
t.create('cloud-hosted build status on named branch')
.get('/drone/drone/master.json')
.expectBadge({
label: 'build',
message: Joi.alternatives().try(isBuildStatus, Joi.equal('none')),
})
t.create('cloud-hosted build status on unknown repo')
.get('/this-repo/does-not-exist.json')
.expectBadge({
label: 'build',
message: 'repo not found or not authorized',
})
t.create('self-hosted build status on default branch')
.get('/badges/shields.json?server=https://drone.shields.io')
.intercept(nock =>
nock('https://drone.shields.io/api/repos')
.get('/badges/shields/builds/latest')
.reply(200, { status: 'success' })
)
.expectBadge({
label: 'build',
message: 'passing',
})
t.create('self-hosted build status on named branch')
.get(
'/badges/shields/feat/awesome-thing.json?server=https://drone.shields.io'
)
.intercept(nock =>
nock('https://drone.shields.io/api/repos')
.get('/badges/shields/builds/latest')
.query({ ref: 'refs/heads/feat/awesome-thing' })
.reply(200, { status: 'success' })
)
.expectBadge({
label: 'build',
message: 'passing',
})