|
1 | 1 | package com.cloudbees.plugins.credentials; |
2 | 2 |
|
| 3 | +import static com.cloudbees.plugins.credentials.CredentialsSelectHelperTest.selectOption; |
3 | 4 | import static com.cloudbees.plugins.credentials.XmlMatchers.isSimilarToIgnoringPrivateAttrs; |
4 | 5 | import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | +import static org.hamcrest.Matchers.hasSize; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
5 | 8 | import static org.junit.jupiter.api.Assertions.assertFalse; |
6 | 9 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
7 | 10 | import static org.junit.jupiter.api.Assertions.assertNull; |
8 | 11 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 12 |
|
| 13 | +import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials; |
| 14 | +import com.cloudbees.plugins.credentials.domains.Domain; |
| 15 | +import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; |
| 16 | +import hudson.ExtensionList; |
| 17 | +import hudson.model.FreeStyleProject; |
| 18 | +import hudson.security.ACL; |
| 19 | +import java.io.IOException; |
10 | 20 | import java.util.ArrayList; |
11 | 21 | import java.util.Collections; |
12 | 22 | import java.util.List; |
13 | 23 | import java.util.Random; |
14 | | - |
15 | 24 | import org.htmlunit.WebResponse; |
| 25 | +import org.htmlunit.html.DomNode; |
| 26 | +import org.htmlunit.html.DomNodeList; |
| 27 | +import org.htmlunit.html.HtmlButton; |
| 28 | +import org.htmlunit.html.HtmlElementUtil; |
| 29 | +import org.htmlunit.html.HtmlForm; |
| 30 | +import org.htmlunit.html.HtmlPage; |
| 31 | +import org.htmlunit.html.HtmlRadioButtonInput; |
16 | 32 | import org.junit.jupiter.api.Test; |
17 | 33 | import org.jvnet.hudson.test.JenkinsRule; |
18 | | -import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
19 | | - |
20 | | -import com.cloudbees.plugins.credentials.domains.Domain; |
21 | | -import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; |
22 | | - |
23 | | -import hudson.ExtensionList; |
24 | | -import hudson.model.FreeStyleProject; |
25 | | - |
26 | 34 | import org.jvnet.hudson.test.MockFolder; |
| 35 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
27 | 36 |
|
28 | 37 | @WithJenkins |
29 | 38 | class ViewCredentialsActionTest { |
@@ -146,5 +155,92 @@ void isVisibleShouldReturnFalseForRegularJobs(JenkinsRule j) throws Exception { |
146 | 155 | assertNull(action.getIconFileName(), |
147 | 156 | "getIconFileName() should return null when action is not visible"); |
148 | 157 | } |
| 158 | + |
| 159 | + @Test |
| 160 | + void createUsernamePasswordCredentials(JenkinsRule r) throws Exception { |
| 161 | + createUsernamePasswordCredentials(r, false); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void createUsernamePasswordCredentialsWithMultipleDomains(JenkinsRule r) throws Exception { |
| 166 | + createTestDomain(r); |
| 167 | + createUsernamePasswordCredentials(r, true); |
| 168 | + } |
| 169 | + |
| 170 | + private static void createTestDomain(JenkinsRule r) throws IOException { |
| 171 | + // Create a test domain here, so there are at least 2 domains to cause the |
| 172 | + // Add credentials button to render a domain selector. |
| 173 | + SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get( |
| 174 | + SystemCredentialsProvider.ProviderImpl.class); |
| 175 | + CredentialsStore systemStore = system.getStore(r.getInstance()); |
| 176 | + String domainName = "test-domain"; |
| 177 | + String domainDescription = "test description"; |
| 178 | + systemStore.addDomain(new Domain(domainName, domainDescription, Collections.emptyList())); |
| 179 | + } |
| 180 | + |
| 181 | + private void createUsernamePasswordCredentials(JenkinsRule r, boolean clickGlobalDomain) throws Exception { |
| 182 | + String displayName = r.jenkins.getDescriptor(UsernamePasswordCredentialsImpl.class).getDisplayName(); |
| 183 | + |
| 184 | + if (clickGlobalDomain) { |
| 185 | + // Ensure there is more than one domain so the UI exposes a domain selector. |
| 186 | + SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class) |
| 187 | + .get(SystemCredentialsProvider.ProviderImpl.class); |
| 188 | + CredentialsStore systemStore = system.getStore(r.jenkins); |
| 189 | + systemStore.addDomain(new Domain("extra-domain", "Extra domain for UI test", Collections.emptyList()), |
| 190 | + Collections.emptyList()); |
| 191 | + } |
| 192 | + |
| 193 | + try (JenkinsRule.WebClient wc = r.createWebClient()) { |
| 194 | + HtmlPage htmlPage = wc.goTo("credentials/"); |
| 195 | + |
| 196 | + HtmlButton button = htmlPage.querySelector(".jenkins-button--primary"); |
| 197 | + HtmlElementUtil.click(button); |
| 198 | + |
| 199 | + if (clickGlobalDomain) { |
| 200 | + button = htmlPage.querySelector("button[data-type='credentials-add-store-item']"); |
| 201 | + HtmlElementUtil.click(button); |
| 202 | + } |
| 203 | + |
| 204 | + HtmlForm form = htmlPage.getFormByName("dialog"); |
| 205 | + |
| 206 | + DomNodeList<DomNode> allOptions = form.querySelectorAll(".jenkins-choice-list__item"); |
| 207 | + boolean optionFound = selectOption(allOptions, displayName); |
| 208 | + assertTrue(optionFound, "The username password option was not found in the credentials type select"); |
| 209 | + |
| 210 | + HtmlButton formSubmitButton = htmlPage.querySelector("#cr-dialog-next"); |
| 211 | + HtmlElementUtil.click(formSubmitButton); |
| 212 | + |
| 213 | + HtmlForm newCredentialsForm = htmlPage.getFormByName("newCredentials"); |
| 214 | + |
| 215 | + if (clickGlobalDomain) { |
| 216 | + // Best-effort: if the domain radio list is present, pick the global domain. |
| 217 | + // (When only one domain exists, Jenkins often omits the selector entirely.) |
| 218 | + DomNodeList<DomNode> radios = newCredentialsForm.querySelectorAll("input[type='radio'][name$='domain']"); |
| 219 | + for (DomNode n : radios) { |
| 220 | + if (n instanceof HtmlRadioButtonInput rbi && rbi.getValueAttribute() != null |
| 221 | + && ("_".equals(rbi.getValueAttribute()) || "global".equalsIgnoreCase(rbi.getValueAttribute()))) { |
| 222 | + rbi.setChecked(true); |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + newCredentialsForm.getInputByName("_.username").setValue("username"); |
| 229 | + newCredentialsForm.getInputByName("_.password").setValue("password"); |
| 230 | + |
| 231 | + List<UsernamePasswordCredentials> credentials = CredentialsProvider.lookupCredentialsInItemGroup(UsernamePasswordCredentials.class, null, ACL.SYSTEM2); |
| 232 | + assertThat(credentials, hasSize(0)); |
| 233 | + |
| 234 | + button = newCredentialsForm.querySelector("#cr-dialog-submit"); |
| 235 | + HtmlElementUtil.click(button); |
| 236 | + |
| 237 | + credentials = CredentialsProvider.lookupCredentialsInItemGroup(UsernamePasswordCredentials.class, null, ACL.SYSTEM2); |
| 238 | + assertThat(credentials, hasSize(1)); |
| 239 | + |
| 240 | + UsernamePasswordCredentials passwordCredentials = credentials.get(0); |
| 241 | + String username = passwordCredentials.getUsername(); |
| 242 | + assertEquals("username", username); |
| 243 | + } |
| 244 | + } |
149 | 245 |
|
150 | 246 | } |
0 commit comments