com.urswolfer.gerrit.client.rest.GerritAuthData Java Examples

The following examples show how to use com.urswolfer.gerrit.client.rest.GerritAuthData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the login cache is used correctly for a host which does NOT
 * support Gerrit-Auth method. It tries the first time to get the GerritAccount-cookie
 * and if that fails it continues to use HTTP auth.
 */
@Test
public void testGerritAuthNotAvailable() throws Exception {
    GerritRestClient gerritRestClient = new GerritRestClient(
        new GerritAuthData.Basic(jettyUrl, "foo", "bar"), new HttpRequestExecutor());
    Field loginCacheField = gerritRestClient.getClass().getDeclaredField("loginCache");
    loginCacheField.setAccessible(true);
    LoginCache loginCache = (LoginCache) loginCacheField.get(gerritRestClient);

    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
    Truth.assertThat(loginCache.getHostSupportsGerritAuth()).isTrue();
    requestChanges(gerritRestClient);
    Truth.assertThat(loginCache.getHostSupportsGerritAuth()).isFalse();
    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
    requestChanges(gerritRestClient);
    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
}
 
Example #2
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * When cookie "GerritAccount" is available (sent in test with "LoginServlet"),
 * "GerritAuth" string is extracted and cached.
 * Note that no username / login is NOT sent - otherwise LoginSimulationServlet would
 * not return a GerritAccount-cookie.
 */
@Test
public void testGerritAuthExtractionAndCache() throws Exception {
    GerritRestClient gerritRestClient = new GerritRestClient(
        new GerritAuthData.Basic(jettyUrl), new HttpRequestExecutor());
    Field loginCacheField = gerritRestClient.getClass().getDeclaredField("loginCache");
    loginCacheField.setAccessible(true);
    LoginCache loginCache = (LoginCache) loginCacheField.get(gerritRestClient);

    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
    gerritRestClient.requestRest("/changes/", null, GerritRestClient.HttpVerb.GET);
    gerritRestClient.requestRest("/changes/?n=5", null, GerritRestClient.HttpVerb.GET);
    Truth.assertThat(loginCache.getHostSupportsGerritAuth()).isTrue();
    Truth.assertThat(loginCache.getGerritAuthOptional()).isPresent();

    loginCache.invalidate();
    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();

    // ensure that even with invalidated cache request is possible and cached filled again
    gerritRestClient.requestRest("/changes/", null, GerritRestClient.HttpVerb.GET);
    Truth.assertThat(loginCache.getGerritAuthOptional()).isPresent();
}
 
Example #3
Source File: GerritApiBuilder.java    From gerrit-code-review-plugin with Apache License 2.0 6 votes vote down vote up
public GerritApi build() {
  GerritApi gerritApi = null;
  if (verifyParameters()) {
    List<HttpClientBuilderExtension> extensions = new ArrayList<>();
    extensions.add(UserAgentClientBuilderExtension.INSTANCE);
    if (Boolean.TRUE.equals(insecureHttps)) {
      extensions.add(SSLNoVerifyCertificateManagerClientBuilderExtension.INSTANCE);
    }
    gerritApi =
        new GerritRestApiFactory()
            .create(
                new GerritAuthData.Basic(gerritApiUrl.toString(), username, password, true),
                extensions.toArray(new HttpClientBuilderExtension[0]));
  }
  return gerritApi;
}
 
Example #4
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhostProjectsQuery() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ProjectInfo> projects = gerritClient.projects().list().withLimit(1).withDescription(true).get();
    System.out.println(String.format("Got %s projects.", projects.size()));
    System.out.println(projects);
}
 
Example #5
Source File: SSLNoVerifyCertificateManagerClientBuilderExtension.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public HttpClientBuilder extend(HttpClientBuilder httpClientBuilder, GerritAuthData authData) {
  HttpClientBuilder builder = super.extend(httpClientBuilder, authData);
  builder.setSSLContext(trustAnyX509Certificate);
  builder.setSSLHostnameVerifier(acceptAnyX509Hostname);
  return builder;
}
 
Example #6
Source File: GerritRestClient.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
public GerritRestClient(GerritAuthData authData,
                        HttpRequestExecutor httpRequestExecutor,
                        HttpClientBuilderExtension... httpClientBuilderExtensions) {
    this.authData = authData;
    this.httpRequestExecutor = httpRequestExecutor;
    this.httpClientBuilderExtensions = Arrays.asList(httpClientBuilderExtensions);

    cookieStore = new BasicCookieStore();
    loginCache = new LoginCache(authData, cookieStore);
}
 
Example #7
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhostProjects() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ProjectInfo> projects = gerritClient.projects().list().get();
    System.out.println(String.format("Got %s projects.", projects.size()));
    System.out.println(projects);
}
 
