Files
scadaproj/ZB.MOM.WW.Secrets/tests/ZB.MOM.WW.Secrets.Tests/Cli/ShippedCliSettingsTests.cs
T
Joseph Doherty 5e2d3dd1bc fix(secrets): malformed ${secret:} references fail closed; README sample validates (scadaproj#45)
Secrets-009: SecretReferenceExpander.ExpandAsync throws FormatException when a value holds a
${secret: marker that is not a complete reference with a non-blank name (${secret:}, an
unterminated ${secret:name, a nested marker), instead of passing the literal text through as the
credential. ExpandConfigurationAsync names the offending key (never the value) and writes nothing.
The README wiring sample drops the relative "SqlitePath": "secrets.db" (which failed validation)
for the per-user default and shows an absolute example; a test validates the sample.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 15:32:28 -05:00

66 lines
2.6 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.Secrets.DependencyInjection;
namespace ZB.MOM.WW.Secrets.Tests.Cli;
/// <summary>
/// scadaproj#49: the <c>appsettings.json</c> the CLI ships must pass the store-path validator. It
/// used to set <c>"SqlitePath": "secrets.db"</c>, which the validator rejects as relative, so the
/// headless CLI could not start from its own directory unless an environment variable overrode it.
/// Same check for the README's wiring sample (Secrets-009).
/// </summary>
public sealed class ShippedCliSettingsTests
{
private static string RepoFile(params string[] relative)
{
for (DirectoryInfo? dir = new(AppContext.BaseDirectory); dir is not null; dir = dir.Parent)
{
if (File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.Secrets.slnx")))
{
return Path.Combine(new[] { dir.FullName }.Concat(relative).ToArray());
}
}
throw new InvalidOperationException("Could not locate ZB.MOM.WW.Secrets.slnx above the test output.");
}
private static SecretsOptions Validate(IConfiguration config)
{
var services = new ServiceCollection();
services.AddZbSecrets(config, "Secrets");
using ServiceProvider sp = services.BuildServiceProvider();
return sp.GetRequiredService<IOptions<SecretsOptions>>().Value;
}
[Fact]
public void The_cli_appsettings_passes_store_path_validation()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(RepoFile("src", "ZB.MOM.WW.Secrets.Cli", "appsettings.json"), optional: false)
.Build();
SecretsOptions options = Validate(config);
Assert.True(Path.IsPathFullyQualified(options.SqlitePath));
}
[Fact]
public void The_readme_wiring_sample_passes_store_path_validation()
{
string readme = File.ReadAllText(RepoFile("README.md"));
int start = readme.IndexOf("\"Secrets\": {", StringComparison.Ordinal);
Assert.True(start >= 0, "README no longer has a Secrets sample block");
int end = readme.IndexOf("\n},", start, StringComparison.Ordinal);
string block = "{" + readme[start..(end + 2)].TrimEnd(',') + "}";
string json = string.Join('\n', block.Split('\n').Where(l => !l.TrimStart().StartsWith("//", StringComparison.Ordinal)));
IConfiguration config = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
.Build();
Validate(config);
}
}