-
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 #67 from PinguApps/41-feat-account-update-preferen…
…ces-implementation Added update preferences implementation
- Loading branch information
Showing
9 changed files
with
248 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
17 changes: 17 additions & 0 deletions
17
src/PinguApps.Appwrite.Shared/Requests/UpdatePreferencesRequest.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,17 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Requests.Validators; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
|
||
/// <summary> | ||
/// The request for updating a users preferences | ||
/// </summary> | ||
public class UpdatePreferencesRequest : BaseRequest<UpdatePreferencesRequest, UpdatePreferencesRequestValidator> | ||
{ | ||
/// <summary> | ||
/// New value for preferences | ||
/// </summary> | ||
[JsonPropertyName("prefs")] | ||
public IDictionary<string, string> Preferences { get; set; } = new Dictionary<string, string>(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdatePreferencesRequestValidator.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 UpdatePreferencesRequestValidator : AbstractValidator<UpdatePreferencesRequest> | ||
{ | ||
public UpdatePreferencesRequestValidator() | ||
{ | ||
RuleFor(x => x.Preferences).NotNull(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...s/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.UpdatePreferences.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 UpdatePreferences_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest() | ||
{ | ||
Preferences = new Dictionary<string, string> { { "key1", "val1" }, { "key2", "val2" } } | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/prefs") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Respond(Constants.AppJson, Constants.UserResponse); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePreferences(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePreferences_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest() | ||
{ | ||
Preferences = new Dictionary<string, string> { { "key1", "val1" }, { "key2", "val2" } } | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/prefs") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePreferences(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePreferences_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest() | ||
{ | ||
Preferences = new Dictionary<string, string> { { "key1", "val1" }, { "key2", "val2" } } | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/account/prefs") | ||
.ExpectedHeaders(true) | ||
.WithJsonContent(request) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(Constants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePreferences(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
tests/PinguApps.Appwrite.Shared.Tests/Requests/UpdatePreferencesRequestTests.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,98 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class UpdatePreferencesRequestTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new UpdatePreferencesRequest(); | ||
|
||
// Assert | ||
Assert.NotNull(request.Preferences); | ||
Assert.Empty(request.Preferences); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(PropertiesToBeSet))] | ||
public void Properties_CanBeSet(IDictionary<string, string> dict) | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest(); | ||
|
||
// Act | ||
request.Preferences = dict; | ||
|
||
// Assert | ||
Assert.Equal(dict, request.Preferences); | ||
} | ||
|
||
public static IEnumerable<object[]> PropertiesToBeSet() | ||
{ | ||
yield return new object[] { new Dictionary<string, string> { { "key1", "val1" }, { "key2", "val2" } } }; | ||
yield return new object[] { new Dictionary<string, string>() }; | ||
} | ||
|
||
[Fact] | ||
public void IsValid_WithValidPhoneAndPassword_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest | ||
{ | ||
Preferences = new Dictionary<string, string> { { "key1", "val1" }, { "key2", "val2" } } | ||
}; | ||
|
||
// Act | ||
bool isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Fact] | ||
public void IsValid_WithInvalidInputs_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest | ||
{ | ||
Preferences = null! | ||
}; | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest | ||
{ | ||
Preferences = null! | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new UpdatePreferencesRequest | ||
{ | ||
Preferences = null! | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |