-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement UpdateTemplate for VerifyV2
- Loading branch information
Showing
12 changed files
with
380 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Vonage.Test/VerifyV2/UpdateTemplate/Data/ShouldDeserialize200-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
Vonage.Test/VerifyV2/UpdateTemplate/Data/ShouldSerialize-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "my-template", | ||
"is_default": true | ||
} |
2 changes: 2 additions & 0 deletions
2
Vonage.Test/VerifyV2/UpdateTemplate/Data/ShouldSerializeEmpty-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
Oops, something went wrong.