Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'Joseph-Refactor' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger committed Mar 12, 2024
2 parents eefa68b + 0371bed commit 2d5e8bd
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace ActiveDirectoryQuerier.Tests;

public class AdCommandParametersTests
// ReSharper disable once InconsistentNaming
public class ADCommandParametersTests
{
[Fact]
public void AvailableParameters_AvailableParametersNotPopulated_NoValidCommandProvided()
Expand All @@ -16,7 +17,7 @@ public void AvailableParameters_AvailableParametersNotPopulated_NoValidCommandPr
}

[Fact]
public async Task LoadParametersAsync_PopulatesAvailableParameters_IsNotEmpty()
public async Task LoadAvailableParametersAsync_PopulatesAvailableParameters_IsNotEmpty()
{
// Arrange
ADCommandParameters adCommandParameters = new();
Expand All @@ -30,7 +31,7 @@ public async Task LoadParametersAsync_PopulatesAvailableParameters_IsNotEmpty()
}

[Fact]
public async Task LoadParametersAsync_CheckAvailableParameters_ContainsNameAndId()
public async Task LoadAvailableParametersAsync_CheckAvailableParameters_ContainsNameAndId()
{
// Arrange
ADCommandParameters adCommandParameters = new();
Expand Down
32 changes: 32 additions & 0 deletions ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.PowerShell;

namespace ActiveDirectoryQuerier.Tests;

// ReSharper disable once InconsistentNaming
public class ADCommandsFetcherTests
{
[Fact]
public async Task GetADCommands_ReturnsCommandList_IsNotEmpty()
{
// Act
ObservableCollection<Command> adCommands = await ADCommandsFetcher.GetADCommands();

// Assert
Assert.NotEmpty(adCommands);
}

[Theory]
[InlineData("Get-ADUser")]
[InlineData("Get-ADGroup")]
[InlineData("Get-ADComputer")]
public async Task GetADCommands_ReturnsCommandList_ContainsCommand(string commandName)
{
// Act
ObservableCollection<Command> adCommands = await ADCommandsFetcher.GetADCommands();

// Assert
Assert.Contains(adCommands, command => command.CommandText == commandName);
}
}
31 changes: 0 additions & 31 deletions ActiveDirectoryQuerier.Tests/AdCommandsFetcherTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

namespace ActiveDirectoryQuerier.Tests;

public class PsExecutorTests
// ReSharper disable once InconsistentNaming
public class PSExecutorTests
{
[Theory]
[InlineData("Get-Command", "Module", "ActiveDirectory")]
[InlineData("Get-Process", "Name", "explorer")]
public void Execute_WhenGivenValidCommand_ReturnsExpectedOutput(string cmd, string paramName, string paramValue)
public void Execute_WhenGivenValidCommand_ReturnsExpectedOutput(string command, string parameter, string parameterValue)
{
// Arrange
Command command = new(cmd);
command.Parameters.Add(paramName, paramValue);
Command psCommand = new(command);
psCommand.Parameters.Add(parameter, parameterValue);
PSExecutor psExecutor = new();

// Act
PSOutput result = psExecutor.Execute(command);
PSOutput result = psExecutor.Execute(psCommand);

// Assert
Assert.False(result.HadErrors);
Expand Down Expand Up @@ -45,17 +46,15 @@ public void Execute_CheckIfOutputChanged_ReturnsDifferentOutput()
[Theory]
[InlineData("Get-Command", "Module", "ActiveDirectory")]
[InlineData("Get-Process", "Name", "explorer")]
public async Task ExecuteAsync_WhenGivenValidCommand_ReturnsExpectedOutput(string cmd,
string paramName,
string paramValue)
public async Task ExecuteAsync_WhenGivenValidCommand_ReturnsExpectedOutput(string command, string parameter, string parameterValue)
{
// Arrange
Command command = new(cmd);
command.Parameters.Add(paramName, paramValue);
Command psCommand = new(command);
psCommand.Parameters.Add(parameter, parameterValue);
PSExecutor psExecutor = new();

// Act
PSOutput result = await psExecutor.ExecuteAsync(command);
PSOutput result = await psExecutor.ExecuteAsync(psCommand);

// Assert
Assert.False(result.HadErrors);
Expand All @@ -66,15 +65,15 @@ public async Task ExecuteAsync_WhenGivenValidCommand_ReturnsExpectedOutput(strin
[Theory]
[InlineData("Get-ADUser", "InvalidParameter", "*")]
[InlineData("InvalidCommand", "Filter", "*")]
public void Execute_WhenGivenInvalidCommand_ReturnsExpectedOutput(string cmd, string paramName, string paramValue)
public void Execute_WhenGivenInvalidCommand_ReturnsExpectedOutput(string command, string parameter, string parameterValue)
{
// Arrange
Command command = new(cmd);
command.Parameters.Add(paramName, paramValue);
Command psCommand = new(command);
psCommand.Parameters.Add(parameter, parameterValue);
PSExecutor psExecutor = new();

// Act
PSOutput result = psExecutor.Execute(command);
PSOutput result = psExecutor.Execute(psCommand);

// Assert
Assert.True(result.HadErrors);
Expand All @@ -85,17 +84,15 @@ public void Execute_WhenGivenInvalidCommand_ReturnsExpectedOutput(string cmd, st
[Theory]
[InlineData("Get-ADUser", "InvalidParameter", "*")]
[InlineData("InvalidCommand", "Filter", "*")]
public async Task ExecuteAsync_WhenGivenInvalidCommand_ReturnsExpectedOutput(string cmd,
string paramName,
string paramValue)
public async Task ExecuteAsync_WhenGivenInvalidCommand_ReturnsExpectedOutput(string command, string parameter, string parameterValue)
{
// Arrange
Command command = new(cmd);
command.Parameters.Add(paramName, paramValue);
Command psCommand = new(command);
psCommand.Parameters.Add(parameter, parameterValue);
PSExecutor psExecutor = new();

// Act
PSOutput result = await psExecutor.ExecuteAsync(command);
PSOutput result = await psExecutor.ExecuteAsync(psCommand);

// Assert
Assert.True(result.HadErrors);
Expand Down
39 changes: 39 additions & 0 deletions ActiveDirectoryQuerier.Tests/PSOutputTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using ActiveDirectoryQuerier.PowerShell;

namespace ActiveDirectoryQuerier.Tests;

// ReSharper disable once InconsistentNaming
public class PSOutputTest
{
[Fact]
public void HadErrors_StdErrHasEntries_ReturnsTrue()
{
// Arrange
PSOutput psOutput = new();
psOutput.StdErr.Add("Error");

// Act
bool result = psOutput.HadErrors;

// Assert
Assert.True(result);
Assert.NotEmpty(psOutput.StdErr);
Assert.Empty(psOutput.StdOut);
}

[Fact]
public void NoErrors_StdOutHasEntries_ReturnsFalse()
{
// Arrange
PSOutput psOutput = new();
psOutput.StdOut.Add("Output");

// Act
var result = psOutput.HadErrors;

// Assert
Assert.False(result);
Assert.Empty(psOutput.StdErr);
Assert.NotEmpty(psOutput.StdOut);
}
}
38 changes: 0 additions & 38 deletions ActiveDirectoryQuerier.Tests/PsOutputTest.cs

This file was deleted.

34 changes: 16 additions & 18 deletions ActiveDirectoryQuerier/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using System.Windows.Controls;
using System.Windows.Input;
using ActiveDirectoryQuerier.PowerShell;
using ActiveDirectoryQuerier.Queries;
using Microsoft.Win32;
using static ActiveDirectoryQuerier.PowerShell.CustomQueries;

namespace ActiveDirectoryQuerier;

Expand All @@ -27,7 +27,7 @@ public sealed class MainWindowViewModel : INotifyPropertyChanged
private string _queryDescription;
private AppConsole _queryBuilderConsoleOutput;
private AppConsole _activeDirectoryInfoConsoleOutput;
private Command? _selectedComboBoxCommandInQueryBuilder; // TODO: Change the name of this field?
private Command? _selectedComboBoxCommandInQueryBuilder;
private ObservableCollection<Button>? _buttons;

// [[ Other fields ]] ----------------------------------------------------------- //
Expand Down Expand Up @@ -281,7 +281,7 @@ private void EditCustomQuery(object sender)

// Fill in the commandName
Command chosenCommand =
ADCommands.FirstOrDefault(item => item.CommandText == currentQuery.CommandName)!;
ADCommands.FirstOrDefault(item => item.CommandText == currentQuery.PSCommandName)!;
SelectedComboBoxCommandInQueryBuilder = chosenCommand;

// Load the Possible Parameters Synchronously
Expand All @@ -298,17 +298,17 @@ private void EditCustomQuery(object sender)
}

// Fill in Parameters and values
for (int i = 0; i < currentQuery.CommandParameters.Length; i++)
for (int i = 0; i < currentQuery.PSCommandParameters.Length; i++)
{
Trace.WriteLine(currentQuery.CommandParameters[i]);
Trace.WriteLine(currentQuery.PSCommandParameters[i]);

// Adds the Parameters boxes
object temp = new();
AddParameterComboBox(temp);

// Fill in the parameter boxes
DynamicParameterComboBoxes[i].SelectedParameter = currentQuery.CommandParameters[i];
DynamicParameterValueTextBoxes[i].SelectedParameterValue = currentQuery.CommandParametersValues[i];
DynamicParameterComboBoxes[i].SelectedParameter = currentQuery.PSCommandParameters[i];
DynamicParameterValueTextBoxes[i].SelectedParameterValue = currentQuery.PSCommandParameterValues[i];
}
}

Expand Down Expand Up @@ -355,7 +355,7 @@ private void DeleteCustomQuery(object _)

QueryButtonStackPanel.Remove(currentButton);
_customQuery.Queries.Remove((Query)currentButton.Tag);
_customQuery.SerializeMethod();
_customQuery.SaveQueriesToJson();
}

/// <summary>
Expand Down Expand Up @@ -398,7 +398,7 @@ private async void ExecuteSelectedCommandAsync(object _)
/// </summary>
private async Task InitializeActiveDirectoryCommandsAsync()
{
ObservableCollection<Command> list = await ADCommandsFetcher.GetActiveDirectoryCommands();
ObservableCollection<Command> list = await ADCommandsFetcher.GetADCommands();
ADCommands = new ObservableCollection<Command>(list);
OnPropertyChanged(nameof(ADCommands));
}
Expand Down Expand Up @@ -642,20 +642,18 @@ private void GetCurrentQuery()

_currentQuery.QueryDescription = QueryDescription;
_currentQuery.QueryName = QueryName;

_currentQuery.CommandName = SelectedComboBoxCommandInQueryBuilder.CommandText;

// TODO: Possibly convert foreach into a for loop...
_currentQuery.PSCommandName = SelectedComboBoxCommandInQueryBuilder.CommandText;

for (int i = 0; i < SelectedComboBoxCommandInQueryBuilder.Parameters.Count; i++)
{
CommandParameter commandParameter = SelectedComboBoxCommandInQueryBuilder.Parameters[i];

commandParameters[i] = commandParameter.Name;
commandParameterValues[i] = commandParameter.Value.ToString()!;
i++;
i++; // TODO: Remove this line!
}
_currentQuery.CommandParameters = commandParameters;
_currentQuery.CommandParametersValues = commandParameterValues;
_currentQuery.PSCommandParameters = commandParameters;
_currentQuery.PSCommandParameterValues = commandParameterValues;
}
}
catch (Exception ex)
Expand Down Expand Up @@ -706,7 +704,7 @@ private void SaveCustomQueries(object commandParameter)
GetCurrentQuery();
Trace.WriteLine(_customQuery.Queries.IndexOf(_isEditing));
_customQuery.Queries[_customQuery.Queries.IndexOf(_isEditing)] = _currentQuery;
_customQuery.SerializeMethod();
_customQuery.SaveQueriesToJson();
_isEditing = null;
EditingEnabled = false;
}
Expand Down Expand Up @@ -774,7 +772,7 @@ private Button CreateCustomButton(Query? query = null)
if (query != null)
{
newButton.Height = 48;
newButton.Content = (string.IsNullOrEmpty(query.QueryName) ? query.CommandName : query.QueryName);
newButton.Content = (string.IsNullOrEmpty(query.QueryName) ? query.PSCommandName : query.QueryName);
newButton.Tag = query;
}
else
Expand Down
4 changes: 2 additions & 2 deletions ActiveDirectoryQuerier/PowerShell/ADCommandsFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace ActiveDirectoryQuerier.PowerShell;
// ReSharper disable once InconsistentNaming
public static class ADCommandsFetcher
{
// TODO: Possibly rename this method to GetADCommandList...
public static async Task<ObservableCollection<Command>> GetActiveDirectoryCommands()
// ReSharper disable once InconsistentNaming
public static async Task<ObservableCollection<Command>> GetADCommands()
{
Command psCommand = new("Get-Command");
psCommand.Parameters.Add("Module", "ActiveDirectory");
Expand Down
Loading

0 comments on commit 2d5e8bd

Please # to comment.