Search in sources :

Example 16 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidScopeExceptionWhenScopeIsNotASetOfStrings.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidScopeExceptionWhenScopeIsNotASetOfStrings() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    ResourceSetDescription resourceSetDescription = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\", \"scopes\":\"SCOPE\"}");
    given(resourceSetStore.read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID")).willReturn(resourceSetDescription);
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_scope");
        assertThat(e.getMessage()).contains("Required attribute", "'scopes'", "must be an array of Strings");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Test(org.testng.annotations.Test)

Example 17 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldReturnPermissionTicket.

@Test
@SuppressWarnings("unchecked")
public void shouldReturnPermissionTicket() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\", " + "\"scopes\":[\"SCOPE_A\", \"SCOPE_B\"]}");
    setupResourceSetStore();
    PermissionTicket ticket = new PermissionTicket("abc", null, null, null);
    given(umaTokenStore.createPermissionTicket(eq("RESOURCE_SET_ID"), anySetOf(String.class), eq("CLIENT_ID"))).willReturn(ticket);
    //When
    Representation responseBody = endpoint.registerPermissionRequest(entity);
    //Then
    Map<String, String> permissionTicket = (Map<String, String>) new ObjectMapper().readValue(responseBody.getText(), Map.class);
    assertThat(permissionTicket).containsEntry("ticket", "abc");
    verify(permissionRequestFilter).onPermissionRequest(any(ResourceSetDescription.class), anySetOf(String.class), anyString());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(201);
}
Also used : Status(org.restlet.data.Status) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Map(java.util.Map) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 18 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidScopeExceptionWhenNoScope.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidScopeExceptionWhenNoScope() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    ResourceSetDescription resourceSetDescription = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\"}");
    given(resourceSetStore.read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID")).willReturn(resourceSetDescription);
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_scope");
        assertThat(e.getMessage()).contains("Missing required attribute", "'scopes'");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Test(org.testng.annotations.Test)

Example 19 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidResourceSetIdExceptionWhenNoResourceSetId.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidResourceSetIdExceptionWhenNoResourceSetId() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("");
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_resource_set_id");
        assertThat(e.getMessage()).contains("Missing required attribute", "'resource_set_id'");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Test(org.testng.annotations.Test)

Example 20 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidResourceSetIdExceptionWhenResourceSetIdIsNotAString.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidResourceSetIdExceptionWhenResourceSetIdIsNotAString() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":[]}");
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_resource_set_id");
        assertThat(e.getMessage()).contains("Required attribute", "'resource_set_id'", "must be a String");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Test(org.testng.annotations.Test)

Aggregations

JsonRepresentation (org.restlet.ext.json.JsonRepresentation)22 JSONObject (org.json.JSONObject)16 Test (org.testng.annotations.Test)13 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)7 Representation (org.restlet.representation.Representation)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Map (java.util.Map)5 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Date (java.util.Date)2 AuditEvent (org.forgerock.audit.events.AuditEvent)2 JsonValue (org.forgerock.json.JsonValue)2 OAuth2Exception (org.forgerock.oauth2.core.exceptions.OAuth2Exception)2 OAuth2RestletException (org.forgerock.oauth2.restlet.OAuth2RestletException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 TextMatch (org.opensextant.extraction.TextMatch)2