1
0
mirror of https://github.com/badges/shields.git synced 2025-11-30 06:21:32 +03:00
Files
shields/services/symfony/symfony-test-helpers.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

118 lines
2.4 KiB
JavaScript

'use strict'
const serverSecrets = require('../../lib/server-secrets')
const sampleProjectUuid = '45afb680-d4e6-4e66-93ea-bcfa79eb8a87'
function createMockResponse({ status = 'finished', grade, violations }) {
let response = `
<project>
<last-analysis>
<status><![CDATA[${status}]]></status>
${grade ? `<grade><![CDATA[${grade}]]></grade>` : ''}`
if (violations) {
response = `${response}<violations>`
violations.forEach(v => {
response = `${response}<violation severity="${v.severity}"></violation>`
})
response = `${response}</violations>`
}
return `${response}</last-analysis></project>`
}
const runningMockResponse = createMockResponse({
status: 'running',
})
const platinumMockResponse = createMockResponse({
grade: 'platinum',
})
const goldMockResponse = createMockResponse({
grade: 'gold',
})
const silverMockResponse = createMockResponse({
grade: 'silver',
})
const bronzeMockResponse = createMockResponse({
grade: 'bronze',
})
const noMedalMockResponse = createMockResponse({
grade: 'none',
})
const criticalViolation = createMockResponse({
violations: [
{
severity: 'critical',
},
],
})
const majorViolation = createMockResponse({
violations: [
{
severity: 'major',
},
],
})
const minorViolation = createMockResponse({
violations: [
{
severity: 'minor',
},
],
})
const infoViolation = createMockResponse({
violations: [
{
severity: 'info',
},
],
})
const multipleViolations = createMockResponse({
violations: [
{
severity: 'info',
},
{
severity: 'critical',
},
],
})
const user = 'admin'
const token = 'password'
const config = {
private: {
sl_insight_userUuid: user,
sl_insight_apiToken: token,
},
}
function checkShouldSkip() {
const noToken =
!serverSecrets.sl_insight_userUuid || !serverSecrets.sl_insight_apiToken
if (noToken) {
console.warn(
'No Symfony credentials configured. Service tests will be skipped. Add credentials in local.yml to run these tests.'
)
}
return noToken
}
module.exports = {
sampleProjectUuid,
runningMockResponse,
platinumMockResponse,
goldMockResponse,
silverMockResponse,
bronzeMockResponse,
noMedalMockResponse,
criticalViolation,
majorViolation,
minorViolation,
infoViolation,
multipleViolations,
user,
token,
config,
checkShouldSkip,
}