From 670443cc5b0de7955859017b327636e22877882c Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Sat, 19 Oct 2024 21:52:13 +0100 Subject: [PATCH 1/3] Implemented Delete Team --- .../Clients/ITeamsClient.cs | 8 +++++++- .../Clients/TeamsClient.cs | 17 +++++++++++++++-- src/PinguApps.Appwrite.Playground/App.cs | 8 ++++---- .../Clients/ITeamsClient.cs | 8 +++++++- .../Clients/TeamsClient.cs | 17 +++++++++++++++-- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs b/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs index ac8726c1..cb9638b7 100644 --- a/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs @@ -28,7 +28,13 @@ public interface ITeamsClient /// The request content /// The team Task> CreateTeam(CreateTeamRequest request); - [Obsolete("This method hasn't yet been implemented!")] + + /// + /// Delete a team using its ID. Only team members with the owner role can delete the team. + /// Appwrite Docs + /// + /// The request content + /// 204 success code Task DeleteTeam(DeleteTeamRequest request); /// diff --git a/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs b/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs index 528df961..46334d25 100644 --- a/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs @@ -57,9 +57,22 @@ public async Task> CreateTeam(CreateTeamRequest request) } } - [ExcludeFromCodeCoverage] /// - public Task DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException(); + public async Task DeleteTeam(DeleteTeamRequest request) + { + try + { + request.Validate(true); + + var result = await _teamsApi.DeleteTeam(GetCurrentSessionOrThrow(), request.TeamId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } /// public async Task> GetTeam(GetTeamRequest request) diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index ceef3350..4bc61b8f 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -19,12 +19,12 @@ public async Task Run(string[] args) { _client.SetSession(_session); - var request = new GetTeamRequest() + var request = new DeleteTeamRequest() { - TeamId = "6712ee0a003799f2c2ed" + TeamId = "6712ee0b0010484ea4fd" }; - var clientResponse = await _client.Teams.GetTeam(request); + var clientResponse = await _client.Teams.DeleteTeam(request); Console.WriteLine(clientResponse.Result.Match( result => result.ToString(), @@ -33,7 +33,7 @@ public async Task Run(string[] args) Console.WriteLine("############################################################################"); - var serverResponse = await _server.Teams.GetTeam(request); + var serverResponse = await _server.Teams.DeleteTeam(request); Console.WriteLine(serverResponse.Result.Match( result => result.ToString(), diff --git a/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs b/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs index f9414712..47ecb0d1 100644 --- a/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs @@ -28,7 +28,13 @@ public interface ITeamsClient /// The request content /// The team Task> CreateTeam(CreateTeamRequest request); - [Obsolete("This method hasn't yet been implemented!")] + + /// + /// Delete a team using its ID. Only team members with the owner role can delete the team. + /// Appwrite Docs + /// + /// The request content + /// 204 success code Task DeleteTeam(DeleteTeamRequest request); /// diff --git a/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs b/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs index 3f2d72e7..4cdf58ef 100644 --- a/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs @@ -57,9 +57,22 @@ public async Task> CreateTeam(CreateTeamRequest request) } } - [ExcludeFromCodeCoverage] /// - public Task DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException(); + public async Task DeleteTeam(DeleteTeamRequest request) + { + try + { + request.Validate(true); + + var result = await _teamsApi.DeleteTeam(request.TeamId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } /// public async Task> GetTeam(GetTeamRequest request) From 73c7c7e43a706537a041c9e16719643b5938ba81 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Sat, 19 Oct 2024 21:54:54 +0100 Subject: [PATCH 2/3] added tests for delete team --- .../Teams/TeamsClientTests.DeleteTeam.cs | 97 +++++++++++++++++++ .../Teams/TeamsClientTests.DeleteTeam.cs | 72 ++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs create mode 100644 tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs new file mode 100644 index 00000000..5ade1587 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs @@ -0,0 +1,97 @@ +using System.Net; +using PinguApps.Appwrite.Client.Clients; +using PinguApps.Appwrite.Shared.Requests.Teams; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Client.Tests.Clients.Teams; +public partial class TeamsClientTests +{ + [Fact] + public async Task DeleteTeam_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.NoContent); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task DeleteTeam_ShouldReturnError_WhenSessionIsNull() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsInternalError); + Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message); + } + + [Fact] + public async Task DeleteTeam_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task DeleteTeam_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Throw(new HttpRequestException("An error occurred")); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} diff --git a/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs new file mode 100644 index 00000000..29ea6968 --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.DeleteTeam.cs @@ -0,0 +1,72 @@ +using System.Net; +using PinguApps.Appwrite.Shared.Requests.Teams; +using PinguApps.Appwrite.Shared.Tests; +using PinguApps.Appwrite.Shared.Utils; +using RichardSzalay.MockHttp; + +namespace PinguApps.Appwrite.Server.Tests.Clients.Teams; +public partial class TeamsClientTests +{ + [Fact] + public async Task DeleteTeam_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.NoContent); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task DeleteTeam_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task DeleteTeam_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new DeleteTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Delete, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Teams.DeleteTeam(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +} From df162c02bd8b851a526f151bb7524e548ea7f043 Mon Sep 17 00:00:00 2001 From: Matthew Parker Date: Sat, 19 Oct 2024 21:56:02 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fde073b4..9832368a 100644 --- a/README.md +++ b/README.md @@ -141,11 +141,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## ⌛ Progress -![Server & Client - 106 / 318](https://img.shields.io/badge/Server_&_Client-106%20%2F%20318-gold?style=for-the-badge) +![Server & Client - 108 / 318](https://img.shields.io/badge/Server_&_Client-108%20%2F%20318-gold?style=for-the-badge) -![Server - 56 / 225](https://img.shields.io/badge/Server-56%20%2F%20225-red?style=for-the-badge) +![Server - 57 / 225](https://img.shields.io/badge/Server-57%20%2F%20225-red?style=for-the-badge) -![Client - 50 / 93](https://img.shields.io/badge/Client-50%20%2F%2093-gold?style=for-the-badge) +![Client - 51 / 93](https://img.shields.io/badge/Client-51%20%2F%2093-gold?style=for-the-badge) ### 🔑 Key | Icon | Definition | @@ -256,14 +256,14 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Update Phone Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhoneVerification) | ❌ | ✅ | ### Teams -![Teams - 6 / 26](https://img.shields.io/badge/Teams-6%20%2F%2026-red?style=for-the-badge) +![Teams - 8 / 26](https://img.shields.io/badge/Teams-8%20%2F%2026-red?style=for-the-badge) | Endpoint | Client | Server | |:-:|:-:|:-:| | [List Teams](https://appwrite.io/docs/references/1.6.x/client-rest/teams#list) | ✅ | ✅ | | [Create Team](https://appwrite.io/docs/references/1.6.x/client-rest/teams#create) | ✅ | ✅ | | [Get Team](https://appwrite.io/docs/references/1.6.x/client-rest/teams#get) | ✅ | ✅ | -| [Update Name](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updateName) | ⬛ | ⬛ | +| [Update Name](https://appwrite.io/docs/references/1.6.x/client-rest/teams#updateName) | ✅ | ✅ | | [Delete Team](https://appwrite.io/docs/references/1.6.x/client-rest/teams#delete) | ⬛ | ⬛ | | [List Team Memberships](https://appwrite.io/docs/references/1.6.x/client-rest/teams#listMemberships) | ⬛ | ⬛ | | [Create Team Membership](https://appwrite.io/docs/references/1.6.x/client-rest/teams#createMembership) | ⬛ | ⬛ |