Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@coderabbitai #325

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 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 - 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 |
Expand Down Expand Up @@ -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) |||
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Client/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public interface ITeamsClient
/// <param name="request">The request content</param>
/// <returns>The team</returns>
Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request);
[Obsolete("This method hasn't yet been implemented!")]

/// <summary>
/// Delete a team using its ID. Only team members with the owner role can delete the team.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/client-rest/teams#delete">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request);

/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Client/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,22 @@ public async Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request)
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request)
{
try
{
request.Validate(true);

var result = await _teamsApi.DeleteTeam(GetCurrentSessionOrThrow(), request.TeamId);

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

/// <inheritdoc/>
public async Task<AppwriteResult<Team>> GetTeam(GetTeamRequest request)
Expand Down
8 changes: 4 additions & 4 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/ITeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public interface ITeamsClient
/// <param name="request">The request content</param>
/// <returns>The team</returns>
Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request);
[Obsolete("This method hasn't yet been implemented!")]

/// <summary>
/// Delete a team using its ID. Only team members with the owner role can delete the team.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/teams#delete">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>204 success code</returns>
Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request);

/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,22 @@ public async Task<AppwriteResult<Team>> CreateTeam(CreateTeamRequest request)
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult> DeleteTeam(DeleteTeamRequest request)
{
try
{
request.Validate(true);

var result = await _teamsApi.DeleteTeam(request.TeamId);

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

/// <inheritdoc/>
public async Task<AppwriteResult<Team>> GetTeam(GetTeamRequest request)
Expand Down
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.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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}