mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 13:32:24 +00:00
[FIXED JENKINS-9363] added API token for REST API.
This commit is contained in:
1 parent
29038528f7
commit
578a2f5bf2
8 files changed
+335
-3
No files matched your search
@@ -0,0 +1,63 @@
|
||||
package jenkins.security;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.HttpWebConnection;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import hudson.model.User;
|
||||
import org.apache.commons.httpclient.Credentials;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
||||
import org.apache.commons.httpclient.auth.AuthScheme;
|
||||
import org.apache.commons.httpclient.auth.AuthScope;
|
||||
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
|
||||
import org.apache.commons.httpclient.auth.CredentialsProvider;
|
||||
import org.jvnet.hudson.test.HudsonTestCase;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class ApiTokenPropertyTest extends HudsonTestCase {
|
||||
/**
|
||||
* Tests the UI interaction and authentication.
|
||||
*/
|
||||
public void testBasics() throws Exception {
|
||||
jenkins.setSecurityRealm(createDummySecurityRealm());
|
||||
User u = User.get("foo");
|
||||
ApiTokenProperty t = u.getProperty(ApiTokenProperty.class);
|
||||
final String token = t.getApiToken();
|
||||
|
||||
// make sure the UI shows the token
|
||||
HtmlPage config = createWebClient().goTo(u.getUrl() + "/configure");
|
||||
HtmlForm form = config.getFormByName("config");
|
||||
assertEquals(token, form.getInputByName("_.apiToken").getValueAttribute());
|
||||
|
||||
// round-trip shouldn't change the API token
|
||||
submit(form);
|
||||
assertSame(t, u.getProperty(ApiTokenProperty.class));
|
||||
|
||||
WebClient wc = createWebClient();
|
||||
wc.setCredentialsProvider(new CredentialsProvider() {
|
||||
public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
|
||||
return new UsernamePasswordCredentials("foo", token);
|
||||
}
|
||||
});
|
||||
wc.setWebConnection(new HttpWebConnection(wc) {
|
||||
@Override
|
||||
protected HttpClient getHttpClient() {
|
||||
HttpClient c = super.getHttpClient();
|
||||
c.getParams().setAuthenticationPreemptive(true);
|
||||
c.getState().setCredentials(new AuthScope("localhost", localPort, AuthScope.ANY_REALM), new UsernamePasswordCredentials("foo", token));
|
||||
return c;
|
||||
}
|
||||
});
|
||||
|
||||
// test the authentication
|
||||
assertEquals(u,wc.executeOnServer(new Callable<User>() {
|
||||
public User call() throws Exception {
|
||||
return User.current();
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user