-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertificateUpdateControllerTests.cs
More file actions
152 lines (129 loc) · 5.72 KB
/
Copy pathCertificateUpdateControllerTests.cs
File metadata and controls
152 lines (129 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using SecureBootDashboard.Api.Controllers;
using SecureBootDashboard.Api.Services;
using SecureBootWatcher.Shared.Models;
using Xunit;
namespace SecureBootDashboard.Api.Tests.Controllers
{
public class CertificateUpdateControllerTests
{
private readonly Mock<ICertificateUpdateService> _mockUpdateService;
private readonly Mock<ILogger<CertificateUpdateController>> _mockLogger;
private readonly CertificateUpdateController _controller;
public CertificateUpdateControllerTests()
{
_mockUpdateService = new Mock<ICertificateUpdateService>();
_mockLogger = new Mock<ILogger<CertificateUpdateController>>();
_controller = new CertificateUpdateController(_mockUpdateService.Object, _mockLogger.Object);
}
[Fact]
public async Task TriggerUpdateAsync_ValidRequest_ReturnsOkResult()
{
// Arrange
var request = new CertificateUpdateController.CertificateUpdateRequest
{
FleetId = "fleet-01",
IssuedBy = "admin@example.com",
Notes = "Test update"
};
var expectedResult = new CertificateUpdateResult(
Guid.NewGuid(),
10,
"Update command sent successfully to 10 device(s)");
_mockUpdateService
.Setup(s => s.SendUpdateCommandAsync(It.IsAny<CertificateUpdateCommand>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedResult);
// Act
var result = await _controller.TriggerUpdateAsync(request, CancellationToken.None);
// Assert
var okResult = Assert.IsType<OkObjectResult>(result.Result);
var returnValue = Assert.IsType<CertificateUpdateResult>(okResult.Value);
Assert.Equal(expectedResult.TargetDeviceCount, returnValue.TargetDeviceCount);
}
[Fact]
public async Task TriggerUpdateAsync_ServiceThrowsException_Returns500()
{
// Arrange
var request = new CertificateUpdateController.CertificateUpdateRequest
{
FleetId = "fleet-01",
IssuedBy = "admin@example.com"
};
_mockUpdateService
.Setup(s => s.SendUpdateCommandAsync(It.IsAny<CertificateUpdateCommand>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new Exception("Test exception"));
// Act
var result = await _controller.TriggerUpdateAsync(request, CancellationToken.None);
// Assert
var statusCodeResult = Assert.IsType<ObjectResult>(result.Result);
Assert.Equal(500, statusCodeResult.StatusCode);
}
[Fact]
public async Task GetCommandStatusAsync_ValidCommandId_ReturnsOkResult()
{
// Arrange
var commandId = Guid.NewGuid();
var expectedStatus = new CertificateUpdateCommandStatus(
commandId,
"fleet-01",
10,
5,
DateTimeOffset.UtcNow,
null,
"InProgress");
_mockUpdateService
.Setup(s => s.GetCommandStatusAsync(commandId, It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedStatus);
// Act
var result = await _controller.GetCommandStatusAsync(commandId, CancellationToken.None);
// Assert
var okResult = Assert.IsType<OkObjectResult>(result.Result);
var returnValue = Assert.IsType<CertificateUpdateCommandStatus>(okResult.Value);
Assert.Equal(commandId, returnValue.CommandId);
}
[Fact]
public async Task GetCommandStatusAsync_CommandNotFound_ReturnsNotFound()
{
// Arrange
var commandId = Guid.NewGuid();
_mockUpdateService
.Setup(s => s.GetCommandStatusAsync(commandId, It.IsAny<CancellationToken>()))
.ReturnsAsync((CertificateUpdateCommandStatus?)null);
// Act
var result = await _controller.GetCommandStatusAsync(commandId, CancellationToken.None);
// Assert
Assert.IsType<NotFoundObjectResult>(result.Result);
}
[Fact]
public async Task TriggerUpdateAsync_WithTargetDevices_SendsCorrectCommand()
{
// Arrange
var targetDevices = new[] { "DEVICE-01", "DEVICE-02" };
var request = new CertificateUpdateController.CertificateUpdateRequest
{
FleetId = "fleet-01",
TargetDevices = targetDevices,
IssuedBy = "admin@example.com",
UpdateFlags = 0x5944
};
CertificateUpdateCommand? capturedCommand = null;
_mockUpdateService
.Setup(s => s.SendUpdateCommandAsync(It.IsAny<CertificateUpdateCommand>(), It.IsAny<CancellationToken>()))
.Callback<CertificateUpdateCommand, CancellationToken>((cmd, ct) => capturedCommand = cmd)
.ReturnsAsync(new CertificateUpdateResult(Guid.NewGuid(), 2, "Success"));
// Act
await _controller.TriggerUpdateAsync(request, CancellationToken.None);
// Assert
Assert.NotNull(capturedCommand);
Assert.Equal("fleet-01", capturedCommand.FleetId);
Assert.Equal(targetDevices, capturedCommand.TargetDevices);
Assert.Equal((uint)0x5944, capturedCommand.UpdateFlags);
Assert.Equal("admin@example.com", capturedCommand.IssuedBy);
}
}
}