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

Commit

Permalink
Merge pull request #98 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger authored Mar 15, 2024
2 parents 7123e14 + a4c8d1c commit 89bf3b1
Show file tree
Hide file tree
Showing 34 changed files with 1,472 additions and 1,382 deletions.
48 changes: 48 additions & 0 deletions ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.ActiveDirectory;
using ActiveDirectoryQuerier.PowerShell;

namespace ActiveDirectoryQuerier.Tests;

// ReSharper disable once InconsistentNaming
public class ADCommandParametersTests
{
[Fact]
public void AvailableParameters_AvailableParametersNotPopulated_NoValidCommandProvided()
{
// Arrange
ADCommandParameters adCommandParameters = new();

// Assert
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);

// Assert
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);

// Assert
Assert.Contains("-Name", adCommandParameters.AvailableParameters);
Assert.Contains("-Id", adCommandParameters.AvailableParameters);
}
}
33 changes: 33 additions & 0 deletions ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.ActiveDirectory;
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/ActiveDirectoryCommandsTests.cs

This file was deleted.

83 changes: 83 additions & 0 deletions ActiveDirectoryQuerier.Tests/AppConsoleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.PowerShell;

namespace ActiveDirectoryQuerier.Tests;

public class AppConsoleTests : IDisposable
{
// TODO: Make sure this is how I should perform cleanups...
public void Dispose()
{
if (File.Exists("output.txt"))
{
File.Delete("output.txt");
}

if (File.Exists("output.csv"))
{
File.Delete("output.csv");
}

GC.SuppressFinalize(this);
}

private static async Task<(AppConsole, PSOutput)> ExecuteCommandAsync(Command command)
{
PSExecutor psExecutor = new();
AppConsole appConsole = new();
PSOutput result = await psExecutor.ExecuteAsync(command);

appConsole.Append(result.HadErrors ? result.StdErr : result.StdOut);

return (appConsole, result);
}

[Fact]
public async Task ClearConsole_ClearsConsole_SuccessfullyCleared()
{
// Arrange
Command command = new("Get-Process");
command.Parameters.Add("Name", "explorer");
var (appConsole, _) = await ExecuteCommandAsync(command);

// Act
appConsole.Clear();

// Assert
Assert.Empty(appConsole.ConsoleOutput);
}

[Fact]
public void Append_AppendsStringToConsole_SuccessfullyAppended()
{
// Arrange
AppConsole appConsole = new();
const string output = "Output";

// Act
appConsole.Append(output);

// Assert
Assert.Equal(output, appConsole.ConsoleOutput);
}

[Fact]
public async Task ExportToText_ExportToText_SuccessfullyExported()
{
// Arrange
Command command = new("Get-Process");
command.Parameters.Add("Name", "explorer");
var (appConsole, returnValues) = await ExecuteCommandAsync(command);

// Act
appConsole.ExportToText();
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),
fileContents);
}
}
76 changes: 76 additions & 0 deletions ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
using ActiveDirectoryQuerier.ActiveDirectory;
using ActiveDirectoryQuerier.PowerShell;
using ActiveDirectoryQuerier.ViewModels;

namespace ActiveDirectoryQuerier.Tests;

public class ComboBoxParameterViewModelTests
{
[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);

// Assert
Assert.NotNull(comboBoxParameterViewModel.AvailableParameters);
Assert.NotEmpty(comboBoxParameterViewModel.AvailableParameters);
}

[Fact]
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);

// Assert
Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-Filter");
Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-Identity");
Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-LDAPFilter");
Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-SearchBase");
Assert.Contains(comboBoxParameterViewModel.AvailableParameters, param => param == "-SearchScope");
}

[Fact]
public async Task ComboBoxParameterViewModel_WhenSelectedParameterChosen_SelectedParameterIsSet()
{
// Arrange
string selectedParameter;
Command command = new("Get-ADUser");
ADCommandParameters adCommandParameters = new();
ComboBoxParameterViewModel comboBoxParameterViewModel;

// Act
await adCommandParameters.LoadAvailableParametersAsync(command);
comboBoxParameterViewModel = new(adCommandParameters.AvailableParameters);
selectedParameter = comboBoxParameterViewModel.AvailableParameters[0];
comboBoxParameterViewModel.SelectedParameter = selectedParameter;

// Assert
Assert.Equal(selectedParameter, comboBoxParameterViewModel.SelectedParameter);
}

[Fact]
public void ComboBoxParameterViewModel_WhenConstructedWithNullPossibleParameters_ThrowsArgumentNullException()
{
// Arrange
ObservableCollection<string> possibleParameters = null!;

// Assert
Assert.Throws<ArgumentNullException>(() => new ComboBoxParameterViewModel(possibleParameters));
}
}
46 changes: 0 additions & 46 deletions ActiveDirectoryQuerier.Tests/CommandParametersTests.cs

This file was deleted.

Loading

0 comments on commit 89bf3b1

Please sign in to comment.