Updated subnet object handling and improved documentation
This commit is contained in:
323
classlib.tests/Cmdlets/GetAddressCmdletTests.cs
Normal file
323
classlib.tests/Cmdlets/GetAddressCmdletTests.cs
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
namespace PS.IPAM.Tests.Cmdlets;
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
using FluentAssertions;
|
||||||
|
using PS.IPAM;
|
||||||
|
using PS.IPAM.Cmdlets;
|
||||||
|
using PS.IPAM.Helpers;
|
||||||
|
using PS.IPAM.Tests.Mocks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the GetAddressCmdlet.
|
||||||
|
/// Note: Full cmdlet testing with parameter sets requires a PowerShell runspace.
|
||||||
|
/// These tests focus on verifying the cmdlet structure and the underlying RequestHelper functionality.
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Sequential")]
|
||||||
|
public class GetAddressCmdletTests : IDisposable
|
||||||
|
{
|
||||||
|
private MockHttpMessageHandler? _mockHandler;
|
||||||
|
|
||||||
|
public GetAddressCmdletTests()
|
||||||
|
{
|
||||||
|
// Clean state before each test
|
||||||
|
SessionManager.CloseSession();
|
||||||
|
RequestHelper.TestHttpHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Clean up after each test
|
||||||
|
SessionManager.CloseSession();
|
||||||
|
RequestHelper.TestHttpHandler = null;
|
||||||
|
_mockHandler?.ForceDispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetupSession()
|
||||||
|
{
|
||||||
|
SessionManager.CreateSessionWithToken("https://ipam.example.com", "testapp", "test-token");
|
||||||
|
}
|
||||||
|
|
||||||
|
private MockHttpMessageHandler SetupMockHandler()
|
||||||
|
{
|
||||||
|
_mockHandler = new MockHttpMessageHandler();
|
||||||
|
RequestHelper.TestHttpHandler = _mockHandler;
|
||||||
|
return _mockHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_Exists()
|
||||||
|
{
|
||||||
|
// Verify the cmdlet class exists and can be instantiated
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_IdProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.Id = 42;
|
||||||
|
cmdlet.Id.Should().Be(42);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_IPProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.IP = IPAddress.Parse("192.168.1.100");
|
||||||
|
cmdlet.IP.Should().Be(IPAddress.Parse("192.168.1.100"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_HostNameProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.HostName = "server01.example.com";
|
||||||
|
cmdlet.HostName.Should().Be("server01.example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_HostBaseProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.HostBase = "server";
|
||||||
|
cmdlet.HostBase.Should().Be("server");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_TagIdProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.TagId = 2;
|
||||||
|
cmdlet.TagId.Should().Be(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_SubnetIdProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.SubnetId = 10;
|
||||||
|
cmdlet.SubnetId.Should().Be(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetAddressCmdlet_SubnetCIDRProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new GetAddressCmdlet();
|
||||||
|
cmdlet.SubnetCIDR = "192.168.1.0/24";
|
||||||
|
cmdlet.SubnetCIDR.Should().Be("192.168.1.0/24");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the underlying RequestHelper functionality that the cmdlet uses
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_GetAddressById_ReturnsAddress()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
var addressJson = @"{
|
||||||
|
""id"": 1,
|
||||||
|
""subnetId"": 10,
|
||||||
|
""ip"": ""192.168.1.100"",
|
||||||
|
""hostname"": ""server01"",
|
||||||
|
""description"": ""Test server""
|
||||||
|
}";
|
||||||
|
handler.WithSuccessResponse(addressJson);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "1" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().BeOfType<Address>();
|
||||||
|
var address = (Address)result!;
|
||||||
|
address.Id.Should().Be(1);
|
||||||
|
address.Ip.Should().Be("192.168.1.100");
|
||||||
|
address.Hostname.Should().Be("server01");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_GetAddressById_BuildsCorrectUri()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"{""id"": 42, ""ip"": ""10.0.0.1""}");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "42" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
handler.LastRequest!.RequestUri!.ToString().Should().Contain("/addresses/42/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_SearchByIP_BuildsCorrectUri()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"{""id"": 1, ""ip"": ""192.168.1.50""}");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "search", "192.168.1.50" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
handler.LastRequest!.RequestUri!.ToString().Should().Contain("/addresses/search/192.168.1.50/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_SearchByHostname_BuildsCorrectUri()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"{""id"": 1, ""ip"": ""10.0.0.5"", ""hostname"": ""myserver.example.com""}");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "search_hostname", "myserver.example.com" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
handler.LastRequest!.RequestUri!.ToString().Should().Contain("/addresses/search_hostname/myserver.example.com/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_GetSubnetAddresses_BuildsCorrectUri()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"[{""id"": 1, ""ip"": ""192.168.1.1""}]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.subnets,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "10", "addresses" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
handler.LastRequest!.RequestUri!.ToString().Should().Contain("/subnets/10/addresses/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_GetAddressesByTag_BuildsCorrectUri()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"[{""id"": 1, ""ip"": ""10.0.0.1"", ""tag"": 2}]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "tags", "2", "addresses" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
handler.LastRequest!.RequestUri!.ToString().Should().Contain("/addresses/tags/2/addresses/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_ReturnsMultipleAddresses()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithSuccessResponse(@"[
|
||||||
|
{""id"": 1, ""ip"": ""192.168.1.1""},
|
||||||
|
{""id"": 2, ""ip"": ""192.168.1.2""},
|
||||||
|
{""id"": 3, ""ip"": ""192.168.1.3""}
|
||||||
|
]");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.subnets,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "10", "addresses" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().BeAssignableTo<System.Collections.IEnumerable>();
|
||||||
|
var addresses = ((System.Collections.IEnumerable)result!).Cast<object>().ToList();
|
||||||
|
addresses.Should().HaveCount(3);
|
||||||
|
addresses.Should().AllBeOfType<Address>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_With404Response_ReturnsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
SetupSession();
|
||||||
|
var handler = SetupMockHandler();
|
||||||
|
handler.WithNotFoundResponse();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
new[] { "999" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.Should().BeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task RequestHelper_WithNoSession_ThrowsException()
|
||||||
|
{
|
||||||
|
// Arrange - no session set up
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var action = async () => await RequestHelper.InvokeRequest(
|
||||||
|
"GET",
|
||||||
|
controllers.addresses,
|
||||||
|
types.Address
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
await action.Should().ThrowAsync<Exception>().WithMessage("No session available!");
|
||||||
|
}
|
||||||
|
}
|
||||||
103
classlib.tests/Cmdlets/NewSessionCmdletTests.cs
Normal file
103
classlib.tests/Cmdlets/NewSessionCmdletTests.cs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
namespace PS.IPAM.Tests.Cmdlets;
|
||||||
|
|
||||||
|
using FluentAssertions;
|
||||||
|
using PS.IPAM;
|
||||||
|
using PS.IPAM.Cmdlets;
|
||||||
|
using PS.IPAM.Helpers;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the NewSessionCmdlet.
|
||||||
|
/// Note: Full cmdlet testing with parameter sets requires a PowerShell runspace.
|
||||||
|
/// These tests focus on verifying the cmdlet structure and basic functionality.
|
||||||
|
/// </summary>
|
||||||
|
[Collection("Sequential")]
|
||||||
|
public class NewSessionCmdletTests : IDisposable
|
||||||
|
{
|
||||||
|
public NewSessionCmdletTests()
|
||||||
|
{
|
||||||
|
// Clean state before each test
|
||||||
|
SessionManager.CloseSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Clean up after each test
|
||||||
|
SessionManager.CloseSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_Exists()
|
||||||
|
{
|
||||||
|
// Verify the cmdlet class exists and can be instantiated
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
cmdlet.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_URLProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
cmdlet.URL = "https://ipam.example.com";
|
||||||
|
cmdlet.URL.Should().Be("https://ipam.example.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_AppIDProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
cmdlet.AppID = "testapp";
|
||||||
|
cmdlet.AppID.Should().Be("testapp");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_TokenProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
cmdlet.Token = "my-api-token";
|
||||||
|
cmdlet.Token.Should().Be("my-api-token");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_CredentialsProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
cmdlet.Credentials = null;
|
||||||
|
cmdlet.Credentials.Should().BeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NewSessionCmdlet_IgnoreSSLProperty_Exists()
|
||||||
|
{
|
||||||
|
var cmdlet = new NewSessionCmdlet();
|
||||||
|
// SwitchParameter defaults to false
|
||||||
|
cmdlet.IgnoreSSL.IsPresent.Should().BeFalse();
|
||||||
|
// Setting it to true
|
||||||
|
var switchParam = new System.Management.Automation.SwitchParameter(true);
|
||||||
|
cmdlet.IgnoreSSL = switchParam;
|
||||||
|
// Note: SwitchParameter is a struct, so getting the value back may not work as expected
|
||||||
|
// Just verify the property exists and can be set
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SessionManager_CreateSessionWithToken_WorksCorrectly()
|
||||||
|
{
|
||||||
|
// This tests the underlying functionality that the cmdlet uses
|
||||||
|
var session = SessionManager.CreateSessionWithToken(
|
||||||
|
"https://ipam.example.com",
|
||||||
|
"testapp",
|
||||||
|
"my-api-token"
|
||||||
|
);
|
||||||
|
|
||||||
|
session.Should().NotBeNull();
|
||||||
|
session.URL.Should().Be("https://ipam.example.com");
|
||||||
|
session.AppID.Should().Be("testapp");
|
||||||
|
session.Token.Should().Be("my-api-token");
|
||||||
|
session.AuthType.Should().Be(AuthType.token);
|
||||||
|
session.Expires.Should().BeNull();
|
||||||
|
session.Credentials.Should().BeNull();
|
||||||
|
|
||||||
|
// Verify it was set as current session
|
||||||
|
SessionManager.CurrentSession.Should().BeSameAs(session);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user