-
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 #134 from PinguApps/101-create-email-password-session
Implemented create email password session
- Loading branch information
Showing
9 changed files
with
258 additions
and
8 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 |
---|---|---|
|
@@ -22,7 +22,11 @@ public async Task Run(string[] args) | |
|
||
Console.WriteLine(_client.Session); | ||
|
||
var response = await _client.Account.CreateAnonymousSession(); | ||
var response = await _client.Account.CreateEmailPasswordSession(new Shared.Requests.CreateEmailPasswordSessionRequest | ||
{ | ||
Email = "[email protected]", | ||
Password = "password" | ||
}); | ||
|
||
Console.WriteLine(response.Result.Match( | ||
account => account.ToString(), | ||
|
22 changes: 22 additions & 0 deletions
22
src/PinguApps.Appwrite.Shared/Requests/CreateEmailPasswordSessionRequest.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 an email password session | ||
/// </summary> | ||
public class CreateEmailPasswordSessionRequest : BaseRequest<CreateEmailPasswordSessionRequest, CreateEmailPasswordSessionRequestValidator> | ||
{ | ||
/// <summary> | ||
/// User email | ||
/// </summary> | ||
[JsonPropertyName("email")] | ||
public string Email { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// User password. Must be at least 8 chars | ||
/// </summary> | ||
[JsonPropertyName("password")] | ||
public string Password { get; set; } = string.Empty; | ||
} |
11 changes: 11 additions & 0 deletions
11
...nguApps.Appwrite.Shared/Requests/Validators/CreateEmailPasswordSessionRequestValidator.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 CreateEmailPasswordSessionRequestValidator : AbstractValidator<CreateEmailPasswordSessionRequest> | ||
{ | ||
public CreateEmailPasswordSessionRequestValidator() | ||
{ | ||
RuleFor(x => x.Email).NotEmpty().EmailAddress(); | ||
RuleFor(x => x.Password).NotEmpty().MinimumLength(8).MaximumLength(256); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.CreateEmailPasswordSession.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 CreateEmailPasswordSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest() | ||
{ | ||
Email = "[email protected]", | ||
Password = "Password" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.SessionResponse); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailPasswordSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailPasswordSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest() | ||
{ | ||
Email = "[email protected]", | ||
Password = "Password" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailPasswordSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateEmailPasswordSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest() | ||
{ | ||
Email = "[email protected]", | ||
Password = "Password" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/account/sessions/email") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.CreateEmailPasswordSession(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
tests/PinguApps.Appwrite.Shared.Tests/Requests/CreateEmailPasswordSessionRequestTests.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,107 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class CreateEmailPasswordSessionRequestTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new CreateEmailPasswordSessionRequest(); | ||
|
||
// Assert | ||
Assert.Equal(string.Empty, request.Email); | ||
Assert.Equal(string.Empty, request.Password); | ||
} | ||
|
||
[Theory] | ||
[InlineData("[email protected]", "password123")] | ||
[InlineData("[email protected]", "diffPassword")] | ||
public void Properties_CanBeSet(string email, string password) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest(); | ||
|
||
// Act | ||
request.Email = email; | ||
request.Password = password; | ||
|
||
// Assert | ||
Assert.Equal(email, request.Email); | ||
Assert.Equal(password, request.Password); | ||
} | ||
|
||
[Theory] | ||
[InlineData("[email protected]", "Password")] | ||
public void IsValid_WithValidData_ReturnsTrue(string email, string password) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest | ||
{ | ||
Email = email, | ||
Password = password | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "Password")] | ||
[InlineData("", "Password")] | ||
[InlineData("not an email", "Password")] | ||
[InlineData("[email protected]", null)] | ||
[InlineData("[email protected]", "")] | ||
[InlineData("[email protected]", "short")] | ||
[InlineData("[email protected]", "A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. \", \"A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. A much longer string. ")] | ||
public void IsValid_WithInvalidData_ReturnsFalse(string? email, string? password) | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest | ||
{ | ||
Email = email!, | ||
Password = password! | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest | ||
{ | ||
Email = "not an email", | ||
Password = "short" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateEmailPasswordSessionRequest | ||
{ | ||
Email = "not an email", | ||
Password = "short" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |