Files
Joseph Doherty bfc1c8064a refactor(securestore): store entire connection strings in SecureStore
Eliminates placeholder substitution (${KEY}) in favor of storing complete
connection strings as single encrypted values. SecureStore now auto-creates
entries for all connection strings defined in appsettings. ConfigManager
editor reads/writes values directly to SecureStore.
2026-01-23 14:44:04 -05:00

85 lines
2.9 KiB
C#

using JdeScoping.Core.Interfaces;
using JdeScoping.DataAccess.Exceptions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
using Xunit;
namespace JdeScoping.DataAccess.Tests;
/// <summary>
/// Unit tests for DbConnectionFactory GIW connection support.
/// Connection strings are retrieved from ISecureStoreService.
/// </summary>
public class DbConnectionFactoryGiwTests
{
private readonly ISecureStoreService _secureStore;
private readonly ILogger<DbConnectionFactory> _logger;
public DbConnectionFactoryGiwTests()
{
_secureStore = Substitute.For<ISecureStoreService>();
_logger = Substitute.For<ILogger<DbConnectionFactory>>();
}
[Fact]
public async Task CreateGiwConnectionAsync_MissingConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns((string?)null);
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync<ConnectionException>(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Connection string not found");
}
[Fact]
public async Task CreateGiwConnectionAsync_EmptyConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns(string.Empty);
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync<ConnectionException>(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Connection string not found");
}
[Fact]
public async Task CreateGiwConnectionAsync_InvalidConnectionString_ThrowsConnectionException()
{
// Arrange
_secureStore.Get("GIW").Returns("Invalid oracle connection");
var factory = new DbConnectionFactory(_secureStore, _logger);
// Act & Assert
var ex = await Should.ThrowAsync<ConnectionException>(
async () => await factory.CreateGiwConnectionAsync());
ex.DataSource.ShouldBe("GIW");
ex.Message.ShouldContain("Failed to open connection");
ex.InnerException.ShouldNotBeNull();
}
[Fact]
public async Task CreateGiwConnectionAsync_CancellationRequested_ThrowsOperationCanceledException()
{
// Arrange
_secureStore.Get("GIW").Returns("User Id=test;Password=test;Data Source=test");
var factory = new DbConnectionFactory(_secureStore, _logger);
using var cts = new CancellationTokenSource();
cts.Cancel();
// Act & Assert
await Should.ThrowAsync<OperationCanceledException>(
async () => await factory.CreateGiwConnectionAsync(cts.Token));
}
}