Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve our approach for testing auth (part 2) - basicAuth #9983

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
764582e
improve stackexchange auth testing
jNullj Oct 23, 2023
2e913a7
Merge branch 'master' into feat/9493/improve-auth-testing
jNullj Oct 23, 2023
ae1a231
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Jan 6, 2024
b2c2a18
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Jan 6, 2024
9dd597d
Remove dummy auth test
jNullj Jan 16, 2024
7bc3cc0
Add getBadgeExampleCall to test-helpers
jNullj Jan 20, 2024
f6da3af
Use getBadgeExampleCall in stackexchange-base tests
jNullj Jan 20, 2024
31c3f94
Fix getBadgeExampleCall Errors
jNullj Jan 20, 2024
18ec387
Add testAuth to test-helpers
jNullj Jan 20, 2024
1688e58
Refactor stackexchange-base.spec.js to use testAuth from test-helpers
jNullj Jan 20, 2024
609c017
Split stackexchange-base.spec into per service test file
jNullj Jan 20, 2024
ffc7800
Add all auth methods to testAuth
jNullj Feb 10, 2024
3e5c98d
Handle non-default bearer and api headers
jNullj Feb 11, 2024
876708f
Add discord.spec.js as first attempt for bearer auth
jNullj Feb 11, 2024
1ddd577
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Feb 11, 2024
c41f60f
Fix basic auth user
jNullj Feb 11, 2024
f4cc1af
Add dynamic authorizedOrigins
jNullj Feb 11, 2024
b471c5c
Add header optional argument
jNullj Feb 11, 2024
7aadc10
Add obs as basicAuth example
jNullj Feb 11, 2024
79dc536
Use apiHeaderKey and bearerHeaderKey function params
jNullj Feb 13, 2024
d1435c2
Remove old comment
jNullj Feb 13, 2024
a53f716
Allow any pass & user key for QueryStringAuth
jNullj Feb 16, 2024
14d0789
Add auth test for PepyDownloads
jNullj Feb 16, 2024
d22de8a
Fix wrong header for jwt login
jNullj Feb 16, 2024
50f4144
Support multiple authOrigins in testAuth
jNullj Feb 16, 2024
2d310bd
Add docker-automated auth test
jNullj Feb 16, 2024
1b79b4c
Fix JwtAuth testing by introducing mandatory jwtLoginEndpoint
jNullj Feb 17, 2024
419bd01
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Feb 17, 2024
a2b838c
Fix type test in generateFakeConfig
jNullj Feb 20, 2024
2590482
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Feb 22, 2024
4d57607
Add exampleOverride to testAuth
jNullj Feb 22, 2024
f156762
Add authOverride to testAuth
jNullj Feb 23, 2024
cd6c65b
Add configOverride to testAuth
jNullj Feb 23, 2024
cf34fae
Fix example params by split into path and query
jNullj Feb 24, 2024
8adaf3d
Refactor BitbucketPullRequest for testAuth
jNullj Feb 23, 2024
988290d
Refactor: use testAuth for BitbucketPullRequest
jNullj Feb 22, 2024
b9d51d1
Fix BitbucketPullRequest tests using authOverride
jNullj Feb 23, 2024
b23e3d4
Add auth testing for jenkins services with testAuth
jNullj Feb 25, 2024
84bee80
Add auth testing for Jira services with testAuth
jNullj Mar 2, 2024
1c15782
Add auth testing for Nexus services with testAuth
jNullj Mar 2, 2024
a713669
Improve error handling in getServiceClassAuthOrigin
jNullj Mar 9, 2024
99a01e1
Fix edge case in testAuth for user only auth
jNullj Mar 9, 2024
74554d7
Add option to not import openApi example in testAuth
jNullj Mar 9, 2024
8b8bf19
Add auth testing for Sonar services with testAuth
jNullj Mar 9, 2024
a2b5331
Add auth testing for SymfonyInsight services with testAuth
jNullj Mar 22, 2024
87c68e2
Refactor TeamCity auth tests for use of testAuth
jNullj Mar 22, 2024
8c38355
Refactor testAuth function to handle defaultToEmptyStringForUser auth…
jNullj Mar 24, 2024
57163eb
Add support for multiple requests in testAuth function
jNullj Mar 24, 2024
5dc774a
Add auth testing for AzureDevOps services with testAuth
jNullj Mar 24, 2024
1b425aa
Fix async mocha tests
jNullj Apr 6, 2024
94a479a
Merge branch 'master' into feat/9493/improve-auth-testing
jNullj Apr 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/custom-environment-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public:
__format: 'json'

