From a838590e317751fb7560ebfcc24a86029466e1d7 Mon Sep 17 00:00:00 2001 From: megala21 Date: Wed, 11 Oct 2017 21:31:13 +0530 Subject: [PATCH] Refactoring --- .../mgt/extensions/utils/UtilsTest.java | 1 - .../authenticator/framework/Utils/Utils.java | 5 ----- .../BaseWebAppAuthenticatorFrameworkTest.java | 21 +++++++++++++++++-- .../BasicAuthAuthenticatorTest.java | 15 +++++++------ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java index 803823d836..96f4c28694 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/utils/UtilsTest.java @@ -138,7 +138,6 @@ public class UtilsTest { ComplianceFeature complianceFeature = new ComplianceFeature(); complianceFeature.setCompliance(true); complianceFeatures.add(complianceFeature); - ComplianceFeature nonCompliant = new ComplianceFeature(); nonCompliant.setCompliance(false); complianceFeatures.add(nonCompliant); diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/Utils.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/Utils.java index 7224575298..db3b71ba7c 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/Utils.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/Utils.java @@ -16,13 +16,11 @@ * under the License. */ - package org.wso2.carbon.webapp.authenticator.framework.Utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.user.api.TenantManager; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; @@ -105,7 +103,6 @@ public class Utils { throw new IllegalArgumentException( "Username to connect to the OAuth token validation endpoint is not provided"); } - String adminPassword = properties.getProperty("Password"); if (adminPassword == null) { throw new IllegalArgumentException( @@ -134,7 +131,6 @@ public class Utils { if (oAuthValidationResponse.isValid()) { String username = oAuthValidationResponse.getUserName(); String tenantDomain = oAuthValidationResponse.getTenantDomain(); - authenticationInfo.setUsername(username); authenticationInfo.setTenantDomain(tenantDomain); authenticationInfo.setTenantId(getTenantIdOFUser(username + "@" + tenantDomain)); @@ -144,7 +140,6 @@ public class Utils { authenticationInfo.setMessage(oAuthValidationResponse.getErrorMsg()); } return authenticationInfo; - } private static String replaceSystemProperty(String urlWithPlaceholders) { diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/BaseWebAppAuthenticatorFrameworkTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/BaseWebAppAuthenticatorFrameworkTest.java index d6210dda36..d4bcaf2e20 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/BaseWebAppAuthenticatorFrameworkTest.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/BaseWebAppAuthenticatorFrameworkTest.java @@ -1,3 +1,21 @@ +/* + * 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.webapp.authenticator.framework; import org.testng.annotations.BeforeSuite; @@ -44,7 +62,6 @@ public class BaseWebAppAuthenticatorFrameworkTest { .getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getUserStoreManager(); Permission adminPermission = new Permission(PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH, CarbonConstants.UI_PERMISSION_ACTION); - userStoreManager.addRole(ADMIN_ROLE + "t", new String[] { ADMIN_USER }, new Permission[] { adminPermission }); - + userStoreManager.addRole(ADMIN_ROLE + "t", new String[] { ADMIN_USER }, new Permission[] { adminPermission }); } } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticatorTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticatorTest.java index e5ca9f0f86..2553f04423 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticatorTest.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/BasicAuthAuthenticatorTest.java @@ -49,6 +49,7 @@ public class BasicAuthAuthenticatorTest { private MimeHeaders mimeHeaders; private org.apache.coyote.Request coyoteRequest; private MessageBytes bytes; + private final String BASIC_HEADER = "basic "; @BeforeTest public void init() throws NoSuchFieldException { @@ -73,13 +74,12 @@ public class BasicAuthAuthenticatorTest { "Without proper Authentication headers request can be handled by BasicAuthAuthenticator."); coyoteRequest = new org.apache.coyote.Request(); mimeHeaders = new MimeHeaders(); - bytes = mimeHeaders.addValue("Authorization"); + bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString("test"); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); Assert.assertFalse(basicAuthAuthenticator.canHandle(request), "With a different authorization header Basic Authenticator can handle the request"); - } @Test(description = "This method tests the canHandle method when all the required parameters are given with the " @@ -90,8 +90,8 @@ public class BasicAuthAuthenticatorTest { context.addParameter("basicAuth", "true"); request.setContext(context); mimeHeaders = new MimeHeaders(); - bytes = mimeHeaders.addValue("Authorization"); - bytes.setString("basic "); + bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); + bytes.setString(BASIC_HEADER); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); Assert.assertTrue(basicAuthAuthenticator.canHandle(request), @@ -108,7 +108,7 @@ public class BasicAuthAuthenticatorTest { request.setContext(context); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); - bytes.setString("basic " + encodedString); + bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); @@ -131,7 +131,7 @@ public class BasicAuthAuthenticatorTest { String encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":test" + ADMIN_USER).getBytes())); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); - bytes.setString("basic " + encodedString); + bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); @@ -142,13 +142,12 @@ public class BasicAuthAuthenticatorTest { encodedString = new String(Base64.getEncoder().encode((ADMIN_USER).getBytes())); mimeHeaders = new MimeHeaders(); bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); - bytes.setString("basic " + encodedString); + bytes.setString(BASIC_HEADER + encodedString); coyoteRequest = new org.apache.coyote.Request(); headersField.set(coyoteRequest, mimeHeaders); request.setCoyoteRequest(coyoteRequest); authenticationInfo = basicAuthAuthenticator.authenticate(request, null); Assert.assertEquals(authenticationInfo.getStatus(), WebappAuthenticator.Status.FAILURE, "For a request with missing password authentication succeeded."); - } }