Skip to content

Commit

Permalink
Merge pull request #294 from PinguApps/290-delete-push-target
Browse files Browse the repository at this point in the history
Implemented delete push target
  • Loading branch information
pingu2k4 authored Oct 13, 2024
2 parents 8d14738 + 1abc4d1 commit 0718023
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(

## ⌛ Progress
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third, `blue` for 100% -->
![Server & Client - 99 / 295](https://img.shields.io/badge/Server_&_Client-99%20%2F%20295-gold?style=for-the-badge)
![Server & Client - 100 / 295](https://img.shields.io/badge/Server_&_Client-100%20%2F%20295-gold?style=for-the-badge)

![Server - 53 / 202](https://img.shields.io/badge/Server-53%20%2F%20202-red?style=for-the-badge)

![Client - 46 / 93](https://img.shields.io/badge/Client-46%20%2F%2093-gold?style=for-the-badge)
![Client - 47 / 93](https://img.shields.io/badge/Client-47%20%2F%2093-gold?style=for-the-badge)

### 🔑 Key
| Icon | Definition |
Expand All @@ -155,7 +155,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
![Account - 57 / 58](https://img.shields.io/badge/Account-57%20%2F%2058-forestgreen?style=for-the-badge)
![Account - 58 / 58](https://img.shields.io/badge/Account-58%20%2F%2058-blue?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -197,7 +197,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Update Status](https://appwrite.io/docs/references/1.6.x/client-rest/account#updateStatus) |||
| [Create Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#createPushTarget) |||
| [Update Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePushTarget) |||
| [Delete Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#deletePushTarget) | ||
| [Delete Push Target](https://appwrite.io/docs/references/1.6.x/client-rest/account#deletePushTarget) | ||
| [Create Email Token (OTP)](https://appwrite.io/docs/references/1.6.x/client-rest/account#createEmailToken) |||
| [Create Magic URL Token](https://appwrite.io/docs/references/1.6.x/client-rest/account#createMagicURLToken) |||
| [Create OAuth2 Token](https://appwrite.io/docs/references/1.6.x/client-rest/account#createOAuth2Token) |||
Expand Down
17 changes: 17 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,23 @@ public async Task<AppwriteResult<Target>> CreatePushTarget(CreatePushTargetReque
}
}

/// <inheritdoc/>
public async Task<AppwriteResult> DeletePushTarget(DeletePushTargetRequest request)
{
try
{
request.Validate(true);

var result = await _accountApi.DeletePushTarget(GetCurrentSessionOrThrow(), request.TargetId);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<Target>> UpdatePushTarget(UpdatePushTargetRequest request)
{
Expand Down
7 changes: 7 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ public interface IAccountClient
/// <returns>The target</returns>
Task<AppwriteResult<Target>> CreatePushTarget(CreatePushTargetRequest request);

/// <summary>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/account#deletePushTarget">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeletePushTarget(DeletePushTargetRequest request);

/// <summary>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePushTarget">Appwrite Docs</see></para>
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ internal interface IAccountApi : IBaseApi
[Post("/account/targets/push")]
Task<IApiResponse<Target>> CreatePushTarget([Header("x-appwrite-session")] string session, CreatePushTargetRequest request);

[Delete("/account/targets/{targetId}/push")]
Task<IApiResponse> DeletePushTarget([Header("x-appwrite-session")] string session, string targetId);

[Put("/account/targets/{targetId}/push")]
Task<IApiResponse<Target>> UpdatePushTarget([Header("x-appwrite-session")] string session, string targetId, UpdatePushTargetRequest request);

Expand Down
7 changes: 3 additions & 4 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var request = new UpdatePushTargetRequest()
var request = new DeletePushTargetRequest()
{
TargetId = "670be3330025c0f23b93",
Identifier = "token"
TargetId = "670be3330025c0f23b93"
};

var response = await _client.Account.UpdatePushTarget(request);
var response = await _client.Account.DeletePushTarget(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;
using PinguApps.Appwrite.Shared.Attributes;
using PinguApps.Appwrite.Shared.Requests.Account.Validators;

namespace PinguApps.Appwrite.Shared.Requests.Account;
public class DeletePushTargetRequest : BaseRequest<DeletePushTargetRequest, DeletePushTargetRequestValidator>
{
/// <summary>
/// Target ID.
/// </summary>
[JsonPropertyName("targetId")]
[SdkExclude]
public string TargetId { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FluentValidation;

namespace PinguApps.Appwrite.Shared.Requests.Account.Validators;
public class DeletePushTargetRequestValidator : AbstractValidator<DeletePushTargetRequest>
{
public DeletePushTargetRequestValidator()
{
RuleFor(x => x.TargetId)
.NotEmpty().WithMessage("TargetId is required.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Net;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Shared.Requests.Account;
using PinguApps.Appwrite.Shared.Tests;
using PinguApps.Appwrite.Shared.Utils;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task DeletePushTarget_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push")
.ExpectedHeaders()
.Respond(HttpStatusCode.NoContent);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.DeletePushTarget(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task DeletePushTarget_ShouldReturnError_WhenSessionIsNull()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = IdUtils.GenerateUniqueId()
};

// Act
var result = await _appwriteClient.Account.DeletePushTarget(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsInternalError);
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message);
}

[Fact]
public async Task DeletePushTarget_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError);

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.DeletePushTarget(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task DeletePushTarget_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(TestConstants.Session);

// Act
var result = await _appwriteClient.Account.DeletePushTarget(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using FluentValidation;
using PinguApps.Appwrite.Shared.Requests.Account;
using PinguApps.Appwrite.Shared.Utils;

namespace PinguApps.Appwrite.Shared.Tests.Requests.Account;
public class DeletePushTargetRequestTests
{
[Fact]
public void Constructor_InitializesWithExpectedValues()
{
// Arrange & Act
var request = new DeletePushTargetRequest();

// Assert
Assert.Equal(string.Empty, request.TargetId);
}

[Fact]
public void Properties_CanBeSet()
{
var targetId = IdUtils.GenerateUniqueId();

// Arrange
var request = new DeletePushTargetRequest();

// Act
request.TargetId = targetId;

// Assert
Assert.Equal(targetId, request.TargetId);
}

public static TheoryData<DeletePushTargetRequest> ValidRequestsData =
[
new DeletePushTargetRequest
{
TargetId = IdUtils.GenerateUniqueId()
}
];

[Theory]
[MemberData(nameof(ValidRequestsData))]
public void IsValid_WithValidData_ReturnsTrue(DeletePushTargetRequest request)
{
// Act
var isValid = request.IsValid();

// Assert
Assert.True(isValid);
}

public static TheoryData<DeletePushTargetRequest> InvalidRequestsData =
[
new DeletePushTargetRequest
{
TargetId = ""
},
new DeletePushTargetRequest
{
TargetId = null!
}
];

[Theory]
[MemberData(nameof(InvalidRequestsData))]
public void IsValid_WithInvalidData_ReturnsFalse(DeletePushTargetRequest request)
{
// Act
var isValid = request.IsValid();

// Assert
Assert.False(isValid);
}

[Fact]
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = ""
};

// Assert
Assert.Throws<ValidationException>(() => request.Validate(true));
}

[Fact]
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure()
{
// Arrange
var request = new DeletePushTargetRequest
{
TargetId = ""
};

// Act
var result = request.Validate(false);

// Assert
Assert.False(result.IsValid);
}
}

0 comments on commit 0718023

Please sign in to comment.