1
1
package com .cloudbees .jenkins .plugins .bitbucket .server .client ;
2
2
3
3
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketApi ;
4
+ import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketAuthenticator ;
4
5
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus ;
5
6
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus .Status ;
6
7
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketRepository ;
7
8
import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory ;
9
+ import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory .BitbucketServerIntegrationClient ;
8
10
import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketIntegrationClientFactory .IRequestAudit ;
9
11
import io .jenkins .cli .shaded .org .apache .commons .lang .RandomStringUtils ;
10
12
import java .io .InputStream ;
11
13
import java .nio .charset .StandardCharsets ;
12
14
import java .util .List ;
13
15
import org .apache .commons .io .IOUtils ;
16
+ import org .apache .http .HttpRequest ;
14
17
import org .apache .http .client .methods .HttpHead ;
15
18
import org .apache .http .client .methods .HttpPost ;
16
19
import org .apache .http .client .methods .HttpRequestBase ;
17
20
import org .apache .http .impl .client .HttpClientBuilder ;
21
+ import org .junit .jupiter .api .BeforeAll ;
18
22
import org .junit .jupiter .api .Test ;
19
23
import org .jvnet .hudson .test .JenkinsRule ;
20
24
import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
27
31
import static org .hamcrest .Matchers .hasItem ;
28
32
import static org .hamcrest .Matchers .is ;
29
33
import static org .hamcrest .Matchers .not ;
34
+ import static org .mockito .ArgumentMatchers .any ;
30
35
import static org .mockito .Mockito .RETURNS_SELF ;
31
36
import static org .mockito .Mockito .mock ;
32
37
import static org .mockito .Mockito .mockStatic ;
38
+ import static org .mockito .Mockito .never ;
33
39
import static org .mockito .Mockito .verify ;
34
40
35
41
@ WithJenkins
36
42
class BitbucketServerAPIClientTest {
37
43
44
+ private static JenkinsRule j ;
45
+
46
+ @ BeforeAll
47
+ static void init (JenkinsRule rule ) {
48
+ j = rule ;
49
+ }
50
+
38
51
@ Test
39
- void verify_status_notitication_name_max_length (JenkinsRule j ) throws Exception {
52
+ void verify_status_notitication_name_max_length () throws Exception {
40
53
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
41
54
BitbucketBuildStatus status = new BitbucketBuildStatus ();
42
55
status .setName (RandomStringUtils .randomAlphanumeric (300 ));
@@ -63,8 +76,13 @@ private HttpRequestBase extractRequest(BitbucketApi client) {
63
76
return captor .getValue ();
64
77
}
65
78
79
+ private BitbucketAuthenticator extractAuthenticator (BitbucketApi client ) {
80
+ assertThat (client ).isInstanceOf (BitbucketServerIntegrationClient .class );
81
+ return ((BitbucketServerIntegrationClient ) client ).getAuthenticator ();
82
+ }
83
+
66
84
@ Test
67
- void verify_checkPathExists_given_a_path (JenkinsRule j ) throws Exception {
85
+ void verify_checkPathExists_given_a_path () throws Exception {
68
86
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
69
87
assertThat (client .checkPathExists ("feature/pipeline" , "folder/Jenkinsfile" )).isTrue ();
70
88
@@ -78,7 +96,7 @@ void verify_checkPathExists_given_a_path(JenkinsRule j) throws Exception {
78
96
}
79
97
80
98
@ Test
81
- void verify_checkPathExists_given_file (JenkinsRule j ) throws Exception {
99
+ void verify_checkPathExists_given_file () throws Exception {
82
100
BitbucketApi client = BitbucketIntegrationClientFactory .getApiMockClient ("https://acme.bitbucket.org" );
83
101
assertThat (client .checkPathExists ("feature/pipeline" , "Jenkinsfile" )).isTrue ();
84
102
@@ -89,7 +107,7 @@ void verify_checkPathExists_given_file(JenkinsRule j) throws Exception {
89
107
}
90
108
91
109
@ Test
92
- void filterArchivedRepositories (JenkinsRule j ) throws Exception {
110
+ void filterArchivedRepositories () throws Exception {
93
111
BitbucketApi client = BitbucketIntegrationClientFactory .getClient ("localhost" , "foo" , "test-repos" );
94
112
List <? extends BitbucketRepository > repos = client .getRepositories ();
95
113
List <String > names = repos .stream ().map (BitbucketRepository ::getRepositoryName ).toList ();
@@ -98,20 +116,30 @@ void filterArchivedRepositories(JenkinsRule j) throws Exception {
98
116
}
99
117
100
118
@ Test
101
- void sortRepositoriesByName (JenkinsRule j ) throws Exception {
119
+ void sortRepositoriesByName () throws Exception {
102
120
BitbucketApi client = BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
103
121
List <? extends BitbucketRepository > repos = client .getRepositories ();
104
122
List <String > names = repos .stream ().map (BitbucketRepository ::getRepositoryName ).toList ();
105
123
assertThat (names , is (List .of ("another-repo" , "dogs-repo" , "test-repos" )));
106
124
}
107
125
108
126
@ Test
109
- void disableCookieManager (JenkinsRule j ) throws Exception {
127
+ void disableCookieManager () throws Exception {
110
128
try (MockedStatic <HttpClientBuilder > staticHttpClientBuilder = mockStatic (HttpClientBuilder .class )) {
111
129
HttpClientBuilder httpClientBuilder = mock (HttpClientBuilder .class , RETURNS_SELF );
112
130
staticHttpClientBuilder .when (HttpClientBuilder ::create ).thenReturn (httpClientBuilder );
113
131
BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
114
132
verify (httpClientBuilder ).disableCookieManagement ();
115
133
}
116
134
}
135
+
136
+ @ Test
137
+ void verify_mirroredRepository_does_not_authenticate_request () throws Exception {
138
+ BitbucketServerAPIClient client = (BitbucketServerAPIClient ) BitbucketIntegrationClientFactory .getClient ("localhost" , "amuniz" , "test-repos" );
139
+
140
+ BitbucketAuthenticator authenticator = extractAuthenticator (client );
141
+ String url = "https://localhost/rest/mirroring/latest/upstreamServers/1/repos/1?jwt=TOKEN" ;
142
+ client .getMirroredRepository (url );
143
+ verify (authenticator , never ()).configureRequest (any (HttpRequest .class ));
144
+ }
117
145
}
0 commit comments