package com.auth0.client.mgmt; import com.auth0.json.mgmt.ClientGrant; import com.auth0.net.Request; import okhttp3.mockwebserver.RecordedRequest; import org.junit.Test; import java.util.ArrayList; import java.util.List; import java.util.Map; import static com.auth0.client.MockServer.*; import static com.auth0.client.RecordedRequestMatcher.hasHeader; import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; public class ClientGrantsEntityTest extends BaseMgmtEntityTest { @Test public void shouldListClientGrants() throws Exception { Request<List<ClientGrant>> request = api.clientGrants().list(); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_CLIENT_GRANTS_LIST, 200); List<ClientGrant> response = request.execute(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/client-grants")); assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); assertThat(response, is(notNullValue())); assertThat(response, hasSize(2)); } @Test public void shouldReturnEmptyClientGrants() throws Exception { Request<List<ClientGrant>> request = api.clientGrants().list(); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_EMPTY_LIST, 200); List<ClientGrant> response = request.execute(); assertThat(response, is(notNullValue())); assertThat(response, is(emptyCollectionOf(ClientGrant.class))); } @Test public void shouldThrowOnCreateClientGrantWithNullClientId() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'client id' cannot be null!"); api.clientGrants().create(null, "audience", new String[]{"openid"}); } @Test public void shouldThrowOnCreateClientGrantWithNullAudience() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'audience' cannot be null!"); api.clientGrants().create("clientId", null, new String[]{"openid"}); } @Test public void shouldThrowOnCreateClientGrantWithNullScope() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'scope' cannot be null!"); api.clientGrants().create("clientId", "audience", null); } @Test public void shouldCreateClientGrant() throws Exception { Request<ClientGrant> request = api.clientGrants().create("clientId", "audience", new String[]{"openid"}); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_CLIENT_GRANT, 200); ClientGrant response = request.execute(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/client-grants")); assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map<String, Object> body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(3)); assertThat(body, hasEntry("client_id", (Object) "clientId")); assertThat(body, hasEntry("audience", (Object) "audience")); assertThat(body, hasKey("scope")); assertThat((Iterable<String>) body.get("scope"), contains("openid")); assertThat(response, is(notNullValue())); } @Test public void shouldThrowOnDeleteClientGrantWithNullId() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'client grant id' cannot be null!"); api.clientGrants().delete(null); } @Test public void shouldDeleteClientGrant() throws Exception { Request request = api.clientGrants().delete("1"); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_CLIENT_GRANT, 200); request.execute(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest, hasMethodAndPath("DELETE", "/api/v2/client-grants/1")); assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); } @Test public void shouldThrowOnUpdateClientGrantWithNullId() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'client grant id' cannot be null!"); api.clientGrants().update(null, new String[]{}); } @Test public void shouldThrowOnUpdateClientGrantWithNullScope() throws Exception { exception.expect(IllegalArgumentException.class); exception.expectMessage("'scope' cannot be null!"); api.clientGrants().update("clientGrantId", null); } @Test public void shouldUpdateClientGrant() throws Exception { Request<ClientGrant> request = api.clientGrants().update("1", new String[]{"openid", "profile"}); assertThat(request, is(notNullValue())); server.jsonResponse(MGMT_CLIENT_GRANT, 200); ClientGrant response = request.execute(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest, hasMethodAndPath("PATCH", "/api/v2/client-grants/1")); assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map<String, Object> body = bodyFromRequest(recordedRequest); assertThat(body.size(), is(1)); assertThat((ArrayList<String>) body.get("scope"), contains("openid", "profile")); assertThat(response, is(notNullValue())); } }