-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathGitLabConnectionConfigTest.java
More file actions
227 lines (193 loc) · 10.1 KB
/
GitLabConnectionConfigTest.java
File metadata and controls
227 lines (193 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.dabsquared.gitlabjenkins.connection;
import static com.dabsquared.gitlabjenkins.connection.Messages.connection_error;
import static com.dabsquared.gitlabjenkins.connection.Messages.connection_success;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.dabsquared.gitlabjenkins.connection.GitLabConnection.DescriptorImpl;
import com.dabsquared.gitlabjenkins.gitlab.api.impl.V4GitLabClientBuilder;
import hudson.ProxyConfiguration;
import hudson.model.Item;
import hudson.security.GlobalMatrixAuthorizationStrategy;
import hudson.util.FormValidation;
import hudson.util.Secret;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import javax.ws.rs.core.Response;
import jenkins.model.Jenkins;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.gitlab4j.api.GitLabApi;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.HttpRequest;
/**
* @author Robin Müller
*/
public class GitLabConnectionConfigTest {
private static final String API_TOKEN = "secret";
private static final String API_TOKEN_ID = "apiTokenId";
@Rule
public MockServerRule mockServer = new MockServerRule(this);
@Rule
public JenkinsRule jenkins = new JenkinsRule();
private MockServerClient mockServerClient;
private String gitLabUrl;
@Before
public void setup() throws IOException {
gitLabUrl = "http://localhost:" + mockServer.getPort() + "/gitlab";
for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
List<Domain> domains = credentialsStore.getDomains();
credentialsStore.addCredentials(
domains.get(0),
new StringCredentialsImpl(
CredentialsScope.SYSTEM,
API_TOKEN_ID,
"GitLab API Token",
Secret.fromString(API_TOKEN)));
}
}
}
@Test
public void doCheckConnection_success() {
String expected = connection_success();
assertThat(doCheckConnection("V4", Response.Status.OK), is(expected));
}
@Test
public void doCheckConnection_forbidden() {
String expected = connection_error("Forbidden");
assertThat(doCheckConnection("V4", Response.Status.FORBIDDEN), is(expected));
}
@Test
public void doCheckConnection_proxy() {
jenkins.getInstance().proxy = new ProxyConfiguration("0.0.0.0", 80);
GitLabConnection.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.jenkins.getDescriptorOrDie(GitLabConnection.class);
FormValidation result = descriptor.doTestConnection(gitLabUrl, API_TOKEN_ID, "V4", true, 60, 60);
// TODO: Should be "connection refused" instead of "connection timeout"
assertThat(
result.getMessage(), containsString("Client error: java.net.SocketTimeoutException: Read timed out"));
}
@Test
public void doCheckConnection_noProxy() {
jenkins.getInstance().proxy = new ProxyConfiguration("0.0.0.0", 80, "", "", "localhost");
assertThat(doCheckConnection("V4", Response.Status.OK), is(connection_success()));
}
private String doCheckConnection(String clientBuilderId, Response.Status status) {
HttpRequest request =
request().withPath("/gitlab/api/" + clientBuilderId + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN);
mockServerClient.when(request).respond(response().withStatusCode(status.getStatusCode()));
GitLabConnection.DescriptorImpl descriptor =
(DescriptorImpl) jenkins.jenkins.getDescriptor(GitLabConnection.class);
FormValidation formValidation =
descriptor.doTestConnection(gitLabUrl, API_TOKEN_ID, clientBuilderId, false, 60, 60);
mockServerClient.verify(request);
return formValidation.getMessage();
}
@Test
public void authenticationEnabled_anonymous_forbidden() throws IOException {
jenkins.get(GitLabConnectionConfig.class).setUseAuthenticatedEndpoint(true);
String username = "anonymous";
jenkins.getInstance().setSecurityRealm(jenkins.createDummySecurityRealm());
GlobalMatrixAuthorizationStrategy authorizationStrategy = new GlobalMatrixAuthorizationStrategy();
authorizationStrategy.add(Jenkins.READ, username);
URL jenkinsURL = jenkins.getURL();
jenkins.createFreeStyleProject("test");
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(jenkinsURL.toExternalForm() + "project/test");
request.addHeader("X-Gitlab-Event", "Push Hook");
String auth = username + ":" + username;
request.addHeader(
HttpHeaders.AUTHORIZATION,
"Basic " + Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.ISO_8859_1)));
request.setEntity(new StringEntity("{" + " \"object_kind\": \"push\"," + " \"event_name\": \"push\"" + "}"));
CloseableHttpResponse response = client.execute(request);
// TODO: should be 403 but is 200
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
@Test
public void authenticationEnabled_registered_success() throws Exception {
String username = "test-user";
jenkins.getInstance().setSecurityRealm(jenkins.createDummySecurityRealm());
GlobalMatrixAuthorizationStrategy authorizationStrategy = new GlobalMatrixAuthorizationStrategy();
authorizationStrategy.add(Item.BUILD, username);
jenkins.getInstance().setAuthorizationStrategy(authorizationStrategy);
URL jenkinsURL = jenkins.getURL();
jenkins.createFreeStyleProject("test");
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(jenkinsURL.toExternalForm() + "project/test");
request.addHeader("X-Gitlab-Event", "Push Hook");
String auth = username + ":" + username;
request.addHeader(
HttpHeaders.AUTHORIZATION,
"Basic " + Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.ISO_8859_1)));
request.setEntity(new StringEntity("{" + " \"object_kind\": \"push\"," + " \"event_name\": \"push\"" + "}"));
CloseableHttpResponse response = client.execute(request);
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
@Test
public void authenticationDisabled_anonymous_success() throws IOException {
jenkins.get(GitLabConnectionConfig.class).setUseAuthenticatedEndpoint(false);
jenkins.getInstance().setAuthorizationStrategy(new GlobalMatrixAuthorizationStrategy());
URL jenkinsURL = jenkins.getURL();
jenkins.createFreeStyleProject("test");
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(jenkinsURL.toExternalForm() + "project/test");
request.addHeader("X-Gitlab-Event", "Push Hook");
request.setEntity(new StringEntity("{" + " \"object_kind\": \"push\"," + " \"event_name\": \"push\"" + "}"));
CloseableHttpResponse response = client.execute(request);
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
@Test
public void setConnectionsTest() {
GitLabConnection connection1 =
new GitLabConnection("1", "http://localhost", null, new V4GitLabClientBuilder(), false, 10, 10);
GitLabConnection connection2 =
new GitLabConnection("2", "http://localhost", null, new V4GitLabClientBuilder(), false, 10, 10);
GitLabConnectionConfig config = jenkins.get(GitLabConnectionConfig.class);
List<GitLabConnection> connectionList1 = new ArrayList<>();
connectionList1.add(connection1);
config.setConnections(connectionList1);
assertThat(config.getConnections(), is(connectionList1));
List<GitLabConnection> connectionList2 = new ArrayList<>();
connectionList2.add(connection1);
connectionList2.add(connection2);
config.setConnections(connectionList2);
assertThat(config.getConnections(), is(connectionList2));
config.setConnections(connectionList1);
assertThat(config.getConnections(), is(connectionList1));
}
@Test
public void getClient_is_cached() {
GitLabConnection connection = new GitLabConnection(
"test", "http://localhost", API_TOKEN_ID, new V4GitLabClientBuilder(), false, 10, 10);
GitLabConnectionConfig config = jenkins.get(GitLabConnectionConfig.class);
List<GitLabConnection> connectionList1 = new ArrayList<>();
connectionList1.add(connection);
config.setConnections(connectionList1);
GitLabApi client = config.getClient(connection.getName(), null, null);
assertNotNull(client);
assertSame(client, config.getClient(connection.getName(), null, null));
}
}