Skip to content

Commit

Permalink
feat: implement GetTemplateFragments for VerifyV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 17, 2024
1 parent b817854 commit 016cbb2
Show file tree
Hide file tree
Showing 12 changed files with 524 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"page_size": 1,
"page": 2,
"total_pages": 10,
"total_items": 25,
"_embedded": {
"template_fragments": [
{
"template_fragment_id": "c70f446e-997a-4313-a081-60a02a31dc19",
"channel": "sms",
"locale": "en-us",
"text": "Text content of the template. May contain 4 reserved variables: `${code}`, `${brand}`, `${time-limit}` and `${time-limit-unit}`",
"date_updated": "2023-08-30T15:20:15.178Z",
"date_created": "2021-08-30T20:12:15.178Z",
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9/template_fragments/c70f446e-997a-4313-a081-60a02a31dc19"
},
"template": {
"href": "https://api.nexmo.com/v2/verify/templates/8f35a1a7-eb2f-4552-8fdf-fffdaee41bc9"
}
}
}
]
},
"_links": {
"self": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=2"
},
"next": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=3"
},
"prev": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=1"
},
"last": {
"href": "https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=5"
}
}
}
99 changes: 99 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplateFragments/E2ETest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#region
using System;
using System.Net;
using System.Threading.Tasks;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.GetTemplateFragments;
using WireMock.ResponseBuilders;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplateFragments;

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

