94d5a864e0
Add SecureStore integration to ConfigManager for secure handling of connection strings and sensitive configuration values. Includes store/secret management UI, encrypted .store file support, and comprehensive test coverage.
344 lines
8.7 KiB
C#
344 lines
8.7 KiB
C#
using JdeScoping.ConfigManager.Constants;
|
|
using JdeScoping.ConfigManager.ViewModels.Dialogs;
|
|
|
|
namespace JdeScoping.ConfigManager.Tests.ViewModels.Dialogs;
|
|
|
|
public class SecretEditDialogViewModelTests
|
|
{
|
|
[Fact]
|
|
public void DefaultConstructor_SetsIsNewSecretToTrue()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel();
|
|
|
|
// Assert
|
|
sut.IsNewSecret.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void DefaultConstructor_SetsKeyToEmpty()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel();
|
|
|
|
// Assert
|
|
sut.Key.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void DefaultConstructor_SetsValueToEmpty()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel();
|
|
|
|
// Assert
|
|
sut.Value.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void EditConstructor_SetsKeyFromParameter()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel("myKey", "myValue");
|
|
|
|
// Assert
|
|
sut.Key.ShouldBe("myKey");
|
|
}
|
|
|
|
[Fact]
|
|
public void EditConstructor_SetsValueFromParameter()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel("myKey", "myValue");
|
|
|
|
// Assert
|
|
sut.Value.ShouldBe("myValue");
|
|
}
|
|
|
|
[Fact]
|
|
public void EditConstructor_SetsIsNewSecretToFalse()
|
|
{
|
|
// Arrange & Act
|
|
var sut = new SecretEditDialogViewModel("myKey", "myValue");
|
|
|
|
// Assert
|
|
sut.IsNewSecret.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenEmpty_IsValidReturnsFalse()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = ""
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenEmpty_ValidationErrorReturnsKeyRequired()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = ""
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.ValidationError.ShouldBe(SecureStoreStrings.KeyRequired);
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenWhitespace_IsValidReturnsFalse()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = " "
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.IsValid.ShouldBeFalse();
|
|
sut.ValidationError.ShouldBe(SecureStoreStrings.KeyRequired);
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenProvided_IsValidReturnsTrue()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = "validKey"
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.IsValid.ShouldBeTrue();
|
|
sut.ValidationError.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsKeyEditable_WhenIsNewSecretIsTrue_ReturnsTrue()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
|
|
// Act & Assert
|
|
sut.IsKeyEditable.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsKeyEditable_WhenIsNewSecretIsFalse_ReturnsFalse()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel("existingKey", "existingValue");
|
|
|
|
// Act & Assert
|
|
sut.IsKeyEditable.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void DialogTitle_WhenIsNewSecretIsTrue_ReturnsAddSecret()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
|
|
// Act & Assert
|
|
sut.DialogTitle.ShouldBe("Add Secret");
|
|
}
|
|
|
|
[Fact]
|
|
public void DialogTitle_WhenIsNewSecretIsFalse_ReturnsEditSecret()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel("existingKey", "existingValue");
|
|
|
|
// Act & Assert
|
|
sut.DialogTitle.ShouldBe("Edit Secret");
|
|
}
|
|
|
|
[Fact]
|
|
public void IsNewSecret_WhenChanged_RaisesIsKeyEditablePropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var isKeyEditablePropertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.IsKeyEditable))
|
|
isKeyEditablePropertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.IsNewSecret = false;
|
|
|
|
// Assert
|
|
isKeyEditablePropertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsNewSecret_WhenChanged_RaisesDialogTitlePropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var dialogTitlePropertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.DialogTitle))
|
|
dialogTitlePropertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.IsNewSecret = false;
|
|
|
|
// Assert
|
|
dialogTitlePropertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenChanged_RaisesPropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var propertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.Key))
|
|
propertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.Key = "newKey";
|
|
|
|
// Assert
|
|
propertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenChanged_RaisesIsValidPropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var isValidPropertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.IsValid))
|
|
isValidPropertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.Key = "newKey";
|
|
|
|
// Assert
|
|
isValidPropertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenChanged_RaisesValidationErrorPropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var validationErrorPropertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.ValidationError))
|
|
validationErrorPropertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.Key = "newKey";
|
|
|
|
// Assert
|
|
validationErrorPropertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Value_WhenChanged_RaisesPropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var propertyChangedRaised = false;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.Value))
|
|
propertyChangedRaised = true;
|
|
};
|
|
|
|
// Act
|
|
sut.Value = "newValue";
|
|
|
|
// Assert
|
|
propertyChangedRaised.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Value_CanBeEmpty_IsValidStillReturnsTrue()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = "validKey",
|
|
Value = ""
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.IsValid.ShouldBeTrue();
|
|
sut.ValidationError.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Value_CanBeNull_IsValidStillReturnsTrue()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel
|
|
{
|
|
Key = "validKey",
|
|
Value = null!
|
|
};
|
|
|
|
// Act & Assert
|
|
sut.IsValid.ShouldBeTrue();
|
|
sut.ValidationError.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsNewSecret_WhenSetToSameValue_DoesNotRaisePropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel();
|
|
var propertyChangedCount = 0;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.IsNewSecret))
|
|
propertyChangedCount++;
|
|
};
|
|
|
|
// Act
|
|
sut.IsNewSecret = true; // Same value as default
|
|
|
|
// Assert
|
|
propertyChangedCount.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Key_WhenSetToSameValue_DoesNotRaisePropertyChanged()
|
|
{
|
|
// Arrange
|
|
var sut = new SecretEditDialogViewModel { Key = "testKey" };
|
|
var propertyChangedCount = 0;
|
|
sut.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(SecretEditDialogViewModel.Key))
|
|
propertyChangedCount++;
|
|
};
|
|
|
|
// Act
|
|
sut.Key = "testKey"; // Same value
|
|
|
|
// Assert
|
|
propertyChangedCount.ShouldBe(0);
|
|
}
|
|
}
|