diff --git a/Vonage.Test/VerifyV2/StartVerification/RequestBuilderTest.cs b/Vonage.Test/VerifyV2/StartVerification/RequestBuilderTest.cs index 299d0957..6baeef19 100644 --- a/Vonage.Test/VerifyV2/StartVerification/RequestBuilderTest.cs +++ b/Vonage.Test/VerifyV2/StartVerification/RequestBuilderTest.cs @@ -1,4 +1,7 @@ -using FluentAssertions; +#region +using FluentAssertions; +using FsCheck; +using FsCheck.Xunit; using Vonage.Common.Failures; using Vonage.Common.Monads; using Vonage.Test.Common.Extensions; @@ -10,6 +13,7 @@ using Vonage.VerifyV2.StartVerification.WhatsApp; using Vonage.VerifyV2.StartVerification.WhatsAppInteractive; using Xunit; +#endregion namespace Vonage.Test.VerifyV2.StartVerification; @@ -50,21 +54,25 @@ public void Create_ShouldReturnFailure_GivenBrandExceeds16Characters() => [Fact] public void Create_ShouldReturnFailure_GivenChannelTimeoutIsHigherThanMaximum() => - BuildBaseRequest() - .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) - .WithChannelTimeout(901) - .Create() - .Should() - .BeParsingFailure("ChannelTimeout cannot be higher than 900."); - - [Fact] - public void Create_ShouldReturnFailure_GivenChannelTimeoutIsLowerThanMinimum() => - BuildBaseRequest() - .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) - .WithChannelTimeout(59) - .Create() - .Should() - .BeParsingFailure("ChannelTimeout cannot be lower than 60."); + Prop.ForAll( + GetChannelTimeoutsAboveMaximum(), + invalidTimeout => BuildBaseRequest() + .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) + .WithChannelTimeout(invalidTimeout) + .Create() + .Should() + .BeParsingFailure("ChannelTimeout cannot be higher than 900.")); + + [Property] + public Property Create_ShouldReturnFailure_GivenChannelTimeoutIsLowerThanMinimum() => + Prop.ForAll( + GetChannelTimeoutsBelowMinimum(), + invalidTimeout => BuildBaseRequest() + .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) + .WithChannelTimeout(invalidTimeout) + .Create() + .Should() + .BeParsingFailure("ChannelTimeout cannot be lower than 15.")); [Fact] public void Create_ShouldReturnFailure_GivenCodeLengthIsHigherThanMaximum() => @@ -106,17 +114,26 @@ public void Create_ShouldSetBrand() => .Should() .BeSuccess("Brand Custom 123"); - [Theory] - [InlineData(60)] - [InlineData(900)] - public void Create_ShouldSetChannelTimeout(int value) => - BuildBaseRequest() - .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) - .WithChannelTimeout(value) - .Create() - .Map(request => request.ChannelTimeout) - .Should() - .BeSuccess(value); + [Property] + public Property Create_ShouldSetChannelTimeout() => + Prop.ForAll( + GetValidChannelTimeouts(), + validTimeout => BuildBaseRequest() + .WithWorkflow(EmailWorkflow.Parse(ValidEmail)) + .WithChannelTimeout(validTimeout) + .Create() + .Map(request => request.ChannelTimeout) + .Should() + .BeSuccess(validTimeout)); + + private static Arbitrary GetChannelTimeoutsAboveMaximum() => + Gen.Choose(901, int.MaxValue).ToArbitrary(); + + private static Arbitrary GetChannelTimeoutsBelowMinimum() => + Gen.Choose(14, -int.MaxValue).ToArbitrary(); + + private static Arbitrary GetValidChannelTimeouts() => + Gen.Choose(15, 900).ToArbitrary(); [Fact] public void Create_ShouldSetClientReference() => diff --git a/Vonage/VerifyV2/StartVerification/StartVerificationRequestBuilder.cs b/Vonage/VerifyV2/StartVerification/StartVerificationRequestBuilder.cs index 132257a0..fd369d12 100644 --- a/Vonage/VerifyV2/StartVerification/StartVerificationRequestBuilder.cs +++ b/Vonage/VerifyV2/StartVerification/StartVerificationRequestBuilder.cs @@ -1,8 +1,10 @@ +#region using System.Collections.Generic; using Vonage.Common.Client; using Vonage.Common.Failures; using Vonage.Common.Monads; using Vonage.Common.Validation; +#endregion namespace Vonage.VerifyV2.StartVerification; @@ -12,6 +14,12 @@ internal class StartVerificationRequestBuilder : IBuilderForWorkflow { private const int MaxBrandLength = 16; + + private const int MinChannelTimeout = 15; + private const int MaxChannelTimeout = 900; + + private const int MinCodeLength = 4; + private const int MaxCodeLength = 10; private readonly List workflows = new List(); private string brand; private int channelTimeout = 300; @@ -138,22 +146,23 @@ private static Result VerifyBrandLength( private static Result VerifyChannelTimeoutHigherThanMinimum( StartVerificationRequest request) => InputValidation - .VerifyHigherOrEqualThan(request, request.ChannelTimeout, 60, nameof(request.ChannelTimeout)); + .VerifyHigherOrEqualThan(request, request.ChannelTimeout, MinChannelTimeout, + nameof(request.ChannelTimeout)); private static Result VerifyChannelTimeoutLowerThanMaximum( StartVerificationRequest request) => InputValidation - .VerifyLowerOrEqualThan(request, request.ChannelTimeout, 900, nameof(request.ChannelTimeout)); + .VerifyLowerOrEqualThan(request, request.ChannelTimeout, MaxChannelTimeout, nameof(request.ChannelTimeout)); private static Result VerifyCodeLengthHigherThanMinimum( StartVerificationRequest request) => InputValidation - .VerifyHigherOrEqualThan(request, request.CodeLength, 4, nameof(request.CodeLength)); + .VerifyHigherOrEqualThan(request, request.CodeLength, MinCodeLength, nameof(request.CodeLength)); private static Result VerifyCodeLengthLowerThanMaximum( StartVerificationRequest request) => InputValidation - .VerifyLowerOrEqualThan(request, request.CodeLength, 10, nameof(request.CodeLength)); + .VerifyLowerOrEqualThan(request, request.CodeLength, MaxCodeLength, nameof(request.CodeLength)); } ///