diff --git a/src/main/java/biz/nynja/account/components/Validator.java b/src/main/java/biz/nynja/account/components/Validator.java index d20ee10a9abb6ae584fb3381e9d245a57ad10685..4f114334cd74b24cda08f114bee605fa351776b3 100644 --- a/src/main/java/biz/nynja/account/components/Validator.java +++ b/src/main/java/biz/nynja/account/components/Validator.java @@ -21,7 +21,10 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; -import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.AuthProviderDetails; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.CreatePendingAccountRequest; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.models.CountryInfo; @@ -36,6 +39,13 @@ public class Validator { private static final Logger logger = LoggerFactory.getLogger(Validator.class); + private static final int MIN_ACCOUNT_NAME_LENGTH = 2; + private static final int MAX_ACCOUNT_NAME_LENGTH = 32; + private static final int MIN_FIRST_NAME_LENGTH = 2; + private static final int MAX_FIRST_NAME_LENGTH = 32; + private static final int MIN_LAST_NAME_LENGTH = 0; + private static final int MAX_LAST_NAME_LENGTH = 32; + private HashMap countryInfoMap; @Autowired @@ -84,6 +94,7 @@ public class Validator { CountryInfo countryInfo = countryInfoMap.get(countryCode); if (countryInfo == null) { logger.debug("Country: {} not found!", countryCode); + return false; } char[] digits = countryInfo.getCountryPhoneCode().toCharArray(); @@ -147,7 +158,7 @@ public class Validator { logger.debug("Checking First Name: {}", firstName); int len = firstName.length(); - boolean isValid = 2 <= len && len <= 32; + boolean isValid = MIN_FIRST_NAME_LENGTH <= len && len <= MAX_FIRST_NAME_LENGTH; logger.debug("First Name: {} is valid: {}", firstName, isValid); return isValid; @@ -157,51 +168,89 @@ public class Validator { logger.debug("Checking Last Name: {}", lastName); int len = lastName.length(); - boolean isValid = 0 <= len && len <= 32; + boolean isValid = MIN_LAST_NAME_LENGTH <= len && len <= MAX_LAST_NAME_LENGTH; logger.debug("Last Name: {} is valid: {}", lastName, isValid); return isValid; } -// public Cause validateCreateAccountRequest(CreateAccountRequest request) { -// -// if (request.getUsername() != null && !request.getUsername().trim().isEmpty() -// && !isUsernameValid(request.getUsername())) { -// return Cause.USERNAME_INVALID; -// } else if (request.getUsername() != null && !request.getUsername().trim().isEmpty() -// && !userInfoByUsernameRepository.findByUsername(request.getUsername()).isEmpty()) { -// return Cause.USERNAME_ALREADY_USED; -// } -// -// if (request.getEmailAddress() != null && !request.getEmailAddress().trim().isEmpty() -// && !isEmailValid(request.getEmailAddress())) { -// return Cause.EMAIL_INVALID; -// } else if (request.getEmailAddress() != null && !request.getEmailAddress().trim().isEmpty() -// && !userInfoByEmailRepository.findByEmailAddress(request.getEmailAddress()).isEmpty()) { -// return Cause.EMAIL_ALREADY_USED; -// } -// -// if (request.getPhoneNumber() != null && !request.getPhoneNumber().trim().isEmpty() -// && !isPhoneNumberValid(request.getPhoneNumber(), request.getCountryCode())) { -// return Cause.PHONE_NUMBER_INVALID; -// } else if (request.getPhoneNumber() != null && !request.getPhoneNumber().trim().isEmpty() -// && !userInfoByPhoneRepository.findByPhoneNumber(request.getPhoneNumber()).isEmpty()) { -// return Cause.PHONE_NUMBER_ALREADY_USED; -// } -// -// if (request.getFirstName() != null && request.getFirstName().trim().isEmpty()) { -// return Cause.MISSING_FIRST_NAME; -// } else if (!isFirstNameValid(request.getFirstName())) { -// return Cause.INVALID_FIRST_NAME; -// } -// -// if (request.getLastName() != null && !request.getLastName().trim().isEmpty() -// && !isLastNameValid(request.getLastName())) { -// return Cause.INVALID_LAST_NAME; -// } -// -// return null; -// } + boolean isAccountNameValid(String accountName) { + logger.debug("Checking Account Name: {}", accountName); + + int len = accountName.length(); + boolean isValid = MIN_ACCOUNT_NAME_LENGTH <= len && len <= MAX_ACCOUNT_NAME_LENGTH; + + logger.debug("First Name: {} is valid: {}", accountName, isValid); + return isValid; + } + + public Cause validateAuthProvider(AuthenticationType type, String authProvider) { + if (authProvider == null || authProvider.trim().isEmpty()) { + return Cause.MISSING_AUTH_PROVIDER_IDENTIFIER; + } + switch (type) { + case MISSING_TYPE: + return Cause.MISSING_AUTH_PROVIDER_TYPE; + case PHONE: + // We expect to receive phone number in the following format : ":" + String[] provider = authProvider.split(":"); + if (provider == null || provider.length != 2) { + return Cause.PHONE_NUMBER_INVALID; + } + if (!isPhoneNumberValid(provider[1], provider[0])) { + return Cause.PHONE_NUMBER_INVALID; + } + break; + case EMAIL: + if (!isEmailValid(authProvider)) { + return Cause.EMAIL_INVALID; + } + break; + default: + break; + } + return null; + } + + public Cause validateCreatePendingAccountRequest(CreatePendingAccountRequest request) { + return validateAuthProvider(request.getAuthenticationType(), request.getAuthenticationProvider()); + } + + public Cause validateCompletePendingAccountCreationRequest(CompletePendingAccountCreationRequest request) { + if (request.getAccountId() == null || request.getAccountId().trim().isEmpty()) { + return Cause.MISSING_ACCOUNT_ID; + } + + if (request.getFirstName() != null && request.getFirstName().trim().isEmpty()) { + return Cause.MISSING_FIRST_NAME; + } else if (!isFirstNameValid(request.getFirstName())) { + return Cause.INVALID_FIRST_NAME; + } + + if (request.getLastName() != null && !request.getLastName().trim().isEmpty() + && !isLastNameValid(request.getLastName())) { + return Cause.INVALID_LAST_NAME; + } + + if (request.getUsername() != null && !request.getUsername().trim().isEmpty() + && !isUsernameValid(request.getUsername())) { + return Cause.USERNAME_INVALID; + } + + for (AuthProviderDetails details : request.getCommunicationProvidersList()) { + Cause cause = validateAuthProvider(details.getAuthenticationType(), details.getAuthenticationProvider()); + if (cause != null) { + return cause; + } + } + + if (request.getAccountName() != null && !request.getAccountName().trim().isEmpty() + && !isAccountNameValid(request.getAccountName())) { + return Cause.ACCOUNT_NAME_INVALID; + } + + return null; + } public Cause validateUpdateAccountRequest(UpdateAccountRequest request) { @@ -209,7 +258,8 @@ public class Validator { && !isUsernameValid(request.getUsername())) { return Cause.USERNAME_INVALID; } else if (request.getUsername() != null && !request.getUsername().trim().isEmpty() - && accountRepositoryAdditional.foundExistingNotOwnUsername(UUID.fromString(request.getAccountId()), request.getUsername())) { + && accountRepositoryAdditional.foundExistingNotOwnUsername(UUID.fromString(request.getAccountId()), + request.getUsername())) { return Cause.USERNAME_ALREADY_USED; } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index b72c3a495b730edae13f30009d8fa7601a061801..e2a85fed72039fbaca70cbca8f1afceec1fa552d 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -215,6 +215,14 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas logger.info("Creating pending account..."); logger.debug("Creating pending account: {} ...", request); + Cause cause = validator.validateCreatePendingAccountRequest(request); + if (cause != null) { + responseObserver.onNext(CreatePendingAccountResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(cause)).build()); + responseObserver.onCompleted(); + return; + } + PendingAccount pendingAccount = PendingAccount.fromProto(request); pendingAccount.setAccountId(UUID.randomUUID()); pendingAccount.setProfileId(UUID.randomUUID()); @@ -236,9 +244,18 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Override public void completePendingAccountCreation(CompletePendingAccountCreationRequest request, StreamObserver responseObserver) { + logger.info("Complete pending account creation..."); logger.debug("Complete pending account creation...: {} ...", request); + Cause cause = validator.validateCompletePendingAccountCreationRequest(request); + if (cause != null) { + responseObserver + .onNext(AccountResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(cause)).build()); + responseObserver.onCompleted(); + return; + } + Account createdAccount = accountRepositoryAdditional.completePendingAccountCreation(request); if (createdAccount == null) { diff --git a/src/test/java/biz/nynja/account/components/ValidatorTests.java b/src/test/java/biz/nynja/account/components/ValidatorTests.java index 67156785d1525188c31c1b1435a3bc4b4c303c15..b8b1b98b08ded7627c5bb74abed2fe8e78ebfa4e 100644 --- a/src/test/java/biz/nynja/account/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/components/ValidatorTests.java @@ -4,16 +4,26 @@ package biz.nynja.account.components; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import biz.nynja.account.components.Validator; +import biz.nynja.account.grpc.AuthProviderDetails; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.CreatePendingAccountRequest; +import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.repositories.AccountRepositoryAdditional; +import biz.nynja.account.utils.Util; /** * Components unit tests. @@ -26,6 +36,9 @@ public class ValidatorTests { @Autowired private Validator validator; + @MockBean + private AccountRepositoryAdditional accountRepositoryAdditional; + @Test public void validPhoneNumberTest() { @@ -123,4 +136,102 @@ public class ValidatorTests { assertFalse(String.format("Last name: '%s' should be invalid.", lastName), isValid == true); } + + @Test + public void validAccountName() { + String accountName = "validAccountName"; + boolean isValid = validator.isAccountNameValid(accountName); + assertTrue(String.format("Account name: '%s' should be valid.", accountName), isValid); + } + + @Test + public void invalidAccountName() { + String accountName = "invalidAccountNameinvalidAccountNameinvalidAccountNameinvalidAccountNameinvalidAccountNameinvalidAccountNameinvalidAccountName"; + boolean isValid = validator.isAccountNameValid(accountName); + assertFalse(String.format("Account name: '%s' should be valid.", accountName), isValid); + } + + @Test + public void validateAuthProviderValidTest() { + assertNull("should be null", + validator.validateAuthProvider(AuthenticationType.EMAIL, "valid.E-mail1@domain-sub.test.com")); + } + + @Test + public void validateAuthProviderInvalidTest() { + assertEquals(validator.validateAuthProvider(AuthenticationType.EMAIL, "invalid.E-mail1.@domain_test.com1"), + Cause.EMAIL_INVALID); + } + + @Test + public void validateAuthProviderEmptyProviderIdentifierTest() { + assertEquals(validator.validateAuthProvider(AuthenticationType.EMAIL, null), + Cause.MISSING_AUTH_PROVIDER_IDENTIFIER); + } + + @Test + public void validateAuthProviderValidPhoneTest() { + assertNull("should be null", validator.validateAuthProvider(AuthenticationType.PHONE, "BG:+359888888888")); + } + + @Test + public void validateAuthProviderInvalidPhoneNoDotsTest() { + assertEquals(validator.validateAuthProvider(AuthenticationType.PHONE, "BG+359881111111"), + Cause.PHONE_NUMBER_INVALID); + } + + @Test + public void validateAuthProviderInvalidPhoneManyDotsTest() { + assertEquals(validator.validateAuthProvider(AuthenticationType.PHONE, "BG:+359:879555555"), + Cause.PHONE_NUMBER_INVALID); + } + + @Test + public void validateAuthProviderInvalidPhoneWrongCountryTest() { + assertEquals(validator.validateAuthProvider(AuthenticationType.PHONE, "BdasG:+359883456789"), + Cause.PHONE_NUMBER_INVALID); + } + + @Test + public void validateCompletePendingAccountCreationRequestGoodRequestTest() { + AuthProviderDetails pad = AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider("valid.E-mail1@domain-sub.test.com").build(); + + CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setFirstName(Util.FIRST_NAME).setLastName(Util.LAST_NAME) + .setUsername(Util.USERNAME).addCommunicationProviders(pad).build(); + assertNull(validator.validateCompletePendingAccountCreationRequest(request)); + } + + @Test + public void validateCompletePendingAccountCreationRequestInvalidUserNameTest() { + CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setFirstName(Util.FIRST_NAME).setLastName(Util.LAST_NAME) + .setUsername("@alabala").build(); + assertEquals(validator.validateCompletePendingAccountCreationRequest(request), Cause.USERNAME_INVALID); + } + + @Test + public void validateCompletePendingAccountCreationRequestMissingAccountIdTest() { + CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setFirstName(Util.FIRST_NAME).setLastName(Util.LAST_NAME).setUsername(Util.USERNAME).build(); + assertEquals(validator.validateCompletePendingAccountCreationRequest(request), Cause.MISSING_ACCOUNT_ID); + } + + @Test + public void validateCreatePendingAccountRequestGoodRequestTest() { + CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider("valid.E-mail1@domain-sub.test.com").build(); + assertNull("should be null", validator.validateCreatePendingAccountRequest(request)); + } + + @Test + public void validateCreatePendingAccountRequestInvalidEmailTest() { + CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider("invalid.E-mail1.@domain_test.com1").build(); + assertEquals(validator.validateCreatePendingAccountRequest(request), Cause.EMAIL_INVALID); + } + } diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 74240f4311c19c8c05133fa2e8bfa229eb98dbe9..fefb2ba4ddbfcbfef45cc38dffa140edbc45fada 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -15,6 +15,7 @@ import java.util.concurrent.ExecutionException; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -33,6 +34,9 @@ import biz.nynja.account.grpc.AccountsByProfileIdRequest; import biz.nynja.account.grpc.AccountsResponse; import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.CreatePendingAccountRequest; +import biz.nynja.account.grpc.CreatePendingAccountResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; @@ -45,6 +49,8 @@ import biz.nynja.account.models.Profile; 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.utils.GrpcServerTestBase; import biz.nynja.account.utils.Util; @@ -80,6 +86,10 @@ public class AccountServiceTests extends GrpcServerTestBase { @Qualifier("newAccount") private Account newAccount; + @Autowired + @Qualifier("savedPendingAccount") + private PendingAccount pendingAccount; + @Autowired @Qualifier("savedAccount") private Account savedAccount; @@ -99,6 +109,9 @@ public class AccountServiceTests extends GrpcServerTestBase { @MockBean private AccountRepository accountRepository; + @MockBean + private PendingAccountRepository pendingAccountRepository; + @MockBean private AccountByProfileIdRepository accountByProffileIdRepository; @@ -300,28 +313,21 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testUpdateAccount() throws ExecutionException, InterruptedException { final UpdateAccountRequest request = UpdateAccountRequest.newBuilder().setAccountId(Util.ACCOUNT_ID.toString()) .setAccountMark(Util.UPDATED_ACCOUNT_MARK).setFirstName(Util.FIRST_NAME).build(); - given(accountRepositoryAdditional.updateAccount(request)).willReturn(updatedAccount); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final AccountResponse reply = accountServiceBlockingStub.updateAccount(request); - assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain account mark '%s'", Util.UPDATED_ACCOUNT_MARK), reply.getAccountDetails().getAccountMark().equals(Util.UPDATED_ACCOUNT_MARK)); - } @Test public void testUpdateAccountMissingFirstName() { final UpdateAccountRequest request = UpdateAccountRequest.newBuilder().setAccountId(Util.ACCOUNT_ID.toString()) .setAccountMark(Util.UPDATED_ACCOUNT_MARK).build(); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final AccountResponse reply = accountServiceBlockingStub.updateAccount(request); assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_FIRST_NAME), @@ -332,10 +338,8 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testUpdateAccountMissingAccountId() { final UpdateAccountRequest request = UpdateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) .setAccountMark(Util.UPDATED_ACCOUNT_MARK).build(); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final AccountResponse reply = accountServiceBlockingStub.updateAccount(request); assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_ACCOUNT_ID), @@ -347,12 +351,9 @@ public class AccountServiceTests extends GrpcServerTestBase { final UpdateAccountRequest request = UpdateAccountRequest.newBuilder() .setAccountId(Util.ACCOUNT_ID_NOT_FOUND.toString()).setFirstName(Util.FIRST_NAME) .setAccountMark(Util.UPDATED_ACCOUNT_MARK).build(); - given(accountRepositoryAdditional.updateAccount(request)).willReturn(null); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final AccountResponse reply = accountServiceBlockingStub.updateAccount(request); assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_UPDATING_ACCOUNT), @@ -364,13 +365,10 @@ public class AccountServiceTests extends GrpcServerTestBase { final UpdateAccountRequest request = UpdateAccountRequest.newBuilder().setAccountId(Util.ACCOUNT_ID.toString()) .setAccountMark(Util.UPDATED_ACCOUNT_MARK).setFirstName(Util.FIRST_NAME).setUsername(Util.USERNAME) .build(); - given(accountRepositoryAdditional.foundExistingNotOwnUsername(UUID.fromString(request.getAccountId()), request.getUsername())).willReturn(true); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final AccountResponse reply = accountServiceBlockingStub.updateAccount(request); assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain cause '%s'", Cause.USERNAME_ALREADY_USED), @@ -379,21 +377,16 @@ public class AccountServiceTests extends GrpcServerTestBase { @Test public void testUpdateProfileAddBackupAuthProvider() throws ExecutionException, InterruptedException { - final UpdateProfileRequest request = UpdateProfileRequest.newBuilder().setProfileId(Util.PROFILE_ID.toString()) .addAuthProviders(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .build(); - given(accountRepositoryAdditional.updateProfile(request)).willReturn(updatedProfile); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(request); - assertNotNull("Reply should not be null", reply); assertTrue( String.format("Reply should contain backup auth provider type '%s'", @@ -409,41 +402,164 @@ public class AccountServiceTests extends GrpcServerTestBase { @Test public void testUpdateProfileMissingProfileId() throws ExecutionException, InterruptedException { - final UpdateProfileRequest request = UpdateProfileRequest.newBuilder() .addAuthProviders(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .build(); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(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 testUpdateProfileNoAuthProviders() throws ExecutionException, InterruptedException { - final UpdateProfileRequest request = UpdateProfileRequest.newBuilder().setProfileId(Util.PROFILE_ID.toString()) .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .build(); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(request); - assertNotNull("Reply should not be null", reply); assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_UPDATING_PROFILE), reply.getError().getCause().equals(Cause.ERROR_UPDATING_PROFILE)); + } + + @Test + public void testCreatePendingAccountOK() { + final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationProvider(Util.EMAIL).build(); + given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain account id '%s'", Util.ACCOUNT_ID), + reply.getPendingAccountDetails().getAccountId().equals(Util.ACCOUNT_ID.toString())); + } + + @Test + public void testCreatePendingAccountInvalidEmail() { + final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL) + .setAuthenticationProvider("invalid.E-mail1.@domain_test.com1").build(); + given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.EMAIL_INVALID), + reply.getError().getCause().equals(Cause.EMAIL_INVALID)); + } + + @Test + public void testCreatePendingAccountInvalidPhone() { + final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() + .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationProvider("BG:084365:5555").build(); + given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.PHONE_NUMBER_INVALID), + reply.getError().getCause().equals(Cause.PHONE_NUMBER_INVALID)); + } + + @Test + public void testCompletePendingAccountCreationOK() { + final CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setUsername(Util.USERNAME).setFirstName(Util.FIRST_NAME) + .addCommunicationProviders( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider("BG:+359881236548").build()) + .build(); + given(accountRepositoryAdditional + .completePendingAccountCreation(Mockito.any(CompletePendingAccountCreationRequest.class))) + .willReturn(savedAccount); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final AccountResponse respose = accountServiceBlockingStub.completePendingAccountCreation(request); + assertNotNull("Reply should not be null", respose); + assertTrue(String.format("Reply should contain username '%s'", Util.USERNAME), + respose.getAccountDetails().getUsername().equals(Util.USERNAME)); + assertTrue(String.format("Reply should contain account id '%s'", Util.ACCOUNT_ID), + respose.getAccountDetails().getAccountId().equals(Util.ACCOUNT_ID.toString())); + } + + @Test + public void testCompletePendingAccountCreationUsernameInvalid() { + final CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setUsername("Invalid!Username").setFirstName(Util.FIRST_NAME) + .addCommunicationProviders( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider("BG:+359881236548").build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final AccountResponse respose = accountServiceBlockingStub.completePendingAccountCreation(request); + assertNotNull("Reply should not be null", respose); + assertTrue(String.format("Reply should contain cause '%s'", Cause.USERNAME_INVALID), + respose.getError().getCause().equals(Cause.USERNAME_INVALID)); + } + + @Test + public void testCompletePendingAccountCreationMissingFirstName() { + final CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setUsername(Util.USERNAME) + .addCommunicationProviders( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider("BG:+359881236548").build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final AccountResponse respose = accountServiceBlockingStub.completePendingAccountCreation(request); + assertNotNull("Reply should not be null", respose); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_FIRST_NAME), + respose.getError().getCause().equals(Cause.MISSING_FIRST_NAME)); + } + + @Test + public void testCompletePendingAccountCreationInvalidLastName() { + final CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setUsername(Util.USERNAME).setFirstName(Util.FIRST_NAME) + .setLastName("VeryInvalidLastNameRepeatVeryInvalidLastNameRepeatVeryInvalidLastNameRepeat") + .addCommunicationProviders( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider("BG:+359881236548").build()) + .build(); + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + final AccountResponse respose = accountServiceBlockingStub.completePendingAccountCreation(request); + assertNotNull("Reply should not be null", respose); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_LAST_NAME), + respose.getError().getCause().equals(Cause.INVALID_LAST_NAME)); + } + + @Test + public void testCompletePendingAccountCreationCreatedAccountIsNull() { + final CompletePendingAccountCreationRequest request = CompletePendingAccountCreationRequest.newBuilder() + .setAccountId(Util.ACCOUNT_ID.toString()).setUsername(Util.USERNAME).setFirstName(Util.FIRST_NAME) + .addCommunicationProviders( + AuthProviderDetails.newBuilder().setAuthenticationType(AuthenticationType.PHONE) + .setAuthenticationProvider("BG:+359881236548").build()) + .build(); + + given(accountRepositoryAdditional + .completePendingAccountCreation(Mockito.any(CompletePendingAccountCreationRequest.class))) + .willReturn(null); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final AccountResponse respose = accountServiceBlockingStub.completePendingAccountCreation(request); + assertNotNull("Reply should not be null", respose); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_CREATING_ACCOUNT), + respose.getError().getCause().equals(Cause.ERROR_CREATING_ACCOUNT)); } } diff --git a/src/test/java/biz/nynja/account/utils/Util.java b/src/test/java/biz/nynja/account/utils/Util.java index f4e58fe4860ff19f900309bd1b3fa38665e6f06c..9f56e4ba49199ecaa127b810270b90411836b3a1 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.PendingAccount; /** * Unit tests variables, beans and help methods. @@ -38,7 +39,7 @@ public class Util { public static final String ACCOUNT_STATUS = "active"; public static final long CREATION_TIMESTAMP = 45; public static final long LAST_UPDATE_TIMESTAMP = 80; - public static final Set COMMUNICATION_PROVIDERS= new HashSet(); + public static final Set COMMUNICATION_PROVIDERS = new HashSet(); public static final String QR_CODE = "QRcode"; public static final String EMAIL = "email@test.com"; public static final String USERNAME = "jdoe"; @@ -65,7 +66,7 @@ public class Util { account.setAuthenticationProviderType(EMAIL_TYPE); account.setAccountStatus(Status.SUSPENDED.name()); return account; - } + } @Bean public AccountByProfileId savedAccountByProfileId() { @@ -93,6 +94,8 @@ public class Util { Account account = new Account(); account.setAccountId(ACCOUNT_ID); account.setProfileId(PROFILE_ID); + account.setAuthenticationProvider(EMAIL); + account.setAuthenticationProviderType(EMAIL_TYPE); account.setUsername(USERNAME); account.setFirstName(FIRST_NAME); account.setLastName(LAST_NAME); @@ -161,4 +164,14 @@ public class Util { accountByPhone.setLastName(LAST_NAME); return accountByPhone; } + + @Bean + public PendingAccount savedPendingAccount() { + PendingAccount account = new PendingAccount(); + account.setAccountId(ACCOUNT_ID); + account.setProfileId(PROFILE_ID); + account.setAuthenticationProvider(EMAIL); + account.setAuthenticationProviderType(EMAIL_TYPE); + return account; + } }