Skip to content

Commit

Permalink
Merge pull request #278 from PinguApps/216-create-user-target
Browse files Browse the repository at this point in the history
Implemented create user target
  • Loading branch information
pingu2k4 authored Oct 11, 2024
2 parents 58a57ad + d519f9e commit 5624aee
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 12 deletions.
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 - 90 / 291](https://img.shields.io/badge/Server_&_Client-90%20%2F%20291-red?style=for-the-badge)
![Server & Client - 91 / 291](https://img.shields.io/badge/Server_&_Client-91%20%2F%20291-red?style=for-the-badge)

![Server - 46 / 201](https://img.shields.io/badge/Server-46%20%2F%20201-red?style=for-the-badge)
![Server - 47 / 201](https://img.shields.io/badge/Server-47%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 - 35 / 41](https://img.shields.io/badge/Users-35%20%2F%2041-forestgreen?style=for-the-badge)
![Account - 36 / 41](https://img.shields.io/badge/Users-36%20%2F%2041-forestgreen?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -247,7 +247,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [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) |||
| [List User Targets](https://appwrite.io/docs/references/1.6.x/server-rest/users#listTargets) |||
| [Create User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#createTarget) || |
| [Create User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#createTarget) || |
| [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) |||
Expand Down
8 changes: 5 additions & 3 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new ListUserTargetsRequest()
var request = new CreateUserTargetRequest()
{
UserId = "664aac1a00113f82e620"
UserId = "664aac1a00113f82e620",
ProviderType = Shared.Enums.TargetProviderType.Push,
Identifier = "token"
};

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

/// <summary>
/// Create a messaging target
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#createTarget">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The target</returns>
Task<AppwriteResult<Target>> CreateUserTarget(CreateUserTargetRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult> DeleteUserTarget(DeleteUserTargetRequest 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 @@ -616,9 +616,22 @@ public async Task<AppwriteResult<TargetList>> ListUserTargets(ListUserTargetsReq
}
}

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

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

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class Create2faChallengeRequest : BaseRequest<Create2faChallengeRequest,
/// </list>
/// </summary>
[JsonPropertyName("factor")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public SecondFactor Factor { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json.Serialization;
using PinguApps.Appwrite.Shared.Enums;
using PinguApps.Appwrite.Shared.Requests.Users.Validators;
using PinguApps.Appwrite.Shared.Utils;

namespace PinguApps.Appwrite.Shared.Requests.Users;

Expand All @@ -13,12 +14,13 @@ public class CreateUserTargetRequest : UserIdBaseRequest<CreateUserTargetRequest
/// Target ID. Choose a custom ID or generate a random ID with ID.unique(). Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.
/// </summary>
[JsonPropertyName("targetId")]
public string TargetId { get; set; } = string.Empty;
public string TargetId { get; set; } = IdUtils.GenerateUniqueId();

/// <summary>
/// The target provider type. Can be one of the following: <see cref="TargetProviderType.Email"/>, <see cref="TargetProviderType.Sms"/>, <see cref="TargetProviderType.Push"/>
/// </summary>
[JsonPropertyName("providerType")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public TargetProviderType ProviderType { get; set; }

/// <summary>
Expand All @@ -31,11 +33,13 @@ public class CreateUserTargetRequest : UserIdBaseRequest<CreateUserTargetRequest
/// Provider ID. Message will be sent to this target from the specified provider ID. If no provider ID is set the first setup provider will be used.
/// </summary>
[JsonPropertyName("providerId")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ProviderId { get; set; }

/// <summary>
/// Target name. Max length: 128 chars. For example: My Awesome App Galaxy S23
/// </summary>
[JsonPropertyName("name")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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 CreateUserTarget_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateUserTargetRequest
{
UserId = IdUtils.GenerateUniqueId(),
ProviderType = Shared.Enums.TargetProviderType.Push,
Identifier = "token"
};

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

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

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

[Fact]
public async Task CreateUserTarget_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateUserTargetRequest
{
UserId = IdUtils.GenerateUniqueId(),
ProviderType = Shared.Enums.TargetProviderType.Push,
Identifier = "token"
};

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

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

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

[Fact]
public async Task CreateUserTarget_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateUserTargetRequest
{
UserId = IdUtils.GenerateUniqueId(),
ProviderType = Shared.Enums.TargetProviderType.Push,
Identifier = "token"
};

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

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

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
13 changes: 13 additions & 0 deletions tests/PinguApps.Appwrite.Shared.Tests/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,17 @@ public static class Constants
]
}
""";

public const string TargetResponse = """
{
"$id": "259125845563242502",
"$createdAt": "2020-10-15T06:38:00.000+00:00",
"$updatedAt": "2020-10-15T06:38:00.000+00:00",
"name": "Aegon apple token",
"userId": "259125845563242502",
"providerId": "259125845563242502",
"providerType": "email",
"identifier": "token"
}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Constructor_InitializesWithExpectedValues()
var request = new CreateUserTargetRequest();

// Assert
Assert.Equal(string.Empty, request.TargetId);
Assert.NotEmpty(request.TargetId);
Assert.Equal(TargetProviderType.Email, request.ProviderType);
Assert.Equal(string.Empty, request.Identifier);
Assert.Null(request.ProviderId);
Expand Down

0 comments on commit 5624aee

Please sign in to comment.