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

Implemented list user logs #253

Merged
merged 4 commits into from
Oct 8, 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
![Server & Client - 71 / 291](https://img.shields.io/badge/Server_&_Client-71%20%2F%20291-red?style=for-the-badge)
![Server & Client - 72 / 291](https://img.shields.io/badge/Server_&_Client-72%20%2F%20291-red?style=for-the-badge)

![Server - 27 / 201](https://img.shields.io/badge/Server-27%20%2F%20201-red?style=for-the-badge)
![Server - 28 / 201](https://img.shields.io/badge/Server-28%20%2F%20201-red?style=for-the-badge)

![Client - 44 / 90](https://img.shields.io/badge/Client-44%20%2F%2090-gold?style=for-the-badge)

Expand Down Expand Up @@ -207,7 +207,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | |

### Users
![Account - 16 / 41](https://img.shields.io/badge/Users-16%20%2F%2041-gold?style=for-the-badge)
![Account - 17 / 41](https://img.shields.io/badge/Users-17%20%2F%2041-gold?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -227,7 +227,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Update Email](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmail) | ❌ | ✅ |
| [Create User JWT](https://appwrite.io/docs/references/1.6.x/server-rest/users#createJWT) | ❌ | ✅ |
| [Update User Labels](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateLabels) | ❌ | ✅ |
| [List User Logs](https://appwrite.io/docs/references/1.6.x/server-rest/users#listLogs) | ❌ | |
| [List User Logs](https://appwrite.io/docs/references/1.6.x/server-rest/users#listLogs) | ❌ | |
| [List User Memberships](https://appwrite.io/docs/references/1.6.x/server-rest/users#listMemberships) | ❌ | ⬛ |
| [Update MFA](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateMfa) | ❌ | ⬛ |
| [Delete Authenticator](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteMfaAuthenticator) | ❌ | ⬛ |
Expand Down
9 changes: 2 additions & 7 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Client.Clients;
Expand Down Expand Up @@ -102,9 +101,7 @@ public async Task<AppwriteResult<IdentitiesList>> ListIdentities(ListIdentitiesR
{
request.Validate(true);

var queryStrings = request.Queries?.Select(x => x.GetQueryString()) ?? [];

var result = await _accountApi.ListIdentities(GetCurrentSessionOrThrow(), queryStrings);
var result = await _accountApi.ListIdentities(GetCurrentSessionOrThrow(), RequestUtils.GetQueryStrings(request.Queries));

return result.GetApiResponse();
}
Expand Down Expand Up @@ -153,9 +150,7 @@ public async Task<AppwriteResult<LogsList>> ListLogs(ListLogsRequest request)
{
request.Validate(true);

var queryStrings = request.Queries?.Select(x => x.GetQueryString()) ?? [];

var result = await _accountApi.ListLogs(GetCurrentSessionOrThrow(), queryStrings);
var result = await _accountApi.ListLogs(GetCurrentSessionOrThrow(), RequestUtils.GetQueryStrings(request.Queries));

return result.GetApiResponse();
}
Expand Down
10 changes: 10 additions & 0 deletions src/PinguApps.Appwrite.Client/Utils/RequestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using PinguApps.Appwrite.Shared.Utils;

namespace PinguApps.Appwrite.Client.Utils;
internal static class RequestUtils
{
internal static IEnumerable<string> GetQueryStrings(List<Query>? queries) =>
queries?.Select(x => x.GetQueryString()) ?? [];
}
4 changes: 2 additions & 2 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new CreateUserJwtRequest()
var request = new ListUserLogsRequest()
{
UserId = "664aac1a00113f82e620",
};

var response = await _server.Users.CreateUserJwt(request);
var response = await _server.Users.ListUserLogs(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdateUserLabels(UpdateUserLabelsRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Get the user activity logs list by its unique ID.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#listLogs">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<LogsList>> ListUserLogs(ListUserLogsRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<MembershipsList>> ListUserMemberships(ListUserMembershipsRequest request);
Expand Down
26 changes: 17 additions & 9 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PinguApps.Appwrite.Server.Internals;
Expand Down Expand Up @@ -29,9 +28,7 @@ public async Task<AppwriteResult<UsersList>> ListUsers(ListUsersRequest request)
{
request.Validate(true);

var queryStrings = request.Queries?.Select(x => x.GetQueryString()) ?? [];

var result = await _usersApi.ListUsers(queryStrings, request.Search);
var result = await _usersApi.ListUsers(RequestUtils.GetQueryStrings(request.Queries), request.Search);

return result.GetApiResponse();
}
Expand Down Expand Up @@ -99,9 +96,7 @@ public async Task<AppwriteResult<IdentitiesList>> ListIdentities(ListIdentitiesR
{
request.Validate(true);

var queryStrings = request.Queries?.Select(x => x.GetQueryString()) ?? [];

var result = await _usersApi.ListIdentities(queryStrings, request.Search);
var result = await _usersApi.ListIdentities(RequestUtils.GetQueryStrings(request.Queries), request.Search);

return result.GetApiResponse();
}
Expand Down Expand Up @@ -298,9 +293,22 @@ public async Task<AppwriteResult<User>> UpdateUserLabels(UpdateUserLabelsRequest
}
}

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

var result = await _usersApi.ListUserLogs(request.UserId, RequestUtils.GetQueryStrings(request.Queries));

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
10 changes: 10 additions & 0 deletions src/PinguApps.Appwrite.Server/Utils/RequestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using PinguApps.Appwrite.Shared.Utils;

namespace PinguApps.Appwrite.Server.Utils;
internal static class RequestUtils
{
internal static IEnumerable<string> GetQueryStrings(List<Query>? queries) =>
queries?.Select(x => x.GetQueryString()) ?? [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Tests;
using PinguApps.Appwrite.Shared.Utils;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Users;
public partial class UsersClientTests
{
public static TheoryData<ListUserLogsRequest> ListUserLogs_ValidRequestData =
new TheoryData<ListUserLogsRequest>
{
new ListUserLogsRequest
{
UserId = IdUtils.GenerateUniqueId(),
},
new ListUserLogsRequest
{
UserId = IdUtils.GenerateUniqueId()
}
};

[Theory]
[MemberData(nameof(ListUserLogs_ValidRequestData))]
public async Task ListUserLogs_ShouldReturnSuccess_WhenApiCallSucceeds(ListUserLogsRequest request)
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/users/{request.UserId}/logs")
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.LogsListResponse);

// Act
var result = await _appwriteClient.Users.ListUserLogs(request);

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

[Fact]
public async Task ListUserLogs_ShouldProvideQueries_WhenQueriesProvided()
{
// Arrange
var query = Query.Limit(5);
var request = new ListUserLogsRequest()
{
UserId = IdUtils.GenerateUniqueId(),
Queries = [query]
};

_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/users/{request.UserId}/logs")
.ExpectedHeaders()
.WithQueryString($"queries[]={query.GetQueryString()}")
.Respond(Constants.AppJson, Constants.LogsListResponse);

// Act
var result = await _appwriteClient.Users.ListUserLogs(request);

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

[Fact]
public async Task ListUserLogs_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new ListUserLogsRequest
{
UserId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/users/{request.UserId}/logs")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Users.ListUserLogs(request);

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

[Fact]
public async Task ListUserLogs_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new ListUserLogsRequest
{
UserId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/users/{request.UserId}/logs")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Users.ListUserLogs(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}