Skip to content

Commit 99b8f69

Browse files
committed
Add bitbucket tests with authoverride
part of Improve our approach for testing auth badges#9493 seperated from PR badges#9983 for work in a later time
1 parent 1fdf395 commit 99b8f69

File tree

2 files changed

+95
-73
lines changed

2 files changed

+95
-73
lines changed

services/bitbucket/bitbucket-pull-request.service.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ function pullRequestClassGenerator(raw) {
3232
queryParamSchema,
3333
}
3434

35+
static auth = {
36+
userKey: 'bitbucket_username',
37+
passKey: 'bitbucket_password',
38+
serviceKey: 'bitbucket',
39+
isRequired: true,
40+
}
41+
42+
static authServer = {
43+
userKey: 'bitbucket_server_username',
44+
passKey: 'bitbucket_server_password',
45+
serviceKey: 'bitbucketServer',
46+
isRequired: true,
47+
}
48+
3549
static get openApi() {
3650
const key = `/bitbucket/${routePrefix}/{user}/{repo}`
3751
const route = {}
@@ -71,27 +85,16 @@ function pullRequestClassGenerator(raw) {
7185
constructor(context, config) {
7286
super(context, config)
7387

74-
this.bitbucketAuthHelper = new AuthHelper(
75-
{
76-
userKey: 'bitbucket_username',
77-
passKey: 'bitbucket_password',
78-
authorizedOrigins: ['https://bitbucket.org'],
79-
},
80-
config,
81-
)
88+
// can only be set here as we must get config
8289
this.bitbucketServerAuthHelper = new AuthHelper(
83-
{
84-
userKey: 'bitbucket_server_username',
85-
passKey: 'bitbucket_server_password',
86-
serviceKey: 'bitbucketServer',
87-
},
90+
BitbucketPullRequest.authServer,
8891
config,
8992
)
9093
}
9194

9295
async fetchCloud({ user, repo }) {
9396
return this._requestJson(
94-
this.bitbucketAuthHelper.withBasicAuth({
97+
this.authHelper.withBasicAuth({
9598
url: `https://bitbucket.org/api/2.0/repositories/${user}/${repo}/pullrequests/`,
9699
schema,
97100
options: { searchParams: { state: 'OPEN', limit: 0 } },
@@ -103,7 +106,7 @@ function pullRequestClassGenerator(raw) {
103106
// https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html#idm46229602363312
104107
async fetchServer({ server, user, repo }) {
105108
return this._requestJson(
106-
this.bitbucketServerAuthHelper.withBasicAuth({
109+
this.authHelper.withBasicAuth({
107110
url: `${server}/rest/api/1.0/projects/${user}/repos/${repo}/pull-requests`,
108111
schema,
109112
options: {
@@ -121,6 +124,7 @@ function pullRequestClassGenerator(raw) {
121124

122125
async fetch({ server, user, repo }) {
123126
if (server !== undefined) {
127+
this.authHelper = this.bitbucketServerAuthHelper
124128
return this.fetchServer({ server, user, repo })
125129
} else {
126130
return this.fetchCloud({ user, repo })
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,91 @@
1-
import { expect } from 'chai'
2-
import nock from 'nock'
3-
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
4-
import { BitbucketRawPullRequests } from './bitbucket-pull-request.service.js'
1+
import { testAuth } from '../test-helpers.js'
2+
import {
3+
BitbucketRawPullRequests,
4+
BitbucketNonRawPullRequests,
5+
} from './bitbucket-pull-request.service.js'
56

6-
describe('BitbucketPullRequest', function () {
7-
cleanUpNockAfterEach()
7+
const serverConfigOverride = {
8+
public: {
9+
services: {
10+
bitbucketServer: {
11+
authorizedOrigins: ['https://bitbucket.mydomain.net'],
12+
},
13+
bitbucket: {
14+
authorizedOrigins: ['https://bitbucket.org'],
15+
},
16+
},
17+
},
18+
private: {
19+
bitbucket_username: 'must-be-set-for-class-constructor',
20+
bitbucket_password: 'must-be-set-for-class-constructor',
21+
},
22+
}
823

9-
const user = 'admin'
10-
const pass = 'password'
24+
const cloudConfigOverride = {
25+
public: {
26+
services: {
27+
bitbucket: {
28+
authorizedOrigins: ['https://bitbucket.org'],
29+
},
30+
bitbucketServer: {
31+
authorizedOrigins: [],
32+
},
33+
},
34+
},
35+
}
1136

12-
it('Sends auth headers to Bitbucket as configured', async function () {
13-
const scope = nock('https://bitbucket.org/api/2.0/repositories/')
14-
.get(/.*/)
15-
.basicAuth({ user, pass })
16-
.reply(200, { size: 42 })
17-
18-
expect(
19-
await BitbucketRawPullRequests.invoke(
20-
defaultContext,
37+
describe('BitbucketRawPullRequests', function () {
38+
describe('auth', function () {
39+
it('sends the auth information to Bitbucket cloud as configured', async function () {
40+
return testAuth(
41+
BitbucketRawPullRequests,
42+
'BasicAuth',
43+
{ size: 42 },
2144
{
22-
public: {
23-
services: {
24-
bitbucketServer: {
25-
authorizedOrigins: [],
26-
},
27-
},
28-
},
29-
private: { bitbucket_username: user, bitbucket_password: pass },
45+
exampleOverride: { server: undefined },
46+
configOverride: cloudConfigOverride,
3047
},
31-
{ user: 'shields-io', repo: 'test-repo' },
32-
),
33-
).to.deep.equal({
34-
message: '42',
35-
color: 'yellow',
48+
)
3649
})
3750

38-
scope.done()
51+
it('sends the auth information to Bitbucket instence as configured', async function () {
52+
return testAuth(
53+
BitbucketRawPullRequests,
54+
'BasicAuth',
55+
{ size: 42 },
56+
{
57+
authOverride: BitbucketRawPullRequests.authServer,
58+
configOverride: serverConfigOverride,
59+
},
60+
)
61+
})
3962
})
63+
})
4064

41-
it('Sends auth headers to Bitbucket Server as configured', async function () {
42-
const scope = nock('https://bitbucket.example.test/rest/api/1.0/projects')
43-
.get(/.*/)
44-
.basicAuth({ user, pass })
45-
.reply(200, { size: 42 })
46-
47-
expect(
48-
await BitbucketRawPullRequests.invoke(
49-
defaultContext,
65+
describe('BitbucketNonRawPullRequests', function () {
66+
describe('auth', function () {
67+
it('sends the auth information to Bitbucket cloud as configured', async function () {
68+
return testAuth(
69+
BitbucketNonRawPullRequests,
70+
'BasicAuth',
71+
{ size: 42 },
5072
{
51-
public: {
52-
services: {
53-
bitbucketServer: {
54-
authorizedOrigins: ['https://bitbucket.example.test'],
55-
},
56-
},
57-
},
58-
private: {
59-
bitbucket_server_username: user,
60-
bitbucket_server_password: pass,
61-
},
73+
exampleOverride: { server: undefined },
74+
configOverride: cloudConfigOverride,
6275
},
63-
{ user: 'project', repo: 'repo' },
64-
{ server: 'https://bitbucket.example.test' },
65-
),
66-
).to.deep.equal({
67-
message: '42',
68-
color: 'yellow',
76+
)
6977
})
7078

71-
scope.done()
79+
it('sends the auth information to Bitbucket instence as configured', async function () {
80+
return testAuth(
81+
BitbucketNonRawPullRequests,
82+
'BasicAuth',
83+
{ size: 42 },
84+
{
85+
authOverride: BitbucketNonRawPullRequests.authServer,
86+
configOverride: serverConfigOverride,
87+
},
88+
)
89+
})
7290
})
7391
})

0 commit comments

Comments
 (0)