-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathConfigurationAsCodeApiTest.java
More file actions
211 lines (161 loc) · 7.65 KB
/
ConfigurationAsCodeApiTest.java
File metadata and controls
211 lines (161 loc) · 7.65 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
package io.jenkins.plugins.casc;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
import org.htmlunit.util.NameValuePair;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
public class ConfigurationAsCodeApiTest {
@Rule
public JenkinsRule j = new JenkinsRule();
private static final String ENDPOINT = "configuration-as-code/configure";
private static final String YAML_CONTENT_TYPE = "application/yaml";
private static final String ADMIN = "admin";
private WebRequest webRequest(HttpMethod method) throws Exception {
return new WebRequest(new URL(j.getURL(), ENDPOINT), method);
}
private WebRequest yamlPost(String requestBody) throws Exception {
WebRequest request = webRequest(HttpMethod.POST);
request.setAdditionalHeader("Content-Type", YAML_CONTENT_TYPE);
if (requestBody != null) {
request.setRequestBody(requestBody);
}
return request;
}
private void configureAdminSecurity() {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy()
.grant(Jenkins.ADMINISTER)
.everywhere()
.to(ADMIN));
}
@Test
public void testDoConfigure_RequiresPost() throws Exception {
try (JenkinsRule.WebClient wc = j.createWebClient()) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response = wc.getPage(webRequest(HttpMethod.GET)).getWebResponse();
assertThat(response.getStatusCode(), is(405));
}
}
@Test
public void testDoConfigure_Success() throws Exception {
configureAdminSecurity();
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response = wc.getPage(yamlPost("jenkins:\n systemMessage: 'Webhook Success'"))
.getWebResponse();
assertThat(response.getStatusCode(), is(200));
assertThat(j.jenkins.getSystemMessage(), is("Webhook Success"));
}
}
@Test
public void testDoConfigure_InvalidYaml() throws Exception {
configureAdminSecurity();
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response =
wc.getPage(yamlPost("jenkins:\n systemMessage: [invalid")).getWebResponse();
assertThat(response.getStatusCode(), is(400));
assertThat(response.getContentAsString(), containsString("message"));
}
}
@Test
public void testDoConfigure_NonAdminForbidden() throws Exception {
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken("user")) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebRequest request = yamlPost("jenkins:\n systemMessage: 'fail'");
WebResponse response = wc.getPage(request).getWebResponse();
assertThat(response.getStatusCode(), is(403));
}
}
@Test
public void testDoConfigure_Unauthenticated() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
j.jenkins.setAuthorizationStrategy(
new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().toEveryone());
try (JenkinsRule.WebClient wc = j.createWebClient()) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebRequest request = yamlPost("jenkins:\n systemMessage: 'anonymous bypass attempt'");
WebResponse response = wc.getPage(request).getWebResponse();
assertThat(response.getStatusCode(), is(403));
}
}
@Test
public void testDoConfigure_EmptyBody() throws Exception {
configureAdminSecurity();
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response = wc.getPage(yamlPost(null)).getWebResponse();
assertThat(response.getStatusCode(), is(400));
assertThat(response.getContentAsString(), containsString("message"));
}
}
@Test
public void testDoConfigure_MalformedStructure() throws Exception {
configureAdminSecurity();
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response = wc.getPage(yamlPost("jenkins:\n invalidRoot:\n foo: bar"))
.getWebResponse();
assertThat(response.getStatusCode(), is(400));
assertThat(response.getContentAsString(), containsString("message"));
}
}
@Test
public void testDoConfigure_ValidYaml_NoChanges() throws Exception {
configureAdminSecurity();
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebResponse response1 = wc.getPage(yamlPost("jenkins:\n systemMessage: 'Idempotency Test'"))
.getWebResponse();
assertThat(response1.getStatusCode(), is(200));
assertThat(j.jenkins.getSystemMessage(), is("Idempotency Test"));
WebResponse response2 = wc.getPage(yamlPost("jenkins:\n systemMessage: 'Idempotency Test'"))
.getWebResponse();
assertThat(response2.getStatusCode(), is(200));
assertThat(j.jenkins.getSystemMessage(), is("Idempotency Test"));
}
}
@Test
public void testDoReplace_ValidSource() throws Exception {
configureAdminSecurity();
Path configFile = Files.createTempFile("valid", ".yaml");
Files.writeString(configFile, "jenkins:\n systemMessage: 'Hello Replace'");
try (JenkinsRule.WebClient wc = j.createWebClient().withBasicApiToken(ADMIN)) {
wc.setThrowExceptionOnFailingStatusCode(false);
WebRequest request =
new WebRequest(new URL(j.getURL(), "manage/configuration-as-code/replace"), HttpMethod.POST);
request.setRequestParameters(List.of(new NameValuePair("_.newSource", configFile.toString())));
WebResponse response = wc.getPage(request).getWebResponse();
assertThat(response.getStatusCode(), is(200));
assertThat(j.jenkins.getSystemMessage(), is("Hello Replace"));
}
}
@Test
public void testExportStrictMode_SuccessOnCleanJenkins() throws Exception {
configureAdminSecurity();
CasCGlobalConfig config = GlobalConfiguration.all().get(CasCGlobalConfig.class);
if (config != null) {
config.setStrictExport(true);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ConfigurationAsCode.get().export(out);
String exportedYaml = out.toString(StandardCharsets.UTF_8);
assertThat(exportedYaml, containsString("jenkins:"));
assertFalse(exportedYaml.contains("FAILED TO EXPORT"));
}
}