Skip to content

Commit

Permalink
feat: [breaking] make WhatsApp 'from' mandatory (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Mar 13, 2024
1 parent 426d91f commit e473554
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
{
"channel": "whatsapp",
"to": "447700900000"
"to": "447700900000",
"from": "447700900001"
},
{
"channel": "voice",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"workflow": [
{
"channel": "whatsapp",
"to": "447700900000"
"to": "447700900000",
"from": "447700900001"
}
]
}
5 changes: 3 additions & 2 deletions Vonage.Test/VerifyV2/StartVerification/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task StartVerificationWithFallbackWorkflows()
.Build()
.WithBrand("ACME, Inc")
.WithWorkflow(WhatsAppInteractiveWorkflow.Parse("447700900000"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("447700900000"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("447700900000", "447700900001"))
.WithFallbackWorkflow(VoiceWorkflow.Parse("447700900000"))
.Create());
VerifyResponseBody(result);
Expand All @@ -89,7 +89,8 @@ public async Task StartWhatsAppInteractiveVerification()
public async Task StartWhatsAppVerification()
{
this.InitializeWireMock(nameof(SerializationTest.ShouldSerializeWhatsAppWorkflow));
var result = await this.StartVerificationAsyncWithWorkflow(WhatsAppWorkflow.Parse("447700900000"));
var result =
await this.StartVerificationAsyncWithWorkflow(WhatsAppWorkflow.Parse("447700900000", "447700900001"));
VerifyResponseBody(result);
}

Expand Down
6 changes: 3 additions & 3 deletions Vonage.Test/VerifyV2/StartVerification/RequestBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class RequestBuilderTest
public void Create_ShouldReturnFailure_GivenOneFallbackWorkflowIsFailure() =>
BuildBaseRequest()
.WithWorkflow(WhatsAppInteractiveWorkflow.Parse("123456789"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("123456789"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("123456789", "123456789"))
.WithFallbackWorkflow(
Result<VoiceWorkflow>.FromFailure(ResultFailure.FromErrorMessage("Random message.")))
.Create()
Expand Down Expand Up @@ -143,7 +143,7 @@ public class RequestBuilderTest
public void Create_ShouldSetFallbackWorkflows() =>
BuildBaseRequest()
.WithWorkflow(WhatsAppInteractiveWorkflow.Parse("123456789"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("123456789"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("123456789", "123456780"))
.WithFallbackWorkflow(VoiceWorkflow.Parse("123456789"))
.Create()
.Map(request => request.Workflows)
Expand All @@ -157,7 +157,7 @@ public class RequestBuilderTest
var fallbackWorkflowOne = workflows[1] as WhatsAppWorkflow? ?? default;
fallbackWorkflowOne.Channel.Should().Be("whatsapp");
fallbackWorkflowOne.To.Number.Should().Be("123456789");
fallbackWorkflowOne.From.Should().BeNone();
fallbackWorkflowOne.From.Number.Should().Be("123456780");
var fallbackWorkflowTwo = workflows[2] as VoiceWorkflow? ?? default;
fallbackWorkflowTwo.Channel.Should().Be("voice");
fallbackWorkflowTwo.To.Number.Should().Be("123456789");
Expand Down
4 changes: 2 additions & 2 deletions Vonage.Test/VerifyV2/StartVerification/SerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class SerializationTest
StartVerificationRequest.Build()
.WithBrand("ACME, Inc")
.WithWorkflow(WhatsAppInteractiveWorkflow.Parse("447700900000"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("447700900000"))
.WithFallbackWorkflow(WhatsAppWorkflow.Parse("447700900000", "447700900001"))
.WithFallbackWorkflow(VoiceWorkflow.Parse("447700900000"))
.Create()
.GetStringContent()
Expand Down Expand Up @@ -145,7 +145,7 @@ public class SerializationTest
public void ShouldSerializeWhatsAppWorkflow() =>
StartVerificationRequest.Build()
.WithBrand("ACME, Inc")
.WithWorkflow(WhatsAppWorkflow.Parse("447700900000"))
.WithWorkflow(WhatsAppWorkflow.Parse("447700900000", "447700900001"))
.Create()
.GetStringContent()
.Should()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class WhatsAppWorkflowTest
[Theory]
[InlineData("")]
[InlineData(" ")]
public void Parse_ShouldReturnFailure_GivenFromIsProvidedButEmpty(string value) =>
[InlineData(null)]
public void Parse_ShouldReturnFailure_GivenFromIsNullOrWhitespace(string value) =>
WhatsAppWorkflow.Parse(ValidToNumber, value)
.Should()
.BeFailure(ResultFailure.FromErrorMessage("Number cannot be null or whitespace."));
Expand All @@ -26,29 +27,18 @@ public class WhatsAppWorkflowTest
[InlineData(" ")]
[InlineData(null)]
public void Parse_ShouldReturnFailure_GivenToIsNullOrWhitespace(string value) =>
WhatsAppWorkflow.Parse(value)
WhatsAppWorkflow.Parse(value, ValidFromNumber)
.Should()
.BeFailure(ResultFailure.FromErrorMessage("Number cannot be null or whitespace."));

[Fact]
public void Parse_ShouldSetWhatsAppWorkflow() =>
WhatsAppWorkflow.Parse(ValidToNumber)
.Should()
.BeSuccess(workflow =>
{
workflow.Channel.Should().Be(ExpectedChannel);
workflow.To.Number.Should().Be(ValidToNumber);
workflow.From.Should().BeNone();
});

[Fact]
public void Parse_ShouldSetWhatsAppWorkflowWithFrom() =>
WhatsAppWorkflow.Parse(ValidToNumber, ValidFromNumber)
.Should()
.BeSuccess(workflow =>
{
workflow.Channel.Should().Be(ExpectedChannel);
workflow.To.Number.Should().Be(ValidToNumber);
workflow.From.Map(from => from.Number).Should().BeSome(ValidFromNumber);
workflow.From.Number.Should().Be(ValidFromNumber);
});
}
21 changes: 6 additions & 15 deletions Vonage/VerifyV2/StartVerification/WhatsApp/WhatsAppWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Vonage.VerifyV2.StartVerification.WhatsApp;
/// </summary>
public readonly struct WhatsAppWorkflow : IVerificationWorkflow
{
private WhatsAppWorkflow(PhoneNumber to, Maybe<PhoneNumber> from)
private WhatsAppWorkflow(PhoneNumber to, PhoneNumber from)
{
this.To = to;
this.From = from;
Expand All @@ -24,10 +24,9 @@ private WhatsAppWorkflow(PhoneNumber to, Maybe<PhoneNumber> from)
/// An optional sender number, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start
/// with the country code, for example, 447700900000.
/// </summary>
[JsonConverter(typeof(MaybeJsonConverter<PhoneNumber>))]
[JsonPropertyOrder(3)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<PhoneNumber> From { get; }
[JsonPropertyOrder(2)]
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber From { get; }

/// <summary>
/// The phone number to contact, in the E.164 format. Don't use a leading + or 00 when entering a phone number, start
Expand All @@ -37,23 +36,15 @@ private WhatsAppWorkflow(PhoneNumber to, Maybe<PhoneNumber> from)
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <summary>
/// Parses the input into a WhatsAppWorkflow.
/// </summary>
/// <param name="to">The phone number to contact.</param>
/// <returns>Success or failure.</returns>
public static Result<WhatsAppWorkflow> Parse(string to) =>
PhoneNumber.Parse(to).Map(phoneNumber => new WhatsAppWorkflow(phoneNumber, Maybe<PhoneNumber>.None));

/// <summary>
/// Parses the input into a WhatsAppWorkflow.
/// </summary>
/// <param name="to">The phone number to contact.</param>
/// <param name="from">The sender number.</param>
/// <returns>Success or failure.</returns>
public static Result<WhatsAppWorkflow> Parse(string to, string from) =>
PhoneNumber.Parse(to)
.Merge(PhoneNumber.Parse(from), (toNumber, fromNumber) => new WhatsAppWorkflow(toNumber, fromNumber));
PhoneNumber.Parse(to).Merge(PhoneNumber.Parse(from),
(toNumber, fromNumber) => new WhatsAppWorkflow(toNumber, fromNumber));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
Expand Down

0 comments on commit e473554

Please sign in to comment.