Skip to content

Commit f341800

Browse files
authored
Update SetupWizardTest to test wizard login (#25939)
1 parent d54deae commit f341800

File tree

1 file changed

+65
-87
lines changed

1 file changed

+65
-87
lines changed

test/src/test/java/jenkins/install/SetupWizardTest.java

Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@
3030
import static org.junit.jupiter.api.Assertions.fail;
3131

3232
import edu.umd.cs.findbugs.annotations.NonNull;
33-
import hudson.FilePath;
3433
import hudson.model.DownloadService;
3534
import hudson.model.UpdateSite;
36-
import hudson.security.AuthorizationStrategy;
37-
import hudson.security.SecurityRealm;
3835
import java.io.ByteArrayInputStream;
39-
import java.io.ByteArrayOutputStream;
4036
import java.io.File;
4137
import java.io.IOException;
4238
import java.net.MalformedURLException;
@@ -63,6 +59,9 @@
6359
import org.eclipse.jetty.server.ServerConnector;
6460
import org.eclipse.jetty.util.Callback;
6561
import org.htmlunit.Page;
62+
import org.htmlunit.html.HtmlForm;
63+
import org.htmlunit.html.HtmlFormUtil;
64+
import org.htmlunit.html.HtmlPage;
6665
import org.junit.jupiter.api.AfterEach;
6766
import org.junit.jupiter.api.BeforeEach;
6867
import org.junit.jupiter.api.Tag;
@@ -83,39 +82,40 @@ class SetupWizardTest {
8382

8483
private JenkinsRule j;
8584

85+
private String initialAdminPassword;
86+
8687
@BeforeEach
8788
void setUp(JenkinsRule rule) throws Exception {
8889
j = rule;
8990

9091
tmpdir = Files.createTempDirectory("junit-").toFile();
9192

92-
final SetupWizard wizard = j.jenkins.getSetupWizard();
93-
wizard.init(true);
93+
j.jenkins.setInstallState(jenkins.install.InstallState.INITIAL_SECURITY_SETUP);
9494

95-
// Retrieve admin credentials
96-
final FilePath adminPassFile = wizard.getInitialAdminPasswordFile();
97-
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
98-
adminPassFile.copyTo(ostream);
99-
final String password = ostream.toString(StandardCharsets.UTF_8);
95+
initialAdminPassword = j.jenkins.getSetupWizard().getInitialAdminPasswordFile().readToString().trim();
10096
}
10197

10298
@AfterEach
10399
void tearDown() {
104100
tmpdir.delete();
105101
}
106102

103+
private void wizardLogin(JenkinsRule.WebClient wc) throws Exception {
104+
HtmlPage page = wc.goTo("login");
105+
HtmlForm form = page.getForms().get(0);
106+
form.getInputByName("j_password").setValue(initialAdminPassword);
107+
HtmlFormUtil.submit(form, null);
108+
}
109+
107110
@Test
108-
void shouldReturnPluginListsByDefault() {
109-
JenkinsRule.WebClient wc = j.createWebClient();
110-
// TODO: This is a hack, wc.login does not work with the form
111-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
112-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
113-
// wc.setCredentialsProvider(adminCredentialsProvider);
114-
// wc.login("admin");
115-
116-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
117-
assertThat("Missing plugin is suggestions ", response, containsString("active-directory"));
118-
assertThat("Missing category is suggestions ", response, containsString("Pipelines and Continuous Delivery"));
111+
void shouldReturnPluginListsByDefault() throws Exception {
112+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
113+
wizardLogin(wc);
114+
115+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
116+
assertThat("Missing plugin is suggestions ", response, containsString("active-directory"));
117+
assertThat("Missing category is suggestions ", response, containsString("Pipelines and Continuous Delivery"));
118+
}
119119
}
120120

121121
@Test
@@ -126,19 +126,15 @@ void shouldReturnUpdateSiteJSONIfSpecified() throws Exception {
126126
us.init();
127127
j.jenkins.getUpdateCenter().getSites().add(us);
128128

129-
// Prepare the connection
130-
JenkinsRule.WebClient wc = j.createWebClient();
131-
// TODO: This is a hack, wc.login does not work with the form
132-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
133-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
134-
// wc.setCredentialsProvider(adminCredentialsProvider);
135-
// wc.login("admin");
136-
137-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
138-
assertThat("Missing plugin in suggestions ", response, containsString("antisamy-markup-formatter"));
139-
assertThat("Missing category in suggestions ", response, containsString("Organization and Administration"));
140-
assertThat("Unexpected plugin in suggestions ", response, not(containsString("active-directory")));
141-
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
129+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
130+
wizardLogin(wc);
131+
132+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
133+
assertThat("Missing plugin in suggestions ", response, containsString("antisamy-markup-formatter"));
134+
assertThat("Missing category in suggestions ", response, containsString("Organization and Administration"));
135+
assertThat("Unexpected plugin in suggestions ", response, not(containsString("active-directory")));
136+
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
137+
}
142138
}
143139

144140
@Test
@@ -149,19 +145,15 @@ void shouldReturnWrappedUpdateSiteJSONIfSpecified() throws Exception {
149145
us.init();
150146
j.jenkins.getUpdateCenter().getSites().add(us);
151147

152-
// Prepare the connection
153-
JenkinsRule.WebClient wc = j.createWebClient();
154-
// TODO: This is a hack, wc.login does not work with the form
155-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
156-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
157-
// wc.setCredentialsProvider(adminCredentialsProvider);
158-
// wc.login("admin");
159-
160-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
161-
assertThat("Missing plugin in suggestions ", response, containsString("dashboard-view"));
162-
assertThat("Missing category in suggestions ", response, containsString("Administration and Organization"));
163-
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
164-
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
148+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
149+
wizardLogin(wc);
150+
151+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
152+
assertThat("Missing plugin in suggestions ", response, containsString("dashboard-view"));
153+
assertThat("Missing category in suggestions ", response, containsString("Administration and Organization"));
154+
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
155+
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
156+
}
165157
}
166158

