ClientCredentialsServerResourceTest.java revision ec6f3779c641cb13f915072f87a5d11a6b74ba0c
/*
* DO NOT REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012 ForgeRock Inc. All rights reserved.
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* http://forgerock.org/license/CDDLv1.0.html
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at http://forgerock.org/license/CDDLv1.0.html
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
package org.forgerock.restlet.ext.oauth2.flow;
import static org.fest.assertions.Assertions.assertThat;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.util.Map;
import org.fest.assertions.Condition;
import org.fest.assertions.MapAssert;
import org.forgerock.restlet.ext.oauth2.OAuth2;
import org.forgerock.restlet.ext.oauth2.consumer.BearerOAuth2Proxy;
import org.forgerock.restlet.ext.oauth2.consumer.BearerToken;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.testng.annotations.Test;
/**
* @author $author$
* @version $Revision$ $Date$
*/
public class ClientCredentialsServerResourceTest extends AbstractFlowTest {
@Test
public void testValidRequest() throws Exception {
Reference reference = new Reference("riap://component/test/oauth2/access_token");
Request request = new Request(Method.POST, reference);
Response response = new Response(request);
Form parameters = new Form();
parameters.add(OAuth2.Params.GRANT_TYPE, OAuth2.TokeEndpoint.CLIENT_CREDENTIALS);
parameters.add(OAuth2.Params.SCOPE, "read write");
request.setEntity(parameters.getWebRepresentation());
// handle
getClient().handle(request, response);
assertTrue(MediaType.APPLICATION_JSON.equals(response.getEntity().getMediaType()));
JacksonRepresentation<Map> representation =
new JacksonRepresentation<Map>(response.getEntity(), Map.class);
// assert
assertThat(representation.getObject()).includes(
MapAssert.entry(OAuth2.Params.TOKEN_TYPE, OAuth2.Bearer.BEARER),
MapAssert.entry(OAuth2.Params.EXPIRES_IN, 3600)).is(new Condition<Map<?, ?>>() {
@Override
public boolean matches(Map<?, ?> value) {
return value.containsKey(OAuth2.Params.ACCESS_TOKEN);
}
});
}
@Test
public void testProxy() throws Exception {
BearerOAuth2Proxy auth2Proxy = BearerOAuth2Proxy.popOAuth2Proxy(component.getContext());
assertNotNull(auth2Proxy);
BearerToken token = auth2Proxy.flowClientCredentials();
assertNotNull(token);
}
}