From 4940f27c40d0cad32f7db2dfbb8b49f52196c6e9 Mon Sep 17 00:00:00 2001 From: Dragomir Todorov Date: Wed, 23 Jan 2019 13:21:52 +0200 Subject: [PATCH 1/2] NY-6523: Removed the requirement to have country code --- .../account/phone/PhoneNumberNormalizer.java | 10 +++--- .../nynja/account/validation/Validators.java | 36 +++++++++++++------ .../account/services/AccountServiceTests.java | 4 +-- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java b/src/main/java/biz/nynja/account/phone/PhoneNumberNormalizer.java index d5834b4..6c09984 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 51ff766..61ca996 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); } @@ -285,14 +291,24 @@ public class Validators { case PHONE: // We expect to receive phone number in the following format : // ":" - String[] provider = authProvider.split(":"); - if (provider == null || provider.length != 2) { - return Optional.of(Cause.INVALID_PHONENUMBER); + if(isDeleteOperation) { + if (StringUtils.isEmpty(authProvider)) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } + if (!phoneValidator.isPhoneNumberValid(authProvider)) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } + break; + } else { + String[] provider = authProvider.split(":"); + if (provider == null || provider.length != 2) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } + if (!phoneValidator.isPhoneNumberValid(provider[1], provider[0])) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } + break; } - if (!phoneValidator.isPhoneNumberValid(provider[1], provider[0])) { - return Optional.of(Cause.INVALID_PHONENUMBER); - } - break; case EMAIL: if (!util.isEmailValid(authProvider)) { return Optional.of(Cause.INVALID_EMAIL); @@ -320,7 +336,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 919803c..9526411 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); -- GitLab From 700bf22f86750d747f48c8cc2d6b12b7edcce145 Mon Sep 17 00:00:00 2001 From: Dragomir Todorov Date: Thu, 24 Jan 2019 14:35:49 +0200 Subject: [PATCH 2/2] NY-6523: Simplify logic to delete auth provider phone --- .../nynja/account/validation/Validators.java | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/biz/nynja/account/validation/Validators.java b/src/main/java/biz/nynja/account/validation/Validators.java index 61ca996..801949e 100644 --- a/src/main/java/biz/nynja/account/validation/Validators.java +++ b/src/main/java/biz/nynja/account/validation/Validators.java @@ -290,25 +290,16 @@ public class Validators { return Optional.of(Cause.MISSING_AUTH_PROVIDER_TYPE); case PHONE: // We expect to receive phone number in the following format : - // ":" - if(isDeleteOperation) { - if (StringUtils.isEmpty(authProvider)) { - return Optional.of(Cause.INVALID_PHONENUMBER); - } + // " + if (StringUtils.isEmpty(authProvider)) { + return Optional.of(Cause.INVALID_PHONENUMBER); + } + if (!isDeleteOperation) { if (!phoneValidator.isPhoneNumberValid(authProvider)) { return Optional.of(Cause.INVALID_PHONENUMBER); } - break; - } else { - String[] provider = authProvider.split(":"); - if (provider == null || provider.length != 2) { - return Optional.of(Cause.INVALID_PHONENUMBER); - } - if (!phoneValidator.isPhoneNumberValid(provider[1], provider[0])) { - return Optional.of(Cause.INVALID_PHONENUMBER); - } - break; } + break; case EMAIL: if (!util.isEmailValid(authProvider)) { return Optional.of(Cause.INVALID_EMAIL); -- GitLab