mirror of
https://github.com/badges/shields.git
synced 2025-11-03 15:13:18 +03:00
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
This commit is contained in:
@@ -2,137 +2,15 @@
|
||||
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
const { withRegex } = require('../test-validators')
|
||||
const {
|
||||
createTest,
|
||||
goldMockResponse,
|
||||
runningMockResponse,
|
||||
prepLiveTest,
|
||||
sampleProjectUuid,
|
||||
realTokenExists,
|
||||
mockSymfonyUser,
|
||||
mockSymfonyToken,
|
||||
criticalViolation,
|
||||
majorViolation,
|
||||
minorViolation,
|
||||
infoViolation,
|
||||
multipleViolations,
|
||||
} = require('./symfony-test-helpers')
|
||||
const { sampleProjectUuid, checkShouldSkip } = require('./symfony-test-helpers')
|
||||
|
||||
createTest(t, 'live: valid project violations', { withMockCreds: false })
|
||||
.before(prepLiveTest)
|
||||
t.create('valid project violations')
|
||||
.skipWhen(checkShouldSkip)
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.timeout(15000)
|
||||
.interceptIf(!realTokenExists, nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.reply(200, multipleViolations)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: withRegex(
|
||||
/\d* critical|\d* critical, \d* major|\d* critical, \d* major, \d* minor|\d* critical, \d* major, \d* minor, \d* info|\d* critical, \d* minor|\d* critical, \d* info|\d* major|\d* major, \d* minor|\d* major, \d* minor, \d* info|\d* major, \d* info|\d* minor|\d* minor, \d* info/
|
||||
),
|
||||
})
|
||||
|
||||
createTest(t, 'pending project grade')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.reply(200, runningMockResponse)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: 'pending',
|
||||
color: 'lightgrey',
|
||||
})
|
||||
|
||||
createTest(t, 'zero violations')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.reply(200, goldMockResponse)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '0',
|
||||
color: 'brightgreen',
|
||||
})
|
||||
|
||||
createTest(t, 'critical violations')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.reply(200, criticalViolation)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '1 critical',
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
createTest(t, 'major violations')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.reply(200, majorViolation)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '1 major',
|
||||
color: 'orange',
|
||||
})
|
||||
|
||||
createTest(t, 'minor violations')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.basicAuth({
|
||||
user: mockSymfonyUser,
|
||||
pass: mockSymfonyToken,
|
||||
})
|
||||
.reply(200, minorViolation)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '1 minor',
|
||||
color: 'yellow',
|
||||
})
|
||||
|
||||
createTest(t, 'info violations')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.basicAuth({
|
||||
user: mockSymfonyUser,
|
||||
pass: mockSymfonyToken,
|
||||
})
|
||||
.reply(200, infoViolation)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '1 info',
|
||||
color: 'yellowgreen',
|
||||
})
|
||||
|
||||
createTest(t, 'multiple violations grade')
|
||||
.get(`/${sampleProjectUuid}.json`)
|
||||
.intercept(nock =>
|
||||
nock('https://insight.symfony.com/api/projects')
|
||||
.get(`/${sampleProjectUuid}`)
|
||||
.basicAuth({
|
||||
user: mockSymfonyUser,
|
||||
pass: mockSymfonyToken,
|
||||
})
|
||||
.reply(200, multipleViolations)
|
||||
)
|
||||
.expectBadge({
|
||||
label: 'violations',
|
||||
message: '1 critical, 1 info',
|
||||
color: 'red',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user