services:
bitbucket:
authorizedOrigins: 'BITBUCKET_ORIGINS'
bitbucketServer:
authorizedOrigins: 'BITBUCKET_SERVER_ORIGINS'
drone:
Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public:
cors:
allowedOrigin: []
services:
bitbucket:
authorizedOrigins: 'https://bitbucket.org'
github:
baseUri: 'https://api.github.com'
debug:
Expand Down
1 change: 1 addition & 0 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const publicConfigSchema = Joi.object({
allowedOrigin: Joi.array().items(optionalUrl).required(),
},
services: Joi.object({
bitbucket: defaultService,
bitbucketServer: defaultService,
drone: defaultService,
github: {
Expand Down
33 changes: 33 additions & 0 deletions services/azure-devops/azure-devops-coverage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { testAuth } from '../test-helpers.js'
import AzureDevOpsCoverage from './azure-devops-coverage.service.js'

describe('AzureDevOpsCoverage', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
AzureDevOpsCoverage,
'BasicAuth',
{
coverageData: [
{
coverageStats: [
{
label: 'Coverage',
total: 95,
covered: 93,
},
],
},
],
count: 1,
value: [
{
id: 90395,
},
],
},
{ multipleRequests: true },
)
})
})
})
35 changes: 35 additions & 0 deletions services/azure-devops/azure-devops-tests.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { testAuth } from '../test-helpers.js'
import AzureDevOpsTests from './azure-devops-tests.service.js'

describe('AzureDevOpsTests', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
AzureDevOpsTests,
'BasicAuth',
{
aggregatedResultsAnalysis: {
totalTests: 95,
resultsByOutcome: {
Passed: {
count: 93,
},
},
},
count: 1,
value: [
{
id: 90395,
},
],
},
{
exampleOverride: {
compact_message: undefined,
},
multipleRequests: true,
},
)
})
})
})
34 changes: 19 additions & 15 deletions services/bitbucket/bitbucket-pull-request.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ function pullRequestClassGenerator(raw) {
queryParamSchema,
}

static auth = {
userKey: 'bitbucket_username',
passKey: 'bitbucket_password',
serviceKey: 'bitbucket',
isRequired: true,
}

static authServer = {
userKey: 'bitbucket_server_username',
passKey: 'bitbucket_server_password',
serviceKey: 'bitbucketServer',
isRequired: true,
}

