diff --git a/README.md b/README.md index c3e301e7..fde073b4 100644 --- a/README.md +++ b/README.md @@ -141,11 +141,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( ## ⌛ Progress -![Server & Client - 104 / 318](https://img.shields.io/badge/Server_&_Client-104%20%2F%20318-red?style=for-the-badge) +![Server & Client - 106 / 318](https://img.shields.io/badge/Server_&_Client-106%20%2F%20318-gold?style=for-the-badge) -![Server - 55 / 225](https://img.shields.io/badge/Server-55%20%2F%20225-red?style=for-the-badge) +![Server - 56 / 225](https://img.shields.io/badge/Server-56%20%2F%20225-red?style=for-the-badge) -![Client - 49 / 93](https://img.shields.io/badge/Client-49%20%2F%2093-gold?style=for-the-badge) +![Client - 50 / 93](https://img.shields.io/badge/Client-50%20%2F%2093-gold?style=for-the-badge) ### 🔑 Key | Icon | Definition | @@ -256,13 +256,13 @@ string emailAddressOrErrorMessage = userResponse.Result.Match( | [Update Phone Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhoneVerification) | ❌ | ✅ | ### Teams -![Teams - 4 / 26](https://img.shields.io/badge/Teams-4%20%2F%2026-red?style=for-the-badge) +![Teams - 6 / 26](https://img.shields.io/badge/Teams-6%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) | ⬛ | ⬛ | +| [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) | ⬛ | ⬛ | | [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) | ⬛ | ⬛ | diff --git a/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs b/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs index ee50cb6d..ac8726c1 100644 --- a/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs @@ -30,7 +30,13 @@ public interface ITeamsClient Task> CreateTeam(CreateTeamRequest request); [Obsolete("This method hasn't yet been implemented!")] Task DeleteTeam(DeleteTeamRequest request); - [Obsolete("This method hasn't yet been implemented!")] + + /// + /// Get a team by its ID. All team members have read access for this resource. + /// Appwrite Docs + /// + /// The request content + /// The team Task> GetTeam(GetTeamRequest request); [Obsolete("This method hasn't yet been implemented!")] Task> UpdateName(UpdateNameRequest request); diff --git a/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs b/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs index baf0746c..528df961 100644 --- a/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs +++ b/src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs @@ -61,9 +61,22 @@ public async Task> CreateTeam(CreateTeamRequest request) /// public Task DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException(); - [ExcludeFromCodeCoverage] /// - public Task> GetTeam(GetTeamRequest request) => throw new NotImplementedException(); + public async Task> GetTeam(GetTeamRequest request) + { + try + { + request.Validate(true); + + var result = await _teamsApi.GetTeam(GetCurrentSessionOrThrow(), request.TeamId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// diff --git a/src/PinguApps.Appwrite.Playground/App.cs b/src/PinguApps.Appwrite.Playground/App.cs index f62dfd0f..ceef3350 100644 --- a/src/PinguApps.Appwrite.Playground/App.cs +++ b/src/PinguApps.Appwrite.Playground/App.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; using PinguApps.Appwrite.Shared.Requests.Teams; -using PinguApps.Appwrite.Shared.Utils; namespace PinguApps.Appwrite.Playground; internal class App @@ -20,13 +19,12 @@ public async Task Run(string[] args) { _client.SetSession(_session); - var request = new CreateTeamRequest() + var request = new GetTeamRequest() { - Name = "Client Team", - Roles = ["clients"] + TeamId = "6712ee0a003799f2c2ed" }; - var clientResponse = await _client.Teams.CreateTeam(request); + var clientResponse = await _client.Teams.GetTeam(request); Console.WriteLine(clientResponse.Result.Match( result => result.ToString(), @@ -35,11 +33,7 @@ public async Task Run(string[] args) Console.WriteLine("############################################################################"); - request.TeamId = IdUtils.GenerateUniqueId(); - request.Name = "Server Team"; - request.Roles = []; - - var serverResponse = await _server.Teams.CreateTeam(request); + var serverResponse = await _server.Teams.GetTeam(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 46fd1294..f9414712 100644 --- a/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs @@ -30,7 +30,13 @@ public interface ITeamsClient Task> CreateTeam(CreateTeamRequest request); [Obsolete("This method hasn't yet been implemented!")] Task DeleteTeam(DeleteTeamRequest request); - [Obsolete("This method hasn't yet been implemented!")] + + /// + /// Get a team by its ID. All team members have read access for this resource. + /// Appwrite Docs + /// + /// The request content + /// The team Task> GetTeam(GetTeamRequest request); [Obsolete("This method hasn't yet been implemented!")] Task> UpdateName(UpdateNameRequest request); diff --git a/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs b/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs index cb34303b..3f2d72e7 100644 --- a/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs +++ b/src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs @@ -61,9 +61,22 @@ public async Task> CreateTeam(CreateTeamRequest request) /// public Task DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException(); - [ExcludeFromCodeCoverage] /// - public Task> GetTeam(GetTeamRequest request) => throw new NotImplementedException(); + public async Task> GetTeam(GetTeamRequest request) + { + try + { + request.Validate(true); + + var result = await _teamsApi.GetTeam(request.TeamId); + + return result.GetApiResponse(); + } + catch (Exception e) + { + return e.GetExceptionResponse(); + } + } [ExcludeFromCodeCoverage] /// diff --git a/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.GetTeam.cs b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.GetTeam.cs new file mode 100644 index 00000000..0f6406c2 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Clients/Teams/TeamsClientTests.GetTeam.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 GetTeam_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Respond(TestConstants.AppJson, TestConstants.TeamResponse); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetTeam_ShouldReturnError_WhenSessionIsNull() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsInternalError); + Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message); + } + + [Fact] + public async Task GetTeam_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task GetTeam_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders(true) + .Throw(new HttpRequestException("An error occurred")); + + _appwriteClient.SetSession(TestConstants.Session); + + // Act + var result = await _appwriteClient.Teams.GetTeam(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.GetTeam.cs b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.GetTeam.cs new file mode 100644 index 00000000..31ef0fad --- /dev/null +++ b/tests/PinguApps.Appwrite.Server.Tests/Clients/Teams/TeamsClientTests.GetTeam.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 GetTeam_ShouldReturnSuccess_WhenApiCallSucceeds() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Respond(TestConstants.AppJson, TestConstants.TeamResponse); + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.True(result.Success); + } + + [Fact] + public async Task GetTeam_ShouldHandleException_WhenApiCallFails() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.True(result.IsError); + Assert.True(result.IsAppwriteError); + } + + [Fact] + public async Task GetTeam_ShouldReturnErrorResponse_WhenExceptionOccurs() + { + // Arrange + var request = new GetTeamRequest + { + TeamId = IdUtils.GenerateUniqueId() + }; + + _mockHttp.Expect(HttpMethod.Get, $"{TestConstants.Endpoint}/teams/{request.TeamId}") + .ExpectedHeaders() + .Throw(new HttpRequestException("An error occurred")); + + // Act + var result = await _appwriteClient.Teams.GetTeam(request); + + // Assert + Assert.False(result.Success); + Assert.True(result.IsInternalError); + Assert.Equal("An error occurred", result.Result.AsT2.Message); + } +}