Skip to content

Commit

Permalink
feat: verifyV2 fraud check (#409)
Browse files Browse the repository at this point in the history
* Implement Fraud Check

* Modify comment after PR request
  • Loading branch information
Tr00d authored May 15, 2023
1 parent abdd721 commit 0d9e0d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"channel": "email",
"to": "[email protected]"
}
]
],
"fraud_check": false
}
21 changes: 21 additions & 0 deletions Vonage.Test.Unit/VerifyV2/StartVerification/RequestBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public class RequestBuilderTest

public RequestBuilderTest() => this.fixture = new Fixture();

[Fact]
public void Create_ShouldEnableFraudCheck_ByDefault() =>
StartVerificationRequest.Build()
.WithBrand(this.fixture.Create<string>())
.WithWorkflow(EmailWorkflow.Parse(ValidEmail))
.Create()
.Map(request => request.FraudCheck)
.Should()
.BeSuccess(true);

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down Expand Up @@ -408,5 +418,16 @@ public void Create_ShouldSetWhatsAppWorkflow() =>
workflow.To.Number.Should().Be("123456789");
workflow.From.Should().BeNone();
});

[Fact]
public void Create_ShouldSkipFraudCheck_GivenSkipFraudCheckIsUsed() =>
StartVerificationRequest.Build()
.WithBrand(this.fixture.Create<string>())
.WithWorkflow(EmailWorkflow.Parse(ValidEmail))
.SkipFraudCheck()
.Create()
.Map(request => request.FraudCheck)
.Should()
.BeSuccess(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void ShouldSerialize() =>
.WithChannelTimeout(300)
.WithClientReference("my-personal-reference")
.WithCodeLength(4)
.SkipFraudCheck()
.WithCode("123456")
.Create()
.GetStringContent()
Expand Down
10 changes: 10 additions & 0 deletions Vonage/VerifyV2/StartVerification/StartVerificationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ namespace Vonage.VerifyV2.StartVerification;
/// </summary>
public int CodeLength { get; internal init; }

/// <summary>
/// Indicates the request will bypass network block, if necessary.
/// </summary>
public bool FraudCheck { get; internal init; }

/// <summary>
/// Gets the request language.
/// </summary>
Expand Down Expand Up @@ -93,6 +98,11 @@ private StringContent GetRequestContent()
.Select(workflow => workflow.Serialize(serializer))
.Select(serializedString => serializer.DeserializeObject<dynamic>(serializedString))
.Select(result => result.IfFailure(default)));
if (!this.FraudCheck)
{
values.Add("fraud_check", false);
}

return new StringContent(serializer.SerializeObject(values), Encoding.UTF8, "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class StartVerificationRequestBuilder :
IBuilderForBrand,
IBuilderForWorkflow
{
private bool fraudCheck = true;
private int channelTimeout = 300;
private int codeLength = 4;
private readonly List<IVerificationWorkflow> workflows = new();
Expand All @@ -34,6 +35,7 @@ public Result<StartVerificationRequest> Create() =>
ClientReference = this.clientReference,
CodeLength = this.codeLength,
Workflows = this.workflows.ToArray(),
FraudCheck = this.fraudCheck,
Code = this.code,
}))
.Bind(VerifyWorkflowsNotEmpty)
Expand All @@ -43,6 +45,13 @@ public Result<StartVerificationRequest> Create() =>
.Bind(VerifyCodeLengthHigherThanMinimum)
.Bind(VerifyCodeLengthLowerThanMaximum);

/// <inheritdoc />
public IOptionalBuilder SkipFraudCheck()
{
this.fraudCheck = false;
return this;
}

/// <inheritdoc />
public IBuilderForWorkflow WithBrand(string value)
{
Expand Down Expand Up @@ -224,6 +233,18 @@ public interface IOptionalBuilderForFallbackWorkflow
IOptionalBuilder WithFallbackWorkflow<T>(Result<T> value) where T : IVerificationWorkflow;
}

/// <summary>
/// Represents a builder for SkipFraudCheck.
/// </summary>
public interface IOptionalBuilderForSkipFraudCheck
{
/// <summary>
/// Sets a fallback workflow.
/// </summary>
/// <returns>The builder.</returns>
IOptionalBuilder SkipFraudCheck();
}

/// <summary>
/// Represents a builder for Brand.
/// </summary>
Expand Down Expand Up @@ -260,6 +281,7 @@ public interface IOptionalBuilder :
IOptionalOptionalBuilderForClientReference,
IOptionalBuilderForCodeLength,
IOptionalBuilderForFallbackWorkflow,
IOptionalBuilderForCode
IOptionalBuilderForCode,
IOptionalBuilderForSkipFraudCheck
{
}

0 comments on commit 0d9e0d4

Please sign in to comment.