diff --git a/ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs b/ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs index bb2cadd..3747f09 100644 --- a/ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs +++ b/ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs @@ -1,48 +1,51 @@ using System.Management.Automation.Runspaces; using ActiveDirectoryQuerier.ActiveDirectory; -using ActiveDirectoryQuerier.PowerShell; +// ReSharper disable InconsistentNaming +// ReSharper disable ConvertConstructorToMemberInitializers namespace ActiveDirectoryQuerier.Tests; -// ReSharper disable once InconsistentNaming public class ADCommandParametersTests { - [Fact] - public void AvailableParameters_AvailableParametersNotPopulated_NoValidCommandProvided() + private readonly ADCommandParameters _adCommandParameters; + + public ADCommandParametersTests() { // Arrange - ADCommandParameters adCommandParameters = new(); + _adCommandParameters = new ADCommandParameters(); + } + [Fact] + public void AvailableParameters_AvailableParametersNotPopulated_NoValidCommandProvided() + { // Assert - Assert.Contains("No valid command provided", adCommandParameters.AvailableParameters); + Assert.Contains("No valid command provided", _adCommandParameters.AvailableParameters); } [Fact] public async Task LoadAvailableParametersAsync_PopulatesAvailableParameters_IsNotEmpty() { // Arrange - ADCommandParameters adCommandParameters = new(); Command command = new("Get-Process"); // Act - await adCommandParameters.LoadAvailableParametersAsync(command); + await _adCommandParameters.LoadAvailableParametersAsync(command); // Assert - Assert.NotEmpty(adCommandParameters.AvailableParameters); + Assert.NotEmpty(_adCommandParameters.AvailableParameters); } [Fact] public async Task LoadAvailableParametersAsync_CheckAvailableParameters_ContainsNameAndId() { // Arrange - ADCommandParameters adCommandParameters = new(); Command command = new("Get-Process"); // Act - await adCommandParameters.LoadAvailableParametersAsync(command); + await _adCommandParameters.LoadAvailableParametersAsync(command); // Assert - Assert.Contains("-Name", adCommandParameters.AvailableParameters); - Assert.Contains("-Id", adCommandParameters.AvailableParameters); + Assert.Contains("-Name", _adCommandParameters.AvailableParameters); + Assert.Contains("-Id", _adCommandParameters.AvailableParameters); } } diff --git a/ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs b/ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs index a5afd55..fef257e 100644 --- a/ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs +++ b/ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs @@ -1,18 +1,17 @@ using System.Collections.ObjectModel; using System.Management.Automation.Runspaces; using ActiveDirectoryQuerier.ActiveDirectory; -using ActiveDirectoryQuerier.PowerShell; +// ReSharper disable InconsistentNaming namespace ActiveDirectoryQuerier.Tests; -// ReSharper disable once InconsistentNaming public class ADCommandsFetcherTests { [Fact] public async Task GetADCommands_ReturnsCommandList_IsNotEmpty() { - // Act - ObservableCollection adCommands = await ADCommandsFetcher.GetADCommands(); + // Arrange + ObservableCollection adCommands = await ADCommandsFetcher.GetADCommandsAsync(); // Assert Assert.NotEmpty(adCommands); @@ -24,8 +23,8 @@ public async Task GetADCommands_ReturnsCommandList_IsNotEmpty() [InlineData("Get-ADComputer")] public async Task GetADCommands_ReturnsCommandList_ContainsCommand(string commandName) { - // Act - ObservableCollection adCommands = await ADCommandsFetcher.GetADCommands(); + // Arrange + ObservableCollection adCommands = await ADCommandsFetcher.GetADCommandsAsync(); // Assert Assert.Contains(adCommands, command => command.CommandText == commandName); diff --git a/ActiveDirectoryQuerier.Tests/ActiveDirectoryInfoTests.cs b/ActiveDirectoryQuerier.Tests/ActiveDirectoryInfoTests.cs new file mode 100644 index 0000000..7a285b3 --- /dev/null +++ b/ActiveDirectoryQuerier.Tests/ActiveDirectoryInfoTests.cs @@ -0,0 +1,90 @@ +using ActiveDirectoryQuerier.PowerShell; +// ReSharper disable ConvertConstructorToMemberInitializers + +namespace ActiveDirectoryQuerier.Tests; + +/// +/// Since ActiveDirectoryInfo executes Active Directory commands, and it's not guaranteed that the tests will be +/// executed on an Active Directory domain, the output will vary in location (StdOut or StdErr) and content. Therefore, +/// it's more important to test that something is returned rather than the actual contents of the output. +/// +public class ActiveDirectoryInfoTests +{ + private readonly ActiveDirectoryInfo _adInfo = new(); + + [Fact] + public async Task GetADUsers_ReturnsExpectedOutput() + { + // Act + PSOutput result = await _adInfo.AvailableOptions["Get user on domain"](); + + // Assert + Assert.NotNull(result); + + if (result.HadErrors) + { + Assert.True(result.StdErr.Count > 0); + } + else + { + Assert.True(result.StdOut.Count > 0); + } + } + + [Fact] + public async Task GetADComputers_ReturnsExpectedOutput() + { + // Act + PSOutput result = await _adInfo.AvailableOptions["Get computers on domain"](); + + // Assert + Assert.NotNull(result); + + if (result.HadErrors) + { + Assert.True(result.StdErr.Count > 0); + } + else + { + Assert.True(result.StdOut.Count > 0); + } + } + + [Fact] + public async Task GetADIPv4Addresses_ReturnsExpectedOutput() + { + // Act + PSOutput result = await _adInfo.AvailableOptions["Get IPv4 of each system on domain"](); + + // Assert + Assert.NotNull(result); + + if (result.HadErrors) + { + Assert.True(result.StdErr.Count > 0); + } + else + { + Assert.True(result.StdOut.Count > 0); + } + } + + [Fact] + public async Task GetADIPv6Addresses_ReturnsExpectedOutput() + { + // Act + PSOutput result = await _adInfo.AvailableOptions["Get IPv6 of each system on domain"](); + + // Assert + Assert.NotNull(result); + + if (result.HadErrors) + { + Assert.True(result.StdErr.Count > 0); + } + else + { + Assert.True(result.StdOut.Count > 0); + } + } +} diff --git a/ActiveDirectoryQuerier.Tests/ActiveDirectoryQuerier.Tests.csproj b/ActiveDirectoryQuerier.Tests/ActiveDirectoryQuerier.Tests.csproj index ee0dbed..fabf189 100644 --- a/ActiveDirectoryQuerier.Tests/ActiveDirectoryQuerier.Tests.csproj +++ b/ActiveDirectoryQuerier.Tests/ActiveDirectoryQuerier.Tests.csproj @@ -7,7 +7,9 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs b/ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs index a98c571..4901014 100644 --- a/ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs +++ b/ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs @@ -1,24 +1,33 @@ using System.Collections.ObjectModel; using System.Management.Automation.Runspaces; using ActiveDirectoryQuerier.ActiveDirectory; -using ActiveDirectoryQuerier.PowerShell; using ActiveDirectoryQuerier.ViewModels; +// ReSharper disable JoinDeclarationAndInitializer +// ReSharper disable ConvertConstructorToMemberInitializers namespace ActiveDirectoryQuerier.Tests; public class ComboBoxParameterViewModelTests { + private readonly Command _command; + private readonly ADCommandParameters _adCommandParameters; + + public ComboBoxParameterViewModelTests() + { + // Arrange + _command = new Command("Get-ADUser"); + _adCommandParameters = new ADCommandParameters(); + } + [Fact] public async Task ComboBoxParameterViewModel_WhenConstructed_PossibleParametersIsNotNullOrEmpty() { // Arrange - Command command = new("Get-ADUser"); - ADCommandParameters adCommandParameters = new(); ComboBoxParameterViewModel comboBoxParameterViewModel; // Act - await adCommandParameters.LoadAvailableParametersAsync(command); - comboBoxParameterViewModel = new(adCommandParameters.AvailableParameters); + await _adCommandParameters.LoadAvailableParametersAsync(_command); + comboBoxParameterViewModel = new ComboBoxParameterViewModel(_adCommandParameters.AvailableParameters); // Assert Assert.NotNull(comboBoxParameterViewModel.AvailableParameters); @@ -29,13 +38,11 @@ public async Task ComboBoxParameterViewModel_WhenConstructed_PossibleParametersI public async Task ComboBoxParameterViewModel_WhenConstructed_PossibleParametersContainsExpectedParameters() { // Arrange - Command command = new("Get-ADUser"); - ADCommandParameters adCommandParameters = new(); ComboBoxParameterViewModel comboBoxParameterViewModel; // Act - await adCommandParameters.LoadAvailableParametersAsync(command); - comboBoxParameterViewModel = new(adCommandParameters.AvailableParameters); + await _adCommandParameters.LoadAvailableParametersAsync(_command); + comboBoxParameterViewModel = new ComboBoxParameterViewModel(_adCommandParameters.AvailableParameters); // Assert Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-Filter"); @@ -50,13 +57,11 @@ public async Task ComboBoxParameterViewModel_WhenSelectedParameterChosen_Selecte { // Arrange string selectedParameter; - Command command = new("Get-ADUser"); - ADCommandParameters adCommandParameters = new(); ComboBoxParameterViewModel comboBoxParameterViewModel; // Act - await adCommandParameters.LoadAvailableParametersAsync(command); - comboBoxParameterViewModel = new(adCommandParameters.AvailableParameters); + await _adCommandParameters.LoadAvailableParametersAsync(_command); + comboBoxParameterViewModel = new ComboBoxParameterViewModel(_adCommandParameters.AvailableParameters); selectedParameter = comboBoxParameterViewModel.AvailableParameters[0]; comboBoxParameterViewModel.SelectedParameter = selectedParameter; diff --git a/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs b/ActiveDirectoryQuerier.Tests/ConsoleViewModelTests.cs similarity index 54% rename from ActiveDirectoryQuerier.Tests/AppConsoleTests.cs rename to ActiveDirectoryQuerier.Tests/ConsoleViewModelTests.cs index 22f00fa..d5a9cb4 100644 --- a/ActiveDirectoryQuerier.Tests/AppConsoleTests.cs +++ b/ActiveDirectoryQuerier.Tests/ConsoleViewModelTests.cs @@ -1,10 +1,22 @@ using System.Management.Automation.Runspaces; using ActiveDirectoryQuerier.PowerShell; +using ActiveDirectoryQuerier.ViewModels; +// ReSharper disable ConvertConstructorToMemberInitializers namespace ActiveDirectoryQuerier.Tests; -public class AppConsoleTests : IDisposable +public class ConsoleViewModelTests : IDisposable { + private readonly PSExecutor _psExecutor; + private readonly ConsoleViewModel _consoleViewModel; + + public ConsoleViewModelTests() + { + // Arrange + _psExecutor = new PSExecutor(); + _consoleViewModel = new ConsoleViewModel(); + } + // TODO: Make sure this is how I should perform cleanups... public void Dispose() { @@ -21,15 +33,13 @@ public void Dispose() GC.SuppressFinalize(this); } - private static async Task<(AppConsole, PSOutput)> ExecuteCommandAsync(Command command) + private async Task<(ConsoleViewModel, PSOutput)> ExecuteCommandAsync(Command command) { - PSExecutor psExecutor = new(); - AppConsole appConsole = new(); - PSOutput result = await psExecutor.ExecuteAsync(command); + PSOutput result = await _psExecutor.ExecuteAsync(command); - appConsole.Append(result.HadErrors ? result.StdErr : result.StdOut); + _consoleViewModel.Append(result.HadErrors ? result.StdErr : result.StdOut); - return (appConsole, result); + return (_consoleViewModel, result); } [Fact] @@ -44,21 +54,20 @@ public async Task ClearConsole_ClearsConsole_SuccessfullyCleared() appConsole.Clear(); // Assert - Assert.Empty(appConsole.ConsoleOutput); + Assert.Empty(appConsole.GetConsoleOutput); } [Fact] public void Append_AppendsStringToConsole_SuccessfullyAppended() { // Arrange - AppConsole appConsole = new(); - const string output = "Output"; + string output = $"Output: {Guid.NewGuid()}"; // Act - appConsole.Append(output); + _consoleViewModel.Append(output); // Assert - Assert.Equal(output, appConsole.ConsoleOutput); + Assert.Equal(output + Environment.NewLine, _consoleViewModel.GetConsoleOutput); } [Fact] @@ -70,14 +79,15 @@ public async Task ExportToText_ExportToText_SuccessfullyExported() var (appConsole, returnValues) = await ExecuteCommandAsync(command); // Act - appConsole.ExportToText(); + appConsole.ExportToTextFile(); string fileContents = await File.ReadAllTextAsync("output.txt"); // Assert Assert.True(File.Exists("output.txt")); - Assert.Equal(returnValues.HadErrors ? string.Join(Environment.NewLine, returnValues.StdErr) - : string.Join(Environment.NewLine, returnValues.StdOut), + Assert.Equal(returnValues.HadErrors + ? string.Join(Environment.NewLine, returnValues.StdErr) + Environment.NewLine + : string.Join(Environment.NewLine, returnValues.StdOut) + Environment.NewLine, fileContents); } } diff --git a/ActiveDirectoryQuerier.Tests/MainWindowViewModelTests.cs b/ActiveDirectoryQuerier.Tests/MainWindowViewModelTests.cs new file mode 100644 index 0000000..002455f --- /dev/null +++ b/ActiveDirectoryQuerier.Tests/MainWindowViewModelTests.cs @@ -0,0 +1,113 @@ +using System.Windows; +using ActiveDirectoryQuerier.MessageBoxService; +using ActiveDirectoryQuerier.ViewModels; +using Moq; + +namespace ActiveDirectoryQuerier.Tests; + +public class MainWindowViewModelTests +{ + private readonly MainWindowViewModel _viewModel; + private readonly Mock _messageBoxServiceMock; + + public MainWindowViewModelTests() + { + // Arrange + _messageBoxServiceMock = new Mock(); + _viewModel = new MainWindowViewModel { MessageBoxService = _messageBoxServiceMock.Object }; + } + + [Fact] + public void ClearConsoleOutput_WhenConsoleIsEmpty_ShowsInformationMessage() + { + _viewModel.ClearConsoleOutput(new ConsoleViewModel()); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Information", MessageBoxButton.OK, MessageBoxImage.Information), + Times.Once); + } + + [Fact] + public void ClearConsoleOutput_WhenConsoleIsNotEmpty_ShowsWarningMessage() + { + var consoleViewModel = new ConsoleViewModel(); + consoleViewModel.Append("Some text"); + + _viewModel.ClearConsoleOutput(consoleViewModel); + + _messageBoxServiceMock.Verify(m => m.Show(It.IsAny(), + "Warning", + MessageBoxButton.YesNo, + MessageBoxImage.Warning, + MessageBoxResult.No), + Times.Once); + } + + [Fact] + public void ExecuteSelectedQueryInADInfo_SelectedQueryInActiveDirectoryInfoIsNull_ShowWarningMessage() + { + // sdf + } + + [Fact] + public void EditQueryFromQueryStackPanel_NullParameter_ShowsMessageBox() + { + // Act + _viewModel.EditQueryFromQueryStackPanel(null); + + // Assert + _messageBoxServiceMock.Verify( + x => x.Show(It.IsAny(), "Error", MessageBoxButton.OK, MessageBoxImage.Error), + Times.Once); + } + + [Fact] + public void DeleteQueryFromQueryStackPanel_WhenNoQuerySelected_ShowsWarningMessage() + { + _viewModel.DeleteQueryFromQueryStackPanel(null); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Warning", MessageBoxButton.OK, MessageBoxImage.Warning), + Times.Once); + } + + [Fact] + public void RemoveParameterComboBoxInQueryBuilder_WhenNoParametersToRemove_ShowsWarningMessage() + { + _viewModel.RemoveParameterComboBoxInQueryBuilder(null); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Warning", MessageBoxButton.OK, MessageBoxImage.Warning), + Times.Once); + } + + [Fact] + public void SaveCurrentQuery_WhenNoCommandSelected_ShowsWarningMessage() + { + _viewModel.SaveCurrentQuery(null); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Warning", MessageBoxButton.OK, MessageBoxImage.Warning), + Times.Once); + } + + [Fact] + public void AddParameterComboBoxInQueryBuilder_WhenNoCommandSelected_ShowsWarningMessage() + { + _viewModel.AddParameterComboBoxInQueryBuilder(null); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Warning", MessageBoxButton.OK, MessageBoxImage.Warning), + Times.Once); + } + + [Fact] + public void ClearQueryBuilder_WhenQueryBuilderIsEmpty_ShowsInformationMessage() + { + _viewModel.ClearQueryBuilder(null); + + _messageBoxServiceMock.Verify( + m => m.Show(It.IsAny(), "Information", MessageBoxButton.OK, MessageBoxImage.Information), + Times.Once); + } +} diff --git a/ActiveDirectoryQuerier.Tests/PSExecutorTests.cs b/ActiveDirectoryQuerier.Tests/PSExecutorTests.cs index ae5f78b..f3fb15f 100644 --- a/ActiveDirectoryQuerier.Tests/PSExecutorTests.cs +++ b/ActiveDirectoryQuerier.Tests/PSExecutorTests.cs @@ -1,11 +1,20 @@ using System.Management.Automation.Runspaces; using ActiveDirectoryQuerier.PowerShell; +// ReSharper disable InconsistentNaming +// ReSharper disable ConvertConstructorToMemberInitializers namespace ActiveDirectoryQuerier.Tests; -// ReSharper disable once InconsistentNaming public class PSExecutorTests { + private readonly PSExecutor _psExecutor; + + public PSExecutorTests() + { + // Arrange + _psExecutor = new PSExecutor(); + } + [Theory] [InlineData("Get-Command", "Module", "ActiveDirectory")] [InlineData("Get-Process", "Name", "explorer")] @@ -16,10 +25,9 @@ public void Execute_WhenGivenValidCommand_ReturnsExpectedOutput(string command, // Arrange Command psCommand = new(command); psCommand.Parameters.Add(parameter, parameterValue); - PSExecutor psExecutor = new(); // Act - PSOutput result = psExecutor.Execute(psCommand); + PSOutput result = _psExecutor.Execute(psCommand); // Assert Assert.False(result.HadErrors); @@ -35,11 +43,10 @@ public void Execute_CheckIfOutputChanged_ReturnsDifferentOutput() command.Parameters.Add("Module", "ActiveDirectory"); Command command2 = new("Get-Process"); command.Parameters.Add("Name", "explorer"); - PSExecutor psExecutor = new(); // Act - PSOutput result = psExecutor.Execute(command); - PSOutput result2 = psExecutor.Execute(command2); + PSOutput result = _psExecutor.Execute(command); + PSOutput result2 = _psExecutor.Execute(command2); // Assert Assert.NotEqual(result, result2); @@ -55,10 +62,9 @@ public async Task ExecuteAsync_WhenGivenValidCommand_ReturnsExpectedOutput(strin // Arrange Command psCommand = new(command); psCommand.Parameters.Add(parameter, parameterValue); - PSExecutor psExecutor = new(); // Act - PSOutput result = await psExecutor.ExecuteAsync(psCommand); + PSOutput result = await _psExecutor.ExecuteAsync(psCommand); // Assert Assert.False(result.HadErrors); @@ -76,10 +82,9 @@ public void Execute_WhenGivenInvalidCommand_ReturnsExpectedOutput(string command // Arrange Command psCommand = new(command); psCommand.Parameters.Add(parameter, parameterValue); - PSExecutor psExecutor = new(); // Act - PSOutput result = psExecutor.Execute(psCommand); + PSOutput result = _psExecutor.Execute(psCommand); // Assert Assert.True(result.HadErrors); @@ -97,14 +102,33 @@ public async Task ExecuteAsync_WhenGivenInvalidCommand_ReturnsExpectedOutput(str // Arrange Command psCommand = new(command); psCommand.Parameters.Add(parameter, parameterValue); - PSExecutor psExecutor = new(); // Act - PSOutput result = await psExecutor.ExecuteAsync(psCommand); + PSOutput result = await _psExecutor.ExecuteAsync(psCommand); // Assert Assert.True(result.HadErrors); Assert.NotEmpty(result.StdErr); Assert.Empty(result.StdOut); } + + [Theory] + [InlineData("Get-Command", "Module", "ActiveDirectory")] + [InlineData("Get-Process", "Name", "explorer")] + public async Task ExecuteAsync_ExecuteToCsv_ReturnsExpectedOutput(string command, + string parameter, + string parameterValue) + { + // Arrange + Command psCommand = new(command); + psCommand.Parameters.Add(parameter, parameterValue); + + // Act + PSOutput result = await _psExecutor.ExecuteAsync(psCommand, OutputFormat.Csv); + + // Assert + Assert.False(result.HadErrors); + Assert.Empty(result.StdErr); + Assert.NotEmpty(result.StdOut); + } } diff --git a/ActiveDirectoryQuerier.Tests/PSOutputTest.cs b/ActiveDirectoryQuerier.Tests/PSOutputTest.cs index dccddf1..a2b2106 100644 --- a/ActiveDirectoryQuerier.Tests/PSOutputTest.cs +++ b/ActiveDirectoryQuerier.Tests/PSOutputTest.cs @@ -1,39 +1,46 @@ using ActiveDirectoryQuerier.PowerShell; +// ReSharper disable InconsistentNaming +// ReSharper disable ConvertConstructorToMemberInitializers namespace ActiveDirectoryQuerier.Tests; -// ReSharper disable once InconsistentNaming public class PSOutputTest { + private readonly PSOutput _psOutput; + + public PSOutputTest() + { + // Arrange + _psOutput = new PSOutput(); + } + [Fact] public void HadErrors_StdErrHasEntries_ReturnsTrue() { // Arrange - PSOutput psOutput = new(); - psOutput.StdErr.Add("Error"); + _psOutput.StdErr.Add("Error"); // Act - bool result = psOutput.HadErrors; + bool result = _psOutput.HadErrors; // Assert Assert.True(result); - Assert.NotEmpty(psOutput.StdErr); - Assert.Empty(psOutput.StdOut); + Assert.NotEmpty(_psOutput.StdErr); + Assert.Empty(_psOutput.StdOut); } [Fact] public void NoErrors_StdOutHasEntries_ReturnsFalse() { // Arrange - PSOutput psOutput = new(); - psOutput.StdOut.Add("Output"); + _psOutput.StdOut.Add("Output"); // Act - var result = psOutput.HadErrors; + var result = _psOutput.HadErrors; // Assert Assert.False(result); - Assert.Empty(psOutput.StdErr); - Assert.NotEmpty(psOutput.StdOut); + Assert.Empty(_psOutput.StdErr); + Assert.NotEmpty(_psOutput.StdOut); } } diff --git a/ActiveDirectoryQuerier.Tests/RelayCommandTests.cs b/ActiveDirectoryQuerier.Tests/RelayCommandTests.cs deleted file mode 100644 index bb4439c..0000000 --- a/ActiveDirectoryQuerier.Tests/RelayCommandTests.cs +++ /dev/null @@ -1,85 +0,0 @@ -using ActiveDirectoryQuerier.ViewModels; - -namespace ActiveDirectoryQuerier.Tests; - -public class RelayCommandTests -{ - [Fact] - public void CanExecute_WithNullPredicate_ReturnsTrue() - { - RelayCommand command = new( - _ => - {}); - Assert.True(command.CanExecute(null)); - } - - [Fact] - public void CanExecute_WithNonNullPredicate_ReturnsTrue() - { - RelayCommand command = new( - _ => - {}, - _ => true); - Assert.True(command.CanExecute(null)); - } - - [Fact] - public void CanExecute_WithNonNullPredicate_ReturnsFalse() - { - RelayCommand command = new( - _ => - {}, - _ => false); - Assert.False(command.CanExecute(null)); - } - - [Fact] - public void Execute_WithNullParameter_Executes() - { - bool executed = false; - RelayCommand command = new( - _ => executed = true); - command.Execute(null); - Assert.True(executed); - } - - [Fact] - public void Execute_WithNonNullParameter_Executes() - { - bool executed = false; - RelayCommand command = new( - _ => executed = true); - command.Execute("test"); - Assert.True(executed); - } - - [Fact] - public void RaiseCanExecuteChanged_WithNullParameter_DoesNotThrow() - { - RelayCommand command = new( - _ => - {}); - command.RaiseCanExecuteChanged(); - } - - [Fact] - public void CanExecuteChanged_WithNullParameter_DoesNotThrow() - { - RelayCommand command = new( - _ => - {}); - command.CanExecuteChanged += (_, _) => - {}; - } - - [Fact] - public void CanExecuteChanged_WithNullParameter_DoesNotThrow2() - { - RelayCommand command = new( - _ => - {}); - // ReSharper disable once EventUnsubscriptionViaAnonymousDelegate - command.CanExecuteChanged -= (_, _) => - {}; - } -} diff --git a/ActiveDirectoryQuerier.Tests/TextBoxViewModelTests.cs b/ActiveDirectoryQuerier.Tests/TextBoxViewModelTests.cs index 87aa8f4..593475c 100644 --- a/ActiveDirectoryQuerier.Tests/TextBoxViewModelTests.cs +++ b/ActiveDirectoryQuerier.Tests/TextBoxViewModelTests.cs @@ -7,10 +7,8 @@ public class TextBoxViewModelTests [Fact] public void TextBoxViewModel_WhenConstructed_SelectedParameterValueIsNotNullOrEmpty() { - // Arrange - TextBoxViewModel textBoxViewModel = new() {// Act - SelectedParameterValue = "*" - }; + // Arrange & Act + TextBoxViewModel textBoxViewModel = new() { SelectedParameterValue = "*" }; // Assert Assert.NotNull(textBoxViewModel.SelectedParameterValue); @@ -20,10 +18,8 @@ public void TextBoxViewModel_WhenConstructed_SelectedParameterValueIsNotNullOrEm [Fact] public void TextBoxViewModel_WhenSetToNull_NoExceptionIsThrown() { - // Arrange - TextBoxViewModel textBoxViewModel = new() {// Act - SelectedParameterValue = null! - }; + // Arrange & Act + TextBoxViewModel textBoxViewModel = new() { SelectedParameterValue = null! }; // Assert Assert.Null(textBoxViewModel.SelectedParameterValue); diff --git a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs index 2b5b107..fc77fc4 100644 --- a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs +++ b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandParameters.cs @@ -10,17 +10,16 @@ public class ADCommandParameters { private readonly ObservableCollection _availableParameters = new(); - /// - /// - /// This property should not be accessed before LoadAvailableParametersAsync or LoadAvailableParameters has been - /// called. If it is accessed before either method has been called, no parameters will be available. - /// + /// + /// IMPORTANT: This property should not be accessed before LoadAvailableParametersAsync or LoadAvailableParameters + /// has been called. If it is accessed before either method has been called, no parameters will be available. + /// public ObservableCollection AvailableParameters { get { if (_availableParameters.Count == 0) { - Debug.WriteLine("Warning: LoadAvailableParametersAsync should be called before accessing " + + Trace.WriteLine("Warning: LoadAvailableParametersAsync should be called before accessing " + "AvailableParameters, to ensure asynchronous loading of parameters."); LoadAvailableParameters(null); } @@ -41,10 +40,9 @@ public void LoadAvailableParameters(Command? psCommand) private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync) { - // psCommand can be null if the user attempts to select an ActiveDirectory command that doesn't exist. - // More specifically, if the entered command doesn't exist in the ADCommands property defined in - // MainWindowViewModel.cs, psCommand will be null, causing an exception to be thrown, crashing the - // program. + // psCommand may be null if the user attempts to select an Active Directory command that doesn't exist. More + // specifically, if the entered command doesn't exist in the ADCommands property defined in + // MainWindowViewModel.cs, psCommand will be null, causing an exception to be thrown, crashing the program. if (psCommand is null) { Trace.WriteLine("Error: command is null"); @@ -54,28 +52,26 @@ private async Task LoadAvailableParametersCore(Command? psCommand, bool isAsync) if (_availableParameters.Count == 0) { + ICollection result; using var powerShell = System.Management.Automation.PowerShell.Create(); - string commandString = - $"Get-Command {psCommand.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}"; + var commandString = $"Get-Command {psCommand.CommandText} | Select -ExpandProperty Parameters | " + + $"ForEach-Object {{ $_.Keys }}"; powerShell.Commands.Clear(); powerShell.AddScript(commandString); if (isAsync) { - PSDataCollection result = await powerShell.InvokeAsync(); - foreach (PSObject adCommandParameter in result) - { - _availableParameters.Add($"-{adCommandParameter}"); - } + result = await powerShell.InvokeAsync(); } else { - Collection result = powerShell.Invoke(); - foreach (PSObject adCommandParameter in result) - { - _availableParameters.Add($"-{adCommandParameter}"); - } + result = powerShell.Invoke(); + } + + foreach (PSObject adCommandParameter in result) + { + _availableParameters.Add($"-{adCommandParameter}"); } } } diff --git a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandsFetcher.cs b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandsFetcher.cs index edd8a0c..ca942d9 100644 --- a/ActiveDirectoryQuerier/ActiveDirectory/ADCommandsFetcher.cs +++ b/ActiveDirectoryQuerier/ActiveDirectory/ADCommandsFetcher.cs @@ -10,7 +10,7 @@ namespace ActiveDirectoryQuerier.ActiveDirectory; public static class ADCommandsFetcher { // ReSharper disable once InconsistentNaming - public static async Task> GetADCommands() + public static async Task> GetADCommandsAsync() { Command psCommand = new("Get-Command"); psCommand.Parameters.Add("Module", "ActiveDirectory"); diff --git a/ActiveDirectoryQuerier/ActiveDirectoryQuerier.csproj b/ActiveDirectoryQuerier/ActiveDirectoryQuerier.csproj index 22d5639..b4e8a31 100644 --- a/ActiveDirectoryQuerier/ActiveDirectoryQuerier.csproj +++ b/ActiveDirectoryQuerier/ActiveDirectoryQuerier.csproj @@ -9,9 +9,9 @@ Active Directory Querier - 0.7.0 + 0.7.2 Hunter T., Pieter, Joseph - https://github.com/StrangeRanger/Active-Directory-Querier + https://github.com/StrangeRanger/FAFB-PowerShell-Tool false diff --git a/ActiveDirectoryQuerier/AssemblyInfo.cs b/ActiveDirectoryQuerier/AssemblyInfo.cs index 020e92b..a1e25af 100644 --- a/ActiveDirectoryQuerier/AssemblyInfo.cs +++ b/ActiveDirectoryQuerier/AssemblyInfo.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using System.Windows; [assembly:ThemeInfo(ResourceDictionaryLocation.None, // where theme specific resource dictionaries are located @@ -7,3 +8,4 @@ // (used if a resource is not found in the page, // app, or any theme specific resource dictionaries) )] +[assembly:InternalsVisibleTo("ActiveDirectoryQuerier.Tests")] diff --git a/ActiveDirectoryQuerier/MainWindow.xaml b/ActiveDirectoryQuerier/MainWindow.xaml index ebf43f6..6ea6dae 100644 --- a/ActiveDirectoryQuerier/MainWindow.xaml +++ b/ActiveDirectoryQuerier/MainWindow.xaml @@ -31,7 +31,7 @@ - + @@ -79,15 +79,16 @@ Query Name - - Query Description + Query Description - Command Parameter - @@ -153,7 +153,7 @@ Value - @@ -166,24 +166,24 @@ - + + Command="{Binding AddCommandComboBoxInQueryBuilderRelay}" /> + Command="{Binding AddParameterComboBoxInQueryBuilderRelay}" /> - - - - + Command="{Binding RemoveParameterComboBoxInQueryBuilderRelay}" /> + + + + + Command="{Binding ExportConsoleOutputToFileRelay}" /> @@ -191,14 +191,14 @@ + Margin="10,6,10,0" VerticalAlignment="Top" + IsChecked="{Binding IsQueryEditingEnabled}" + Command="{Binding CheckBoxCheckedRelay}" Height="15" Width="57" /> - @@ -214,13 +214,13 @@