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 create session #272

Merged
merged 4 commits into from
Oct 11, 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 @@ -141,9 +141,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(

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

![Server - 42 / 201](https://img.shields.io/badge/Server-42%20%2F%20201-red?style=for-the-badge)
![Server - 43 / 201](https://img.shields.io/badge/Server-43%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 @@ -208,7 +208,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | |

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

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -242,7 +242,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Get User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#getPrefs) | ❌ | ✅ |
| [Update User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePrefs) | ❌ | ✅ |
| [List User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#listSessions) | ❌ | ✅ |
| [Create Session](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSession) | ❌ | |
| [Create Session](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSession) | ❌ | |
| [Delete User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteSessions) | ❌ | ✅ |
| [Delete User Session](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteSession) | ❌ | ⬛ |
| [Update User Status](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateStatus) | ❌ | ⬛ |
Expand Down
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 ListUserSessionsRequest()
var request = new CreateSessionRequest()
{
UserId = "664aac1a00113f82e620"
};

var response = await _server.Users.ListUserSessions(request);
var response = await _server.Users.CreateSession(request);

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

/// <summary>
/// <para>Creates a session for a user. Returns an immediately usable session object.</para>
/// <para>If you want to generate a token for a custom authentication flow, use <see cref="CreateToken(CreateTokenRequest)"/>.</para>
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#createSession">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The session</returns>
Task<AppwriteResult<Session>> CreateSession(CreateSessionRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult> DeleteUserSession(DeleteUserSessionRequest request);
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,22 @@ public async Task<AppwriteResult<SessionsList>> ListUserSessions(ListUserSession
}
}

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

var result = await _usersApi.CreateSession(request.UserId);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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
{
[Fact]
public async Task CreateSession_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateSessionRequest
{
UserId = IdUtils.GenerateUniqueId()
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/users/{request.UserId}/sessions")
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.SessionResponse);

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

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

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

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

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

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

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

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

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

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