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 token #283

Merged
merged 3 commits into from
Oct 12, 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, `blue` for 100% -->
![Server & Client - 94 / 291](https://img.shields.io/badge/Server_&_Client-94%20%2F%20291-red?style=for-the-badge)
![Server & Client - 95 / 292](https://img.shields.io/badge/Server_&_Client-95%20%2F%20292-red?style=for-the-badge)

![Server - 50 / 201](https://img.shields.io/badge/Server-50%20%2F%20201-red?style=for-the-badge)
![Server - 51 / 202](https://img.shields.io/badge/Server-51%20%2F%20202-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 - 39 / 41](https://img.shields.io/badge/Users-39%20%2F%2041-forestgreen?style=for-the-badge)
![Account - 40 / 42](https://img.shields.io/badge/Users-40%20%2F%2042-forestgreen?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -251,7 +251,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Get User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#getTarget) |||
| [Update User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateTarget) |||
| [Delete User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteTarget) |||
| [Create Token](https://appwrite.io/docs/references/1.6.x/server-rest/users#createToken) || |
| [Create Token](https://appwrite.io/docs/references/1.6.x/server-rest/users#createToken) || |
| [Update Email Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmailVerification) |||
| [Update Phone Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhoneVerification) |||

Expand Down
8 changes: 3 additions & 5 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new UpdateUserTargertRequest()
var request = new CreateTokenRequest()
{
UserId = "664aac1a00113f82e620",
TargetId = "def",
Name = "New Name"
UserId = "664aac1a00113f82e620"
};

var response = await _server.Users.UpdateUserTarget(request);
var response = await _server.Users.CreateToken(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 @@ -326,7 +326,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The target</returns>
Task<AppwriteResult<Target>> UpdateUserTarget(UpdateUserTargertRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Returns a token with a secret key for creating a session. Use the user ID and secret and submit a request to the PUT /account/sessions/token endpoint to complete the login process
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#createToken">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The token</returns>
Task<AppwriteResult<Token>> CreateToken(CreateTokenRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<User>> UpdateEmailVerification(UpdateEmailVerificationRequest 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 @@ -684,9 +684,22 @@ public async Task<AppwriteResult<Target>> UpdateUserTarget(UpdateUserTargertRequ
}
}

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

var result = await _usersApi.CreateToken(request.UserId, request);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public class CreateTokenRequest : UserIdBaseRequest<CreateTokenRequest, CreateTo
/// Token length in characters. The default length is 6 characters
/// </summary>
[JsonPropertyName("length")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? Length { get; set; }

/// <summary>
/// Token expiration period in seconds. The default expiration is 15 minutes
/// </summary>
[JsonPropertyName("expire")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? Expire { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 CreateToken_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateTokenRequest
{
UserId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

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

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

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

[Fact]
public async Task CreateToken_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateTokenRequest
{
UserId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

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

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

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

[Fact]
public async Task CreateToken_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateTokenRequest
{
UserId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

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

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

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