diff --git a/src/main/java/com/descope/sdk/mgmt/UserService.java b/src/main/java/com/descope/sdk/mgmt/UserService.java index e096f281..022d5be0 100644 --- a/src/main/java/com/descope/sdk/mgmt/UserService.java +++ b/src/main/java/com/descope/sdk/mgmt/UserService.java @@ -240,6 +240,22 @@ UserResponseDetails updateEmail(String loginId, String email, Boolean isVerified * Update the email address for an existing user. * * @param loginId The loginID is required. + * @param email The email parameter can be empty in which case the email will be removed. + * @param isVerified The isVerified flag must be true for the user to be able to login with the + * email address. + * @param failOnConflict The failOnConflict flag indicates whether to fail the update if the new + * email is also the login ID of another user. + * @return {@link UserResponseDetails UserResponseDetails} + * @throws DescopeException If there occurs any exception, a subtype of this exception will be + * thrown. + */ + UserResponseDetails updateEmail(String loginId, String email, Boolean isVerified, + Boolean failOnConflict) throws DescopeException; + + /** + * Update the phone number for an existing user. + * + * @param loginId The loginID is required. * @param phone The phone parameter can be empty in which case the phone will be removed. * @param isVerified The isVerified flag must be true for the user to be able to login with the * email address. @@ -250,6 +266,22 @@ UserResponseDetails updateEmail(String loginId, String email, Boolean isVerified UserResponseDetails updatePhone(String loginId, String phone, Boolean isVerified) throws DescopeException; + /** + * Update the phone number for an existing user. + * + * @param loginId The loginID is required. + * @param phone The phone parameter can be empty in which case the phone will be removed. + * @param isVerified The isVerified flag must be true for the user to be able to login with the + * email address. + * @param failOnConflict The failOnConflict flag indicates whether to fail the update if the new + * phone number is also the login ID of another user. + * @return {@link UserResponseDetails UserResponseDetails} + * @throws DescopeException If there occurs any exception, a subtype of this exception will be + * thrown. + */ + UserResponseDetails updatePhone(String loginId, String phone, Boolean isVerified, + Boolean failOnConflict) throws DescopeException; + /** * Update an existing user's display name (i.e., their full name). * diff --git a/src/main/java/com/descope/sdk/mgmt/impl/UserServiceImpl.java b/src/main/java/com/descope/sdk/mgmt/impl/UserServiceImpl.java index 47cc8159..9499d78c 100644 --- a/src/main/java/com/descope/sdk/mgmt/impl/UserServiceImpl.java +++ b/src/main/java/com/descope/sdk/mgmt/impl/UserServiceImpl.java @@ -382,22 +382,36 @@ public UserResponseDetails deactivate(String loginId) throws DescopeException { @Override public UserResponseDetails updateEmail(String loginId, String email, Boolean isVerified) throws DescopeException { + return updateEmail(loginId, email, isVerified, false); + } + + @Override + public UserResponseDetails updateEmail(String loginId, String email, Boolean isVerified, + Boolean failOnConflict) throws DescopeException { if (StringUtils.isBlank(loginId)) { throw ServerCommonException.invalidArgument("Login ID"); } URI updateEmailUri = composeUpdateEmailUri(); - Map request = mapOf("loginId", loginId, "email", email, "verified", isVerified); + Map request = mapOf("loginId", loginId, "email", email, "verified", isVerified, + "failOnConflict", failOnConflict); ApiProxy apiProxy = getApiProxy(); return apiProxy.post(updateEmailUri, request, UserResponseDetails.class); } @Override public UserResponseDetails updatePhone(String loginId, String phone, Boolean isVerified) throws DescopeException { + return updatePhone(loginId, phone, isVerified, false); + } + + @Override + public UserResponseDetails updatePhone(String loginId, String phone, Boolean isVerified, + Boolean failOnConflict) throws DescopeException { if (StringUtils.isBlank(loginId)) { throw ServerCommonException.invalidArgument("Login ID"); } URI updatePhoneUri = composeUpdatePhoneUri(); - Map request = mapOf("loginId", loginId, "phone", phone, "verified", isVerified); + Map request = mapOf("loginId", loginId, "phone", phone, "verified", isVerified, + "failOnConflict", failOnConflict); ApiProxy apiProxy = getApiProxy(); return apiProxy.post(updatePhoneUri, request, UserResponseDetails.class); } diff --git a/src/test/java/com/descope/sdk/mgmt/impl/UserServiceImplTest.java b/src/test/java/com/descope/sdk/mgmt/impl/UserServiceImplTest.java index 6d6400d0..ecdf542b 100644 --- a/src/test/java/com/descope/sdk/mgmt/impl/UserServiceImplTest.java +++ b/src/test/java/com/descope/sdk/mgmt/impl/UserServiceImplTest.java @@ -367,6 +367,18 @@ void testUpdateEmailForSuccess() { } } + @Test + void testUpdateEmailWithFailOnConflictForSuccess() { + UserResponseDetails userResponseDetails = mock(UserResponseDetails.class); + ApiProxy apiProxy = mock(ApiProxy.class); + doReturn(userResponseDetails).when(apiProxy).post(any(), any(), any()); + try (MockedStatic mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) { + mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy); + UserResponseDetails response = userService.updateEmail("someLoginId", "someEmail", false, true); + Assertions.assertThat(response).isNotNull(); + } + } + @Test void testUpdatePhoneForEmptyLoginId() { ServerCommonException thrown = assertThrows(ServerCommonException.class, @@ -387,6 +399,18 @@ void testUpdatePhoneForSuccess() { } } + @Test + void testUpdatePhoneWithFailOnConflictForSuccess() { + UserResponseDetails userResponseDetails = mock(UserResponseDetails.class); + ApiProxy apiProxy = mock(ApiProxy.class); + doReturn(userResponseDetails).when(apiProxy).post(any(), any(), any()); + try (MockedStatic mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) { + mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy); + UserResponseDetails response = userService.updatePhone("someLoginId", "1234567890", false, true); + Assertions.assertThat(response).isNotNull(); + } + } + @Test void testUpdateDisplayNameForEmptyLoginId() { ServerCommonException thrown = assertThrows(ServerCommonException.class, @@ -886,8 +910,8 @@ void testFunctionalFullCycle() { userService.updateDisplayName(loginId, "Testing Test"); userService.updateDisplayNames(loginId, "G Test", "M Test", "F Test"); email = TestUtils.getRandomName("test-") + "@descope.com"; - userService.updateEmail(loginId, email, true); - userService.updatePhone(loginId, "+1-555-555-6666", true); + userService.updateEmail(loginId, email, true, false); + userService.updatePhone(loginId, "+1-555-555-6666", true, false); String newLoginId = TestUtils.getRandomName("u-"); userService.updateLoginId(loginId, newLoginId); UserResponseDetails loadResponse = userService.load(newLoginId);