-
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 #166 from PinguApps/102-update-magic-url-session
Implemented update magic url session
- Loading branch information
Showing
13 changed files
with
378 additions
and
14 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 |
---|---|---|
|
@@ -21,17 +21,30 @@ public async Task Run(string[] args) | |
{ | ||
//_client.SetSession(_session); | ||
|
||
var response = await _server.Account.CreateMagicUrlToken(new CreateMagicUrlTokenRequest | ||
//var response = await _server.Account.CreateMagicUrlToken(new CreateMagicUrlTokenRequest | ||
//{ | ||
// UserId = "", | ||
// Email = "[email protected]", | ||
// Url = "https://localhost:1234/magic", | ||
// Phrase = true | ||
//}); | ||
|
||
var response = await _server.Account.UpdateMagicUrlSession(new UpdateMagicUrlSessionRequest | ||
{ | ||
UserId = "664aac1a00113f82e620", | ||
Email = "[email protected]", | ||
Url = "https://localhost:1234/magic", | ||
Phrase = true | ||
UserId = "", | ||
Secret = "" | ||
}); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.Message)); | ||
|
||
var res2 = await _server.Account.CreateAnonymousSession(); | ||
|
||
Console.WriteLine(res2.Result.Match( | ||
account => account.ToString(), | ||
appwriteError => appwriteError.Message, | ||
internalERror => internalERror.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
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
18 changes: 18 additions & 0 deletions
18
src/PinguApps.Appwrite.Shared/Requests/UpdateMagicUrlSessionRequest.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,18 @@ | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Requests.Validators; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
public class UpdateMagicUrlSessionRequest : BaseRequest<UpdateMagicUrlSessionRequest, UpdateMagicUrlSessionRequestValidator> | ||
{ | ||
/// <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/UpdateMagicUrlSessionRequestValidator.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 UpdateMagicUrlSessionRequestValidator : AbstractValidator<UpdateMagicUrlSessionRequest> | ||
{ | ||
public UpdateMagicUrlSessionRequestValidator() | ||
{ | ||
RuleFor(x => x.UserId) | ||
.NotEmpty().WithMessage("UserId is required.") | ||
.Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$").WithMessage("UserId can only contain a-z, A-Z, 0-9, period, hyphen, and underscore, and can't start with a special char. Max length is 36 chars."); | ||
|
||
RuleFor(x => x.Secret) | ||
.NotEmpty().WithMessage("Secret is required."); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...nguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.UpdateMagicUrlSession.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 UpdateMagicUrlSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.SessionResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateMagicUrlSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateMagicUrlSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateMagicUrlSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateMagicUrlSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdateMagicUrlSession(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
...nguApps.Appwrite.Server.Tests/Servers/Account/AccountServerTests.UpdateMagicUrlSession.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 UpdateMagicUrlSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.SessionResponse); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdateMagicUrlSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateMagicUrlSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdateMagicUrlSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateMagicUrlSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdateMagicUrlSessionRequest() | ||
{ | ||
UserId = "user123", | ||
Secret = "validSecret" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{Constants.Endpoint}/account/sessions/magic-url") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteServer.Account.UpdateMagicUrlSession(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
Oops, something went wrong.