diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index d3e18c04451e86698dd1fde758e7cfbd6b35fab7..265d52f32610bf2e2ddf2c7c8bbf9ab2132c536c 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -491,7 +491,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } if (authenticationProviderAlreadyUsedInProfile(updatedAuthProvider)) { - logger.error("The requested update auth provider for profile {} is already used.", profileId); + logger.error("The requested update auth provider {}:{} for profile {} is already used.", updatedAuthProvider.getType(), updatedAuthProvider.getValue(), profileId); return false; } diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 202a8b4fc6c2bde312e69b625987afb8de40f4f6..8b361710451eff3a8934e3cea1cc9c9276dba9b5 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -66,6 +66,7 @@ import biz.nynja.account.grpc.Role; import biz.nynja.account.grpc.SearchResponse; import biz.nynja.account.grpc.StatusResponse; import biz.nynja.account.grpc.UpdateAccountRequest; +import biz.nynja.account.grpc.UpdateAuthenticationProviderRequest; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; import biz.nynja.account.models.AccountByProfileId; @@ -1420,7 +1421,7 @@ public class AccountServiceTests extends GrpcServerTestBase { .setEditedContactInfo(ContactDetails.newBuilder().setType(ContactType.EMAIL_CONTACT) .setValue(Util.EMAIL_EDITED).build()) .build(); - given(accountRepositoryAdditional.addContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class))) + given(accountRepositoryAdditional.editContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class), Mockito.any(ContactInfo.class))) .willReturn(false); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc @@ -1770,4 +1771,183 @@ public class AccountServiceTests extends GrpcServerTestBase { reply.getError().getCause().equals(Cause.MISSING_PROFILE_ID)); } + @Test + public void testUpdateAuthProviderPhoneForProfileOK() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + given(accountRepositoryAdditional.updateAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class), Mockito.any(AuthenticationProvider.class))).willReturn(true); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertEquals("SUCCESS", reply.getStatus()); + } + + @Test + public void testUpdateAuthProviderEmailForProfileOK() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.EMAIL).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.EMAIL_EDITED).build()) + .build(); + given(accountRepositoryAdditional.updateAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class), Mockito.any(AuthenticationProvider.class))).willReturn(true); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertEquals("SUCCESS", reply.getStatus()); + } + + @Test + public void testUpdateAuthProviderForProfileErrorUpdatingAuthProvider() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + given(accountRepositoryAdditional.updateAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class), Mockito.any(AuthenticationProvider.class))) + .willReturn(false); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_UPDATING_AUTH_PROVIDER), + reply.getError().getCause().equals(Cause.ERROR_UPDATING_AUTH_PROVIDER)); + } + + @Test + public void testUpdateAuthProviderEmailForProfileAuthProviderUsed() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.EMAIL).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.EMAIL_EDITED).build()) + .build(); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile1AuthProvider); + given(accountRepositoryAdditional + .authenticationProviderAlreadyUsedInAccount(Mockito.any(AuthenticationProvider.class))) + .willReturn(true); + given(accountRepositoryAdditional.updateAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class), Mockito.any(AuthenticationProvider.class))) + .willReturn(false); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_UPDATING_AUTH_PROVIDER), + reply.getError().getCause().equals(Cause.ERROR_UPDATING_AUTH_PROVIDER)); + } + + @Test + public void testUpdateAuthProviderForProfileInvalidProfileId() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.INVALID_PROFILE_ID) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_PROFILE_ID), + reply.getError().getCause().equals(Cause.INVALID_PROFILE_ID)); + } + + @Test + public void testUpdateAuthProviderForProfileMissingProfileId() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_PROFILE_ID), + reply.getError().getCause().equals(Cause.MISSING_PROFILE_ID)); + } + + @Test + public void testUpdateAuthProviderForProfileMissingAuthProviderType() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider( + AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_AUTH_PROVIDER_TYPE), + reply.getError().getCause().equals(Cause.MISSING_AUTH_PROVIDER_TYPE)); + } + + @Test + public void testUpdateAuthProviderForProfileMissingAuthProviderIdentifier() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER_EDITED).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_AUTH_PROVIDER_ID), + reply.getError().getCause().equals(Cause.MISSING_AUTH_PROVIDER_ID)); + } + + @Test + public void testUpdateAuthProviderForProfileInvalidPhone() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.PHONE_PROVIDER).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider(Util.INVALID_PHONE_PROVIDER).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_PHONENUMBER), + reply.getError().getCause().equals(Cause.INVALID_PHONENUMBER)); + } + + @Test + public void testUpdateAuthProviderForProfileInvalidEmail() { + final UpdateAuthenticationProviderRequest request = UpdateAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setOldAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.EMAIL).build()) + .setUpdatedAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider(Util.INVALID_EMAIL).build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final StatusResponse reply = accountServiceBlockingStub.updateAuthenticationProviderForProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_EMAIL), + reply.getError().getCause().equals(Cause.INVALID_EMAIL)); + } } diff --git a/src/test/java/biz/nynja/account/utils/Util.java b/src/test/java/biz/nynja/account/utils/Util.java index 3eeade46168625d41da4199dfc1505f209d39c2c..1ae6d9cc6ebadb5e1d6917c1e4ac7a2fd591cca6 100644 --- a/src/test/java/biz/nynja/account/utils/Util.java +++ b/src/test/java/biz/nynja/account/utils/Util.java @@ -40,6 +40,7 @@ public class Util { public static final UUID ACCOUNT_ID_NOT_FOUND = UUID.fromString("44532732-12b3-132d-e156-223732152202"); public static final UUID PROFILE_ID_NOT_FOUND = UUID.fromString("12352345-e89b-43d3-d156-456732452202"); public static final String INVALID_ACCOUNT_ID = "111-222"; + public static final String INVALID_PROFILE_ID = "222-333"; public static final String ACCOUNT_MARK = "AccountMark"; public static final String UPDATED_ACCOUNT_MARK = "PRIVATE"; public static final String AUTHENTICATION_PROVIDER = "Provider";