Skip to content

Commit

Permalink
feat: use Uri instead of string for RedirectUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Nov 24, 2023
1 parent b3c44a6 commit b15aac9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public void ShouldSerializeSilentAuthWorkflow() =>
.GetStringContent()
.Should()
.BeSuccess(this.helper.GetRequestJson());

[Fact]
public void ShouldSerializeSilentAuthWorkflowWithRedirectUrl() =>
StartVerificationRequest.Build()
.WithBrand("ACME, Inc")
.WithWorkflow(SilentAuthWorkflow.Parse("447700900000", "https://acme-app.com/sa/redirect"))
.WithWorkflow(SilentAuthWorkflow.Parse("447700900000", new Uri("https://acme-app.com/sa/redirect")))
.Create()
.GetStringContent()
.Should()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System;
using FluentAssertions;
using Vonage.Common.Failures;
using Vonage.Common.Test.Extensions;
using Vonage.VerifyV2.StartVerification.SilentAuth;
Expand Down Expand Up @@ -32,12 +33,9 @@ public void Parse_ShouldReturnSuccess() =>
workflow.RedirectUrl.Should().BeNone();
});

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void Parse_ShouldReturnSuccess_GivenRedirectIsNullOrWhitespace(string value) =>
SilentAuthWorkflow.Parse(ValidNumber, value)
[Fact]
public void Parse_ShouldReturnSuccess_GivenRedirectIsNull() =>
SilentAuthWorkflow.Parse(ValidNumber, null)
.Should()
.BeSuccess(workflow =>
{
Expand All @@ -47,14 +45,14 @@ public void Parse_ShouldReturnSuccess_GivenRedirectIsNullOrWhitespace(string val
});

[Fact]
public void Parse_ShouldreturnSuccessWithRedirect() =>
SilentAuthWorkflow.Parse(ValidNumber, ValidRedirectUrl)
public void Parse_ShouldReturnSuccessWithRedirect() =>
SilentAuthWorkflow.Parse(ValidNumber, new Uri(ValidRedirectUrl))
.Should()
.BeSuccess(workflow =>
{
workflow.Channel.Should().Be(ExpectedChannel);
workflow.To.Number.Should().Be(ValidNumber);
workflow.RedirectUrl.Should().BeSome(ValidRedirectUrl);
workflow.RedirectUrl.Should().BeSome(new Uri(ValidRedirectUrl));
});
}
}
26 changes: 13 additions & 13 deletions Vonage/VerifyV2/StartVerification/SilentAuth/SilentAuthWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Text.Json.Serialization;
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
using Vonage.Common.Validation;

namespace Vonage.VerifyV2.StartVerification.SilentAuth;

Expand All @@ -11,16 +11,24 @@ namespace Vonage.VerifyV2.StartVerification.SilentAuth;
/// </summary>
public readonly struct SilentAuthWorkflow : IVerificationWorkflow
{
private SilentAuthWorkflow(PhoneNumber to, string redirectUrl = null)
private SilentAuthWorkflow(PhoneNumber to, Uri redirectUrl = null)
{
this.To = to;
this.RedirectUrl = string.IsNullOrWhiteSpace(redirectUrl) ? Maybe<string>.None : redirectUrl;
this.RedirectUrl = redirectUrl ?? Maybe<Uri>.None;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
public string Channel => "silent_auth";

/// <summary>
/// Final redirect added at the end of the check_url request/response lifecycle. See the documentation for integrations. Will contain the request_id and code as a url fragment after the URL.
/// </summary>
[JsonPropertyOrder(2)]
[JsonConverter(typeof(MaybeJsonConverter<Uri>))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<Uri> RedirectUrl { get; }

/// <summary>
/// The phone number to use for authentication, 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.
Expand All @@ -29,14 +37,6 @@ private SilentAuthWorkflow(PhoneNumber to, string redirectUrl = null)
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <summary>
/// Final redirect added at the end of the check_url request/response lifecycle. See the documentation for integrations. Will contain the request_id and code as a url fragment after the URL.
/// </summary>
[JsonPropertyOrder(2)]
[JsonConverter(typeof(MaybeJsonConverter<string>))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<string> RedirectUrl { get; }

/// <summary>
/// Parses the input into a SilentAuthWorkflow.
/// </summary>
Expand All @@ -51,9 +51,9 @@ public static Result<SilentAuthWorkflow> Parse(string to) =>
/// <param name="to">The phone number to use for authentication.</param>
/// <param name="redirectUrl">The final redirect added at the end of the check_url request/response lifecycle</param>
/// <returns>Success or failure.</returns>
public static Result<SilentAuthWorkflow> Parse(string to, string redirectUrl) =>
public static Result<SilentAuthWorkflow> Parse(string to, Uri redirectUrl) =>
PhoneNumber.Parse(to).Map(phoneNumber => new SilentAuthWorkflow(phoneNumber, redirectUrl));

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

0 comments on commit b15aac9

Please sign in to comment.