Replace the CLI's Akka.NET ClusterClient transport with a simple HTTP client targeting a new POST /management endpoint on the Central Host. The endpoint handles Basic Auth, LDAP authentication, role resolution, and ManagementActor dispatch in a single round-trip — eliminating the CLI's Akka, LDAP, and Security dependencies. Also fixes DCL ReSubscribeAll losing subscriptions on repeated reconnect by deriving the tag list from _subscriptionsByInstance instead of _subscriptionIds.
153 lines
5.1 KiB
C#
153 lines
5.1 KiB
C#
using ScadaLink.CLI;
|
|
using ScadaLink.CLI.Commands;
|
|
|
|
namespace ScadaLink.CLI.Tests;
|
|
|
|
public class CommandHelpersTests
|
|
{
|
|
[Fact]
|
|
public void HandleResponse_Success_JsonFormat_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var response = new ManagementResponse(200, "{\"id\":1,\"name\":\"test\"}", null, null);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
Assert.Contains("{\"id\":1,\"name\":\"test\"}", writer.ToString());
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Success_TableFormat_ArrayData_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var json = "[{\"Id\":1,\"Name\":\"Alpha\"},{\"Id\":2,\"Name\":\"Beta\"}]";
|
|
var response = new ManagementResponse(200, json, null, null);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
var output = writer.ToString();
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Name", output);
|
|
Assert.Contains("Alpha", output);
|
|
Assert.Contains("Beta", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Success_TableFormat_ObjectData_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var json = "{\"Id\":1,\"Name\":\"Alpha\",\"Status\":\"Active\"}";
|
|
var response = new ManagementResponse(200, json, null, null);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
var output = writer.ToString();
|
|
Assert.Contains("Property", output);
|
|
Assert.Contains("Value", output);
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Alpha", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Success_TableFormat_EmptyArray_ShowsNoResults()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var response = new ManagementResponse(200, "[]", null, null);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
Assert.Contains("(no results)", writer.ToString());
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Error_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementResponse(400, null, "Something failed", "FAIL_CODE");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("Something failed", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Unauthorized_ReturnsTwo()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementResponse(403, null, "Access denied", "UNAUTHORIZED");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(2, exitCode);
|
|
Assert.Contains("Access denied", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_AuthFailure_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementResponse(401, null, "Invalid credentials", "AUTH_FAILED");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("Invalid credentials", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ConnectionFailure_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementResponse(0, null, "Connection failed: No such host", "CONNECTION_FAILED");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("Connection failed", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_Timeout_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementResponse(504, null, "Request timed out.", "TIMEOUT");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("timed out", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
}
|