Add ProfileApiTests for Profiles API functionality
Introduced a new `ProfileApiTests` class to test the `Profiles` API in the `ReC.Client`. Added test methods to verify API behavior, including dependency injection resolution, handling of unknown IDs, dynamic payload retrieval, update operations, and query string validation for delete operations. Key changes: - Added `ReCClient_profiles_api_is_resolvable_through_dependency_injection` test. - Added `GetAsync_with_unknown_id_throws_not_found` test. - Added `GetDynamicAsync_with_unknown_id_throws_not_found` test. - Added `GetDynamicAsync_without_filters_returns_dynamic_payload_or_throws_not_found` test. - Added `UpdateAsync_with_unknown_id_throws_or_completes` test. - Added `DeleteAsync_sends_payload_as_query_string` test. Included necessary `using` directives, scoped client creation, and exception handling to ensure robust test coverage for the `Profiles` API.
This commit is contained in:
118
tests/ReC.Tests/Client/ProfileApiTests.cs
Normal file
118
tests/ReC.Tests/Client/ProfileApiTests.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using ReC.Application.Common.Dto;
|
||||
using ReC.Application.Common.Procedures.UpdateProcedure.Dto;
|
||||
using ReC.Application.Profile.Commands;
|
||||
using ReC.Client;
|
||||
|
||||
namespace ReC.Tests.Client;
|
||||
|
||||
[TestFixture]
|
||||
public class ProfileApiTests : RecClientTestBase
|
||||
{
|
||||
[Test]
|
||||
public void ReCClient_profiles_api_is_resolvable_through_dependency_injection()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
Assert.That(client, Is.Not.Null);
|
||||
Assert.That(client.Profiles, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAsync_with_unknown_id_throws_not_found()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
var ex = Assert.ThrowsAsync<ReCApiException>(async () =>
|
||||
await client.Profiles.GetAsync<ProfileViewDto[]>(id: long.MaxValue));
|
||||
|
||||
Assert.That(ex, Is.Not.Null);
|
||||
Assert.That(ex!.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
Assert.That(ex.Method, Is.EqualTo("GET"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDynamicAsync_with_unknown_id_throws_not_found()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
var ex = Assert.ThrowsAsync<ReCApiException>(async () =>
|
||||
await client.Profiles.GetDynamicAsync(id: long.MaxValue));
|
||||
|
||||
Assert.That(ex, Is.Not.Null);
|
||||
Assert.That(ex!.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
Assert.That(ex.Method, Is.EqualTo("GET"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetDynamicAsync_without_filters_returns_dynamic_payload_or_throws_not_found()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
try
|
||||
{
|
||||
dynamic profiles = await client.Profiles.GetDynamicAsync();
|
||||
Assert.That(profiles, Is.Not.Null);
|
||||
Assert.That(profiles, Is.TypeOf<JsonElement>());
|
||||
}
|
||||
catch (ReCApiException ex)
|
||||
{
|
||||
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
||||
Assert.That(ex.Method, Is.EqualTo("GET"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateAsync_with_unknown_id_throws_or_completes()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
var payload = new UpdateProfileDto
|
||||
{
|
||||
Active = false,
|
||||
Name = "test-profile"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
client.Profiles.UpdateAsync(long.MaxValue, payload).GetAwaiter().GetResult();
|
||||
Assert.Pass("Update completed.");
|
||||
}
|
||||
catch (ReCApiException ex)
|
||||
{
|
||||
Assert.That(ex.Method, Is.EqualTo("PUT"));
|
||||
Assert.That(ex.RequestUri!.AbsolutePath, Does.EndWith($"api/Profile/{long.MaxValue}"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteAsync_sends_payload_as_query_string()
|
||||
{
|
||||
var (client, scope) = CreateScopedClient();
|
||||
using var _ = scope;
|
||||
|
||||
var payload = new DeleteProfileCommand
|
||||
{
|
||||
Start = long.MaxValue - 1,
|
||||
End = long.MaxValue,
|
||||
Force = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
client.Profiles.DeleteAsync(payload).GetAwaiter().GetResult();
|
||||
}
|
||||
catch (ReCApiException ex)
|
||||
{
|
||||
Assert.That(ex.Method, Is.EqualTo("DELETE"));
|
||||
Assert.That(ex.RequestUri!.Query, Does.Contain("Start=").And.Contains("End=").And.Contains("Force="));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user