mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-15 13:02:19 +00:00
Add tests for REST API authenticated by token in parameter (#2768)
* Add tests for REST API authenticated by token * Avoid adding the crumb that is not needed anymore
This commit is contained in:
1 parent
831fdb132c
commit
b652b07f13
1 file changed
+31
-6
@@ -9,7 +9,10 @@ import com.gargoylesoftware.htmlunit.HttpMethod;
|
||||
import com.gargoylesoftware.htmlunit.WebRequest;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.util.NameValuePair;
|
||||
import hudson.Util;
|
||||
import hudson.model.Cause;
|
||||
import hudson.model.FreeStyleProject;
|
||||
import hudson.model.User;
|
||||
import hudson.security.ACL;
|
||||
import hudson.security.ACLContext;
|
||||
@@ -21,6 +24,8 @@ import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
import org.jvnet.hudson.test.JenkinsRule.WebClient;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Callable;
|
||||
import javax.annotation.Nonnull;
|
||||
import org.jvnet.hudson.test.Issue;
|
||||
@@ -128,17 +133,37 @@ public class ApiTokenPropertyTest {
|
||||
assertEquals("Update token response is incorrect",
|
||||
Messages.ApiTokenProperty_ChangeToken_SuccessHidden(), "<div>" + res.getBody().asText() + "</div>");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void postWithUsernameAndTokenInBasicAuthHeader() throws Exception {
|
||||
FreeStyleProject p = j.createFreeStyleProject("bar");
|
||||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
|
||||
User.get("foo");
|
||||
|
||||
WebClient wc = createClientForUser("foo");
|
||||
WebRequest wr = new WebRequest(new URL(j.getURL(), "job/bar/build"), HttpMethod.POST);
|
||||
|
||||
assertEquals(201, wc.getPage(wr).getWebResponse().getStatusCode());
|
||||
|
||||
j.waitUntilNoActivity();
|
||||
|
||||
Cause.UserIdCause triggeredBy = p.getBuildByNumber(1).getCause(Cause.UserIdCause.class);
|
||||
assertEquals("foo", triggeredBy.getUserId());
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private WebClient createClientForUser(final String username) throws Exception {
|
||||
User u = User.get(username);
|
||||
final ApiTokenProperty t = u.getProperty(ApiTokenProperty.class);
|
||||
// Yes, we use the insecure call in the test stuff
|
||||
final String token = t.getApiTokenInsecure();
|
||||
|
||||
final String token = getUserToken(username);
|
||||
|
||||
WebClient wc = j.createWebClient();
|
||||
wc.addRequestHeader("Authorization", "Basic " + Scrambler.scramble(username + ":" + token));
|
||||
return wc;
|
||||
}
|
||||
|
||||
private String getUserToken(String username) {
|
||||
User u = User.get(username);
|
||||
final ApiTokenProperty t = u.getProperty(ApiTokenProperty.class);
|
||||
// Yes, we use the insecure call in the test stuff
|
||||
return t.getApiTokenInsecure();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user