@@ -66,8 +66,20 @@ static void init(JenkinsRule rule) {
66
66
@ BeforeEach
67
67
void setup () throws Exception {
68
68
req = mock (StaplerRequest2 .class );
69
+
70
+ hookProcessor = mock (HookProcessor .class );
71
+ sut = new BitbucketSCMSourcePushHookReceiver () {
72
+ @ Override
73
+ HookProcessor getHookProcessor (HookEventType type ) {
74
+ return hookProcessor ;
75
+ }
76
+ };
77
+
78
+ credentialsId = BitbucketTestUtil .registerHookCredentials ("Gkvl$k$wyNpQAF42" , j ).getId ();
79
+ }
80
+
81
+ private void mockCloudRequest () {
69
82
when (req .getRemoteHost ()).thenReturn ("https://bitbucket.org" );
70
- when (req .getParameter ("server_url" )).thenReturn ("https://bitbucket.org" );
71
83
when (req .getRemoteAddr ()).thenReturn ("185.166.143.48" );
72
84
when (req .getScheme ()).thenReturn ("https" );
73
85
when (req .getServerName ()).thenReturn ("jenkins.example.com" );
@@ -78,16 +90,21 @@ void setup() throws Exception {
78
90
when (req .getHeader ("Content-Type" )).thenReturn ("application/json" );
79
91
when (req .getHeader ("X-Hook-UUID" )).thenReturn (UUID .randomUUID ().toString ());
80
92
when (req .getHeader ("X-Request-UUID" )).thenReturn (UUID .randomUUID ().toString ());
93
+ when (req .getHeader ("traceparent" )).thenReturn (UUID .randomUUID ().toString ());
94
+ when (req .getHeader ("User-Agent" )).thenReturn ("Bitbucket-Webhooks/2.0" );
95
+ }
81
96
82
- hookProcessor = mock (HookProcessor .class );
83
- sut = new BitbucketSCMSourcePushHookReceiver () {
84
- @ Override
85
- HookProcessor getHookProcessor (HookEventType type ) {
86
- return hookProcessor ;
87
- }
88
- };
89
-
90
- credentialsId = BitbucketTestUtil .registerHookCredentials ("Gkvl$k$wyNpQAF42" , j ).getId ();
97
+ private void mockServerRequest (String serverURL ) {
98
+ when (req .getRemoteHost ()).thenReturn ("http://localhost:7990" );
99
+ when (req .getParameter ("server_url" )).thenReturn (serverURL );
100
+ when (req .getRemoteAddr ()).thenReturn ("127.0.0.1" );
101
+ when (req .getScheme ()).thenReturn ("https" );
102
+ when (req .getServerName ()).thenReturn ("jenkins.example.com" );
103
+ when (req .getLocalPort ()).thenReturn (80 );
104
+ when (req .getRequestURI ()).thenReturn ("/bitbucket-scmsource-hook/notify" );
105
+ when (req .getHeader ("Content-Type" )).thenReturn ("application/json; charset=utf-8" );
106
+ when (req .getHeader ("X-Request-Id" )).thenReturn (UUID .randomUUID ().toString ());
107
+ when (req .getHeader ("User-Agent" )).thenReturn ("Atlassian HttpClient 4.2.0 / Bitbucket-9.5.2 (9005002) / Default" );
91
108
}
92
109
93
110
@ Test
@@ -98,7 +115,6 @@ void test_signature_is_missing_from_cloud_payload() throws Exception {
98
115
99
116
try {
100
117
when (req .getHeader ("X-Event-Key" )).thenReturn ("repo:push" );
101
- when (req .getHeader ("X-Bitbucket-Type" )).thenReturn ("cloud" );
102
118
when (req .getInputStream ()).thenReturn (loadResource ("cloud/signed_payload.json" ));
103
119
104
120
/*HttpResponse response = */ sut .doNotify (req );
@@ -111,13 +127,13 @@ void test_signature_is_missing_from_cloud_payload() throws Exception {
111
127
112
128
@ Test
113
129
void test_signature_from_cloud () throws Exception {
130
+ mockCloudRequest ();
114
131
BitbucketCloudEndpoint endpoint = new BitbucketCloudEndpoint (false , 0 , 0 , false , null , true , credentialsId );
115
132
endpoint .setBitbucketJenkinsRootUrl ("http://jenkins.acme.com:8080/jenkins" );
116
133
BitbucketEndpointConfiguration .get ().updateEndpoint (endpoint );
117
134
118
135
try {
119
136
when (req .getHeader ("X-Event-Key" )).thenReturn ("repo:push" );
120
- when (req .getHeader ("X-Bitbucket-Type" )).thenReturn ("cloud" );
121
137
when (req .getHeader ("X-Hub-Signature" )).thenReturn ("sha256=f205c729821c6954aff2afe72b965c34015b4baf96ea8ddc2cc44999c014a035" );
122
138
when (req .getInputStream ()).thenReturn (loadResource ("cloud/signed_payload.json" ));
123
139
@@ -141,7 +157,6 @@ void test_bad_signature_from_cloud() throws Exception {
141
157
142
158
try {
143
159
when (req .getHeader ("X-Event-Key" )).thenReturn ("repo:push" );
144
- when (req .getHeader ("X-Bitbucket-Type" )).thenReturn ("cloud" );
145
160
when (req .getHeader ("X-Hub-Signature" )).thenReturn ("sha256=f205c729821c6954aff2afe72b965c34015b4baf96ea8ddc2cc44999c014a036" );
146
161
when (req .getInputStream ()).thenReturn (loadResource ("cloud/signed_payload.json" ));
147
162
@@ -160,10 +175,8 @@ void test_signature_from_native_server() throws Exception {
160
175
BitbucketEndpointConfiguration .get ().updateEndpoint (endpoint );
161
176
162
177
try {
163
- when (req .getRemoteHost ()).thenReturn ("http://localhost:7990" );
164
- when (req .getParameter ("server_url" )).thenReturn (endpoint .getServerUrl ());
178
+ mockServerRequest (endpoint .getServerUrl ());
165
179
when (req .getHeader ("X-Event-Key" )).thenReturn ("repo:refs_changed" );
166
- when (req .getHeader ("X-Request-Id" )).thenReturn ("2b15f131-4d3a-436e-bc63-caa9ae92580d" );
167
180
when (req .getHeader ("X-Hub-Signature" )).thenReturn ("sha256=4ffba9e7b58ea3d7e1a230446e8c92baea0aeec89b73f598932387254f0de13e" );
168
181
when (req .getInputStream ()).thenReturn (loadResource ("native/signed_payload.json" ));
169
182
@@ -173,7 +186,7 @@ void test_signature_from_native_server() throws Exception {
173
186
eq (HookEventType .SERVER_REFS_CHANGED ),
174
187
anyString (),
175
188
eq (BitbucketType .SERVER ),
176
- eq ("http://localhost:7990/185.166.143.48 ⇒ https://jenkins.example.com:80/bitbucket-scmsource-hook/notify" ),
189
+ eq ("http://localhost:7990/127.0.0.1 ⇒ https://jenkins.example.com:80/bitbucket-scmsource-hook/notify" ),
177
190
eq (endpoint .getServerUrl ()));
178
191
179
192
// verify bad signature
@@ -188,17 +201,38 @@ void test_signature_from_native_server() throws Exception {
188
201
}
189
202
}
190
203
204
+ @ Test
205
+ void test_ping_from_native_server () throws Exception {
206
+ BitbucketServerEndpoint endpoint = new BitbucketServerEndpoint ("datacenter" , "http://localhost:7990/bitbucket" , false , null , false , null );
207
+ endpoint .setBitbucketJenkinsRootUrl ("https://jenkins.example.com" );
208
+ BitbucketEndpointConfiguration .get ().updateEndpoint (endpoint );
209
+
210
+ try {
211
+ mockServerRequest (endpoint .getServerUrl ());
212
+ when (req .getHeader ("X-Event-Key" )).thenReturn ("diagnostics:ping" );
213
+ when (req .getInputStream ()).thenReturn (loadResource ("native/ping_payload.json" ));
214
+
215
+ sut .doNotify (req );
216
+ verify (hookProcessor ).process (
217
+ eq (HookEventType .SERVER_PING ),
218
+ anyString (),
219
+ eq (BitbucketType .SERVER ),
220
+ eq ("http://localhost:7990/127.0.0.1 ⇒ https://jenkins.example.com:80/bitbucket-scmsource-hook/notify" ),
221
+ eq (endpoint .getServerUrl ()));
222
+ } finally {
223
+ BitbucketEndpointConfiguration .get ().removeEndpoint (endpoint .getServerUrl ());
224
+ }
225
+ }
226
+
191
227
@ Test
192
228
void test_bad_signature_from_native_server () throws Exception {
193
229
BitbucketServerEndpoint endpoint = new BitbucketServerEndpoint ("datacenter" , "http://localhost:7990/bitbucket" , false , null , true , credentialsId );
194
230
endpoint .setBitbucketJenkinsRootUrl ("https://jenkins.example.com" );
195
231
BitbucketEndpointConfiguration .get ().updateEndpoint (endpoint );
196
232
197
233
try {
198
- when (req .getRemoteHost ()).thenReturn ("http://localhost:7990" );
199
- when (req .getParameter ("server_url" )).thenReturn (endpoint .getServerUrl ());
234
+ mockServerRequest (endpoint .getServerUrl ());
200
235
when (req .getHeader ("X-Event-Key" )).thenReturn ("repo:refs_changed" );
201
- when (req .getHeader ("X-Request-Id" )).thenReturn ("2b15f131-4d3a-436e-bc63-caa9ae92580d" );
202
236
when (req .getHeader ("X-Hub-Signature" )).thenReturn ("sha256=4ffba9e7b58ea3d7e1a230446e8c92baea0aeec89b73f598932387254f0de13f" );
203
237
when (req .getInputStream ()).thenReturn (loadResource ("native/signed_payload.json" ));
204
238
/*HttpResponse response = */ sut .doNotify (req );
@@ -210,9 +244,9 @@ void test_bad_signature_from_native_server() throws Exception {
210
244
}
211
245
212
246
@ Test
213
- void test_pullrequest_created () throws Exception {
247
+ void test_cloud_pullrequest_created () throws Exception {
248
+ mockCloudRequest ();
214
249
when (req .getHeader ("X-Event-Key" )).thenReturn ("pullrequest:created" );
215
- when (req .getHeader ("X-Bitbucket-Type" )).thenReturn ("cloud" );
216
250
when (req .getInputStream ()).thenReturn (loadResource ("cloud/pullrequest_created.json" ));
217
251
218
252
sut .doNotify (req );
@@ -226,9 +260,9 @@ void test_pullrequest_created() throws Exception {
226
260
}
227
261
228
262
@ Test
229
- void test_pullrequest_declined () throws Exception {
263
+ void test_cloud_pullrequest_declined () throws Exception {
264
+ mockCloudRequest ();
230
265
when (req .getHeader ("X-Event-Key" )).thenReturn ("pullrequest:rejected" );
231
- when (req .getHeader ("X-Bitbucket-Type" )).thenReturn ("cloud" );
232
266
when (req .getInputStream ()).thenReturn (loadResource ("cloud/pullrequest_rejected.json" ));
233
267
234
268
sut .doNotify (req );
0 commit comments