mirror of
https://github.com/GoodOlClint/PSProxmoxVE.git
synced 2026-08-05 20:27:41 +00:00
d187b0de52
- CS8601: Add null-forgiving operator on guarded dictionary assignments in cmdlets where IsNullOrEmpty check precedes the assignment - CS8602: Add Assert.NotNull after JObject["data"] in xUnit model tests (JToken indexer returns nullable on net48) - CS8604: Add null-forgiving on guarded arguments in NodeService, WaitPveTaskCmdlet, GetPveTemplateCmdlet, and auth test parameters Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
using System;
|
|
using Xunit;
|
|
using PSProxmoxVE.Core.Authentication;
|
|
|
|
namespace PSProxmoxVE.Core.Tests.Authentication
|
|
{
|
|
/// <summary>
|
|
/// Tests for PveAuthenticator input validation. These tests exercise the
|
|
/// argument-validation logic without making actual HTTP calls.
|
|
/// </summary>
|
|
public class PveAuthenticatorTests
|
|
{
|
|
private const string ValidHostname = "pve.example.com";
|
|
private const int ValidPort = 8006;
|
|
|
|
[Fact]
|
|
public void ValidateUsername_MissingAtSign_Throws()
|
|
{
|
|
var ex = Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithCredentials(ValidHostname, ValidPort, false, "rootpam", "password"));
|
|
|
|
Assert.Contains("realm", ex.Message, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateApiToken_InvalidFormat_NoTokenId_Throws()
|
|
{
|
|
// Missing the !TOKENID part — no exclamation mark
|
|
var ex = Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithApiToken(ValidHostname, ValidPort, false, "root@pam=someuuid"));
|
|
|
|
Assert.Contains("token", ex.Message, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateApiToken_InvalidFormat_NoEquals_Throws()
|
|
{
|
|
// Has ! but no = sign after TOKENID
|
|
var ex = Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithApiToken(ValidHostname, ValidPort, false, "root@pam!mytoken"));
|
|
|
|
Assert.Contains("token", ex.Message, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateApiToken_InvalidFormat_NoRealm_Throws()
|
|
{
|
|
// Missing the @realm portion
|
|
var ex = Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithApiToken(ValidHostname, ValidPort, false, "root!mytoken=someuuid"));
|
|
|
|
Assert.Contains("token", ex.Message, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public void ValidateHostname_NullOrEmpty_Throws_ForCredentials(string? hostname)
|
|
{
|
|
Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithCredentials(hostname!, ValidPort, false, "root@pam", "password"));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public void ValidateHostname_NullOrEmpty_Throws_ForApiToken(string? hostname)
|
|
{
|
|
Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithApiToken(hostname!, ValidPort, false, "root@pam!mytoken=uuid"));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
public void ValidateApiToken_NullOrEmpty_Throws(string? token)
|
|
{
|
|
Assert.Throws<ArgumentException>(() =>
|
|
PveAuthenticator.AuthenticateWithApiToken(ValidHostname, ValidPort, false, token!));
|
|
}
|
|
}
|
|
}
|