Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passwordExpiryTime in user claims on request #856

Prev Previous commit
Next Next commit
Add more unit tests
PasinduYeshan committed Nov 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit a61a5d7ce16dac59e1ce4601bf60a45e7c0e970b
Original file line number Diff line number Diff line change
@@ -112,6 +112,7 @@ public class PasswordPolicyUtilsTest {
private final String tenantAwareUsername = "[email protected]";
private final String userId = "testUserId";
private static final long TIME_TOLERANCE_MS = 2000;
private static final int DEFAULT_EXPIRY_DAYS = 30;

private static final Map<String, String> ROLE_MAP = new HashMap<>();
static {
@@ -410,6 +411,70 @@ public void testGetUserPasswordExpiryTime(Integer daysAgo, String[] roles, Strin
}
}

@Test
public void testGetUserPasswordExpiryTime()
throws IdentityGovernanceException, UserStoreException, PostAuthenticationFailedException {

// Case 1: Password expiry disabled.
mockPasswordExpiryEnabled(identityGovernanceService, PasswordPolicyConstants.FALSE);
Long expiryTime =
PasswordPolicyUtils.getUserPasswordExpiryTime(
tenantDomain, tenantAwareUsername, null, null);
Assert.assertNull(expiryTime);

// Case 2: Password expiry enabled, but no rules.
mockPasswordExpiryEnabled(identityGovernanceService, PasswordPolicyConstants.TRUE);
when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(3);
when(realmService.getTenantUserRealm(anyInt())).thenReturn(userRealm);
when(userRealm.getUserStoreManager()).thenReturn(abstractUserStoreManager);
when(userRealm.getClaimManager()).thenReturn(claimManager);
when(abstractUserStoreManager.getUserIDFromUserName(tenantAwareUsername)).thenReturn(userId);
when(UserCoreUtil.addDomainToName(any(), any())).thenReturn(tenantAwareUsername);

// Mock last password update time to 20 days.
Long updateTime = System.currentTimeMillis() - getDaysTimeInMillis(20);
mockLastPasswordUpdateTime(updateTime, abstractUserStoreManager);

// Mock empty password expiry rules.
ConnectorConfig connectorConfig = new ConnectorConfig();
connectorConfig.setProperties( new Property[0]);
when(identityGovernanceService.getConnectorWithConfigs(tenantDomain,
PasswordPolicyConstants.CONNECTOR_CONFIG_NAME)).thenReturn(connectorConfig);

when(identityGovernanceService.getConfiguration(
new String[]{PasswordPolicyConstants.CONNECTOR_CONFIG_PASSWORD_EXPIRY_IN_DAYS},
tenantDomain)).thenReturn(getPasswordExpiryInDaysProperty());
when(identityGovernanceService.getConfiguration(
new String[]{PasswordPolicyConstants.CONNECTOR_CONFIG_SKIP_IF_NO_APPLICABLE_RULES},
tenantDomain)).thenReturn(getSkipIfNoRulesApplicableProperty(PasswordPolicyConstants.FALSE));

expiryTime = PasswordPolicyUtils.getUserPasswordExpiryTime(
tenantDomain, tenantAwareUsername, null, null);

long expectedExpiryTime = updateTime + getDaysTimeInMillis(DEFAULT_EXPIRY_DAYS);
Assert.assertTrue(Math.abs(expiryTime - expectedExpiryTime) <= TIME_TOLERANCE_MS);

// Case 3: Password expiry enabled, no applicable rules, skipIfNoApplicableRules enabled.
when(identityGovernanceService.getConfiguration(
new String[]{PasswordPolicyConstants.CONNECTOR_CONFIG_SKIP_IF_NO_APPLICABLE_RULES},
tenantDomain)).thenReturn(getSkipIfNoRulesApplicableProperty(PasswordPolicyConstants.TRUE));

expiryTime = PasswordPolicyUtils.getUserPasswordExpiryTime(
tenantDomain, tenantAwareUsername, null, null);
Assert.assertNull(expiryTime);

// Case 4: UserStoreException.
when(abstractUserStoreManager.getUserIDFromUserName(tenantAwareUsername)).thenThrow(
new org.wso2.carbon.user.core.UserStoreException());
try {
PasswordPolicyUtils.getUserPasswordExpiryTime(
tenantDomain, tenantAwareUsername, null, null);
Assert.fail("Expected PostAuthenticationFailedException was not thrown");
} catch (Exception e) {
Assert.assertTrue(e instanceof PostAuthenticationFailedException);
}
}

@Test
public void testGetPasswordResetPageUrl() throws Exception {

@@ -512,7 +577,7 @@ private Property[] getPasswordExpiryInDaysProperty() {

Property property1 = new Property();
property1.setName(PasswordPolicyConstants.CONNECTOR_CONFIG_PASSWORD_EXPIRY_IN_DAYS);
property1.setValue(String.valueOf(30));
property1.setValue(String.valueOf(DEFAULT_EXPIRY_DAYS));
Property[] properties = new Property[1];
properties[0] = property1;
return properties;