Skip to content

Improved handling of invalid mobile numbers #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Altinn.Notifications.Core/Helpers/MobileNumberHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ public static bool IsValidMobileNumber(string? mobileNumber)
}

PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.GetInstance();
PhoneNumber phoneNumber = phoneNumberUtil.Parse(mobileNumber, null);
return phoneNumberUtil.IsValidNumber(phoneNumber);

bool isValidNumber;

try
{
PhoneNumber phoneNumber = phoneNumberUtil.Parse(mobileNumber, null);
isValidNumber = phoneNumberUtil.IsValidNumber(phoneNumber);
}
catch (NumberParseException)
{
isValidNumber = false;
}

return isValidNumber;
}
}
}
6 changes: 3 additions & 3 deletions src/Altinn.Notifications/Validators/Rules/RecipientRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ private static IRuleBuilderOptions<T, IEnumerable<RecipientExt>> MustProvideReci
mobileNumber.RuleFor(mobileNumber => mobileNumber)
.Must(mobileNumber => MobileNumberHelper.IsValidMobileNumber(mobileNumber))
.When(mobileNumber => !string.IsNullOrEmpty(mobileNumber))
.WithMessage("Invalid mobile number format.");
.WithMessage("Mobile number can contain only '+' and numeric characters, and it must adhere to the E.164 standard.");
});
}

private static IRuleBuilderOptions<T, string?> MustBeValidNationalIdentityNumber<T>(this IRuleBuilder<T, string?> ruleBuilder)
{
return ruleBuilder
Expand Down Expand Up @@ -234,7 +234,7 @@ internal static bool IsValidEmail(string? email)
return false;
}

string emailRegexPattern = @"((&quot;[^&quot;]+&quot;)|(([a-zA-Z0-9!#$%&amp;'*+\-=?\^_`{|}~])+(\.([a-zA-Z0-9!#$%&amp;'*+\-=?\^_`{|}~])+)*))@((((([a-zA-Z0-9æøåÆØÅ]([a-zA-Z0-9\-æøåÆØÅ]{0,61})[a-zA-Z0-9æøåÆØÅ]\.)|[a-zA-Z0-9æøåÆØÅ]\.){1,9})([a-zA-Z]{2,14}))|((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})))";
string emailRegexPattern = @"((&quot;[^&quot;]+&quot;)|(([a-zA-Z0-9!#$%&amp;'*+\-=?\^_`{|}~])+(\.([a-zA-Z0-9!#$%&amp;'*+\-=?\^_`{|}~])+)*))@((((([a-zA-Z0-9æøåÆØÅ]([a-zA-Z0-9\-æøåÆØÅ]{0,61})[a-zA-Z0-9æøåÆØÅ]\.)|[a-zA-Z0-9æøåÆØÅ]\.){1,9})([a-zA-Z]{2,14}))|((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})))";

Regex regex = new(emailRegexPattern, RegexOptions.None, TimeSpan.FromSeconds(1));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

using Altinn.Notifications.Models;
using Altinn.Notifications.Validators;
Expand Down Expand Up @@ -212,6 +212,7 @@ public void Validate_ConditionEndpointIsUrn_ReturnsFalse()
}

[Theory]
[InlineData("æge_sjøåsen@domain.com", true)]
[InlineData("stephanie@kul.no", true)]
[InlineData("bakken_kundeservice@sykkelverksted.com", true)]
[InlineData("john.doe@sub.domain.example", true)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

using Altinn.Notifications.Models;
using Altinn.Notifications.Validators;
Expand All @@ -14,10 +14,10 @@ public class NotificationOrderRequestValidatorTests
{
private readonly NotificationOrderRequestValidator _validator;
private readonly NotificationOrderRequestExt _validEmailOrder;

private readonly NotificationOrderRequestExt _validSmsOrder;
private readonly NotificationOrderRequestExt _validEmailPreferredOrder;
private readonly NotificationOrderRequestExt _validSmsPreferredOrder;
private readonly NotificationOrderRequestExt _invalidSmsPreferredOrder;

public NotificationOrderRequestValidatorTests()
{
Expand Down Expand Up @@ -52,6 +52,14 @@ public NotificationOrderRequestValidatorTests()
SmsTemplate = new SmsTemplateExt { Body = "Test Body" },
Recipients = [new RecipientExt { OrganizationNumber = "123456789" }]
};

_invalidSmsPreferredOrder = new()
{
NotificationChannel = NotificationChannelExt.SmsPreferred,
EmailTemplate = new EmailTemplateExt { Subject = "Test", Body = "Test Body" },
SmsTemplate = new SmsTemplateExt { Body = "Test Body" },
Recipients = [new RecipientExt { MobileNumber = "+47invalidChar" }]
};
}

[Fact]
Expand Down Expand Up @@ -149,5 +157,19 @@ public void Validate_SmsPreferred_OrgNumberCombinedWithEmail_IsNotValid()
Assert.False(result.IsValid);
Assert.Contains(result.Errors, a => a.ErrorMessage.Equals("Organization number cannot be combined with email address, mobile number, or national identity number."));
}

[Fact]
public void Validate_SmsPreferred_LettersInMobileNumber_IsNotValid()
{
// Arrange
var model = _invalidSmsPreferredOrder;

// Act
var result = _validator.TestValidate(model);

// Assert
Assert.False(result.IsValid);
Assert.Contains(result.Errors, a => a.ErrorMessage.Equals("Mobile number can contain only '+' and numeric characters, and it must adhere to the E.164 standard."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void Validate_InvalidMobileNumberFormatProvided_ReturnsFalse()
var actual = _validator.Validate(order);

Assert.False(actual.IsValid);
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("Invalid mobile number format."));
Assert.Contains(actual.Errors, a => a.ErrorMessage.Equals("Mobile number can contain only '+' and numeric characters, and it must adhere to the E.164 standard."));
}

[Fact]
Expand Down
Loading