Example #8
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false) // requires running Gerrit instance
public void testBasicRestCallToLocalhost() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic("http://localhost:8080"));
    List<ChangeInfo> changes = gerritClient.changes().query().get();
    System.out.println(String.format("Got %s changes.", changes.size()));
    System.out.println(changes);
}
 
Example #9
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the login cache is used correctly for a host that has GitHub/OAuth
 * configured as authentication method. It tries the first time to get the GerritAccount-cookie
 * and, after having detected the GitHub/OAuth handshake, use HTTP auth.
 */
@Test
public void testGerritWithGitHubOAuth() throws Exception {
    GerritRestClient gerritRestClient = new GerritRestClient(
        new GerritAuthData.Basic(githubOAuthJettyUrl, "foo", "bar"), new HttpRequestExecutor());
    Field loginCacheField = gerritRestClient.getClass().getDeclaredField("loginCache");
    loginCacheField.setAccessible(true);
    LoginCache loginCache = (LoginCache) loginCacheField.get(gerritRestClient);

    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
    requestChanges(gerritRestClient);
    Truth.assertThat(loginCache.getHostSupportsGerritAuth()).isFalse();
    Truth.assertThat(loginCache.getGerritAuthOptional().isPresent()).isFalse();
    Truth.assertThat(loginCache.isGithubOAuthDetected()).isTrue();
}
 
Example #10
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = RestApiException.class)
public void testInvalidHost() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritAuthData.Basic authData = new GerritAuthData.Basic("http://averyinvaliddomainforgerritresttest.com:8089");
    GerritApi gerritClient = gerritRestApiFactory.create(authData);
    gerritClient.changes().query().get();
}
 
Example #11
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Tests authentication with a login which us handled by HTTP auth (preemptive authentication is assumed)
 * (path: "/a/changes/" isn't mapped -> status 404).
 */
@Test
public void testUserAuth() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl, "foo", "bar"));
    boolean catched = false;
    try {
        gerritClient.changes().query().get();
    } catch (HttpStatusException e) {
        catched = true;
        // 404 because this url does not provide a valid response (not set up in this test case)
        Truth.assertThat(e.getStatusCode()).isEqualTo(404);
    }
    Truth.assertThat(catched).isTrue();
}
 
Example #12
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Tests authentication with an invalid HTTP login (preemptive authentication is assumed). Status 401 expected.
 */
@Test
public void testInvalidUserAuth() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl, "foox", "bar"));
    boolean catched = false;
    try {
        gerritClient.changes().query().get();
    } catch (HttpStatusException e) {
        catched = true;
        Truth.assertThat(e.getStatusCode()).isEqualTo(401);
        Truth.assertThat(e.getStatusText().toLowerCase()).contains("unauthorized");
    }
    Truth.assertThat(catched).isTrue();
}
 
Example #13
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testVersion() throws Exception {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    GerritApi gerritClient = gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl));
    String version = gerritClient.config().server().getVersion();
    Truth.assertThat(version).isEqualTo("2.10");
}
 
Example #14
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
public void testUnsupportedHttpMethod() throws Exception {
    GerritRestClient gerritRestClient = new GerritRestClient(
        new GerritAuthData.Basic(jettyUrl), new HttpRequestExecutor());
    gerritRestClient.requestRest("/invalid/", null, GerritRestClient.HttpVerb.HEAD);
}
 
Example #15
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
private GerritRestApi getGerritApiWithJettyHost() {
    GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
    return gerritRestApiFactory.create(new GerritAuthData.Basic(jettyUrl));
}
 
Example #16
Source File: LoginCache.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
public LoginCache(GerritAuthData authData, BasicCookieStore cookieStore) {
    this.authData = authData;
    this.cookieStore = cookieStore;
}
 
Example #17
Source File: HttpClientBuilderExtension.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
public CredentialsProvider extendCredentialProvider(HttpClientBuilder httpClientBuilder, CredentialsProvider credentialsProvider, GerritAuthData authData) {
    return credentialsProvider;
}
 
Example #18
Source File: HttpClientBuilderExtension.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
public HttpClientBuilder extend(HttpClientBuilder httpClientBuilder, GerritAuthData authData) {
    return httpClientBuilder;
}
 
Example #19
Source File: PreemptiveAuthHttpRequestInterceptor.java    From gerrit-rest-java-client with Apache License 2.0 4 votes vote down vote up
PreemptiveAuthHttpRequestInterceptor(GerritAuthData authData) {
    this.authData = authData;
}
 
Example #20
Source File: UserAgentClientBuilderExtension.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public HttpClientBuilder extend(HttpClientBuilder httpClientBuilder, GerritAuthData authData) {
  HttpClientBuilder builder = super.extend(httpClientBuilder, authData);
  httpClientBuilder.addInterceptorLast(new UserAgentHttpRequestInterceptor());
  return builder;
}