static get openApi() {
const key = `/bitbucket/${routePrefix}/{user}/{repo}`
const route = {}
Expand Down Expand Up @@ -71,27 +85,16 @@ function pullRequestClassGenerator(raw) {
constructor(context, config) {
super(context, config)

this.bitbucketAuthHelper = new AuthHelper(
{
userKey: 'bitbucket_username',
passKey: 'bitbucket_password',
authorizedOrigins: ['https://bitbucket.org'],
},
config,
)
// can only be set here as we must get config
this.bitbucketServerAuthHelper = new AuthHelper(
{
userKey: 'bitbucket_server_username',
passKey: 'bitbucket_server_password',
serviceKey: 'bitbucketServer',
},
BitbucketPullRequest.authServer,
config,
)
}

async fetchCloud({ user, repo }) {
return this._requestJson(
this.bitbucketAuthHelper.withBasicAuth({
this.authHelper.withBasicAuth({
url: `https://bitbucket.org/api/2.0/repositories/${user}/${repo}/pullrequests/`,
schema,
options: { searchParams: { state: 'OPEN', limit: 0 } },
Expand All @@ -103,7 +106,7 @@ function pullRequestClassGenerator(raw) {
// https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm46229602363312
async fetchServer({ server, user, repo }) {
return this._requestJson(
this.bitbucketServerAuthHelper.withBasicAuth({
this.authHelper.withBasicAuth({
url: `${server}/rest/api/1.0/projects/${user}/repos/${repo}/pull-requests`,
schema,
options: {
Expand All @@ -121,6 +124,7 @@ function pullRequestClassGenerator(raw) {

async fetch({ server, user, repo }) {
if (server !== undefined) {
this.authHelper = this.bitbucketServerAuthHelper
return this.fetchServer({ server, user, repo })
} else {
return this.fetchCloud({ user, repo })
Expand Down
134 changes: 76 additions & 58 deletions services/bitbucket/bitbucket-pull-request.spec.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,91 @@
import { expect } from 'chai'
import nock from 'nock'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import { BitbucketRawPullRequests } from './bitbucket-pull-request.service.js'
import { testAuth } from '../test-helpers.js'
import {
BitbucketRawPullRequests,
BitbucketNonRawPullRequests,
} from './bitbucket-pull-request.service.js'

describe('BitbucketPullRequest', function () {
cleanUpNockAfterEach()
const serverConfigOverride = {
public: {
services: {
bitbucketServer: {
authorizedOrigins: ['https://bitbucket.mydomain.net'],
},
bitbucket: {
authorizedOrigins: ['https://bitbucket.org'],
},
},
},
private: {
bitbucket_username: 'must-be-set-for-class-constructor',
bitbucket_password: 'must-be-set-for-class-constructor',
},
}

const user = 'admin'
const pass = 'password'
const cloudConfigOverride = {
public: {
services: {
bitbucket: {
authorizedOrigins: ['https://bitbucket.org'],
},
bitbucketServer: {
authorizedOrigins: [],
},
},
},
}

it('Sends auth headers to Bitbucket as configured', async function () {
const scope = nock('https://bitbucket.org/api/2.0/repositories/')
.get(/.*/)
.basicAuth({ user, pass })
.reply(200, { size: 42 })

expect(
await BitbucketRawPullRequests.invoke(
defaultContext,
describe('BitbucketRawPullRequests', function () {
describe('auth', function () {
it('sends the auth information to Bitbucket cloud as configured', async function () {
return testAuth(
BitbucketRawPullRequests,
'BasicAuth',
{ size: 42 },
{
public: {
services: {
bitbucketServer: {
authorizedOrigins: [],
},
},
},
private: { bitbucket_username: user, bitbucket_password: pass },
exampleOverride: { server: undefined },
configOverride: cloudConfigOverride,
},
{ user: 'shields-io', repo: 'test-repo' },
),
).to.deep.equal({
message: '42',
color: 'yellow',
)
})

scope.done()
it('sends the auth information to Bitbucket instence as configured', async function () {
return testAuth(
BitbucketRawPullRequests,
'BasicAuth',
{ size: 42 },
{
authOverride: BitbucketRawPullRequests.authServer,
configOverride: serverConfigOverride,
},
)
})
})
})

it('Sends auth headers to Bitbucket Server as configured', async function () {
const scope = nock('https://bitbucket.example.test/rest/api/1.0/projects')
.get(/.*/)
.basicAuth({ user, pass })
.reply(200, { size: 42 })

expect(
await BitbucketRawPullRequests.invoke(
defaultContext,
describe('BitbucketNonRawPullRequests', function () {
describe('auth', function () {
it('sends the auth information to Bitbucket cloud as configured', async function () {
return testAuth(
BitbucketNonRawPullRequests,
'BasicAuth',
{ size: 42 },
{
public: {
services: {
bitbucketServer: {
authorizedOrigins: ['https://bitbucket.example.test'],
},
},
},
private: {
bitbucket_server_username: user,
bitbucket_server_password: pass,
},
exampleOverride: { server: undefined },
configOverride: cloudConfigOverride,
},
{ user: 'project', repo: 'repo' },
{ server: 'https://bitbucket.example.test' },
),
).to.deep.equal({
message: '42',
color: 'yellow',
)
})

scope.done()
it('sends the auth information to Bitbucket instence as configured', async function () {
return testAuth(
BitbucketNonRawPullRequests,
'BasicAuth',
{ size: 42 },
{
authOverride: BitbucketNonRawPullRequests.authServer,
configOverride: serverConfigOverride,
},
)
})
})
})
62 changes: 17 additions & 45 deletions services/jenkins/jenkins-build.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { expect } from 'chai'
import nock from 'nock'
import { test, forCases, given } from 'sazerac'
import { renderBuildStatusBadge } from '../build-status.js'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import { testAuth } from '../test-helpers.js'
import JenkinsBuild from './jenkins-build.service.js'

const authConfigOverride = {
public: {
services: {
jenkins: {
authorizedOrigins: ['https://ci.eclipse.org'],
},
},
},
}

describe('JenkinsBuild', function () {
test(JenkinsBuild.prototype.transform, () => {
forCases([
Expand Down Expand Up @@ -57,49 +65,13 @@ describe('JenkinsBuild', function () {
})

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

const user = 'admin'
const pass = 'password'
const config = {
public: {
services: {
jenkins: {
authorizedOrigins: ['https://jenkins.ubuntu.com'],
},
},
},
private: {
jenkins_user: user,
jenkins_pass: pass,
},
}

it('sends the auth information as configured', async function () {
const scope = nock('https://jenkins.ubuntu.com')
.get('/server/job/curtin-vmtest-daily-x/api/json?tree=color')
// 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, pass })
.reply(200, { color: 'blue' })

expect(
await JenkinsBuild.invoke(
defaultContext,
config,
{},
{
jobUrl:
'https://jenkins.ubuntu.com/server/job/curtin-vmtest-daily-x',
},
),
).to.deep.equal({
label: undefined,
message: 'passing',
color: 'brightgreen',
})

scope.done()
return testAuth(
JenkinsBuild,
'BasicAuth',
{ color: 'blue' },
{ configOverride: authConfigOverride },
)
})
})
})