[Fact]
public async Task GetTemplateFragmentsEmpty()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateFragmentsAsync(GetTemplateFragmentsRequest
.Build()
.WithTemplateId(RequestBuilderTest.ValidTemplateId)
.Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplatesEmptyFromResponse()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateFragmentsAsync(
new GetTemplateFragmentsHalLink(new Uri(
"https://api.nexmo.com/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments"))
.BuildRequest())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplatesFromResponse()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments")
.WithParam("page_size", "15")
.WithParam("page", "50")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateFragmentsAsync(
new GetTemplateFragmentsHalLink(new Uri(
"https://api.nexmo.com/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments?page_size=15&page=50"))
.BuildRequest())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

[Fact]
public async Task GetTemplates()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments")
.WithParam("page_size", "15")
.WithParam("page", "50")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateFragmentsAsync(GetTemplateFragmentsRequest
.Build()
.WithTemplateId(RequestBuilderTest.ValidTemplateId)
.WithPageSize(15).WithPage(50).Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
}
76 changes: 76 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplateFragments/RequestBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#region
using System;
using Vonage.Common.Monads;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.GetTemplateFragments;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplateFragments;

[Trait("Category", "Request")]
public class RequestBuilderTest
{
internal static readonly Guid ValidTemplateId = new Guid("68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb");

[Fact]
public void Parse_ShouldReturnFailure_GivenRequestIsEmpty() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(Guid.Empty)
.Create()
.Should()
.BeParsingFailure("TemplateId cannot be empty.");

[Fact]
public void Build_ShouldHaveNoDefaultPageSize() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(ValidTemplateId)
.Create()
.Map(request => request.PageSize)
.Should()
.BeSuccess(Maybe<int>.None);

[Fact]
public void Build_ShouldHaveNoDefaultPage() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(ValidTemplateId)
.Create()
.Map(request => request.Page)
.Should()
.BeSuccess(Maybe<int>.None);

[Fact]
public void Build_ShouldSetTemplateId() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(ValidTemplateId)
.Create()
.Map(request => request.TemplateId)
.Should()
.BeSuccess(ValidTemplateId);

[Fact]
public void Build_ShouldSetPageSize() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(ValidTemplateId)
.WithPageSize(50)
.Create()
.Map(request => request.PageSize)
.Should()
.BeSuccess(50);

[Fact]
public void Build_ShouldSetPage() =>
GetTemplateFragmentsRequest
.Build()
.WithTemplateId(ValidTemplateId)
.WithPage(50)
.Create()
.Map(request => request.Page)
.Should()
.BeSuccess(50);
}
33 changes: 33 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplateFragments/RequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#region
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.GetTemplateFragments;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplateFragments;

[Trait("Category", "Request")]
public class RequestTest
{
[Theory]
[InlineData(null, null, "/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments")]
[InlineData(30, null, "/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments?page_size=30")]
[InlineData(null, 50, "/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments?page=50")]
[InlineData(30, 50,
"/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb/template_fragments?page_size=30&page=50")]
public void GetEndpointPath_ShouldReturnApiEndpoint(int? pageSize, int? page, string expectedEndpoint)
{
var builder = GetTemplateFragmentsRequest.Build().WithTemplateId(RequestBuilderTest.ValidTemplateId);
if (pageSize.HasValue)
{
builder = builder.WithPageSize(pageSize.Value);
}

if (page.HasValue)
{
builder = builder.WithPage(page.Value);
}

builder.Create().Map(request => request.GetEndpointPath()).Should().BeSuccess(expectedEndpoint);
}
}
59 changes: 59 additions & 0 deletions Vonage.Test/VerifyV2/GetTemplateFragments/SerializationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region
using System;
using FluentAssertions;
using Vonage.Serialization;
using Vonage.Test.Common;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2;
using Vonage.VerifyV2.GetTemplateFragments;
using Vonage.VerifyV2.StartVerification;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.GetTemplateFragments;

[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<GetTemplateFragmentsResponse>(this.helper.GetResponseJson())
.Should()
.BeSuccess(VerifyExpectedResponse);

internal static void VerifyExpectedResponse(GetTemplateFragmentsResponse response)
{
response.PageSize.Should().Be(1);
response.Page.Should().Be(2);
response.TotalPages.Should().Be(10);
response.TotalItems.Should().Be(25);
response.Links.Self.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=2"));
response.Links.Next.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=3"));
response.Links.Previous.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=1"));
response.Links.Last.Href.Should()
.Be(new Uri(
"https://api.nexmo.com/v2/verify/templates/c70f446e-997a-4313-a081-60a02a31dc19/template_fragments?page=5"));
response.Embedded.Fragments.Should().HaveCount(1);
response.Embedded.Fragments.Should().BeEquivalentTo(new[]
{
new TemplateFragment(
new Guid("c70f446e-997a-4313-a081-60a02a31dc19"),
VerificationChannel.Sms,
Locale.EnUs,
"Text content of the template. May contain 4 reserved variables: `${code}`, `${brand}`, `${time-limit}` and `${time-limit-unit}`",
DateTimeOffset.Parse("2021-08-30T20:12:15.178Z"),
DateTimeOffset.Parse("2023-08-30T15:20:15.178Z")),
});
}
}
8 changes: 4 additions & 4 deletions Vonage.Test/VerifyV2/GetTemplates/E2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task GetTemplatesEmpty()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(GetTemplatesRequest.Build().Create())
.GetTemplatesAsync(GetTemplatesRequest.Build().Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
Expand All @@ -44,7 +44,7 @@ public async Task GetTemplatesEmptyFromResponse()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(
.GetTemplatesAsync(
new GetTemplatesHalLink(new Uri("https://api.nexmo.com/v2/verify/templates")).BuildRequest())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
Expand All @@ -63,7 +63,7 @@ public async Task GetTemplatesFromResponse()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(
.GetTemplatesAsync(
new GetTemplatesHalLink(new Uri("https://api.nexmo.com/v2/verify/templates?page_size=15&page=50"))
.BuildRequest())
.Should()
Expand All @@ -83,7 +83,7 @@ public async Task GetTemplates()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client
.GetTemplateAsync(GetTemplatesRequest.Build().WithPageSize(15).WithPage(50).Create())
.GetTemplatesAsync(GetTemplatesRequest.Build().WithPageSize(15).WithPage(50).Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}
Expand Down
3 changes: 3 additions & 0 deletions Vonage.Test/Vonage.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,9 @@
<None Update="VerifyV2\GetTemplateFragment\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\GetTemplateFragments\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
Loading

0 comments on commit 016cbb2

Please sign in to comment.