From 53dc5c437e27e8fd318371f43c21afea8d25ed47 Mon Sep 17 00:00:00 2001 From: dhaura Date: Tue, 9 Apr 2024 11:03:05 +0530 Subject: [PATCH] Add unit tests for getApplicationDetails() and extractScopesFromURL() methods. --- .../oidc/OpenIDConnectAuthenticatorTest.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.application.authenticator.oidc/src/test/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticatorTest.java b/components/org.wso2.carbon.identity.application.authenticator.oidc/src/test/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticatorTest.java index 38d06d20..6b0750cb 100644 --- a/components/org.wso2.carbon.identity.application.authenticator.oidc/src/test/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticatorTest.java +++ b/components/org.wso2.carbon.identity.application.authenticator.oidc/src/test/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticatorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, WSO2 LLC. (http://www.wso2.com). + * Copyright (c) 2017-2024, WSO2 LLC. (http://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -43,7 +43,9 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; +import org.wso2.carbon.identity.application.authentication.framework.config.model.ApplicationConfig; import org.wso2.carbon.identity.application.authentication.framework.config.model.ExternalIdPConfig; +import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException; import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException; @@ -57,6 +59,7 @@ import org.wso2.carbon.identity.application.common.model.ClaimMapping; import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.common.model.IdentityProviderProperty; +import org.wso2.carbon.identity.application.common.model.ServiceProvider; import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants; import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils; import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementService; @@ -1161,6 +1164,25 @@ public Object[][] shareFederatedTokenParamsForAdaptiveScriptConfigs() { }; } + /** + * This method generates test criteria on extracting scopes from given URL query parameters. + * + * @return Object[][] The test criteria. + */ + @DataProvider(name = "authorizeUrls") + public Object[][] authorizeUrls() { + + return new Object[][]{ + {"https://localhost:9443/oauth2/authorize?scope=openid+profile+email&clientID=aishf784hwbf947gfo", + "openid profile email"}, + {"https://localhost:9443/oauth2/authorize?scope=openid+profile+email", "openid profile email"}, + {"https://localhost:9443/oauth2/authorize?scope=openid", "openid"}, + {"https://localhost:9443/oauth2/authorize?clientID=aishf784hwbf947gfo", ""}, + {"https://localhost:9443/oauth2/authorize", ""}, + {"", ""} + }; + } + @Test(dataProvider = "shareFederatedTokenParamsForAdaptiveScriptConfigs") public void testShareFederatedTokenParamsForAdaptiveScriptConfigs(String errorMessage, String enableShareTokenIDPConfig, @@ -1213,6 +1235,35 @@ public void testShareFederatedTokenForIDPConfigAndQueryParameter(String errorMes } } + @Test + public void testGetApplicationDetails() throws Exception { + + AuthenticationContext mockedAuthenticationContext = mock(AuthenticationContext.class); + when(mockedAuthenticationContext.getServiceProviderName()).thenReturn("Test App"); + + SequenceConfig mockedSequenceConfig = mock(SequenceConfig.class); + ApplicationConfig mockedApplicationConfig = mock(ApplicationConfig.class); + ServiceProvider mockedServiceProvider = mock(ServiceProvider.class); + when(mockedAuthenticationContext.getSequenceConfig()).thenReturn(mockedSequenceConfig); + when(mockedSequenceConfig.getApplicationConfig()).thenReturn(mockedApplicationConfig); + when(mockedApplicationConfig.getServiceProvider()).thenReturn(mockedServiceProvider); + when(mockedServiceProvider.getApplicationResourceId()).thenReturn("test-app-resource-id"); + + Map applicationDetails = + openIDConnectAuthenticator.getApplicationDetails(mockedAuthenticationContext); + assertEquals(applicationDetails.get("application name"), "Test App", + "Application name is not set properly."); + assertEquals(applicationDetails.get("app id"), "test-app-resource-id", + "Application id is not set properly."); + } + + @Test(dataProvider = "authorizeUrls") + public void testExtractScopesFromURL(String url, String expectedScopes) throws Exception { + + String scopes = openIDConnectAuthenticator.extractScopesFromURL(url); + assertEquals(scopes, expectedScopes, "Scopes are not extracted properly."); + } + /** * This method generates an authentication instance for the federated token sharing. *