-
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 #71 from PinguApps/45-create-magic-url
Implemented create magic url
- Loading branch information
Showing
16 changed files
with
459 additions
and
19 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 |
---|---|---|
|
@@ -19,24 +19,16 @@ public App(IAppwriteClient client, IAppwriteServer server, IConfiguration config | |
|
||
public async Task Run(string[] args) | ||
{ | ||
_client.SetSession(_session); | ||
//_client.SetSession(_session); | ||
|
||
var request = new UpdatePhoneRequest | ||
var request = new CreateEmailTokenRequest | ||
{ | ||
Password = "sword", | ||
Phone = "14155552671" | ||
Email = "[email protected]", | ||
UserId = "664aac1a00113f82e620", | ||
Phrase = true | ||
}; | ||
|
||
var f = request.IsValid(); | ||
|
||
var result = await _client.Account.UpdatePreferences(new UpdatePreferencesRequest | ||
{ | ||
Preferences = new Dictionary<string, string> | ||
{ | ||
{ "key1", "val1" }, | ||
{ "key2", "val2" } | ||
} | ||
}); | ||
var result = await _server.Account.CreateEmailToken(request); | ||
|
||
result.Result.Switch( | ||
account => Console.WriteLine(string.Join(',', account)), | ||
|
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
29 changes: 29 additions & 0 deletions
29
src/PinguApps.Appwrite.Shared/Requests/CreateEmailTokenRequest.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,29 @@ | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Requests.Validators; | ||
using PinguApps.Appwrite.Shared.Utils; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
|
||
/// <summary> | ||
/// The request for creating an email token | ||
/// </summary> | ||
public class CreateEmailTokenRequest : BaseRequest<CreateEmailTokenRequest, CreateEmailTokenRequestValidator> | ||
{ | ||
/// <summary> | ||
/// User ID. Choose a custom ID or generate a random ID with <see cref="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; } = IdUtils.GenerateUniqueId(); | ||
|
||
/// <summary> | ||
/// User email | ||
/// </summary> | ||
[JsonPropertyName("email")] | ||
public string Email { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Toggle for security phrase. If enabled, email will be send with a randomly generated phrase and the phrase will also be included in the response. Confirming phrases match increases the security of your authentication flow. | ||
/// </summary> | ||
[JsonPropertyName("phrase")] | ||
public bool Phrase { get; set; } = false; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PinguApps.Appwrite.Shared/Requests/Validators/CreateEmailTokenRequestValidator.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 FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class CreateEmailTokenRequestValidator : AbstractValidator<CreateEmailTokenRequest> | ||
{ | ||
public CreateEmailTokenRequestValidator() | ||
{ | ||
RuleFor(x => x.UserId).NotEmpty().Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$"); | ||
RuleFor(x => x.Email).NotEmpty().EmailAddress(); | ||
} | ||
} |
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; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PinguApps.Appwrite.Shared.Responses; | ||
|
||
/// <summary> | ||
/// An Appwrite Token object | ||
/// </summary> | ||
/// <param name="Id">Token ID</param> | ||
/// <param name="CreatedAt">Token creation date in ISO 8601 format</param> | ||
/// <param name="UserId">User ID</param> | ||
/// <param name="Secret">Token secret key. This will return an empty string unless the response is returned using an API key or as part of a webhook payload</param> | ||
/// <param name="ExpiresAt">Token expiration date in ISO 8601 format</param> | ||
/// <param name="Phrase">Security phrase of a token. Empty if security phrase was not requested when creating a token. It includes randomly generated phrase which is also sent in the external resource such as email</param> | ||
public record Token( | ||
[property: JsonPropertyName("$id")] string Id, | ||
[property: JsonPropertyName("$createdAt")] DateTime CreatedAt, | ||
[property: JsonPropertyName("userId")] string UserId, | ||
[property: JsonPropertyName("secret")] string Secret, | ||
[property: JsonPropertyName("expire")] DateTime ExpiresAt, | ||
[property: JsonPropertyName("phrase")] string Phrase | ||
); |
77 changes: 77 additions & 0 deletions
77
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateEmailToken.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 CreateEmailToken_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailToken(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailToken_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailToken(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailToken_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailToken(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.CreateEmailToken.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 CreateEmailToken_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.TokenResponse); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreateEmailToken(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailToken_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreateEmailToken(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailToken_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailTokenRequest() | ||
{ | ||
UserId = "123456", | ||
Email = "[email protected]" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/tokens/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.CreateEmailToken(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
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
Oops, something went wrong.