-
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.
* Add shared request for delete session * bump response format to 1.6 * implemented delete session on client * added shared tests * bump expected appwrite response header * Add client tests * Update README.md * [CodeFactor] Apply fixes --------- Co-authored-by: codefactor-io <[email protected]>
- Loading branch information
1 parent
d5f11ef
commit d966892
Showing
12 changed files
with
282 additions
and
18 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
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
16 changes: 16 additions & 0 deletions
16
src/PinguApps.Appwrite.Shared/Requests/DeleteSessionRequest.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 deleting a session (logging out of current session) | ||
/// </summary> | ||
public class DeleteSessionRequest : BaseRequest<DeleteSessionRequest, DeleteSessionRequestValidator> | ||
{ | ||
/// <summary> | ||
/// Session ID. Use the string 'current' to delete the current device session. | ||
/// </summary> | ||
[JsonPropertyName("sessionId")] | ||
public string SessionId { get; set; } = "current"; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/PinguApps.Appwrite.Shared/Requests/Validators/DeleteSessionRequestValidator.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,10 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class DeleteSessionRequestValidator : AbstractValidator<DeleteSessionRequest> | ||
{ | ||
public DeleteSessionRequestValidator() | ||
{ | ||
RuleFor(x => x.SessionId).NotEmpty(); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.DeleteSession.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,109 @@ | ||
using System.Net; | ||
using PinguApps.Appwrite.Client.Clients; | ||
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 DeleteSession_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest(); | ||
|
||
_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.MfaTypeResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.DeleteSession(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteSession_ShouldHitDifferentEndpoint_WhenNewTypeIsUsed() | ||
{ | ||
// Arrange | ||
var sessionId = "mySessionId"; | ||
var request = new DeleteSessionRequest() | ||
{ | ||
SessionId = sessionId | ||
}; | ||
var requestUri = $"{Constants.Endpoint}/account/sessions/{sessionId}"; | ||
var mockRequest = _mockHttp.Expect(HttpMethod.Delete, requestUri) | ||
.ExpectedHeaders(true) | ||
.Respond(Constants.AppJson, Constants.MfaTypeResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.DeleteSession(request); | ||
|
||
// Assert | ||
_mockHttp.VerifyNoOutstandingExpectation(); | ||
var matches = _mockHttp.GetMatchCount(mockRequest); | ||
Assert.Equal(1, matches); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteSession_ShouldReturnError_WhenSessionIsNull() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest(); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.DeleteSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteSession_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest(); | ||
|
||
_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.DeleteSession(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteSession_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest(); | ||
|
||
_mockHttp.Expect(HttpMethod.Delete, $"{Constants.Endpoint}/account/sessions/current") | ||
.ExpectedHeaders(true) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.DeleteSession(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
93 changes: 93 additions & 0 deletions
93
tests/PinguApps.Appwrite.Shared.Tests/Requests/DeleteSessionRequestTests.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,93 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class DeleteSessionRequestTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new DeleteSessionRequest(); | ||
|
||
// Assert | ||
Assert.Equal("current", request.SessionId); | ||
} | ||
|
||
[Theory] | ||
[InlineData("A string")] | ||
public void Properties_CanBeSet(string sessionId) | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest(); | ||
|
||
// Act | ||
request.SessionId = sessionId; | ||
|
||
// Assert | ||
Assert.Equal(sessionId, request.SessionId); | ||
} | ||
|
||
[Fact] | ||
public void IsValid_WithValidInputs_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest | ||
{ | ||
SessionId = "blahblah" | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(null)] | ||
public void IsValid_WithInvalidInputs_ReturnsFalse(string? sessionId) | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest | ||
{ | ||
SessionId = sessionId! | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest | ||
{ | ||
SessionId = "" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new DeleteSessionRequest | ||
{ | ||
SessionId = "" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |