Skip to content

Commit

Permalink
Merge pull request #234 from PinguApps/185-create-user-with-bcrypt-pa…
Browse files Browse the repository at this point in the history
…ssword

Implemented create user with bcrypt password
  • Loading branch information
pingu2k4 authored Oct 6, 2024
2 parents 2f89042 + 399646e commit 535c275
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

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

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

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

| Endpoint | Client | Server |
|:-:|:-:|:-:|
| [List Users](https://appwrite.io/docs/references/1.6.x/server-rest/users#list) |||
| [Create User](https://appwrite.io/docs/references/1.6.x/server-rest/users#create) |||
| [Create User with Argon2 Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createArgon2User) |||
| [Create User with Bcrypt Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createBcryptUser) || |
| [Create User with Bcrypt Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createBcryptUser) || |
| [List Identities](https://appwrite.io/docs/references/1.6.x/server-rest/users#listIdentities) |||
| [Delete Identity](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteIdentity) |||
| [Create User with MD5 Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMD5User) |||
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 @@ -18,14 +18,14 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new CreateUserWithArgon2PasswordRequest()
var request = new CreateUserWithBcryptPasswordRequest()
{
UserId = IdUtils.GenerateUniqueId(),
Email = "p@example.com",
Password = "$argon2i$v=19$m=16,t=2,p=1$ck5UdVZSeEVEZHFDc1VTZA$Ru/mskiTeTfoBlGqkMcKSA"
Email = "pingu@example.com",
Password = "$2y$10$I1A85SWJhLzjIFatLK7/SuFFBDML1J7RQzWzF5/38bMPPOWK/gsqC"
};

var response = await _server.Users.CreateUserWithArgon2Password(request);
var response = await _server.Users.CreateUserWithBcryptPassword(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 @@ -36,7 +36,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> CreateUserWithArgon2Password(CreateUserWithArgon2PasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Create a new user. Password provided must be hashed with the <see href="https://en.wikipedia.org/wiki/Bcrypt">Bcrypt</see> algorithm. Use <see cref="CreateUser(CreateUserRequest)"/> to create users with a plain text password.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#createBcryptUser">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> CreateUserWithBcryptPassword(CreateUserWithBcryptPasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<IdentitiesList>> ListIdentities(ListIdentitiesRequest 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 @@ -75,9 +75,22 @@ public async Task<AppwriteResult<User>> CreateUserWithArgon2Password(CreateUserW
}
}

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

var result = await _usersApi.CreateUserWithBcryptPassword(request);

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Users;
public partial class UsersClientTests
{
[Fact]
public async Task CreateUserWithBcryptPassword_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateUserWithBcryptPasswordRequest
{
Email = "[email protected]",
Password = "password",
Name = "Pingu"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/users/bcrypt")
.ExpectedHeaders()
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);

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

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

[Fact]
public async Task CreateUserWithBcryptPassword_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateUserWithBcryptPasswordRequest
{
Email = "[email protected]",
Password = "password",
Name = "Pingu"
};

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

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

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

[Fact]
public async Task CreateUserWithBcryptPassword_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateUserWithBcryptPasswordRequest
{
Email = "[email protected]",
Password = "password",
Name = "Pingu"
};

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

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

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

0 comments on commit 535c275

Please sign in to comment.