-
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 #129 from PinguApps/96-create-password-recovery
Implemented create password recovery
- Loading branch information
Showing
9 changed files
with
258 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,11 @@ public async Task Run(string[] args) | |
{ | ||
_client.SetSession(_session); | ||
|
||
var response = await _client.Account.RegenerateMfaRecoveryCodes(); | ||
var response = await _client.Account.CreatePasswordRecovery(new Shared.Requests.CreatePasswordRecoveryRequest | ||
{ | ||
Email = "[email protected]", | ||
Url = "https://localhost:5001/abc" | ||
}); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
account => account.ToString(), | ||
|
22 changes: 22 additions & 0 deletions
22
src/PinguApps.Appwrite.Shared/Requests/CreatePasswordRecoveryRequest.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 creating a password recovery | ||
/// </summary> | ||
public class CreatePasswordRecoveryRequest : BaseRequest<CreatePasswordRecoveryRequest, CreatePasswordRecoveryRequestValidator> | ||
{ | ||
/// <summary> | ||
/// User email | ||
/// </summary> | ||
[JsonPropertyName("email")] | ||
public string Email { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// URL to redirect the user back to your app from the recovery email. Only URLs from hostnames in your project platform list are allowed. This requirement helps to prevent an <see href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">open redirect</see> attack against your project API | ||
/// </summary> | ||
[JsonPropertyName("url")] | ||
public string Url { get; set; } = string.Empty; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/PinguApps.Appwrite.Shared/Requests/Validators/CreatePasswordRecoveryRequestValidator.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,12 @@ | ||
using System; | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class CreatePasswordRecoveryRequestValidator : AbstractValidator<CreatePasswordRecoveryRequest> | ||
{ | ||
public CreatePasswordRecoveryRequestValidator() | ||
{ | ||
RuleFor(x => x.Email).NotEmpty().EmailAddress(); | ||
RuleFor(x => x.Url).NotEmpty().Must(uri => Uri.TryCreate(uri, UriKind.Absolute, out _)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...guApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreatePasswordRecovery.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 CreatePasswordRecovery_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest() | ||
{ | ||
Email = "[email protected]", | ||
Url = "https://localhost:1234/abc" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/recovery") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePasswordRecovery(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePasswordRecovery_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest() | ||
{ | ||
Email = "[email protected]", | ||
Url = "https://localhost:1234/abc" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/recovery") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePasswordRecovery(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatePasswordRecovery_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest() | ||
{ | ||
Email = "[email protected]", | ||
Url = "https://localhost:1234/abc" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/recovery") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreatePasswordRecovery(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
tests/PinguApps.Appwrite.Shared.Tests/Requests/CreatePasswordRecoveryTests.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,106 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class CreatePasswordRecoveryTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new CreatePasswordRecoveryRequest(); | ||
|
||
// Assert | ||
Assert.Equal(string.Empty, request.Email); | ||
Assert.Equal(string.Empty, request.Url); | ||
} | ||
|
||
[Fact] | ||
public void Properties_CanBeSet() | ||
{ | ||
var url = "https://localhost:1234/abc"; | ||
var email = "[email protected]"; | ||
|
||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest(); | ||
|
||
// Act | ||
request.Url = url; | ||
request.Email = email; | ||
|
||
// Assert | ||
Assert.Equal(url, request.Url); | ||
Assert.Equal(email, request.Email); | ||
} | ||
|
||
[Fact] | ||
public void IsValid_WithValidData_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest | ||
{ | ||
Email = "[email protected]", | ||
Url = "https://localhost:1234/abc" | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "https://localhost:1234/abc")] | ||
[InlineData("", "https://localhost:1234/abc")] | ||
[InlineData("Not an email", "https://localhost:1234/abc")] | ||
[InlineData("[email protected]", null)] | ||
[InlineData("[email protected]", "")] | ||
[InlineData("[email protected]", "Not a URL")] | ||
public void IsValid_WithInvalidData_ReturnsFalse(string? email, string? url) | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest | ||
{ | ||
Email = email!, | ||
Url = url! | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest | ||
{ | ||
Email = "", | ||
Url = "" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreatePasswordRecoveryRequest | ||
{ | ||
Email = "", | ||
Url = "" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |