Skip to content

Commit

Permalink
test: Add tests on OpenFgaClient with static & credential-based OAuth…
Browse files Browse the repository at this point in the history
…2 tokens
  • Loading branch information
booniepepper committed Sep 25, 2023
1 parent 4d4f5bc commit 62176c2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import java.util.concurrent.CompletableFuture;

public class OpenFgaClient {
private final ClientConfiguration configuration;
private final OpenFgaApi api;
private ApiClient apiClient;
private ClientConfiguration configuration;
private OpenFgaApi api;

private static final String CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id";
private static final String CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method";
private static final int DEFAULT_MAX_METHOD_PARALLEL_REQS = 10;

public OpenFgaClient(ApiClient apiClient, ClientConfiguration configuration) throws FgaInvalidParameterException {
this.apiClient = apiClient;
this.configuration = configuration;
this.api = new OpenFgaApi(apiClient, configuration);
}
Expand All @@ -45,6 +47,11 @@ public void setAuthorizationModelId(String authorizationModelId) {
configuration.authorizationModelId(authorizationModelId);
}

public void setConfiguration(ClientConfiguration configuration) throws FgaInvalidParameterException {
this.configuration = configuration;
this.api = new OpenFgaApi(apiClient, configuration);
}

/* ********
* Stores *
**********/
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,87 @@ public void beforeEachTest() throws Exception {
fga = new OpenFgaClient(mockApiClient, clientConfiguration);
}

/* ******************
* Credential tests *
********************/
@Test
public void createStore_withApiToken() throws Exception {
// Given
String apiToken = "some-static-token";
clientConfiguration.credentials(new Credentials(new ApiToken(apiToken)));
fga.setConfiguration(clientConfiguration);
String expectedBody = String.format("{\"name\":\"%s\"}", DEFAULT_STORE_NAME);
String requestBody = String.format("{\"id\":\"%s\",\"name\":\"%s\"}", DEFAULT_STORE_ID, DEFAULT_STORE_NAME);
mockHttpClient
.onPost("https://localhost/stores")
.withBody(is(expectedBody))
.withHeader("Authorization", String.format("Bearer %s", apiToken))
.doReturn(201, requestBody);
CreateStoreRequest request = new CreateStoreRequest().name(DEFAULT_STORE_NAME);

// When
CreateStoreResponse response = fga.createStore(request).get();

// Then
mockHttpClient
.verify()
.post("https://localhost/stores")
.withBody(is(expectedBody))
.withHeader("Authorization", String.format("Bearer %s", apiToken))
.called(1);
assertEquals(DEFAULT_STORE_ID, response.getId());
assertEquals(DEFAULT_STORE_NAME, response.getName());
}

@Test
public void createStore_withClientCredentials() throws Exception {
// Given
String apiTokenIssuer = "oauth2.server";
String clientId = "some-client-id";
String clientSecret = "some-client-secret";
String apiToken = "some-generated-token";
String apiAudience = "some-audience";
clientConfiguration.credentials(new Credentials(new ClientCredentials()
.clientId(clientId)
.clientSecret(clientSecret)
.apiTokenIssuer(apiTokenIssuer)
.apiAudience(apiAudience)));
fga.setConfiguration(clientConfiguration);

String expectedOAuth2Body = String.format(
"{\"client_id\":\"%s\",\"client_secret\":\"%s\",\"audience\":\"%s\",\"grant_type\":\"client_credentials\"}",
clientId, clientSecret, apiAudience);
String expectedBody = String.format("{\"name\":\"%s\"}", DEFAULT_STORE_NAME);
String requestBody = String.format("{\"id\":\"%s\",\"name\":\"%s\"}", DEFAULT_STORE_ID, DEFAULT_STORE_NAME);
mockHttpClient
.onPost(String.format("https://%s/oauth/token", apiTokenIssuer))
.withBody(is(expectedOAuth2Body))
.doReturn(200, String.format("{\"access_token\":\"%s\"}", apiToken));
mockHttpClient
.onPost("https://localhost/stores")
.withBody(is(expectedBody))
.withHeader("Authorization", String.format("Bearer %s", apiToken))
.doReturn(201, requestBody);
CreateStoreRequest request = new CreateStoreRequest().name(DEFAULT_STORE_NAME);

// When
CreateStoreResponse response = fga.createStore(request).get();

// Then
mockHttpClient
.verify()
.post(String.format("https://%s/oauth/token", apiTokenIssuer))
.called(1);
mockHttpClient
.verify()
.post("https://localhost/stores")
.withBody(is(expectedBody))
.withHeader("Authorization", String.format("Bearer %s", apiToken))
.called(1);
assertEquals(DEFAULT_STORE_ID, response.getId());
assertEquals(DEFAULT_STORE_NAME, response.getName());
}

/**
* List all stores.
*/
Expand Down

0 comments on commit 62176c2

Please sign in to comment.