Skip to content

Commit

Permalink
Merge pull request #61 from PinguApps/39-update-phone-implementation
Browse files Browse the repository at this point in the history
Update phone implementation
  • Loading branch information
pingu2k4 authored Jul 9, 2024
2 parents 8eeecc1 + fc2a70d commit 981399a
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(

## ⌛ Progress
### Server & Client
![6 / 298](https://progress-bar.dev/6/?scale=298&suffix=%20/%20298&width=500)
![7 / 298](https://progress-bar.dev/7/?scale=298&suffix=%20/%20298&width=500)
### Server Only
![1 / 195](https://progress-bar.dev/1/?scale=195&suffix=%20/%20195&width=300)
### Client Only
![5 / 93](https://progress-bar.dev/5/?scale=93&suffix=%20/%2093&width=300)
![6 / 93](https://progress-bar.dev/6/?scale=93&suffix=%20/%2093&width=300)

### 🔑 Key
| Icon | Definition |
Expand All @@ -153,7 +153,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
![6 / 52](https://progress-bar.dev/6/?scale=52&suffix=%20/%2052&width=120)
![7 / 52](https://progress-bar.dev/7/?scale=52&suffix=%20/%2052&width=120)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -176,7 +176,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Regenerate MFA Recovery Codes](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMfaRecoveryCodes) |||
| [Update Name](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateName) |||
| [Update Password](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePassword) |||
| [Update Phone](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePhone) | ||
| [Update Phone](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePhone) | ||
| [Get Account Preferences](https://appwrite.io/docs/references/1.5.x/client-rest/account#getPrefs) |||
| [Update Preferences](https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePrefs) |||
| [Create Password Recovery](https://appwrite.io/docs/references/1.5.x/client-rest/account#createRecovery) |||
Expand Down
15 changes: 15 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,19 @@ public async Task<AppwriteResult<User>> UpdatePassword(UpdatePasswordRequest req
return e.GetExceptionResponse<User>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest request)
{
try
{
var result = await _accountApi.UpdatePhone(Session, request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<User>();
}
}
}
8 changes: 8 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ public interface IAccountClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePassword(UpdatePasswordRequest request);

/// <summary>
/// Update the currently logged in user's phone number. After updating the phone number, the phone verification status will be reset. A confirmation SMS is not sent automatically, however you can use the POST /account/verification/phone endpoint to send a confirmation SMS
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#updatePhone">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest request);
}
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public interface IAccountApi : IBaseApi

[Patch("/account/password")]
Task<IApiResponse<User>> UpdatePassword([Header("x-appwrite-session")] string? session, UpdatePasswordRequest name);

[Patch("/account/phone")]
Task<IApiResponse<User>> UpdatePhone([Header("x-appwrite-session")] string? session, UpdatePhoneRequest name);
}
8 changes: 4 additions & 4 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var request = new UpdatePasswordRequest
var request = new UpdatePhoneRequest
{
OldPassword = "MyNewPassword",
NewPassword = "MyNewPassword123"
Password = "MyNewPassword",
Phone = "+14155552671"
};

var result = await _client.Account.UpdatePassword(request);
var result = await _client.Account.UpdatePhone(request);

result.Result.Switch(
account => Console.WriteLine(account.Email),
Expand Down
21 changes: 21 additions & 0 deletions src/PinguApps.Appwrite.Shared/Requests/UpdatePhoneRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;

namespace PinguApps.Appwrite.Shared.Requests;

/// <summary>
/// The request for updating a users phone
/// </summary>
public class UpdatePhoneRequest
{
/// <summary>
/// Phone number. Format this number with a leading '+' and a country code, e.g., +16175551212
/// </summary>
[JsonPropertyName("phone")]
public string Phone { get; set; } = string.Empty;

/// <summary>
/// New user password. Must be at least 8 chars
/// </summary>
[JsonPropertyName("password")]
public string Password { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task UpdatePhone_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdatePhoneRequest()
{
Password = "Password",
Phone = "+14155552671"
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/phone")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(Constants.AppJson, Constants.UserResponse);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdatePhone(request);

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

[Fact]
public async Task UpdatePhone_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdatePhoneRequest()
{
Password = "Password",
Phone = "+14155552671"
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/phone")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdatePhone(request);

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

[Fact]
public async Task UpdatePhone_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdatePhoneRequest()
{
Password = "Password",
Phone = "+14155552671"
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/phone")
.ExpectedHeaders(true)
.WithJsonContent(request)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.UpdatePhone(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using PinguApps.Appwrite.Shared.Requests;

namespace PinguApps.Appwrite.Shared.Tests.Requests;

public class UpdatePhoneRequestTests
{
[Fact]
public void Constructor_InitializesWithExpectedValues()
{
// Arrange & Act
var request = new UpdatePhoneRequest();

// Assert
Assert.NotNull(request.Password);
Assert.NotNull(request.Phone);
Assert.Equal(string.Empty, request.Password);
Assert.Equal(string.Empty, request.Phone);
}

[Theory]
[InlineData("password", "+123456789")]
[InlineData("drowssap", "+987654321")]
public void Properties_CanBeSet(string password, string phone)
{
// Arrange
var request = new UpdatePhoneRequest();

// Act
request.Password = password;
request.Phone = phone;

// Assert
Assert.Equal(password, request.Password);
Assert.Equal(phone, request.Phone);
}
}

0 comments on commit 981399a

Please sign in to comment.