From 4dced1f0ef42e7396a62fc32de2370dbc00f6fa1 Mon Sep 17 00:00:00 2001 From: amalhub Date: Mon, 16 Oct 2017 15:23:28 +0530 Subject: [PATCH 1/2] Adding tests for org.wso2.carbon.identity.jwt.client.extension --- .../extension/service/JWTClientUtilTest.java | 105 ++++++++++++++++++ .../repository/conf/etc/jwt.properties | 57 ++++++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 163 insertions(+) create mode 100644 components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/java/org/wso2/carbon/identity/jwt/client/extension/service/JWTClientUtilTest.java create mode 100644 components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties diff --git a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/java/org/wso2/carbon/identity/jwt/client/extension/service/JWTClientUtilTest.java b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/java/org/wso2/carbon/identity/jwt/client/extension/service/JWTClientUtilTest.java new file mode 100644 index 0000000000..603ea9994d --- /dev/null +++ b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/java/org/wso2/carbon/identity/jwt/client/extension/service/JWTClientUtilTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.identity.jwt.client.extension.service; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.HttpVersion; +import org.apache.http.entity.BasicHttpEntity; +import org.apache.http.message.BasicHttpResponse; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.testng.Assert; +import org.testng.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.jwt.client.extension.dto.JWTConfig; +import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; +import org.wso2.carbon.identity.jwt.client.extension.util.JWTClientUtil; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +@PrepareForTest({PrivilegedCarbonContext.class, JWTClientUtil.class}) +public class JWTClientUtilTest { + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + public void init() throws Exception { + + } + + @Test(description = "Test get response string.") + public void testGetResponseString() throws IOException { + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null); + BasicHttpEntity httpEntity = new BasicHttpEntity(); + httpEntity.setContent(new ByteArrayInputStream("test message".getBytes(StandardCharsets.UTF_8.name()))); + response.setEntity(httpEntity); + String result = JWTClientUtil.getResponseString(response); + Assert.assertEquals(result, "test message"); + } + + @Test(description = "Test initialize.") + public void testInitialize() throws Exception { + JWTClientManagerServiceImpl jwtManagerService = new JWTClientManagerServiceImpl(); + Properties prop = new Properties(); + prop.put("default-jwt-client", "true"); + prop.put("TokenEndpoint", "http://example.com"); + jwtManagerService.setDefaultJWTClient(prop); + try { + JWTClientUtil.initialize(jwtManagerService); + } catch (RegistryException e) { + Assert.fail("Test failed", e); + } + } + + @Test(description = "Test generate signed JWT assertion.") + public void testGenerateSignedJWTAssertion() { + Properties prop = new Properties(); + prop.put("default-jwt-client", "true"); + prop.put("TokenEndpoint", "http://example.com"); + try { + JWTClientUtil.generateSignedJWTAssertion("admin", new JWTConfig(prop), true); + } catch (JWTClientException e) { + Assert.fail("Test failed", e); + } + } + + @Test(description = "Test generate signed JWT assertion with claims.") + public void testGenerateSignedJWTAssertionWithClaims() { + Properties prop = new Properties(); + prop.put("default-jwt-client", "true"); + prop.put("TokenEndpoint", "http://example.com"); + try { + Map customClaims = new HashMap<>(); + JWTClientUtil.generateSignedJWTAssertion("admin", new JWTConfig(prop), true, customClaims); + } catch (JWTClientException e) { + Assert.fail("Test failed", e); + } + } +} diff --git a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties new file mode 100644 index 0000000000..0c9641fa2d --- /dev/null +++ b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties @@ -0,0 +1,57 @@ +# +# Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# +# WSO2 Inc. licenses this file to you under the Apache License, +# Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#issuer of the JWT +iss=wso2.org/products/iot + +TokenEndpoint=https://${iot.gateway.host}:${iot.gateway.https.port}/token?tenantDomain=carbon.super + +#audience of JWT claim +#comma seperated values +aud=devicemgt + +#expiration time of JWT (number of minutes from the current time) +exp=1000 + +#issued at time of JWT (number of minutes from the current time) +iat=0 + +#nbf time of JWT (number of minutes from current time) +nbf=0 + +#skew between IDP and issuer(seconds) +skew=0 + +# JWT Id +#jti=token123 + +#KeyStore to cryptographic credentials +#KeyStore=repository/resources/security/wso2carbon.jks + +#Password of the KeyStore +#KeyStorePassword=wso2carbon + +#Alias of the SP's private key +#PrivateKeyAlias=wso2carbon + +#Private key password to retrieve the private key used to sign +#AuthnRequest and LogoutRequest messages +#PrivateKeyPassword=wso2carbon + +#this will be used as the default IDP config if there isn't any config available for tenants. +default-jwt-client=true diff --git a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/testng.xml b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/testng.xml index ca96aae95b..d837c6f8a4 100644 --- a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/testng.xml +++ b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/testng.xml @@ -25,6 +25,7 @@ + \ No newline at end of file From e430efe43b53e2637543bb956c954cf533477309 Mon Sep 17 00:00:00 2001 From: amalhub Date: Mon, 16 Oct 2017 15:34:44 +0530 Subject: [PATCH 2/2] Updating license year --- .../resources/carbon-home/repository/conf/etc/jwt.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties index 0c9641fa2d..6e012b7a04 100644 --- a/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties +++ b/components/identity-extensions/org.wso2.carbon.identity.jwt.client.extension/src/test/resources/carbon-home/repository/conf/etc/jwt.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. # # WSO2 Inc. licenses this file to you under the Apache License, # Version 2.0 (the "License"); you may not use this file except