Skip to content

Commit

Permalink
Update the tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Malith-19 committed Nov 7, 2024
1 parent 17fdb79 commit d0b5a5e
Showing 1 changed file with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.event.services.IdentityEventService;
import org.wso2.carbon.identity.governance.service.notification.NotificationChannels;
import org.wso2.carbon.identity.recovery.IdentityRecoveryClientException;
import org.wso2.carbon.identity.recovery.IdentityRecoveryConstants;
import org.wso2.carbon.identity.recovery.IdentityRecoveryException;
import org.wso2.carbon.identity.recovery.RecoveryScenarios;
import org.wso2.carbon.identity.recovery.RecoverySteps;
import org.wso2.carbon.identity.recovery.dto.NotificationChannelDTO;
import org.wso2.carbon.identity.recovery.dto.RecoveryChannelInfoDTO;
import org.wso2.carbon.identity.recovery.dto.RecoveryInformationDTO;
import org.wso2.carbon.identity.recovery.dto.UsernameRecoverDTO;
Expand All @@ -44,6 +46,7 @@
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand All @@ -55,6 +58,7 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

/**
* Test class for UsernameRecoveryManagerImpl.
Expand Down Expand Up @@ -156,9 +160,10 @@ public void testConfigValidation() throws IdentityRecoveryException {
*/
@DataProvider
public Object[][] channelIDProvider() {
return new Object[][] {
{ null },
{ "0" }

return new Object[][]{
{null},
{"0"}
};
}

Expand Down Expand Up @@ -192,7 +197,8 @@ public void testInvalidateRecoveryCode() throws IdentityRecoveryException {
Map<String, String> properties = new HashMap<>();
properties.put("useLegacyAPI", FALSE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(Utils.getRecoveryConfigs(anyString(), anyString())).thenReturn(TRUE);
Expand All @@ -211,7 +217,8 @@ public void testInvalidateRecoveryCodeWithException() throws IdentityRecoveryExc
Map<String, String> properties = new HashMap<>();
properties.put("useLegacyAPI", FALSE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRecoveryFlowId()).thenReturn("FlowID");
Expand All @@ -231,7 +238,8 @@ public void testExtractChannelDetails() throws IdentityRecoveryException {
Map<String, String> properties = new HashMap<>();
properties.put("useLegacyAPI", FALSE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRemainingSetIds()).thenReturn("123");
Expand All @@ -253,7 +261,8 @@ public void testNotifyUser() throws IdentityRecoveryException {
Map<String, String> properties = new HashMap<>();
properties.put("useLegacyAPI", FALSE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRemainingSetIds()).thenReturn("EXTERNAL,EXTERNAL");
Expand Down Expand Up @@ -282,7 +291,8 @@ public void testNotifyUserException() throws IdentityRecoveryException {
Map<String, String> properties = new HashMap<>();
properties.put("useLegacyAPI", FALSE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRemainingSetIds()).thenReturn("SMS,SMS");
Expand All @@ -294,7 +304,8 @@ public void testNotifyUserException() throws IdentityRecoveryException {
when(mockUserRecoveryData.getUser()).thenReturn(mockUser);
UsernameRecoverDTO result = usernameRecoveryManager.notify(recoveryCode, "2", TENANT_DOMAIN, properties);
assertEquals(result.getCode(), "UNR-02001");
assertEquals(result.getMessage(), "Username recovery information sent via user preferred notification channel.");
assertEquals(result.getMessage(),
"Username recovery information sent via user preferred notification channel.");
}

/**
Expand All @@ -312,7 +323,8 @@ public void testCallbackURLValidation() throws IdentityRecoveryException {
properties.put("useLegacyAPI", TRUE);
properties.put(IdentityRecoveryConstants.CALLBACK, callbackURL);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRemainingSetIds()).thenReturn("SMS,SMS");
Expand Down Expand Up @@ -343,13 +355,15 @@ public void testCallbackURLDecoding() throws IdentityRecoveryException {
properties.put("useLegacyAPI", TRUE);
properties.put(IdentityRecoveryConstants.CALLBACK, callbackURL);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserRecoveryData(recoveryCode, RecoverySteps.SEND_RECOVERY_INFORMATION))
.thenReturn(mockUserRecoveryData);
when(mockUserRecoveryData.getRemainingSetIds()).thenReturn("SMS,SMS");
when(Utils.getRecoveryConfigs(anyString(), anyString())).thenReturn(TRUE);
when(Utils.resolveEventName(anyString())).thenReturn("TRIGGER_SMS_NOTIFICATION_LOCAL");
mockURLDecoder.when(() -> URLDecoder.decode(anyString(), anyString())).thenThrow(new UnsupportedEncodingException());
mockURLDecoder.when(() -> URLDecoder.decode(anyString(), anyString()))
.thenThrow(new UnsupportedEncodingException());
usernameRecoveryManager.notify(recoveryCode, "2", TENANT_DOMAIN, properties);
}

Expand All @@ -371,7 +385,8 @@ public void testInitiateRecoveryWithNullUsername() throws IdentityRecoveryExcept
properties.put("useLegacyAPI", TRUE);
when(Utils.getRecoveryConfigs(anyString(), anyString())).thenReturn(TRUE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserListByClaims(null, TENANT_DOMAIN)).thenReturn(userList);
when(IdentityUtil.getProperty(anyString())).thenReturn(TRUE);
when(Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_USER_FOUND, null))
Expand All @@ -396,7 +411,8 @@ public void testInitiateRecoveryValidUsername() throws IdentityRecoveryException
properties.put("useLegacyAPI", TRUE);
when(Utils.getRecoveryConfigs(anyString(), anyString())).thenReturn(TRUE);
mockedJDBCRecoveryDataStore.when(JDBCRecoveryDataStore::getInstance).thenReturn(mockUserRecoveryDataStore);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance).thenReturn(mockUserAccountRecoveryManager);
mockedRecoveryManagerStatic.when(UserAccountRecoveryManager::getInstance)
.thenReturn(mockUserAccountRecoveryManager);
when(mockUserAccountRecoveryManager.getUserListByClaims(null, TENANT_DOMAIN)).thenReturn(userList);
when(IdentityUtil.getProperty(anyString())).thenReturn(TRUE);
when(Utils.isNotificationsInternallyManaged(TENANT_DOMAIN, properties)).thenReturn(true);
Expand All @@ -414,9 +430,24 @@ public void testInitiateRecoveryValidUsername() throws IdentityRecoveryException
@Test
public void testInitiateRecoveryWithUseLegacyAPIFalse() throws IdentityRecoveryException {

String TEST_USER = "testUser";
mockIdentityEventService();
RecoveryChannelInfoDTO recoveryChannelInfoDTO = new RecoveryChannelInfoDTO();
recoveryChannelInfoDTO.setUsername("testUser");
List<NotificationChannelDTO> notificationChannelDTOList = new ArrayList<>();

NotificationChannelDTO notificationChannelDTO1 = new NotificationChannelDTO();
notificationChannelDTO1.setId(1);
notificationChannelDTO1.setType(NotificationChannels.EMAIL_CHANNEL.getChannelType());

NotificationChannelDTO notificationChannelDTO2 = new NotificationChannelDTO();
notificationChannelDTO2.setId(2);
notificationChannelDTO2.setType(NotificationChannels.SMS_CHANNEL.getChannelType());

notificationChannelDTOList.add(notificationChannelDTO1);
notificationChannelDTOList.add(notificationChannelDTO2);
recoveryChannelInfoDTO.setNotificationChannelDTOs(
notificationChannelDTOList.toArray(new NotificationChannelDTO[0]));

Map<String, String> properties = new HashMap<>();
when(Utils.getRecoveryConfigs(anyString(), anyString())).thenReturn(TRUE);
Expand All @@ -431,7 +462,8 @@ public void testInitiateRecoveryWithUseLegacyAPIFalse() throws IdentityRecoveryE
any())).thenReturn(recoveryChannelInfoDTO);
when(Utils.isNotificationsInternallyManaged(TENANT_DOMAIN, properties)).thenReturn(true);
RecoveryInformationDTO result = usernameRecoveryManager.initiate(null, TENANT_DOMAIN, properties);
assertEquals(result.getRecoveryChannelInfoDTO().getUsername(), "testUser");
assertEquals(result.getRecoveryChannelInfoDTO().getUsername(), TEST_USER);
assertEquals(result.getRecoveryChannelInfoDTO().getNotificationChannelDTOs().length, 2);

// Case 2: When Recovery.Notification.Username.NonUniqueUsername is disabled.
when(IdentityUtil.getProperty(
Expand All @@ -440,7 +472,19 @@ public void testInitiateRecoveryWithUseLegacyAPIFalse() throws IdentityRecoveryE
eq(RecoveryScenarios.USERNAME_RECOVERY),
any())).thenReturn(recoveryChannelInfoDTO);
result = usernameRecoveryManager.initiate(null, TENANT_DOMAIN, properties);
assertEquals(result.getRecoveryChannelInfoDTO().getUsername(), "testUser");
assertEquals(result.getRecoveryChannelInfoDTO().getUsername(), TEST_USER);
assertEquals(result.getRecoveryChannelInfoDTO().getNotificationChannelDTOs().length, 2);

// Case 3: Disable the sms channel.
when(Utils.getRecoveryConfigs(eq(IdentityRecoveryConstants.ConnectorConfig.USERNAME_RECOVERY_SMS_ENABLE),
eq(TENANT_DOMAIN)))
.thenReturn(FALSE);
result = usernameRecoveryManager.initiate(null, TENANT_DOMAIN, properties);
assertEquals(result.getRecoveryChannelInfoDTO().getUsername(), TEST_USER);
assertEquals(result.getRecoveryChannelInfoDTO().getNotificationChannelDTOs().length, 1);
assertEquals(result.getRecoveryChannelInfoDTO().getNotificationChannelDTOs()[0].getType(),
NotificationChannels.EMAIL_CHANNEL.getChannelType());

}

/**
Expand Down

0 comments on commit d0b5a5e

Please sign in to comment.