From 5d8f8064f5d0d613f2b07f08215d90bcf3c9ceea Mon Sep 17 00:00:00 2001 From: tr00d Date: Tue, 10 Sep 2024 09:45:22 +0200 Subject: [PATCH] feat: implement DeleteTemplate for VerifyV2 --- .../VerifyV2/DeleteTemplate/E2ETest.cs | 29 ++++++++++++++ .../VerifyV2/DeleteTemplate/RequestTest.cs | 32 +++++++++++++++ .../DeleteTemplate/DeleteTemplateRequest.cs | 40 +++++++++++++++++++ Vonage/VerifyV2/IVerifyV2Client.cs | 8 ++++ Vonage/VerifyV2/VerifyV2Client.cs | 5 +++ 5 files changed, 114 insertions(+) create mode 100644 Vonage.Test/VerifyV2/DeleteTemplate/E2ETest.cs create mode 100644 Vonage.Test/VerifyV2/DeleteTemplate/RequestTest.cs create mode 100644 Vonage/VerifyV2/DeleteTemplate/DeleteTemplateRequest.cs diff --git a/Vonage.Test/VerifyV2/DeleteTemplate/E2ETest.cs b/Vonage.Test/VerifyV2/DeleteTemplate/E2ETest.cs new file mode 100644 index 00000000..b82bf10a --- /dev/null +++ b/Vonage.Test/VerifyV2/DeleteTemplate/E2ETest.cs @@ -0,0 +1,29 @@ +#region +using System; +using System.Net; +using System.Threading.Tasks; +using Vonage.Test.Common.Extensions; +using Vonage.VerifyV2.DeleteTemplate; +using WireMock.ResponseBuilders; +using Xunit; +#endregion + +namespace Vonage.Test.VerifyV2.DeleteTemplate; + +[Trait("Category", "E2E")] +public class E2ETest : E2EBase +{ + [Fact] + public async Task CancelVerification() + { + this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() + .WithPath("/v2/verify/templates/68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb") + .WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) + .UsingDelete()) + .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK)); + await this.Helper.VonageClient.VerifyV2Client.DeleteTemplateAsync( + DeleteTemplateRequest.Parse(Guid.Parse("68c2b32e-55ba-4a8e-b3fa-43b3ae6cd1fb"))) + .Should() + .BeSuccessAsync(); + } +} \ No newline at end of file diff --git a/Vonage.Test/VerifyV2/DeleteTemplate/RequestTest.cs b/Vonage.Test/VerifyV2/DeleteTemplate/RequestTest.cs new file mode 100644 index 00000000..cc2b1cc0 --- /dev/null +++ b/Vonage.Test/VerifyV2/DeleteTemplate/RequestTest.cs @@ -0,0 +1,32 @@ +#region +using System; +using Vonage.Test.Common.Extensions; +using Vonage.VerifyV2.DeleteTemplate; +using Xunit; +#endregion + +namespace Vonage.Test.VerifyV2.DeleteTemplate; + +[Trait("Category", "Request")] +public class RequestTest +{ + [Fact] + public void GetEndpointPath_ShouldReturnApiEndpoint() => + DeleteTemplateRequest.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() => + DeleteTemplateRequest.Parse(Guid.Empty) + .Should() + .BeParsingFailure("TemplateId cannot be empty."); + + [Fact] + public void Parse_ShouldReturnSuccess() => + DeleteTemplateRequest.Parse(new Guid("f3a065af-ac5a-47a4-8dfe-819561a7a287")) + .Map(request => request.TemplateId) + .Should() + .BeSuccess(new Guid("f3a065af-ac5a-47a4-8dfe-819561a7a287")); +} \ No newline at end of file diff --git a/Vonage/VerifyV2/DeleteTemplate/DeleteTemplateRequest.cs b/Vonage/VerifyV2/DeleteTemplate/DeleteTemplateRequest.cs new file mode 100644 index 00000000..9dc9d7ae --- /dev/null +++ b/Vonage/VerifyV2/DeleteTemplate/DeleteTemplateRequest.cs @@ -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.DeleteTemplate; + +/// +public readonly struct DeleteTemplateRequest : IVonageRequest +{ + /// + /// ID of the template. + /// + public Guid TemplateId { get; private init; } + + /// + public HttpRequestMessage BuildRequestMessage() => VonageRequestBuilder + .Initialize(HttpMethod.Delete, this.GetEndpointPath()) + .Build(); + + /// + public string GetEndpointPath() => $"/v2/verify/templates/{this.TemplateId}"; + + /// + /// Parses the input into a DeleteRequest. + /// + /// The template identifier. + /// A success state with the request if the parsing succeeded. A failure state with an error if it failed. + public static Result Parse(Guid templateId) => + Result + .FromSuccess(new DeleteTemplateRequest {TemplateId = templateId}) + .Map(InputEvaluation.Evaluate) + .Bind(evaluation => evaluation.WithRules(VerifyRequestId)); + + private static Result VerifyRequestId(DeleteTemplateRequest templateRequest) => + InputValidation.VerifyNotEmpty(templateRequest, templateRequest.TemplateId, nameof(TemplateId)); +} \ No newline at end of file diff --git a/Vonage/VerifyV2/IVerifyV2Client.cs b/Vonage/VerifyV2/IVerifyV2Client.cs index f73ed7bd..c5453245 100644 --- a/Vonage/VerifyV2/IVerifyV2Client.cs +++ b/Vonage/VerifyV2/IVerifyV2Client.cs @@ -3,6 +3,7 @@ using Vonage.Common.Monads; using Vonage.VerifyV2.Cancel; using Vonage.VerifyV2.CreateTemplate; +using Vonage.VerifyV2.DeleteTemplate; using Vonage.VerifyV2.NextWorkflow; using Vonage.VerifyV2.StartVerification; using Vonage.VerifyV2.VerifyCode; @@ -49,4 +50,11 @@ public interface IVerifyV2Client /// The request. /// Success or Failure. Task> CreateTemplateAsync(Result request); + + /// + /// Deletes a template. + /// + /// The request. + /// Success or Failure. + Task> DeleteTemplateAsync(Result request); } \ No newline at end of file diff --git a/Vonage/VerifyV2/VerifyV2Client.cs b/Vonage/VerifyV2/VerifyV2Client.cs index 1f48f4c1..e26ec9b1 100644 --- a/Vonage/VerifyV2/VerifyV2Client.cs +++ b/Vonage/VerifyV2/VerifyV2Client.cs @@ -5,6 +5,7 @@ using Vonage.Serialization; using Vonage.VerifyV2.Cancel; using Vonage.VerifyV2.CreateTemplate; +using Vonage.VerifyV2.DeleteTemplate; using Vonage.VerifyV2.NextWorkflow; using Vonage.VerifyV2.StartVerification; using Vonage.VerifyV2.VerifyCode; @@ -42,4 +43,8 @@ public Task> VerifyCodeAsync(Result request) => /// public Task> CreateTemplateAsync(Result request) => this.vonageClient.SendWithResponseAsync(request); + + /// + public Task> DeleteTemplateAsync(Result request) => + this.vonageClient.SendAsync(request); } \ No newline at end of file