Skip to content

Commit

Permalink
feat: implement UpdateTemplate for VerifyV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 11, 2024
1 parent fd96bc2 commit a591a15
Show file tree
Hide file tree
Showing 12 changed files with 380 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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-template",
"is_default": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
57 changes: 57 additions & 0 deletions Vonage.Test/VerifyV2/UpdateTemplate/E2ETest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region
using System;
using System.Net;
using System.Threading.Tasks;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.UpdateTemplate;
using WireMock.ResponseBuilders;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.UpdateTemplate;

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

[Fact]
public async Task UpdateTemplate()
{
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create()
.WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb")
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue)
.WithBody(this.Serialization.GetRequestJson(nameof(SerializationTest.ShouldSerialize)))
.UsingPatch())
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserialize200))));
await this.Helper.VonageClient.VerifyV2Client.UpdateTemplateAsync(UpdateTemplateRequest.Build()
.WithId(new Guid("68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb"))
.WithName("my-template")
.SetAsDefaultTemplate()
.Create())
.Should()
.BeSuccessAsync(SerializationTest.VerifyExpectedResponse);
}

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

namespace Vonage.Test.VerifyV2.UpdateTemplate;

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

[Fact]
public void Create_ShouldReturnFailure_GivenIdIsEmpty() =>
UpdateTemplateRequest.Build()
.WithId(Guid.Empty)
.Create()
.Should()
.BeParsingFailure("TemplateId cannot be empty.");

[Fact]
public void Create_ShouldSetId() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.Create()
.Map(request => request.TemplateId)
.Should()
.BeSuccess(ValidTemplateId);

[Fact]
public void Create_ShouldSetName() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.WithName("my-template")
.Create()
.Map(request => request.Name)
.Should()
.BeSuccess("my-template");

[Fact]
public void Create_ShouldSetDefaultToTrue_GivenSetAsDefaultTemplate() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.SetAsDefaultTemplate()
.Create()
.Map(request => request.IsDefault)
.Should()
.BeSuccess(true);

[Fact]
public void Create_ShouldSetDefaultToFalse_GivenSetAsNonDefaultTemplate() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.SetAsNonDefaultTemplate()
.Create()
.Map(request => request.IsDefault)
.Should()
.BeSuccess(false);

[Fact]
public void Create_ShouldHaveNoName_GivenDefault() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.Create()
.Map(request => request.Name)
.Should()
.BeSuccess(Maybe<string>.None);

[Fact]
public void Create_ShouldHaveNoDefault_GivenDefault() =>
UpdateTemplateRequest.Build()
.WithId(ValidTemplateId)
.Create()
.Map(request => request.IsDefault)
.Should()
.BeSuccess(Maybe<bool>.None);
}
20 changes: 20 additions & 0 deletions Vonage.Test/VerifyV2/UpdateTemplate/RequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#region
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2.UpdateTemplate;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.UpdateTemplate;

[Trait("Category", "Request")]
public class RequestTest
{
[Fact]
public void GetEndpointPath_ShouldReturnApiEndpoint() =>
UpdateTemplateRequest.Build()
.WithId(RequestBuilderTest.ValidTemplateId)
.Create()
.Map(request => request.GetEndpointPath())
.Should()
.BeSuccess("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb");
}
53 changes: 53 additions & 0 deletions Vonage.Test/VerifyV2/UpdateTemplate/SerializationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#region
using System;
using FluentAssertions;
using Vonage.Common;
using Vonage.Serialization;
using Vonage.Test.Common;
using Vonage.Test.Common.Extensions;
using Vonage.VerifyV2;
using Vonage.VerifyV2.UpdateTemplate;
using Xunit;
#endregion

namespace Vonage.Test.VerifyV2.UpdateTemplate;

[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")))));

[Fact]
public void ShouldSerialize() => UpdateTemplateRequest.Build()
.WithId(RequestBuilderTest.ValidTemplateId)
.WithName("my-template")
.SetAsDefaultTemplate()
.Create()
.GetStringContent()
.Should()
.BeSuccess(this.helper.GetRequestJson());

[Fact]
public void ShouldSerializeEmpty() => UpdateTemplateRequest.Build()
.WithId(RequestBuilderTest.ValidTemplateId)
.Create()
.GetStringContent()
.Should()
.BeSuccess(this.helper.GetRequestJson());
}
9 changes: 9 additions & 0 deletions Vonage.Test/Vonage.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,15 @@
<None Update="VerifyV2\CreateTemplate\Data\ShouldSerialize-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\UpdateTemplate\Data\ShouldSerialize-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\UpdateTemplate\Data\ShouldSerializeEmpty-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\UpdateTemplate\Data\ShouldDeserialize200-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
8 changes: 8 additions & 0 deletions Vonage/VerifyV2/IVerifyV2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Vonage.VerifyV2.DeleteTemplate;
using Vonage.VerifyV2.NextWorkflow;
using Vonage.VerifyV2.StartVerification;
using Vonage.VerifyV2.UpdateTemplate;
using Vonage.VerifyV2.VerifyCode;
#endregion

Expand Down Expand Up @@ -57,4 +58,11 @@ public interface IVerifyV2Client
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<Unit>> DeleteTemplateAsync(Result<DeleteTemplateRequest> request);

/// <summary>
/// Updates a template.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Success or Failure.</returns>
Task<Result<Template>> UpdateTemplateAsync(Result<UpdateTemplateRequest> request);
}
55 changes: 55 additions & 0 deletions Vonage/VerifyV2/UpdateTemplate/UpdateTemplateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#region
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using Vonage.Common.Client;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
using Vonage.Serialization;
#endregion

namespace Vonage.VerifyV2.UpdateTemplate;

/// <inheritdoc />
public readonly struct UpdateTemplateRequest : IVonageRequest
{
/// <summary>
/// Reference name for template.
/// </summary>
[JsonConverter(typeof(MaybeJsonConverter<string>))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<string> Name { get; internal init; }

/// <summary>
/// ID of the template.
/// </summary>
[JsonIgnore]
public Guid TemplateId { get; internal init; }

/// <summary>
/// Whether the template is the default template for that locale/channel combination.
/// </summary>
[JsonConverter(typeof(MaybeJsonConverter<bool>))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<bool> IsDefault { get; internal init; }

/// <inheritdoc />
public HttpRequestMessage BuildRequestMessage() => VonageRequestBuilder
.Initialize(new HttpMethod("PATCH"), this.GetEndpointPath())
.WithContent(this.GetRequestContent())
.Build();

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

/// <summary>
/// Initializes a builder.
/// </summary>
/// <returns></returns>
public static IBuilderForId Build() => new UpdateTemplateRequestBuilder();

private StringContent GetRequestContent() =>
new StringContent(JsonSerializerBuilder.BuildWithSnakeCase().SerializeObject(this), Encoding.UTF8,
"application/json");
}
Loading

0 comments on commit a591a15

Please sign in to comment.