Skip to content

Commit

Permalink
Add auth testing for Sonar services with testAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
jNullj committed Mar 9, 2024
1 parent 74554d7 commit 8b8bf19
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 45 deletions.
19 changes: 19 additions & 0 deletions services/sonar/sonar-coverage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { testAuth } from '../test-helpers.js'
import SonarCoverage from './sonar-coverage.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarCoverage', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarCoverage,
'BasicAuth',
legacySonarResponse('coverage', 95),
{ configOverride: testAuthConfigOverride },
)
})
})
})
16 changes: 16 additions & 0 deletions services/sonar/sonar-documented-api-density.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { test, given } from 'sazerac'
import { testAuth } from '../test-helpers.js'
import SonarDocumentedApiDensity from './sonar-documented-api-density.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarDocumentedApiDensity', function () {
test(SonarDocumentedApiDensity.render, () => {
Expand All @@ -24,4 +29,15 @@ describe('SonarDocumentedApiDensity', function () {
color: 'brightgreen',
})
})

describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarDocumentedApiDensity,
'BasicAuth',
legacySonarResponse('density', 93),
{ configOverride: testAuthConfigOverride },
)
})
})
})
58 changes: 13 additions & 45 deletions services/sonar/sonar-fortify-rating.spec.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
import { expect } from 'chai'
import nock from 'nock'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import { testAuth } from '../test-helpers.js'
import SonarFortifyRating from './sonar-fortify-rating.service.js'

const token = 'abc123def456'
const config = {
public: {
services: {
sonar: { authorizedOrigins: ['http://sonar.petalslink.com'] },
},
},
private: {
sonarqube_token: token,
},
}
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarFortifyRating', function () {
cleanUpNockAfterEach()

it('sends the auth information as configured', async function () {
const scope = nock('http://sonar.petalslink.com')
.get('/api/measures/component')
.query({
componentKey: 'org.ow2.petals:petals-se-ase',
metricKeys: 'fortify-security-rating',
})
// This ensures that the expected credentials are actually being sent with the HTTP request.
// Without this the request wouldn't match and the test would fail.
.basicAuth({ user: token })
.reply(200, {
component: {
measures: [{ metric: 'fortify-security-rating', value: 4 }],
},
})

expect(
await SonarFortifyRating.invoke(
defaultContext,
config,
{ component: 'org.ow2.petals:petals-se-ase' },
{ server: 'http://sonar.petalslink.com' },
),
).to.deep.equal({
color: 'green',
message: '4/5',
describe('auth', function () {
it('sends the auth information as configured', async function () {
testAuth(
SonarFortifyRating,
'BasicAuth',
legacySonarResponse('fortify-security-rating', 4),
{ configOverride: testAuthConfigOverride },
)
})

scope.done()
})
})
25 changes: 25 additions & 0 deletions services/sonar/sonar-generic.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { testAuth } from '../test-helpers.js'
import SonarGeneric from './sonar-generic.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarGeneric', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
testAuth(SonarGeneric, 'BasicAuth', legacySonarResponse('test', 903), {
configOverride: testAuthConfigOverride,
exampleOverride: {
component: 'test',
metricName: 'test',
branch: 'home',
server:
testAuthConfigOverride.public.services.sonar.authorizedOrigins[0],
sonarVersion: '4.2',
},
ignoreOpenApiExample: true,
})
})
})
})
16 changes: 16 additions & 0 deletions services/sonar/sonar-quality-gate.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { test, given } from 'sazerac'
import { testAuth } from '../test-helpers.js'
import SonarQualityGate from './sonar-quality-gate.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarQualityGate', function () {
test(SonarQualityGate.render, () => {
Expand All @@ -12,4 +17,15 @@ describe('SonarQualityGate', function () {
color: 'critical',
})
})

describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarQualityGate,
'BasicAuth',
legacySonarResponse('alert_status', 'OK'),
{ configOverride: testAuthConfigOverride },
)
})
})
})
36 changes: 36 additions & 0 deletions services/sonar/sonar-spec-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import SonarBase from './sonar-base.js'
import { openApiQueryParams } from './sonar-helpers.js'

const testAuthConfigOverride = {
public: {
services: {
[SonarBase.auth.serviceKey]: {
authorizedOrigins: [
openApiQueryParams.find(v => v.name === 'server').example,
],
},
},
},
}

/**
* Returns a legacy sonar api response with desired key and value
*
* @param {string} key Key for the response value
* @param {string|number} val Value to assign to response key
* @returns {object} Sonar api response
*/
function legacySonarResponse(key, val) {
return [
{
msr: [
{
key,
val,
},
],
},
]
}

export { testAuthConfigOverride, legacySonarResponse }
16 changes: 16 additions & 0 deletions services/sonar/sonar-tech-debt.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { test, given } from 'sazerac'
import { testAuth } from '../test-helpers.js'
import SonarTechDebt from './sonar-tech-debt.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarTechDebt', function () {
test(SonarTechDebt.render, () => {
Expand Down Expand Up @@ -29,4 +34,15 @@ describe('SonarTechDebt', function () {
color: 'red',
})
})

describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarTechDebt,
'BasicAuth',
legacySonarResponse('sqale_debt_ratio', 95),
{ configOverride: testAuthConfigOverride },
)
})
})
})
16 changes: 16 additions & 0 deletions services/sonar/sonar-tests.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { test, given } from 'sazerac'
import { testAuth } from '../test-helpers.js'
import { SonarTests } from './sonar-tests.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarTests', function () {
test(SonarTests.render, () => {
Expand Down Expand Up @@ -34,4 +39,15 @@ describe('SonarTests', function () {
color: 'red',
})
})

describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarTests,
'BasicAuth',
legacySonarResponse('tests', 95),
{ configOverride: testAuthConfigOverride },
)
})
})
})
19 changes: 19 additions & 0 deletions services/sonar/sonar-violations.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { test, given } from 'sazerac'
import { metric } from '../text-formatters.js'
import { testAuth } from '../test-helpers.js'
import SonarViolations from './sonar-violations.service.js'
import {
legacySonarResponse,
testAuthConfigOverride,
} from './sonar-spec-helpers.js'

describe('SonarViolations', function () {
test(SonarViolations.render, () => {
Expand Down Expand Up @@ -110,4 +115,18 @@ describe('SonarViolations', function () {
color: 'red',
})
})

describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
SonarViolations,
'BasicAuth',
legacySonarResponse('violations', 95),
{
configOverride: testAuthConfigOverride,
exampleOverride: { format: 'short' },
},
)
})
})
})

0 comments on commit 8b8bf19

Please sign in to comment.