forked from community/device-mgt-core
Merge pull request #1071 from amalhub/master
Adding tests for org.wso2.carbon.identity.jwt.client.extensionrevert-70aa11f8
commit
30ce5b1404
@ -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<String, String> customClaims = new HashMap<>();
|
||||
JWTClientUtil.generateSignedJWTAssertion("admin", new JWTConfig(prop), true, customClaims);
|
||||
} catch (JWTClientException e) {
|
||||
Assert.fail("Test failed", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
#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
|
Loading…
Reference in new issue