Search in sources :

Example 66 with Builder

use of okhttp3.FormBody.Builder in project keywhiz by square.

the class GroupResourceTest method createSecret.

private Response createSecret(String group, String secret) throws IOException {
    SecretResourceTest secretResourceTest = new SecretResourceTest();
    secretResourceTest.mutualSslClient = mutualSslClient;
    Response response = secretResourceTest.create(CreateSecretRequestV2.builder().name(secret).content("test").groups(group).build());
    assertThat(response.code()).isEqualTo(201);
    return response;
}
Also used : Response(okhttp3.Response)

Example 67 with Builder

use of okhttp3.FormBody.Builder in project keywhiz by square.

the class GroupResourceTest method createGroup_success.

@Test
public void createGroup_success() throws Exception {
    Response httpResponse = create(CreateGroupRequestV2.builder().name("group1").build());
    assertThat(httpResponse.code()).isEqualTo(201);
    URI location = URI.create(httpResponse.header(LOCATION));
    assertThat(location.getPath()).isEqualTo("/automation/v2/groups/group1");
}
Also used : Response(okhttp3.Response) URI(java.net.URI) Test(org.junit.Test)

Example 68 with Builder

use of okhttp3.FormBody.Builder in project keywhiz by square.

the class SecretResourceTest method secretChangeVersion_invalidVersion.

@Test
public void secretChangeVersion_invalidVersion() throws Exception {
    int totalVersions = 3;
    String name = "secret22";
    List<SecretDetailResponseV2> versions;
    SecretDetailResponseV2 initialCurrentVersion;
    SecretDetailResponseV2 finalCurrentVersion;
    assertThat(listing()).doesNotContain(name);
    // get current time to calculate timestamps off for expiry
    long now = System.currentTimeMillis() / 1000L;
    // Create secrets
    for (int i = 0; i < totalVersions; i++) {
        createOrUpdate(CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString(format("supa secret22_v%d", i).getBytes(UTF_8))).description(format("%s, version %d", name, i)).expiry(now + 86400 * 2).metadata(ImmutableMap.of("version", Integer.toString(i))).build(), name);
    }
    // Get the current version (the last version created)
    initialCurrentVersion = lookup(name);
    assertThat(initialCurrentVersion.name().equals(name));
    assertThat(initialCurrentVersion.description().equals(format("%s, version %d", name, totalVersions)));
    // Get an invalid version of this secret
    versions = listVersions(name, 0, totalVersions);
    Optional<Long> maxValidVersion = versions.stream().map(SecretDetailResponseV2::version).max(Long::compare);
    if (maxValidVersion.isPresent()) {
        // Reset the current version to this version
        Request post = clientRequest(String.format("/automation/v2/secrets/%s/setversion", name)).post(RequestBody.create(JSON, mapper.writeValueAsString(SetSecretVersionRequestV2.builder().name(name).version(maxValidVersion.get() + 1).build()))).build();
        Response httpResponse = mutualSslClient.newCall(post).execute();
        assertThat(httpResponse.code()).isEqualTo(400);
        // Get the current version, which should not have changed
        finalCurrentVersion = lookup(name);
        assertThat(finalCurrentVersion.equals(initialCurrentVersion));
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) SecretDetailResponseV2(keywhiz.api.automation.v2.SecretDetailResponseV2) Test(org.junit.Test)

Example 69 with Builder

use of okhttp3.FormBody.Builder in project keywhiz by square.

the class SecretResourceTest method partialUpdateSecret_notFound.

@Test
public void partialUpdateSecret_notFound() throws Exception {
    PartialUpdateSecretRequestV2 request = PartialUpdateSecretRequestV2.builder().description("a more detailed description").descriptionPresent(true).expiry(1487268151L).expiryPresent(true).build();
    RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(request));
    Request post = clientRequest("/automation/v2/secrets/non-existent/partialupdate").post(body).build();
    Response httpResponse = mutualSslClient.newCall(post).execute();
    assertThat(httpResponse.code()).isEqualTo(404);
}
Also used : Response(okhttp3.Response) PartialUpdateSecretRequestV2(keywhiz.api.automation.v2.PartialUpdateSecretRequestV2) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 70 with Builder

use of okhttp3.FormBody.Builder in project keywhiz by square.

the class SecretResourceTest method partialUpdateSecret_success.

//---------------------------------------------------------------------------------------
// partialUpdateSecret
//---------------------------------------------------------------------------------------
@Test
public void partialUpdateSecret_success() throws Exception {
    // Create a secret to update
    CreateOrUpdateSecretRequestV2 createRequest = CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString("supa secret".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build();
    Response httpResponse = createOrUpdate(createRequest, "secret3");
    assertThat(httpResponse.code()).isEqualTo(201);
    URI location = URI.create(httpResponse.header(LOCATION));
    assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
    // Update the secret's description and set its expiry
    PartialUpdateSecretRequestV2 request = PartialUpdateSecretRequestV2.builder().description("a more detailed description").descriptionPresent(true).expiry(1487268151L).expiryPresent(true).build();
    httpResponse = partialUpdate(request, "secret3");
    assertThat(httpResponse.code()).isEqualTo(201);
    location = URI.create(httpResponse.header(LOCATION));
    assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
}
Also used : Response(okhttp3.Response) PartialUpdateSecretRequestV2(keywhiz.api.automation.v2.PartialUpdateSecretRequestV2) CreateOrUpdateSecretRequestV2(keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)47 Response (okhttp3.Response)44 OkHttpClient (okhttp3.OkHttpClient)31 IOException (java.io.IOException)28 RequestBody (okhttp3.RequestBody)27 Test (org.junit.Test)17 Interceptor (okhttp3.Interceptor)11 File (java.io.File)9 Provides (dagger.Provides)8 X509TrustManager (javax.net.ssl.X509TrustManager)8 Retrofit (retrofit2.Retrofit)8 URI (java.net.URI)7 Map (java.util.Map)7 MultipartBody (okhttp3.MultipartBody)7 Singleton (javax.inject.Singleton)6 HttpUrl (okhttp3.HttpUrl)6 ResponseBody (okhttp3.ResponseBody)6 SSLContext (javax.net.ssl.SSLContext)5 TrustManager (javax.net.ssl.TrustManager)5 TestClients.clientRequest (keywhiz.TestClients.clientRequest)5