-
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 #172 from PinguApps/104-update-phone-session
Implemented update phone session
- Loading branch information
Showing
13 changed files
with
376 additions
and
13 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/UpdatePhoneSessionRequest.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 for updating the phone session. | ||
/// </summary> | ||
public class UpdatePhoneSessionRequest : BaseRequest<UpdatePhoneSessionRequest, UpdatePhoneSessionRequestValidator> | ||
{ | ||
/// <summary> | ||
/// User 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> | ||
/// Valid verification token. | ||
/// </summary> | ||
[JsonPropertyName("secret")] | ||
public string Secret { get; set; } = string.Empty; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdatePhoneSessionRequestValidator.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,15 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class UpdatePhoneSessionRequestValidator : AbstractValidator<UpdatePhoneSessionRequest> | ||
{ | ||
public UpdatePhoneSessionRequestValidator() | ||
{ | ||
RuleFor(x => x.UserId) | ||
.NotEmpty().WithMessage("The user ID is required.") | ||
.Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$").WithMessage("The user ID must be between 1 and 36 characters long and can only contain a-z, A-Z, 0-9, period, hyphen, and underscore."); | ||
|
||
RuleFor(x => x.Secret) | ||
.NotEmpty().WithMessage("The secret is required."); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
.../PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.UpdatePhoneSession.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 UpdatePhoneSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.SessionResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePhoneSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePhoneSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePhoneSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePhoneSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePhoneSession(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
.../PinguApps.Appwrite.Server.Tests/Servers/Account/AccountServerTests.UpdatePhoneSession.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 UpdatePhoneSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.SessionResponse); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdatePhoneSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePhoneSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdatePhoneSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePhoneSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdatePhoneSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/phone") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdatePhoneSession(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
Oops, something went wrong.