diff --git a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java index d5834b4eba0b797e4c8138346c12fe2750549942..6c0998445a79ed4bb6f203b191970c852bbe9c9c 100644 --- a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java +++ b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java @@ -28,7 +28,7 @@ public class PhoneNumberNormalizer { public AddContactInfoRequest normalizePhoneNumber(AddContactInfoRequest request) { // Get the normalized phone number from libphone ContactDetails newContactDetails = ContactDetails.newBuilder().setType(request.getContactInfo().getType()) - .setValue(getNormalizedPhoneNumberContactInfo(request.getContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberWithSelector(request.getContactInfo().getValue())) .setLabel(request.getContactInfo().getLabel()).build(); AddContactInfoRequest newRequest = AddContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setContactInfo(newContactDetails).build(); @@ -39,7 +39,7 @@ public class PhoneNumberNormalizer { public DeleteContactInfoRequest normalizePhoneNumber(DeleteContactInfoRequest request) { // Get the normalized phone number from libphone ContactDetails newContactDetails = ContactDetails.newBuilder().setType(request.getContactInfo().getType()) - .setValue(getNormalizedPhoneNumberContactInfo(request.getContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberWithSelector(request.getContactInfo().getValue())) .setLabel(request.getContactInfo().getLabel()).build(); DeleteContactInfoRequest newRequest = DeleteContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setContactInfo(newContactDetails).build(); @@ -51,12 +51,12 @@ public class PhoneNumberNormalizer { // Get the normalized phone number from libphone for the old number ContactDetails contactDetailsOldNumber = ContactDetails.newBuilder() .setType(request.getOldContactInfo().getType()) - .setValue(getNormalizedPhoneNumberContactInfo(request.getOldContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberWithSelector(request.getOldContactInfo().getValue())) .setLabel(request.getOldContactInfo().getLabel()).build(); // Get the normalized phone number from libphone for the edited number ContactDetails contactDetailsEditedNumber = ContactDetails.newBuilder() .setType(request.getEditedContactInfo().getType()) - .setValue(getNormalizedPhoneNumberContactInfo(request.getEditedContactInfo().getValue())) + .setValue(getNormalizedPhoneNumberWithSelector(request.getEditedContactInfo().getValue())) .setLabel(request.getEditedContactInfo().getLabel()).build(); EditContactInfoRequest newRequest = EditContactInfoRequest.newBuilder().setAccountId(request.getAccountId()) .setOldContactInfo(contactDetailsOldNumber).setEditedContactInfo(contactDetailsEditedNumber).build(); @@ -135,7 +135,7 @@ public class PhoneNumberNormalizer { return normalizedPhoneNumber; } - public String getNormalizedPhoneNumberContactInfo(String rawPhoneNumber) throws InvalidPhoneNumberException { + public String getNormalizedPhoneNumberWithSelector(String rawPhoneNumber) throws InvalidPhoneNumberException { String normalizedPhoneNumber = getNormalizedPhoneNumber(rawPhoneNumber); String country; diff --git a/src/main/java/biz/nynja/account/validation/Validators.java b/src/main/java/biz/nynja/account/validation/Validators.java index 51ff76671da8914e949549d0cfbd0d50331fab11..801949ebd326464b9539edf5d34eb7c52ef87dc6 100644 --- a/src/main/java/biz/nynja/account/validation/Validators.java +++ b/src/main/java/biz/nynja/account/validation/Validators.java @@ -9,6 +9,7 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.validator.routines.EmailValidator; import org.slf4j.Logger; @@ -142,8 +143,8 @@ public class Validators { Validation validation = new Validation(); if (!isContactInfoPhoneLabelValid(oldPhoneNumberLabel)) { - validation.addError( - new ValidationError("Invalid old phone label: " + oldPhoneNumberLabel, Cause.INVALID_PHONE_LABEL)); + validation.addError(new ValidationError("Invalid old phone label: " + oldPhoneNumberLabel, + Cause.INVALID_PHONE_LABEL)); } if (!isContactInfoPhoneLabelValid(editedPhoneNumberLabel)) { validation.addError(new ValidationError("Invalid new phone label: " + editedPhoneNumberLabel, @@ -276,6 +277,11 @@ public class Validators { } public Optional validateAuthenticationProvider(AuthenticationType type, String authProvider) { + return validateAuthenticationProvider(type, authProvider, false); + } + + public Optional validateAuthenticationProvider(AuthenticationType type, String authProvider, + boolean isDeleteOperation) { if (authProvider == null || authProvider.trim().isEmpty()) { return Optional.of(Cause.MISSING_AUTH_PROVIDER_ID); } @@ -284,13 +290,14 @@ public class Validators { return Optional.of(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) { + // " + if (StringUtils.isEmpty(authProvider)) { return Optional.of(Cause.INVALID_PHONENUMBER); } - if (!phoneValidator.isPhoneNumberValid(provider[1], provider[0])) { - return Optional.of(Cause.INVALID_PHONENUMBER); + if (!isDeleteOperation) { + if (!phoneValidator.isPhoneNumberValid(authProvider)) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } } break; case EMAIL: @@ -320,7 +327,7 @@ public class Validators { return Optional.of(Cause.INVALID_PROFILE_ID); } return validateAuthenticationProvider(request.getAuthenticationProvider().getAuthenticationType(), - request.getAuthenticationProvider().getAuthenticationProvider()); + request.getAuthenticationProvider().getAuthenticationProvider(), true); } public Optional validateAddAuthenticationProviderRequest(AddAuthenticationProviderRequest request) { diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 919803c8623dab181f96bc474f327be5f7fab6ff..9526411aba8cd59ef6dd2368fb86de017d9d1dd7 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -589,7 +589,7 @@ public class AccountServiceTests extends GrpcServerTestBase { verify(accountRepositoryAdditional).completePendingAccountCreation(argument.capture()); assertNotNull(argument.getValue().getRolesList()); assertNotNull(Role.USER.equals(argument.getValue().getRoles(0))); - assertNotNull(Role.USER.equals(argument.getValue().getRoles(1))); + assertEquals(argument.getAllValues().size(),1); } @Test @@ -975,7 +975,7 @@ public class AccountServiceTests extends GrpcServerTestBase { final DeleteAuthenticationProviderRequest request = DeleteAuthenticationProviderRequest.newBuilder() .setProfileId(Util.PROFILE_ID.toString()) .setAuthenticationProvider(AuthProviderDetails.newBuilder() - .setAuthenticationProvider(Util.PHONE_PROVIDER).setAuthenticationType(AuthenticationType.PHONE)) + .setAuthenticationProvider(Util.PHONE_NUMBER_STREIGHT).setAuthenticationType(AuthenticationType.PHONE)) .build(); given(profileRepository.findByProfileId(Util.PROFILE_ID)).willReturn(profile1AuthProviderNormalized);