965 lines
28 KiB
C#
965 lines
28 KiB
C#
using System.Net;
|
|
using Heimdall.Web.Services;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Web.Tests.Services;
|
|
|
|
/// <summary>
|
|
/// Additional HeimdallApiClient tests to increase line coverage on
|
|
/// Sites, Users, Cameras, Settings, Events, Notifications, AgentSettings, 2FA.
|
|
/// </summary>
|
|
public sealed class HeimdallApiClientCoverageTests : IDisposable
|
|
{
|
|
private readonly MockHttpMessageHandler _handler;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly IHttpClientFactory _httpFactory;
|
|
private readonly AuthState _authState;
|
|
private readonly HeimdallApiClient _sut;
|
|
|
|
public HeimdallApiClientCoverageTests()
|
|
{
|
|
_handler = new MockHttpMessageHandler();
|
|
_httpClient = new HttpClient(_handler) { BaseAddress = new Uri("http://localhost:8080/") };
|
|
_httpFactory = Substitute.For<IHttpClientFactory>();
|
|
_httpFactory.CreateClient("HeimdallApi").Returns(_httpClient);
|
|
_authState = new AuthState();
|
|
_sut = new HeimdallApiClient(_httpFactory, _authState);
|
|
}
|
|
|
|
public void Dispose() => _httpClient.Dispose();
|
|
|
|
private void SetAuthToken() =>
|
|
_authState.Login(new AuthResponse(
|
|
"token", "refresh", DateTime.UtcNow.AddHours(1), false,
|
|
new UserInfo(Guid.NewGuid(), "a@b.com", "Admin", ["Admin"])));
|
|
|
|
// ── Sites ───────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetSitesAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<SiteItem>(
|
|
[new SiteItem(Guid.NewGuid(), "Site A", null, "UTC", null, true, false, DateTime.UtcNow)], 1, 1, 20);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetSitesAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
Assert.Equal("Site A", result.Items[0].Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSiteAsync_ReturnsSite()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
var expected = new SiteItem(id, "My Site", null, "UTC", null, true, false, DateTime.UtcNow);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetSiteAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("My Site", result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateSiteAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateSiteAsync(Guid.NewGuid(), "New Name", "Addr", "UTC");
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateSiteAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.UpdateSiteAsync(Guid.NewGuid(), "", null, null);
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteSiteAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.DeleteSiteAsync(Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GenerateSiteTokenAsync_WhenSuccess_ReturnsToken()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new TokenResult("tok-123", DateTime.UtcNow.AddHours(1));
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GenerateSiteTokenAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("tok-123", result.Token);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GenerateSiteTokenAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.NotFound);
|
|
|
|
var result = await _sut.GenerateSiteTokenAsync(Guid.NewGuid());
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
// ── Users ───────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetUsersAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<UserItem>(
|
|
[new UserItem { Id = Guid.NewGuid(), Email = "u@t.com" }], 1, 1, 20);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetUsersAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetUserAsync_ReturnsUser()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new UserItem { Id = id, Email = "u@t.com" });
|
|
|
|
var result = await _sut.GetUserAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(id, result.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateUserAsync_WhenSuccess_ReturnsUser()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new UserItem { Id = Guid.NewGuid(), Email = "new@t.com" };
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.CreateUserAsync("new@t.com", "pwd", "New", ["Admin"]);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("new@t.com", result.Email);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateUserAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.CreateUserAsync("bad", "", "", []);
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateUserAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateUserAsync(Guid.NewGuid(), "New Name", null, null);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AssignRolesAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.AssignRolesAsync(Guid.NewGuid(), ["Admin"]);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AssignSitesAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.AssignSitesAsync(Guid.NewGuid(), [Guid.NewGuid()]);
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResetPasswordAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PasswordResetResult("new-pwd-123");
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.ResetPasswordAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("new-pwd-123", result.TemporaryPassword);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResetPasswordAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.NotFound);
|
|
|
|
var result = await _sut.ResetPasswordAsync(Guid.NewGuid());
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LockUserAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.LockUserAsync(Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UnlockUserAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UnlockUserAsync(Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
// ── Cameras ─────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetCamerasBySiteAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<CameraItem>(
|
|
[new CameraItem { Id = Guid.NewGuid(), Name = "Cam 1" }], 1, 1, 20);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetCamerasBySiteAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCameraAsync_ReturnsCamera()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new CameraItem { Id = id, Name = "Cam" });
|
|
|
|
var result = await _sut.GetCameraAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("Cam", result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateCameraAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateCameraAsync(Guid.NewGuid(), "Cam", "rtsp://cam", "Online");
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCameraAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.DeleteCameraAsync(Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteCameraAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.NotFound);
|
|
|
|
var result = await _sut.DeleteCameraAsync(Guid.NewGuid());
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
// ── Settings ────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetSettingsAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<AppSettingItem>());
|
|
|
|
var result = await _sut.GetSettingsAsync();
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSettingAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.CreateSettingAsync(new CreateAppSettingRequest("key", "val"));
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateSettingAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateSettingAsync("key", new UpdateAppSettingRequest("new-val"));
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteSettingAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.DeleteSettingAsync("key");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestStorageAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new StorageTestResult("Minio", true, true));
|
|
|
|
var result = await _sut.TestStorageAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.True(result.Healthy);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestStorageAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.InternalServerError);
|
|
|
|
var result = await _sut.TestStorageAsync();
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestEmailAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new TestActionResult(true, "Sent"));
|
|
|
|
var result = await _sut.TestEmailAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.True(result.Success);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestEmailAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.InternalServerError);
|
|
|
|
var result = await _sut.TestEmailAsync();
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestNtfyAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new TestActionResult(true, "Sent"));
|
|
|
|
var result = await _sut.TestNtfyAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.True(result.Success);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestAiAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new TestActionResult(true, "OK"));
|
|
|
|
var result = await _sut.TestAiAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.True(result.Success);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReloadAllConfigAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.ReloadAllConfigAsync();
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
// ── Agent Settings ──────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetAgentSettingsAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<AgentSettingItem>());
|
|
|
|
var result = await _sut.GetAgentSettingsAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetAgentSettingAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.SetAgentSettingAsync(
|
|
Guid.NewGuid(), new SetAgentSettingRequest("key", "val"));
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RemoveAgentSettingAsync_ReturnsResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.RemoveAgentSettingAsync(Guid.NewGuid(), "key");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetEffectiveAgentConfigAsync_ReturnsDict()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new Dictionary<string, string> { ["k"] = "v" });
|
|
|
|
var result = await _sut.GetEffectiveAgentConfigAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("v", result["k"]);
|
|
}
|
|
|
|
// ── Agents ──────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetAgentAsync_ReturnsAgent()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new AgentItem { Id = id, Name = "Agent 1" });
|
|
|
|
var result = await _sut.GetAgentAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("Agent 1", result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAgentsBySiteAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<AgentItem>(
|
|
[new AgentItem { Id = Guid.NewGuid(), Name = "A1" }], 1, 1, 20);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetAgentsBySiteAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllAgentsAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<AgentItem>(
|
|
[new AgentItem { Id = Guid.NewGuid(), Name = "All" }], 1, 1, 20);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetAllAgentsAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAgentStatsAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<AgentStatItem>());
|
|
|
|
var result = await _sut.GetAgentStatsAsync();
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
// ── Events ──────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetEventsAsync_ReturnsPageResult()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new EventPageResult([], 0, 1, 50);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetEventsAsync(null, null);
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetRecentEventsAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<EventItem>());
|
|
|
|
var result = await _sut.GetRecentEventsAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetEventAsync_ReturnsEvent()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new EventItem(
|
|
id, Guid.NewGuid(), "Motion", 0.95, "person", null, null,
|
|
DateTime.UtcNow, DateTime.UtcNow));
|
|
|
|
var result = await _sut.GetEventAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(id, result.Id);
|
|
}
|
|
|
|
// ── Notification Config ─────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetNotificationConfigAsync_ReturnsConfig()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new NotificationConfigItem { EmailEnabled = true });
|
|
|
|
var result = await _sut.GetNotificationConfigAsync();
|
|
|
|
Assert.NotNull(result);
|
|
Assert.True(result.EmailEnabled);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateNotificationConfigAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateNotificationConfigAsync(
|
|
new UpdateNotificationConfigRequest(true, "a@b.com", false, null, null, null, null, []));
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateNotificationConfigAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.UpdateNotificationConfigAsync(
|
|
new UpdateNotificationConfigRequest(null, null, null, null, null, null, null, []));
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
// ── Site Credentials ────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetSiteCredentialsAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<SiteCredentialItem>());
|
|
|
|
var result = await _sut.GetSiteCredentialsAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddSiteCredentialAsync_WhenSuccess_ReturnsItem()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new SiteCredentialItem { Id = Guid.NewGuid(), Label = "cred" };
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.AddSiteCredentialAsync(Guid.NewGuid(), "cred", "user", "pass");
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("cred", result.Label);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddSiteCredentialAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.AddSiteCredentialAsync(Guid.NewGuid(), "", "", "");
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteSiteCredentialAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.DeleteSiteCredentialAsync(Guid.NewGuid(), Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
// ── Discovery ───────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetPendingDiscoveriesAsync_ReturnsList()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new List<PendingDiscoveryItem>());
|
|
|
|
var result = await _sut.GetPendingDiscoveriesAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ScanNowAsync_WhenSuccess_ReturnsResult()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new ScanNowResult(3));
|
|
|
|
var result = await _sut.ScanNowAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(3, result.AgentsNotified);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ScanNowAsync_WhenFails_ReturnsNull()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.NotFound);
|
|
|
|
var result = await _sut.ScanNowAsync(Guid.NewGuid());
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectDiscoveryAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.RejectDiscoveryAsync(Guid.NewGuid());
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
// ── 2FA ─────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task RegisterAsync_WhenSuccess_ReturnsAuthResponse()
|
|
{
|
|
var expected = new AuthResponse("access", "refresh", DateTime.UtcNow.AddHours(1), false,
|
|
new UserInfo(Guid.NewGuid(), "a@b.com", "A", null));
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.RegisterAsync("a@b.com", "pwd", "Display");
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("access", result.AccessToken);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RegisterAsync_WhenFails_ReturnsNull()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.RegisterAsync("bad", "", "");
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Verify2faAsync_WhenSuccess_ReturnsAuthResponse()
|
|
{
|
|
var expected = new AuthResponse("access", "refresh", DateTime.UtcNow.AddHours(1), false,
|
|
new UserInfo(Guid.NewGuid(), "a@b.com", "A", null));
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.Verify2faAsync("a@b.com", "123456");
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Verify2faAsync_WhenFails_ReturnsNull()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.Unauthorized);
|
|
|
|
var result = await _sut.Verify2faAsync("a@b.com", "wrong");
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Enable2faAsync_WhenSuccess_ReturnsResponse()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.OK, new { secret = "abc", qrCode = "data" });
|
|
|
|
var result = await _sut.Enable2faAsync();
|
|
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Enable2faAsync_WhenFails_ReturnsNull()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.Enable2faAsync();
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Disable2faAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.Disable2faAsync();
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Disable2faAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.Disable2faAsync();
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
// ── Profile ─────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task UpdateMyProfileAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateMyProfileAsync("New Name", "dark", "fr");
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateMyProfileAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.UpdateMyProfileAsync("", null, null);
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChangePasswordAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.ChangePasswordAsync("old", "new");
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChangePasswordAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.ChangePasswordAsync("wrong", "new");
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
// ── Agent Logs ──────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetAgentLogsAsync_ReturnsPagedData()
|
|
{
|
|
SetAuthToken();
|
|
var expected = new PagedData<AgentLogItem>(
|
|
[new AgentLogItem { Id = Guid.NewGuid(), Message = "log" }], 1, 1, 50);
|
|
_handler.QueueResponse(HttpStatusCode.OK, expected);
|
|
|
|
var result = await _sut.GetAgentLogsAsync(Guid.NewGuid());
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Single(result.Items);
|
|
}
|
|
|
|
// ── SiteItem ────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task GetGroupAsync_ReturnsGroup()
|
|
{
|
|
SetAuthToken();
|
|
var id = Guid.NewGuid();
|
|
_handler.QueueResponse(HttpStatusCode.OK, new GroupItem { Id = id, Name = "G1" });
|
|
|
|
var result = await _sut.GetGroupAsync(id);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("G1", result.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateGroupAsync_WhenSuccess_ReturnsTrue()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.OK);
|
|
|
|
var result = await _sut.UpdateGroupAsync(Guid.NewGuid(), "New", "desc");
|
|
|
|
Assert.True(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateGroupAsync_WhenFails_ReturnsFalse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest);
|
|
|
|
var result = await _sut.UpdateGroupAsync(Guid.NewGuid(), "", null);
|
|
|
|
Assert.False(result);
|
|
}
|
|
|
|
// ── ReadErrorMessage edge cases ─────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task CreateSiteAsync_WhenValidationErrors_ReturnsJoinedMessages()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueResponse(HttpStatusCode.BadRequest, new
|
|
{
|
|
errors = new
|
|
{
|
|
Name = new[] { "Le nom est requis" },
|
|
Timezone = new[] { "Fuseau horaire invalide" }
|
|
}
|
|
});
|
|
|
|
var result = await _sut.CreateSiteAsync("", null, "bad");
|
|
|
|
Assert.False(result.Succeeded);
|
|
Assert.NotNull(result.ErrorMessage);
|
|
Assert.Contains("Le nom est requis", result.ErrorMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSiteAsync_WhenNullBody_ReturnsInvalidResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueRawResponse(HttpStatusCode.OK, "null");
|
|
|
|
var result = await _sut.CreateSiteAsync("Site", null, "UTC");
|
|
|
|
Assert.False(result.Succeeded);
|
|
Assert.Equal("Réponse du serveur invalide.", result.ErrorMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateCameraAsync_WhenNullBody_ReturnsInvalidResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueRawResponse(HttpStatusCode.OK, "null");
|
|
|
|
var result = await _sut.CreateCameraAsync(Guid.NewGuid(), "Cam", "rtsp://cam", null, null);
|
|
|
|
Assert.False(result.Succeeded);
|
|
Assert.Equal("Réponse du serveur invalide.", result.ErrorMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfirmDiscoveryAsync_WhenNullBody_ReturnsInvalidResponse()
|
|
{
|
|
SetAuthToken();
|
|
_handler.QueueRawResponse(HttpStatusCode.OK, "null");
|
|
|
|
var result = await _sut.ConfirmDiscoveryAsync(
|
|
Guid.NewGuid(), Guid.NewGuid(), "Cam", "rtsp://cam", null, null);
|
|
|
|
Assert.False(result.Succeeded);
|
|
Assert.Equal("Réponse du serveur invalide.", result.ErrorMessage);
|
|
}
|
|
}
|