Skip to content

Commit

Permalink
refactor: use different wiremock instances to differentiate Vonage re…
Browse files Browse the repository at this point in the history
…quests and OIDC requests
  • Loading branch information
Tr00d committed Jun 20, 2024
1 parent 8b39bc1 commit ccbe6f4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Vonage.Test/NumberVerification/Authenticate/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Vonage.Test.NumberVerification.Authenticate;

[Trait("Category", "E2E")]
public class E2ETest : SimSwap.E2EBase
public class E2ETest : E2EBase
{
public E2ETest() : base(typeof(E2ETest).Namespace)
{
Expand All @@ -17,15 +17,15 @@ public E2ETest() : base(typeof(E2ETest).Namespace)
[Fact]
public async Task Authenticate()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
this.Helper.OidcServer.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/oauth2/auth")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.WithBody(
"login_hint=tel:%2B447700900000&scope=openid+dpv%3AFraudPreventionAndDetection%23check-sim-swap")
.UsingPost())
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAuthorize))));
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
this.Helper.VonageServer.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/oauth2/token")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.WithBody("auth_req_id=123456789&grant_type=urn:openid:params:grant-type:ciba")
Expand Down
5 changes: 2 additions & 3 deletions Vonage.Test/NumberVerification/E2EBase.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Vonage.Serialization;
using Vonage.Test.Common;
using Vonage.Test.TestHelpers;

namespace Vonage.Test.NumberVerification;

public class E2EBase
{
internal readonly TestingContext Helper;
internal readonly OidcTestingContext Helper;
internal readonly SerializationTestHelper Serialization;

protected E2EBase(string serializationNamespace) : this() => this.Serialization =
new SerializationTestHelper(serializationNamespace, JsonSerializerBuilder.BuildWithSnakeCase());

protected E2EBase() => this.Helper = TestingContext.WithBearerCredentials();
protected E2EBase() => this.Helper = OidcTestingContext.WithBearerCredentials();
}
59 changes: 59 additions & 0 deletions Vonage.Test/NumberVerification/OidcTestingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Vonage.Request;
using Vonage.Test.Common.TestHelpers;
using WireMock.Server;
using TimeProvider = Vonage.Common.TimeProvider;

namespace Vonage.Test.NumberVerification;

internal class OidcTestingContext : IDisposable
{
private OidcTestingContext(Credentials credentials, string authorizationHeaderValue,
Dictionary<string, string> settings)
{
this.ExpectedAuthorizationHeaderValue = authorizationHeaderValue;
this.VonageServer = WireMockServer.Start();
this.OidcServer = WireMockServer.Start();
settings.Add(VonageUrls.NexmoApiKey, this.VonageServer.Url);
settings.Add($"{VonageUrls.NexmoApiKey}.EMEA", this.VonageServer.Url);
settings.Add($"{VonageUrls.NexmoApiKey}.APAC", this.VonageServer.Url);
settings.Add($"{VonageUrls.NexmoApiKey}.AMER", this.VonageServer.Url);
settings.Add(VonageUrls.NexmoRestKey, this.VonageServer.Url);
settings.Add(VonageUrls.VideoApiKey, this.VonageServer.Url);
settings.Add(VonageUrls.OidcApiKey, this.OidcServer.Url);
var configuration =
Configuration.FromConfiguration(new ConfigurationBuilder().AddInMemoryCollection(settings).Build());
this.VonageClient = new VonageClient(credentials, configuration, new TimeProvider());
}

public string ExpectedAuthorizationHeaderValue { get; protected init; }
public WireMockServer VonageServer { get; }
public WireMockServer OidcServer { get; }
public VonageClient VonageClient { get; }

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

public static OidcTestingContext WithBearerCredentials() =>
new OidcTestingContext(CreateBearerCredentials(), "Bearer *", new Dictionary<string, string>());

private static Credentials CreateBearerCredentials() => Credentials.FromAppIdAndPrivateKey(
Guid.NewGuid().ToString(),
TokenHelper.GetKey());

protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}

this.VonageServer.Stop();
this.VonageServer.Dispose();
}
}
12 changes: 6 additions & 6 deletions Vonage.Test/NumberVerification/Verify/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Vonage.Test.NumberVerification.Verify;

[Trait("Category", "E2E")]
public class E2ETest : SimSwap.E2EBase
public class E2ETest : E2EBase
{
public E2ETest() : base(typeof(E2ETest).Namespace)
{
Expand All @@ -19,15 +19,15 @@ public async Task CheckAsync()
{
this.SetupAuthorization();
this.SetupToken();
this.SetupCheck(nameof(SerializationTest.ShouldSerialize));
this.SetupVerify(nameof(SerializationTest.ShouldSerialize));
await this.Helper.VonageClient.NumberVerificationClient
.VerifyAsync(VerifyRequest.Parse("346661113334"))
.Should()
.BeSuccessAsync(true);
}

private void SetupCheck(string expectedOutput) =>
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
private void SetupVerify(string expectedOutput) =>
this.Helper.VonageServer.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/camara/number-verification/v031/verify")
.WithHeader("Authorization", "Bearer ABCDEFG")
.WithBody(this.Serialization.GetRequestJson(expectedOutput))
Expand All @@ -36,7 +36,7 @@ await this.Helper.VonageClient.NumberVerificationClient
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeVerify))));

private void SetupToken() =>
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
this.Helper.VonageServer.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/oauth2/token")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.WithBody("auth_req_id=123456789&grant_type=urn:openid:params:grant-type:ciba")
Expand All @@ -45,7 +45,7 @@ await this.Helper.VonageClient.NumberVerificationClient
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAccessToken))));

private void SetupAuthorization() =>
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
this.Helper.OidcServer.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/oauth2/auth")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.WithBody(
Expand Down

0 comments on commit ccbe6f4

Please sign in to comment.