diff --git a/services/sonar/sonar-coverage.spec.js b/services/sonar/sonar-coverage.spec.js new file mode 100644 index 0000000000000..c6c2e1e5086b2 --- /dev/null +++ b/services/sonar/sonar-coverage.spec.js @@ -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 }, + ) + }) + }) +}) diff --git a/services/sonar/sonar-documented-api-density.spec.js b/services/sonar/sonar-documented-api-density.spec.js index a5ca4fe55267b..d8b01594bb1b6 100644 --- a/services/sonar/sonar-documented-api-density.spec.js +++ b/services/sonar/sonar-documented-api-density.spec.js @@ -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, () => { @@ -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 }, + ) + }) + }) }) diff --git a/services/sonar/sonar-fortify-rating.spec.js b/services/sonar/sonar-fortify-rating.spec.js index 4fa12729598b6..aea73043d4241 100644 --- a/services/sonar/sonar-fortify-rating.spec.js +++ b/services/sonar/sonar-fortify-rating.spec.js @@ -1,51 +1,20 @@ -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 { 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 }], + describe('auth', function () { + it('sends the auth information as configured', async function () { + testAuth( + SonarFortifyRating, + 'BasicAuth', + { + 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', + { configOverride: testAuthConfigOverride }, + ) }) - - scope.done() }) }) diff --git a/services/sonar/sonar-generic.spec.js b/services/sonar/sonar-generic.spec.js new file mode 100644 index 0000000000000..23b9374efff6f --- /dev/null +++ b/services/sonar/sonar-generic.spec.js @@ -0,0 +1,24 @@ +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], + }, + ignoreOpenApiExample: true, + }) + }) + }) +}) diff --git a/services/sonar/sonar-quality-gate.spec.js b/services/sonar/sonar-quality-gate.spec.js index aa694ae066ba4..e9bc91fd28afd 100644 --- a/services/sonar/sonar-quality-gate.spec.js +++ b/services/sonar/sonar-quality-gate.spec.js @@ -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, () => { @@ -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 }, + ) + }) + }) }) diff --git a/services/sonar/sonar-spec-helpers.js b/services/sonar/sonar-spec-helpers.js new file mode 100644 index 0000000000000..1d142f4800576 --- /dev/null +++ b/services/sonar/sonar-spec-helpers.js @@ -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 } diff --git a/services/sonar/sonar-tech-debt.spec.js b/services/sonar/sonar-tech-debt.spec.js index b6ef2009205bd..a636f8c78facb 100644 --- a/services/sonar/sonar-tech-debt.spec.js +++ b/services/sonar/sonar-tech-debt.spec.js @@ -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, () => { @@ -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 }, + ) + }) + }) }) diff --git a/services/sonar/sonar-tests.spec.js b/services/sonar/sonar-tests.spec.js index 81909602e18d3..9dd7681076bc1 100644 --- a/services/sonar/sonar-tests.spec.js +++ b/services/sonar/sonar-tests.spec.js @@ -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, () => { @@ -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 }, + ) + }) + }) }) diff --git a/services/sonar/sonar-violations.spec.js b/services/sonar/sonar-violations.spec.js index 08aa8279125a9..2c24087279bc2 100644 --- a/services/sonar/sonar-violations.spec.js +++ b/services/sonar/sonar-violations.spec.js @@ -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, () => { @@ -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' }, + }, + ) + }) + }) })