Skip to content

Commit

Permalink
feat: implement GetTemplate for VerifyV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 11, 2024
1 parent a591a15 commit ce23ccd
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"template_id": "8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9",
"name": "my-template",
"is_default": true,
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9"
},
"fragments": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments"
}
}
}
35 changes: 35 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplate/E2ETest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#region
using System;
using System.Net;
using System.Threading.Tasks;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.GetTemplate;
using WireMock.ResponseBuilders;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplate;

[Trait("Category", "E2E")]
public class E2ETest : E2EBase
{
public E2ETest() : base(typeof(E2ETest).Namespace)
{
}

[Fact]
public async Task GetTemplate()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(GetTemplateRequest.Parse(new Guid("68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb")))
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
}
32 changes: 32 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplate/RequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#region
using System;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.GetTemplate;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplate;

[Trait("Category", "Request")]
public class RequestTest
{
[Fact]
public void GetEndpointPath_ShouldReturnApiEndpoint() =>
GetTemplateRequest.Parse(new Guid("f3a065af-ac5a-47a4-8dfe-819561a7a287"))
.Map(request => request.GetEndpointPath())
.Should()
.BeSuccess("/v2/verify/templates/f3a065af-ac5a-47a4-8dfe-819561a7a287");

[Fact]
public void Parse_ShouldReturnFailure_GivenRequestIsEmpty() =>
GetTemplateRequest.Parse(Guid.Empty)
.Should()
.BeParsingFailure("TemplateId cannot be empty.");

[Fact]
public void Parse_ShouldReturnSuccess() =>
GetTemplateRequest.Parse(new Guid("f3a065af-ac5a-47a4-8dfe-819561a7a287"))
.Map(request => request.TemplateId)
.Should()
.BeSuccess(new Guid("f3a065af-ac5a-47a4-8dfe-819561a7a287"));
}
34 changes: 34 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplate/SerializationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#region
using System;
using FluentAssertions;
using Vonage.Common;
using Vonage.Serialization;
using Vonage.Test.Common;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplate;

[Trait("Category", "Serialization")]
public class SerializationTest
{
private readonly SerializationTestHelper helper = new SerializationTestHelper(
typeof(SerializationTest).Namespace,
JsonSerializerBuilder.BuildWithSnakeCase());

[Fact]
public void ShouldDeserialize200() =>
this.helper.Serializer
.DeserializeObject<Template>(this.helper.GetResponseJson())
.Should()
.BeSuccess(VerifyExpectedResponse);

internal static void VerifyExpectedResponse(Template response) =>
response.Should().Be(new Template(new Guid("8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9"), "my-template", true,
new TemplateLinks(
new HalLink(new Uri("https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9")),
new HalLink(new Uri(
"https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments")))));
}
3 changes: 3 additions & 0 deletions Vonage.Test/Vonage.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,9 @@
<None Update="VerifyV2\UpdateTemplate\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\GetTemplate\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
40 changes: 40 additions & 0 deletions Vonage/VerifyV2/GetTemplate/GetTemplateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#region
using System;
using System.Net.Http;
using Vonage.Common.Client;
using Vonage.Common.Monads;
using Vonage.Common.Validation;
#endregion

namespace Vonage.VerifyV2.GetTemplate;

/// <inheritdoc />
public readonly struct GetTemplateRequest : IVonageRequest
{
/// <summary>
/// ID of the template.
/// </summary>
public Guid TemplateId { get; private init; }

/// <inheritdoc />
public HttpRequestMessage BuildRequestMessage() => VonageRequestBuilder
.Initialize(HttpMethod.Get, this.GetEndpointPath())
.Build();

/// <inheritdoc />
public string GetEndpointPath() => $"/v2/verify/templates/{this.TemplateId}";

/// <summary>
/// Parses the input into a GetTemplateRequest.
/// </summary>
/// <param name="templateId">The template identifier.</param>
/// <returns>A success state with the request if the parsing succeeded. A failure state with an error if it failed.</returns>
public static Result<GetTemplateRequest> Parse(Guid templateId) =>
Result<GetTemplateRequest>
.FromSuccess(new GetTemplateRequest {TemplateId = templateId})
.Map(InputEvaluation<GetTemplateRequest>.Evaluate)
.Bind(evaluation => evaluation.WithRules(VerifyRequestId));

private static Result<GetTemplateRequest> VerifyRequestId(GetTemplateRequest templateRequest) =>
InputValidation.VerifyNotEmpty(templateRequest, templateRequest.TemplateId, nameof(TemplateId));
}
8 changes: 8 additions & 0 deletions Vonage/VerifyV2/IVerifyV2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Vonage.VerifyV2.Cancel;
using Vonage.VerifyV2.CreateTemplate;
using Vonage.VerifyV2.DeleteTemplate;
using Vonage.VerifyV2.GetTemplate;
using Vonage.VerifyV2.NextWorkflow;
using Vonage.VerifyV2.StartVerification;
using Vonage.VerifyV2.UpdateTemplate;
Expand Down Expand Up @@ -65,4 +66,11 @@ public interface IVerifyV2Client
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<Template>> UpdateTemplateAsync(Result<UpdateTemplateRequest> request);

/// <summary>
/// Retrieves a template.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<Template>> GetTemplateAsync(Result<GetTemplateRequest> request);
}
5 changes: 5 additions & 0 deletions Vonage/VerifyV2/VerifyV2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Vonage.VerifyV2.Cancel;
using Vonage.VerifyV2.CreateTemplate;
using Vonage.VerifyV2.DeleteTemplate;
using Vonage.VerifyV2.GetTemplate;
using Vonage.VerifyV2.NextWorkflow;
using Vonage.VerifyV2.StartVerification;
using Vonage.VerifyV2.UpdateTemplate;
Expand Down Expand Up @@ -52,4 +53,8 @@ public Task<Result<Unit>> DeleteTemplateAsync(Result<DeleteTemplateRequest> requ
/// <inheritdoc />
public Task<Result<Template>> UpdateTemplateAsync(Result<UpdateTemplateRequest> request) =>
this.vonageClient.SendWithResponseAsync<UpdateTemplateRequest, Template>(request);

/// <inheritdoc />
public Task<Result<Template>> GetTemplateAsync(Result<GetTemplateRequest> request) =>
this.vonageClient.SendWithResponseAsync<GetTemplateRequest, Template>(request);
}

0 comments on commit ce23ccd

Please sign in to comment.