-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auth testing for Sonar services with testAuth
- Loading branch information
Showing
9 changed files
with
176 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters