-
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 #80 from PinguApps/46-create-email-verification
create email verification
- Loading branch information
Showing
9 changed files
with
243 additions
and
25 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using PinguApps.Appwrite.Client; | ||
using PinguApps.Appwrite.Server.Servers; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Playground; | ||
internal class App | ||
|
@@ -20,32 +21,16 @@ public async Task Run(string[] args) | |
{ | ||
_client.SetSession(_session); | ||
|
||
Console.WriteLine("Getting Session..."); | ||
var request = new CreateEmailVerificationRequest | ||
{ | ||
Url = "https://localhost:5001/abc123" | ||
}; | ||
|
||
//var response = await _client.Account.CreateEmailToken(new CreateEmailTokenRequest | ||
//{ | ||
// Email = "[email protected]", | ||
// UserId = "664aac1a00113f82e620" | ||
//}); | ||
|
||
//var response = await _client.Account.CreateSession(new CreateSessionRequest | ||
//{ | ||
// UserId = "664aac1a00113f82e620", | ||
// Secret = "623341" | ||
//}); | ||
|
||
var response = await _client.Account.GetSession("66a810f2e55b1329e25b"); | ||
|
||
var response2 = await _client.Account.UpdateSession("66a810f2e55b1329e25b"); | ||
var response = await _client.Account.CreateEmailVerification(request); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.Message)); | ||
|
||
Console.WriteLine(response2.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.Message)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/PinguApps.Appwrite.Shared/Requests/CreateEmailVerificationRequest.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 System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Requests.Validators; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
|
||
/// <summary> | ||
/// The request for creating an email verification (will email the user a link to verify their email) | ||
/// </summary> | ||
public class CreateEmailVerificationRequest : BaseRequest<CreateEmailVerificationRequest, CreateEmailVerificationRequestValidator> | ||
{ | ||
/// <summary> | ||
/// URL to redirect the user back to your app from the verification 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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PinguApps.Appwrite.Shared/Requests/Validators/CreateEmailVerificationRequestValidator.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,11 @@ | ||
using System; | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class CreateEmailVerificationRequestValidator : AbstractValidator<CreateEmailVerificationRequest> | ||
{ | ||
public CreateEmailVerificationRequestValidator() | ||
{ | ||
RuleFor(x => x.Url).NotEmpty().Must(uri => Uri.TryCreate(uri, UriKind.Absolute, out _)); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...uApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateEmailVerification.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,80 @@ | ||
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 CreateEmailVerification_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest() | ||
{ | ||
Url = "https://localhost:5001/abc123" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/verification") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailVerification(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailVerification_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest() | ||
{ | ||
Url = "https://localhost:5001/abc123" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/verification") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailVerification(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailVerification_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest() | ||
{ | ||
Url = "https://localhost:5001/abc123" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/verification") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailVerification(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
tests/PinguApps.Appwrite.Shared.Tests/Requests/CreateEmailVerificationRequestTests.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,97 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class CreateEmailVerificationRequestTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new CreateEmailVerificationRequest(); | ||
|
||
// Assert | ||
Assert.NotNull(request.Url); | ||
Assert.Equal(string.Empty, request.Url); | ||
} | ||
|
||
[Theory] | ||
[InlineData("https://google.com")] | ||
[InlineData("https://localhost:1234")] | ||
public void Properties_CanBeSet(string url) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest(); | ||
|
||
// Act | ||
request.Url = url; | ||
|
||
// Assert | ||
Assert.Equal(url, request.Url); | ||
} | ||
|
||
[Theory] | ||
[InlineData("https://google.com")] | ||
[InlineData("https://localhost:1234")] | ||
public void IsValid_WithValidData_ReturnsTrue(string url) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest | ||
{ | ||
Url = url | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("not a url")] | ||
public void IsValid_WithInvalidData_ReturnsFalse(string url) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest | ||
{ | ||
Url = url | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest | ||
{ | ||
Url = "" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailVerificationRequest | ||
{ | ||
Url = "" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |