diff --git a/src/main/java/biz/nynja/account/components/Validator.java b/src/main/java/biz/nynja/account/components/Validator.java index 8bffbe0c9730c69f8bcd4290e450a4ab197c912d..64c0873b29b3675de727102f8f256bbf1555aad0 100644 --- a/src/main/java/biz/nynja/account/components/Validator.java +++ b/src/main/java/biz/nynja/account/components/Validator.java @@ -21,6 +21,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; +import biz.nynja.account.grpc.AddAuthenticationProviderRequest; import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.AuthenticationType; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; @@ -185,6 +186,10 @@ public class Validator { return isValid; } + boolean isValidUuid(String id) { + return id.matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); + } + public Cause validateAuthProvider(AuthenticationType type, String authProvider) { if (authProvider == null || authProvider.trim().isEmpty()) { return Cause.MISSING_AUTH_PROVIDER_IDENTIFIER; @@ -295,4 +300,12 @@ public class Validator { } return null; } + + public Cause validateAddAuthenticationProviderRequest(AddAuthenticationProviderRequest request) { + if (!isValidUuid(request.getProfileId())) { + return Cause.INVALID_PROFILE_ID; + } + return validateAuthProvider(request.getAuthenticationProvider().getAuthenticationType(), + request.getAuthenticationProvider().getAuthenticationProvider()); + } } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index ba54237abaf2618e751858037e5ac73efd2cae43..d0fdd3676321ed515d33e5cf9d4b0056dc873677 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -34,4 +34,5 @@ public interface AccountRepositoryAdditional { boolean authenticationProviderAlreadyUsedInAccount(AuthenticationProvider authProvider); + boolean addAuthenticationProvider(UUID profileId, AuthenticationProvider authProvider); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index be11888af9af727ae3fe13a85f82acc1612795bb..6baf5ba879d28f3728be30b43ea8e9ea03310f7f 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -18,6 +18,12 @@ import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.core.WriteResult; import org.springframework.stereotype.Service; +import com.datastax.driver.core.BatchStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.SimpleStatement; +import com.datastax.driver.core.Statement; + import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; import biz.nynja.account.grpc.UpdateAccountRequest; @@ -68,6 +74,9 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Autowired private PendingAccountRepository pendingAccountRepository; + @Autowired + private Session session; + public Account completePendingAccountCreation(CompletePendingAccountCreationRequest request) { CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); PendingAccount pendingAccount = pendingAccountRepository @@ -640,4 +649,46 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio existingAccount.getAuthenticationProvider())); } } + + public boolean authenticationProviderAlreadyUsedInProfile(AuthenticationProvider authProvider) { + ProfileByAuthenticationProvider profile = profileByAuthenticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(authProvider.getValue(), authProvider.getType()); + if (profile != null) { + return true; + } + return false; + } + + @Override + public boolean addAuthenticationProvider(UUID profileId, AuthenticationProvider authProvider) { + BatchStatement batchOperations = new BatchStatement(); + ResultSet wr = null; + try { + Statement updateAuthProvidersInProfile = new SimpleStatement( + "UPDATE profile SET authenticationproviders = authenticationproviders + {('" + + authProvider.getType() + "', '" + authProvider.getValue() + "')} WHERE profileid = " + + profileId.toString()); + Statement insertInProfileByAuthProvider = new SimpleStatement(" INSERT INTO profilebyauthenticationprovider " + + "(authenticationprovider, authenticationprovidertype, profileid) VALUES ('" + + authProvider.getValue() + "', '" + authProvider.getType() + "', " + profileId + ");"); + + batchOperations.add(updateAuthProvidersInProfile); + batchOperations.add(insertInProfileByAuthProvider); + wr = session.execute(batchOperations); + + } catch (IllegalArgumentException | IllegalStateException e) { + logger.info("Exception while adding new authenication provider to profile with id: {}.", profileId); + logger.debug("Exception while adding new authenication provider to profile with id: {}, {}", profileId, + e.getMessage()); + return false; + } + + if (wr != null) { + boolean applied = wr.wasApplied(); + if (applied) { + return true; + } + } + return false; + } } diff --git a/src/main/java/biz/nynja/account/repositories/ProfileByAuthenticationProviderRepository.java b/src/main/java/biz/nynja/account/repositories/ProfileByAuthenticationProviderRepository.java index 05f8048c96a692448fa46156959080d284770cfa..3ab43dc5919064db1854bded76132351fd6457fb 100644 --- a/src/main/java/biz/nynja/account/repositories/ProfileByAuthenticationProviderRepository.java +++ b/src/main/java/biz/nynja/account/repositories/ProfileByAuthenticationProviderRepository.java @@ -9,6 +9,9 @@ import org.springframework.stereotype.Repository; import biz.nynja.account.models.ProfileByAuthenticationProvider; @Repository -public interface ProfileByAuthenticationProviderRepository extends CassandraRepository{ +public interface ProfileByAuthenticationProviderRepository + extends CassandraRepository { + ProfileByAuthenticationProvider findByAuthenticationProviderAndAuthenticationProviderType(String authProvider, + String authProviderType); } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index c6b55390f80fb7c123a418dee98d84e266a8580c..617cc28708ac52dd5c571c4a5455c2f4876ad892 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -22,6 +22,7 @@ import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByProfileIdRequest; import biz.nynja.account.grpc.AccountsList; import biz.nynja.account.grpc.AccountsResponse; +import biz.nynja.account.grpc.AddAuthenticationProviderRequest; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountRequest; @@ -31,6 +32,7 @@ import biz.nynja.account.grpc.StatusResponse; import biz.nynja.account.grpc.DeleteProfileRequest; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.grpc.StatusResponse; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; import biz.nynja.account.models.AccountByProfileId; @@ -39,11 +41,14 @@ import biz.nynja.account.models.PendingAccount; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; import biz.nynja.account.repositories.AccountByAuthenticationProviderRepository; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.ProfileByAuthenticationProvider; import biz.nynja.account.repositories.AccountByProfileIdRepository; import biz.nynja.account.repositories.AccountRepository; import biz.nynja.account.repositories.AccountRepositoryAdditional; import biz.nynja.account.repositories.PendingAccountByAuthenticationProviderRepository; import biz.nynja.account.repositories.PendingAccountRepository; +import biz.nynja.account.repositories.ProfileByAuthenticationProviderRepository; +import biz.nynja.account.repositories.ProfileRepository; import biz.nynja.account.grpc.AccountsResponse; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; @@ -79,6 +84,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private AccountRepositoryAdditional accountRepositoryAdditional; + @Autowired + private ProfileRepository profileRepository; + + @Autowired + private ProfileByAuthenticationProviderRepository profileByAutheticationProviderRepository; + @Autowired private Validator validator; @@ -467,4 +478,77 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas responseObserver.onCompleted(); return; } + + @Override + public void addAuthenticationProviderToProfile(AddAuthenticationProviderRequest request, + StreamObserver responseObserver) { + logger.info("Adding authentication provider to profile requested."); + logger.debug("Adding authentication provider to profile requested: {}", request); + if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_PROFILE_ID)).build()); + responseObserver.onCompleted(); + return; + } + if (request.getAuthenticationProvider().getAuthenticationTypeValue() == 0) { + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_AUTH_PROVIDER_TYPE)).build()); + responseObserver.onCompleted(); + return; + } + if (request.getAuthenticationProvider().getAuthenticationProvider() == null + || request.getAuthenticationProvider().getAuthenticationProvider().isEmpty()) { + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_AUTH_PROVIDER_IDENTIFIER)).build()); + responseObserver.onCompleted(); + return; + } + Cause cause = validator.validateAddAuthenticationProviderRequest(request); + if (cause != null) { + responseObserver + .onNext(StatusResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(cause)).build()); + responseObserver.onCompleted(); + return; + } + // Make sure that the requested profile id for update exists in DB. + Profile profile = profileRepository.findByProfileId(UUID.fromString(request.getProfileId())); + if (profile == null) { + logger.error("Profile id {} missing in DB.", request.getProfileId()); + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.PROFILE_NOT_FOUND)).build()); + responseObserver.onCompleted(); + return; + } + // Make sure that the requested authentication provider is not already used in the system. + ProfileByAuthenticationProvider profileByAuthProvider = profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType( + request.getAuthenticationProvider().getAuthenticationProvider(), + request.getAuthenticationProvider().getAuthenticationType().name()); + if (profileByAuthProvider != null) { + logger.error("Authentication provider {}:{} already used.", + request.getAuthenticationProvider().getAuthenticationType().name(), + request.getAuthenticationProvider().getAuthenticationProvider()); + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.AUTH_PROVIDER_ALREADY_USED)).build()); + responseObserver.onCompleted(); + return; + } + boolean result = accountRepositoryAdditional.addAuthenticationProvider(UUID.fromString(request.getProfileId()), + AuthenticationProvider.createAuthenticationProviderFromProto(request.getAuthenticationProvider())); + if (result) { + logger.info("Authentication provider {}:{} successfuly added for profile id {}.", + request.getAuthenticationProvider().getAuthenticationType().name(), + request.getAuthenticationProvider().getAuthenticationProvider(), request.getProfileId()); + responseObserver.onNext(StatusResponse.newBuilder().setStatus("SUCCESS").build()); + responseObserver.onCompleted(); + return; + } + logger.error("Authentication provider {}:{} was not successfuly added for profile id {}.", + request.getAuthenticationProvider().getAuthenticationType().name(), + request.getAuthenticationProvider().getAuthenticationProvider(), request.getProfileId()); + responseObserver.onNext(StatusResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.INTERNAL_SERVER_ERROR)).build()); + responseObserver.onCompleted(); + return; + } } \ No newline at end of file diff --git a/src/test/java/biz/nynja/account/components/ValidatorTests.java b/src/test/java/biz/nynja/account/components/ValidatorTests.java index b8b1b98b08ded7627c5bb74abed2fe8e78ebfa4e..67648182a4fc04e02dfc1b5da8bb3e4753e47f05 100644 --- a/src/test/java/biz/nynja/account/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/components/ValidatorTests.java @@ -151,6 +151,36 @@ public class ValidatorTests { assertFalse(String.format("Account name: '%s' should be valid.", accountName), isValid); } + @Test + public void isValidUuidTestOK() { + String uuid = "11d4b22a-1470-4c18-afed-bb9283b4040f"; + assertTrue(validator.isValidUuid(uuid)); + } + + @Test + public void isValidUuidTestTooLong() { + String uuid = "11d4b22a-1470-4c18-a25b-afed-bb9283b4040f"; + assertFalse(validator.isValidUuid(uuid)); + } + + @Test + public void isValidUuidTestTooShort() { + String uuid = "11d4b22a-1470-afed-bb9283b4040f"; + assertFalse(validator.isValidUuid(uuid)); + } + + @Test + public void isValidUuidTestNotHex() { + String uuid = "11d4bp2a-1470-afed-bb9283b4040f"; + assertFalse(validator.isValidUuid(uuid)); + } + + @Test + public void isValidUuidTestHyphen() { + String uuid = "11d4bp2a--1470-afed-bb9283b4040f"; + assertFalse(validator.isValidUuid(uuid)); + } + @Test public void validateAuthProviderValidTest() { assertNull("should be null", diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index f5b60b405b31aa0dbf946bb3c77071ca551c454d..1038ebf2653efbed5b298f5605ec739beace6876 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -3,6 +3,7 @@ */ package biz.nynja.account.services; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; @@ -32,6 +33,7 @@ import biz.nynja.account.grpc.AccountByAccountIdRequest; import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByProfileIdRequest; import biz.nynja.account.grpc.AccountsResponse; +import biz.nynja.account.grpc.AddAuthenticationProviderRequest; import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.AuthenticationType; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; @@ -52,11 +54,12 @@ import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.PendingAccount; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.ProfileByAuthenticationProvider; import biz.nynja.account.repositories.AccountByProfileIdRepository; import biz.nynja.account.repositories.AccountRepository; import biz.nynja.account.repositories.AccountRepositoryAdditional; -import biz.nynja.account.models.PendingAccount; import biz.nynja.account.repositories.PendingAccountRepository; +import biz.nynja.account.repositories.ProfileByAuthenticationProviderRepository; import biz.nynja.account.repositories.ProfileRepository; import biz.nynja.account.utils.GrpcServerTestBase; import biz.nynja.account.utils.Util; @@ -121,6 +124,22 @@ public class AccountServiceTests extends GrpcServerTestBase { @Qualifier("savedAccountByProfileId") private AccountByProfileId savedAccountByProfileId; + @Autowired + @Qualifier("phoneAccount") + private Account phoneAccount; + + @Autowired + @Qualifier("profile1AuthProvider") + private Profile profile1AuthProvider; + + @Autowired + @Qualifier("profile2AuthProviders") + private Profile profile2AuthProviders; + + @Autowired + @Qualifier("profileByAuthenticationProvider") + private ProfileByAuthenticationProvider profileByAuthenticationProvider; + @MockBean private AccountRepository accountRepository; @@ -136,6 +155,10 @@ public class AccountServiceTests extends GrpcServerTestBase { @MockBean private AccountRepositoryAdditional accountRepositoryAdditional; + @MockBean + private ProfileByAuthenticationProviderRepository profileByAutheticationProviderRepository; + + @Test public void testGetAccountByAccountId() throws ExecutionException, InterruptedException { final AccountByAccountIdRequest request = AccountByAccountIdRequest.newBuilder() @@ -713,4 +736,158 @@ public class AccountServiceTests extends GrpcServerTestBase { assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_DELETING_PROFILE), reply.getError().getCause().equals(Cause.ERROR_DELETING_PROFILE)); } + + @Test + public void testAddAuthenticationProviderToProfileOK() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setAuthenticationProvider(AuthProviderDetails.newBuilder() + .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(true); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile2AuthProviders); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(null); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertEquals("SUCCESS", reply.getStatus()); + } + + @Test + public void testAddAuthenticationProviderToProfileAuthProviderUsed() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setAuthenticationProvider(AuthProviderDetails.newBuilder() + .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(true); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile2AuthProviders); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(profileByAuthenticationProvider); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.AUTH_PROVIDER_ALREADY_USED), + reply.getError().getCause().equals(Cause.AUTH_PROVIDER_ALREADY_USED)); + } + + @Test + public void testAddAuthenticationProviderToProfileAuthProviderIdMissing() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setAuthenticationProvider(AuthProviderDetails.newBuilder() + .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(true); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile2AuthProviders); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(null); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_PROFILE_ID), + reply.getError().getCause().equals(Cause.MISSING_PROFILE_ID)); + } + + @Test + public void testAddAuthenticationProviderToProfileMissingAuthProvider() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).setAuthenticationProvider( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(true); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile2AuthProviders); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(null); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_AUTH_PROVIDER_IDENTIFIER), + reply.getError().getCause().equals(Cause.MISSING_AUTH_PROVIDER_IDENTIFIER)); + } + + @Test + public void testAddAuthenticationProviderToProfileInternalServerError() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setAuthenticationProvider(AuthProviderDetails.newBuilder() + .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(false); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile2AuthProviders); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(null); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INTERNAL_SERVER_ERROR), + reply.getError().getCause().equals(Cause.INTERNAL_SERVER_ERROR)); + } + + @Test + public void testAddAuthenticationProviderToProfileNotFound() { + final AddAuthenticationProviderRequest request = AddAuthenticationProviderRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()) + .setAuthenticationProvider(AuthProviderDetails.newBuilder() + .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .build(); + + given(accountRepositoryAdditional.addAuthenticationProvider(Mockito.any(UUID.class), + Mockito.any(AuthenticationProvider.class))).willReturn(true); + + given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(null); + + given(profileByAutheticationProviderRepository + .findByAuthenticationProviderAndAuthenticationProviderType(Util.PHONE_PROVIDER, "PHONE")) + .willReturn(profileByAuthenticationProvider); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final StatusResponse reply = accountServiceBlockingStub.addAuthenticationProviderToProfile(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.PROFILE_NOT_FOUND), + reply.getError().getCause().equals(Cause.PROFILE_NOT_FOUND)); + } + } diff --git a/src/test/java/biz/nynja/account/utils/Util.java b/src/test/java/biz/nynja/account/utils/Util.java index a7897de0ad58bb1b9d6ccc96a100b103cecfab51..64557c1b06333a0e9aa7b7759734f5ee03c54cf8 100644 --- a/src/test/java/biz/nynja/account/utils/Util.java +++ b/src/test/java/biz/nynja/account/utils/Util.java @@ -18,6 +18,7 @@ import biz.nynja.account.models.AccountByAuthenticationProvider; import biz.nynja.account.models.AccountByProfileId; import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.ProfileByAuthenticationProvider; import biz.nynja.account.models.PendingAccount; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; @@ -51,11 +52,62 @@ public class Util { public static final String LAST_NAME = "Doe"; public static final String MIDDLE_NAME = "Kass"; public static final String PHONE_NUMBER = "+359887434646"; + public static final String PHONE_PROVIDER = "BG:+359887434646"; public static final String PHONE_TYPE = "PHONE"; public static final String EMAIL_TYPE = "EMAIL"; public static final String FACBOOK_TYPE = "FACEBOOK"; public static final String GOOGLEPLUS_TYPE = "GOOGLEPLUS"; + @Bean + public ProfileByAuthenticationProvider profileByAuthenticationProvider() { + ProfileByAuthenticationProvider profile = new ProfileByAuthenticationProvider(); + profile.setProfileId(PROFILE_ID); + profile.setAuthenticationProvider(PHONE_PROVIDER); + profile.setAuthenticationProviderType(PHONE_TYPE); + return profile; + } + + @Bean + public Profile profile1AuthProvider() { + Set aps = new HashSet<>(); + AuthenticationProvider ap1 = new AuthenticationProvider(); + ap1.setType(PHONE_TYPE); + ap1.setValue(PHONE_PROVIDER); + aps.add(ap1); + Profile profile = new Profile(); + profile.setProfileId(PROFILE_ID); + profile.setPasscode("123456"); + profile.setAuthenticationProviders(aps); + return profile; + } + + @Bean + public Profile profile2AuthProviders() { + Set aps = new HashSet<>(); + AuthenticationProvider ap1 = new AuthenticationProvider(); + ap1.setType(PHONE_TYPE); + ap1.setValue(PHONE_PROVIDER); + aps.add(ap1); + AuthenticationProvider ap2 = new AuthenticationProvider(); + ap2.setType(EMAIL_TYPE); + ap2.setValue(EMAIL); + aps.add(ap2); + Profile profile = new Profile(); + profile.setProfileId(PROFILE_ID); + profile.setPasscode("123456"); + profile.setAuthenticationProviders(aps); + return profile; + } + + @Bean + public Account phoneAccount() { + Account account = new Account(); + account.setAccountId(ACCOUNT_ID); + account.setAuthenticationProvider(PHONE_PROVIDER); + account.setAuthenticationProviderType(PHONE_TYPE); + return account; + } + @Bean public Account newAccount() { Account account = new Account();