167159
@Test
@@ -235,18 +227,14 @@ void testRemoteUpdateSiteFailingValidation() throws Exception {
235227
CustomRemoteUpdateSite us = new CustomRemoteUpdateSite(baseUrl.toString(), false);
236228
j.jenkins.getUpdateCenter().getSites().add(us);
237229

238-
// Prepare the connection
239-
JenkinsRule.WebClient wc = j.createWebClient();
240-
// TODO: This is a hack, wc.login does not work with the form
241-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
242-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
243-
// wc.setCredentialsProvider(adminCredentialsProvider);
244-
// wc.login("admin");
230+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
231+
wizardLogin(wc);
245232

246-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
247-
// We need to assert that signature check fails, and we're falling back to the bundled resource
248-
assertThat("Missing plugin in suggestions ", response, not(containsString("my-plugin")));
249-
assertThat("Missing category in suggestions ", response, not(containsString("Very Useful Category")));
233+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
234+
// We need to assert that signature check fails, and we're falling back to the bundled resource
235+
assertThat("Missing plugin in suggestions ", response, not(containsString("my-plugin")));
236+
assertThat("Missing category in suggestions ", response, not(containsString("Very Useful Category")));
237+
}
250238
} finally {
251239
server.stop();
252240
}
@@ -269,21 +257,16 @@ void testRemoteUpdateSiteSkippingValidation() throws Exception {
269257
CustomRemoteUpdateSite us = new CustomRemoteUpdateSite(baseUrl.toString(), false);
270258
j.jenkins.getUpdateCenter().getSites().add(us);
271259

260+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
261+
wizardLogin(wc);
272262

273-
// Prepare the connection
274-
JenkinsRule.WebClient wc = j.createWebClient();
275-
// TODO: This is a hack, wc.login does not work with the form
276-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
277-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
278-
// wc.setCredentialsProvider(adminCredentialsProvider);
279-
// wc.login("admin");
280-
281-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
282-
// We need to assert that signature check fails, and we're falling back to the bundled resource
283-
assertThat("Missing plugin in suggestions ", response, containsString("my-plugin"));
284-
assertThat("Missing category in suggestions ", response, containsString("Very Useful Category"));
285-
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
286-
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
263+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
264+
// We need to assert that signature check fails, and we're falling back to the bundled resource
265+
assertThat("Missing plugin in suggestions ", response, containsString("my-plugin"));
266+
assertThat("Missing category in suggestions ", response, containsString("Very Useful Category"));
267+
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
268+
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
269+
}
287270
} finally {
288271
DownloadService.signatureCheck = true;
289272
server.stop();
@@ -306,21 +289,16 @@ void testRemoteUpdateSitePerformingValidation() throws Exception {
306289
CustomRemoteUpdateSite us = new CustomRemoteUpdateSite(baseUrl.toString(), true);
307290
j.jenkins.getUpdateCenter().getSites().add(us);
308291

292+
try (JenkinsRule.WebClient wc = j.createWebClient()) {
293+
wizardLogin(wc);
309294

310-
// Prepare the connection
311-
JenkinsRule.WebClient wc = j.createWebClient();
312-
// TODO: This is a hack, wc.login does not work with the form
313-
j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
314-
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
315-
// wc.setCredentialsProvider(adminCredentialsProvider);
316-
// wc.login("admin");
317-
318-
String response = jsonRequest(wc, "setupWizard/platformPluginList");
319-
// We need to assert that signature check fails, and we're falling back to the bundled resource
320-
assertThat("Missing plugin in suggestions ", response, containsString("my-plugin"));
321-
assertThat("Missing category in suggestions ", response, containsString("Very Useful Category"));
322-
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
323-
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
295+
String response = jsonRequest(wc, "setupWizard/platformPluginList");
296+
// We need to assert that signature check fails, and we're falling back to the bundled resource
297+
assertThat("Missing plugin in suggestions ", response, containsString("my-plugin"));
298+
assertThat("Missing category in suggestions ", response, containsString("Very Useful Category"));
299+
assertThat("Unexpected plugin in suggestions ", response, not(containsString("matrix-auth")));
300+
assertThat("Unexpected category in suggestions ", response, not(containsString("Pipelines and Continuous Delivery")));
301+
}
324302
} finally {
325303
server.stop();
326304
}

0 commit comments

Comments
 (0)