-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from PinguApps/112-create-phone-token
Implemented create phone token
- Loading branch information
Showing
13 changed files
with
372 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/PinguApps.Appwrite.Shared/Requests/CreatePhoneTokenRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Requests.Validators; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
|
||
/// <summary> | ||
/// The request to create a phone token | ||
/// </summary> | ||
public class CreatePhoneTokenRequest : BaseRequest<CreatePhoneTokenRequest, CreatePhoneTokenRequestValidator> | ||
{ | ||
/// <summary> | ||
/// Unique Id. Choose a custom ID or generate a random ID with <see cref="Utils.IdUtils.GenerateUniqueId(int)"/>. 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("userId")] | ||
public string UserId { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Phone number. Format this number with a leading '+' and a country code, e.g., +16175551212 | ||
/// </summary> | ||
[JsonPropertyName("phone")] | ||
public string PhoneNumber { get; set; } = string.Empty; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/PinguApps.Appwrite.Shared/Requests/Validators/CreatePhoneTokenRequestValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class CreatePhoneTokenRequestValidator : AbstractValidator<CreatePhoneTokenRequest> | ||
{ | ||
public CreatePhoneTokenRequestValidator() | ||
{ | ||
RuleFor(request => request.UserId) | ||
.NotEmpty().WithMessage("User ID must not be empty.") | ||
.Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$").WithMessage("User ID must be alphanumeric and can include periods, hyphens, and underscores. It cannot start with a special character and must be at most 36 characters long."); | ||
|
||
RuleFor(request => request.PhoneNumber) | ||
.NotEmpty().WithMessage("Phone number must not be empty.") | ||
.Matches(@"^\+\d{1,15}$").WithMessage("Phone number must start with a '+' and include the country code, followed by up to 15 digits."); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreatePhoneToken.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 CreatePhoneToken_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePhoneToken_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePhoneToken_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/PinguApps.Appwrite.Server.Tests/Servers/Account/AccountServerTests.CreatePhoneToken.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Net; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
using PinguApps.Appwrite.Shared.Tests; | ||
using RichardSzalay.MockHttp; | ||
|
||
namespace PinguApps.Appwrite.Server.Tests.Servers.Account; | ||
public partial class AccountServerTests | ||
{ | ||
[Fact] | ||
public async Task CreatePhoneToken_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePhoneToken_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePhoneToken_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreatePhoneTokenRequest() | ||
{ | ||
UserId = "123456", | ||
PhoneNumber = "+16175551212" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreatePhoneToken(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
Oops, something went wrong.