From 9a27e8f1aa0139d7d0b77bda51ae6416294e19ce Mon Sep 17 00:00:00 2001 From: Guillaume Faas Date: Thu, 30 Nov 2023 11:55:41 +0100 Subject: [PATCH] refactor: E2E tests for FraudCheck --- .../NumberInsightsV2/FraudCheck/E2ETest.cs | 48 ++++++++++++------ .../FraudCheck/SerializationTest.cs | 50 +++++++++++-------- 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/E2ETest.cs b/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/E2ETest.cs index e510116b9..8c1a47688 100644 --- a/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/E2ETest.cs +++ b/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/E2ETest.cs @@ -1,6 +1,7 @@ using System.Net; using System.Threading.Tasks; using Vonage.Common; +using Vonage.Common.Monads; using Vonage.Common.Test; using Vonage.Common.Test.Extensions; using Vonage.NumberInsightV2.FraudCheck; @@ -13,41 +14,60 @@ namespace Vonage.Test.Unit.NumberInsightsV2.FraudCheck [Trait("Category", "E2E")] public class E2ETest { + private const string ApiUrl = "Vonage.Url.Api"; + private readonly SerializationTestHelper serialization = new SerializationTestHelper(typeof(E2ETest).Namespace, JsonSerializer.BuildWithSnakeCase()); - private readonly TestingContext helperWithBasicCredentials = - TestingContext.WithBasicCredentials("Vonage.Url.Api"); + [Fact] + public async Task PerformFraudCheck_WithFraudScore_UsingBasicCredentials() => + await this.PerformFraudCheckWithFraudScore(TestingContext.WithBasicCredentials(ApiUrl)); - private readonly TestingContext helperWithBearerCredentials = - TestingContext.WithBearerCredentials("Vonage.Url.Api"); + [Fact] + public async Task PerformFraudCheck_WithFraudScore_UsingBearerCredentials() => + await this.PerformFraudCheckWithFraudScore(TestingContext.WithBearerCredentials(ApiUrl)); [Fact] public async Task PerformFraudCheck_WithFraudScoreAndSimSwap_UsingBasicCredentials() => - await this.PerformFraudCheckWithFraudScoreAndSimSwap(this.helperWithBasicCredentials); + await this.PerformFraudCheckWithFraudScoreAndSimSwap(TestingContext.WithBasicCredentials(ApiUrl)); [Fact] public async Task PerformFraudCheck_WithFraudScoreAndSimSwap_UsingBearerCredentials() => - await this.PerformFraudCheckWithFraudScoreAndSimSwap(this.helperWithBearerCredentials); + await this.PerformFraudCheckWithFraudScoreAndSimSwap(TestingContext.WithBearerCredentials(ApiUrl)); + + [Fact] + public async Task PerformFraudCheck_WithSimSwap_UsingBasicCredentials() => + await this.PerformFraudCheckWithSimSwap(TestingContext.WithBasicCredentials(ApiUrl)); - private async Task PerformFraudCheckWithFraudScoreAndSimSwap(TestingContext helper) + [Fact] + public async Task PerformFraudCheck_WithSimSwap_UsingBearerCredentials() => + await this.PerformFraudCheckWithSimSwap(TestingContext.WithBearerCredentials(ApiUrl)); + + private async Task PerformFraudCheck(TestingContext helper, string body, Result request) { helper.Server.Given(WireMock.RequestBuilders.Request.Create() .WithPath("/v2/ni") .WithHeader("Authorization", helper.ExpectedAuthorizationHeaderValue) - .WithBody(this.serialization.GetRequestJson(nameof(SerializationTest - .ShouldSerializeWithFraudScoreAndSimSwap))) + .WithBody(body) .UsingPost()) .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) .WithBody(this.serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200)))); await helper.VonageClient.NumberInsightV2Client - .PerformFraudCheckAsync(FraudCheckRequest.Build() - .WithPhone("447009000000") - .WithFraudScore() - .WithSimSwap() - .Create()) + .PerformFraudCheckAsync(request) .Should() .BeSuccessAsync(SerializationTest.GetExpectedFraudCheckResponse()); } + + private Task PerformFraudCheckWithFraudScore(TestingContext helper) => + this.PerformFraudCheck(helper, this.serialization.GetRequestJson(nameof(SerializationTest + .ShouldSerializeWithFraudScore)), SerializationTest.BuildRequestWithFraudScore()); + + private Task PerformFraudCheckWithFraudScoreAndSimSwap(TestingContext helper) => + this.PerformFraudCheck(helper, this.serialization.GetRequestJson(nameof(SerializationTest + .ShouldSerializeWithFraudScoreAndSimSwap)), SerializationTest.BuildRequestWithFraudScoreAndSimSwap()); + + private Task PerformFraudCheckWithSimSwap(TestingContext helper) => + this.PerformFraudCheck(helper, this.serialization.GetRequestJson(nameof(SerializationTest + .ShouldSerializeWithSimSwap)), SerializationTest.BuildRequestWithSimSwap()); } } \ No newline at end of file diff --git a/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/SerializationTest.cs b/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/SerializationTest.cs index 683a5c84f..4673f3b80 100644 --- a/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/SerializationTest.cs +++ b/Vonage.Test.Unit/NumberInsightsV2/FraudCheck/SerializationTest.cs @@ -1,5 +1,6 @@ using System; using Vonage.Common; +using Vonage.Common.Monads; using Vonage.Common.Test; using Vonage.Common.Test.Extensions; using Vonage.NumberInsightV2.FraudCheck; @@ -13,13 +14,6 @@ public class SerializationTest typeof(SerializationTest).Namespace, JsonSerializer.BuildWithSnakeCase()); - public static FraudCheckResponse GetExpectedFraudCheckResponse() => new FraudCheckResponse( - new Guid("6cb4c489-0fc8-4c40-8c3d-95e7e74f9450"), - "phone", - new PhoneData("16197363066", "Orange France", "MOBILE"), - new FraudScore("54", "flag", "medium", "completed"), - new SimSwap("failed", true, "Mobile Network Operator Not Supported")); - [Fact] public void ShouldDeserialize200() => this.helper.Serializer .DeserializeObject(this.helper.GetResponseJson()) @@ -27,31 +21,47 @@ public class SerializationTest .BeSuccess(GetExpectedFraudCheckResponse()); [Fact] - public void ShouldSerializeWithFraudScore() => FraudCheckRequest.Build() - .WithPhone("447009000000") - .WithFraudScore() - .Create() + public void ShouldSerializeWithFraudScore() => BuildRequestWithFraudScore() .GetStringContent() .Should() .BeSuccess(this.helper.GetRequestJson()); [Fact] - public void ShouldSerializeWithFraudScoreAndSimSwap() => FraudCheckRequest.Build() - .WithPhone("447009000000") - .WithFraudScore() - .WithSimSwap() - .Create() + public void ShouldSerializeWithFraudScoreAndSimSwap() => BuildRequestWithFraudScoreAndSimSwap() .GetStringContent() .Should() .BeSuccess(this.helper.GetRequestJson()); [Fact] - public void ShouldSerializeWithSimSwap() => FraudCheckRequest.Build() - .WithPhone("447009000000") - .WithSimSwap() - .Create() + public void ShouldSerializeWithSimSwap() => BuildRequestWithSimSwap() .GetStringContent() .Should() .BeSuccess(this.helper.GetRequestJson()); + + internal static Result BuildRequestWithFraudScoreAndSimSwap() => + FraudCheckRequest.Build() + .WithPhone("447009000000") + .WithFraudScore() + .WithSimSwap() + .Create(); + + internal static Result BuildRequestWithSimSwap() => + FraudCheckRequest.Build() + .WithPhone("447009000000") + .WithSimSwap() + .Create(); + + internal static FraudCheckResponse GetExpectedFraudCheckResponse() => new FraudCheckResponse( + new Guid("6cb4c489-0fc8-4c40-8c3d-95e7e74f9450"), + "phone", + new PhoneData("16197363066", "Orange France", "MOBILE"), + new FraudScore("54", "flag", "medium", "completed"), + new SimSwap("failed", true, "Mobile Network Operator Not Supported")); + + internal static Result BuildRequestWithFraudScore() => + FraudCheckRequest.Build() + .WithPhone("447009000000") + .WithFraudScore() + .Create(); } } \ No newline at end of file