-
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 #293 from PinguApps/289-update-push-target
Implemented update push target
- Loading branch information
Showing
9 changed files
with
305 additions
and
10 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
20 changes: 20 additions & 0 deletions
20
src/PinguApps.Appwrite.Shared/Requests/Account/UpdatePushTargetRequest.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,20 @@ | ||
using System.Text.Json.Serialization; | ||
using PinguApps.Appwrite.Shared.Attributes; | ||
using PinguApps.Appwrite.Shared.Requests.Account.Validators; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Account; | ||
public class UpdatePushTargetRequest : BaseRequest<UpdatePushTargetRequest, UpdatePushTargetRequestValidator> | ||
{ | ||
/// <summary> | ||
/// Target ID. | ||
/// </summary> | ||
[JsonPropertyName("targetId")] | ||
[SdkExclude] | ||
public string TargetId { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The target identifier (token, email, phone etc.) | ||
/// </summary> | ||
[JsonPropertyName("identifier")] | ||
public string Identifier { get; set; } = string.Empty; | ||
} |
14 changes: 14 additions & 0 deletions
14
...PinguApps.Appwrite.Shared/Requests/Account/Validators/UpdatePushTargetRequestValidator.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,14 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Account.Validators; | ||
public class UpdatePushTargetRequestValidator : AbstractValidator<UpdatePushTargetRequest> | ||
{ | ||
public UpdatePushTargetRequestValidator() | ||
{ | ||
RuleFor(x => x.TargetId) | ||
.NotEmpty().WithMessage("TargetId is required."); | ||
|
||
RuleFor(x => x.Identifier) | ||
.NotEmpty().WithMessage("Identifier is required."); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
tests/PinguApps.Appwrite.Client.Tests/Clients/Account/AccountClientTests.UpdatePushTarget.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,104 @@ | ||
using System.Net; | ||
using PinguApps.Appwrite.Client.Clients; | ||
using PinguApps.Appwrite.Shared.Requests.Account; | ||
using PinguApps.Appwrite.Shared.Tests; | ||
using PinguApps.Appwrite.Shared.Utils; | ||
using RichardSzalay.MockHttp; | ||
|
||
namespace PinguApps.Appwrite.Client.Tests.Clients.Account; | ||
public partial class AccountClientTests | ||
{ | ||
[Fact] | ||
public async Task UpdatePushTarget_ShouldReturnSuccess_WhenApiCallSucceeds() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "token" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request, _jsonSerializerOptions) | ||
.Respond(TestConstants.AppJson, TestConstants.TargetResponse); | ||
|
||
_appwriteClient.SetSession(TestConstants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePushTarget(request); | ||
|
||
// Assert | ||
Assert.True(result.Success); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePushTarget_ShouldReturnError_WhenSessionIsNull() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "token" | ||
}; | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePushTarget(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePushTarget_ShouldHandleException_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "token" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request, _jsonSerializerOptions) | ||
.Respond(HttpStatusCode.BadRequest, TestConstants.AppJson, TestConstants.AppwriteError); | ||
|
||
_appwriteClient.SetSession(TestConstants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePushTarget(request); | ||
|
||
// Assert | ||
Assert.True(result.IsError); | ||
Assert.True(result.IsAppwriteError); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdatePushTarget_ShouldReturnErrorResponse_WhenExceptionOccurs() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "token" | ||
}; | ||
|
||
_mockHttp.Expect(HttpMethod.Put, $"{TestConstants.Endpoint}/account/targets/{request.TargetId}/push") | ||
.ExpectedHeaders() | ||
.WithJsonContent(request, _jsonSerializerOptions) | ||
.Throw(new HttpRequestException("An error occurred")); | ||
|
||
_appwriteClient.SetSession(TestConstants.Session); | ||
|
||
// Act | ||
var result = await _appwriteClient.Account.UpdatePushTarget(request); | ||
|
||
// Assert | ||
Assert.False(result.Success); | ||
Assert.True(result.IsInternalError); | ||
Assert.Equal("An error occurred", result.Result.AsT2.Message); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
tests/PinguApps.Appwrite.Shared.Tests/Requests/Account/UpdatePushTargetRequestTests.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,132 @@ | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests.Account; | ||
using PinguApps.Appwrite.Shared.Utils; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests.Account; | ||
public class UpdatePushTargetRequestTests | ||
{ | ||
[Fact] | ||
public void Constructor_InitializesWithExpectedValues() | ||
{ | ||
// Arrange & Act | ||
var request = new UpdatePushTargetRequest(); | ||
|
||
// Assert | ||
Assert.Equal(string.Empty, request.TargetId); | ||
Assert.Equal(string.Empty, request.Identifier); | ||
} | ||
|
||
[Fact] | ||
public void Properties_CanBeSet() | ||
{ | ||
var targetId = IdUtils.GenerateUniqueId(); | ||
var identifier = "email"; | ||
|
||
// Arrange | ||
var request = new UpdatePushTargetRequest(); | ||
|
||
// Act | ||
request.TargetId = targetId; | ||
request.Identifier = identifier; | ||
|
||
// Assert | ||
Assert.Equal(targetId, request.TargetId); | ||
Assert.Equal(identifier, request.Identifier); | ||
} | ||
|
||
public static TheoryData<UpdatePushTargetRequest> ValidRequestsData = new() | ||
{ | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "token" | ||
}, | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "email" | ||
}, | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "phone" | ||
} | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(ValidRequestsData))] | ||
public void IsValid_WithValidData_ReturnsTrue(UpdatePushTargetRequest request) | ||
{ | ||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
public static TheoryData<UpdatePushTargetRequest> InvalidRequestsData = new() | ||
{ | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = "", | ||
Identifier = "token" | ||
}, | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = null!, | ||
Identifier = "token" | ||
}, | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = "" | ||
}, | ||
new UpdatePushTargetRequest | ||
{ | ||
TargetId = IdUtils.GenerateUniqueId(), | ||
Identifier = null! | ||
} | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidRequestsData))] | ||
public void IsValid_WithInvalidData_ReturnsFalse(UpdatePushTargetRequest request) | ||
{ | ||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = "", | ||
Identifier = "" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new UpdatePushTargetRequest | ||
{ | ||
TargetId = "", | ||
Identifier = "" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |