Skip to content

Commit

Permalink
Add unit tests to check generated redirect url in fragment and query …
Browse files Browse the repository at this point in the history
…response modes.
  • Loading branch information
anjuchamantha committed Oct 4, 2023
1 parent f63ebe7 commit 89aab77
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.wso2.carbon.identity.oauth2.responsemode.provider;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.oauth2.responsemode.provider.impl.FragmentResponseModeProvider;
import org.wso2.carbon.identity.oauth2.responsemode.provider.impl.QueryResponseModeProvider;

import java.util.Arrays;
import java.util.HashSet;

public class ResponseModeProviderTest {

@DataProvider(name = "fragmentDataProvider")
private Object[][] fragmentDataProvider() {

return new Object[][] {
// AuthorizationResponseDTO, provided callback url, expected redirect url
{getAuthResponseDTO("https://www.google.com/redirects/redirect1", "code1"),
"https://www.google.com/redirects/redirect1",
"https://www.google.com/redirects/redirect1#code=code1&scope=openid"},
{getAuthResponseDTO("https://www.google.com/redirects/redirect2?param1=abc&param2=xyz",
"code2"),
"https://www.google.com/redirects/redirect2?param1=abc&param2=xyz",
"https://www.google.com/redirects/redirect2?param1=abc&param2=xyz#code=code2&scope=openid"},

};
}

@Test(dataProvider = "fragmentDataProvider", description = "Test whether the redirect url generated " +
"by the FragmentResponseModeProvider is correct.")
public void testFragmentRedirectUrl(AuthorizationResponseDTO authorizationResponseDTO, String callbackUrl,
String expectedRedirectUrl) {

FragmentResponseModeProvider fragmentResponseModeProvider = new FragmentResponseModeProvider();
String redirectUrl = fragmentResponseModeProvider.getAuthResponseRedirectUrl(authorizationResponseDTO);

Assert.assertTrue(redirectUrl.contains(callbackUrl), "Redirect url does not " +
"contain the callback url provided.");
Assert.assertTrue(redirectUrl.contains("#"), "Redirect url does not contain a fragment part.");
Assert.assertTrue(redirectUrl.contains("code="), "Redirect url does not contain the authorization code.");
Assert.assertEquals(redirectUrl, expectedRedirectUrl, "Redirect url is not as expected.");
}

@DataProvider(name = "queryDataProvider")
private Object[][] queryDataProvider() {

return new Object[][] {
// AuthorizationResponseDTO, provided callback url, expected redirect url
{getAuthResponseDTO("https://www.google.com/redirects/redirect1", "code1"),
"https://www.google.com/redirects/redirect1",
"https://www.google.com/redirects/redirect1?code=code1&scope=openid"},
{getAuthResponseDTO("https://www.google.com/redirects/redirect2?param1=abc&param2=xyz",
"code2"),
"https://www.google.com/redirects/redirect2?param1=abc&param2=xyz",
"https://www.google.com/redirects/redirect2?param1=abc&param2=xyz&code=code2&scope=openid"},

};
}

@Test(dataProvider = "queryDataProvider", description = "Test whether the redirect url generated " +
"by the QueryResponseModeProvider is correct.")
public void testQueryRedirectUrl(AuthorizationResponseDTO authorizationResponseDTO, String callbackUrl,
String expectedRedirectUrl) {

QueryResponseModeProvider queryResponseModeProvider = new QueryResponseModeProvider();
String redirectUrl = queryResponseModeProvider.getAuthResponseRedirectUrl(authorizationResponseDTO);

Assert.assertTrue(redirectUrl.contains(callbackUrl), "Redirect url does not " +
"contain the callback url provided.");
Assert.assertTrue(redirectUrl.contains("?"), "Redirect url does not contain a query part.");
Assert.assertTrue(redirectUrl.contains("code="), "Redirect url does not contain the authorization code.");
Assert.assertEquals(redirectUrl, expectedRedirectUrl, "Redirect url is not as expected.");
}

/**
* This method creates and returns dummy AuthorizationResponseDTO instance.
* @return AuthorizationResponseDTO DTO
*/
private AuthorizationResponseDTO getAuthResponseDTO(String redirectURI, String code) {

AuthorizationResponseDTO authorizationResponseDTO = new AuthorizationResponseDTO();
authorizationResponseDTO.setRedirectUrl(redirectURI);

authorizationResponseDTO.getSuccessResponseDTO().setAuthorizationCode(code);
authorizationResponseDTO.getSuccessResponseDTO().setScope(new HashSet<>(Arrays.asList("openid")));

return authorizationResponseDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<class name="org.wso2.carbon.identity.openidconnect.DefaultOIDCClaimsCallbackHandlerTest"/>
<class name="org.wso2.carbon.identity.oauth2.validators.jwt.JWKSBasedJWTValidatorTest"/>
<class name="org.wso2.carbon.identity.oauth2.device.codegenerator.GenerateKeysTest"/>
<class name="org.wso2.carbon.identity.oauth2.responsemode.provider.ResponseModeProviderTest"/>
</classes>
</test>

Expand Down

0 comments on commit 89aab77

Please sign in to comment.