Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-9363] added API token for REST API.
Browse files Browse the repository at this point in the history
Originally-Committed-As: 578a2f5bf2c8421e248b41fe4a2bdba472172aba
  • Loading branch information
kohsuke committed Aug 6, 2011
1 parent f773f0e commit fbee7ed
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/src/main/java/jenkins/security/ApiTokenPropertyTest.java
@@ -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();
}
}));
}
}

0 comments on commit fbee7ed

Please sign in to comment.