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 #105 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger authored Mar 29, 2024
2 parents 0fbfadd + 1a32af1 commit 5ef9f5c
Show file tree
Hide file tree
Showing 31 changed files with 1,094 additions and 925 deletions.
29 changes: 16 additions & 13 deletions ActiveDirectoryQuerier.Tests/ADCommandParametersTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 5 additions & 6 deletions ActiveDirectoryQuerier.Tests/ADCommandsFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -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<Command> adCommands = await ADCommandsFetcher.GetADCommands();
// Arrange
ObservableCollection<Command> adCommands = await ADCommandsFetcher.GetADCommandsAsync();

// Assert
Assert.NotEmpty(adCommands);
Expand All @@ -24,8 +23,8 @@ public async Task GetADCommands_ReturnsCommandList_IsNotEmpty()
[InlineData("Get-ADComputer")]
public async Task GetADCommands_ReturnsCommandList_ContainsCommand(string commandName)
{
// Act
ObservableCollection<Command> adCommands = await ADCommandsFetcher.GetADCommands();
// Arrange
ObservableCollection<Command> adCommands = await ADCommandsFetcher.GetADCommandsAsync();

// Assert
Assert.Contains(adCommands, command => command.CommandText == commandName);
Expand Down
90 changes: 90 additions & 0 deletions ActiveDirectoryQuerier.Tests/ActiveDirectoryInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using ActiveDirectoryQuerier.PowerShell;
// ReSharper disable ConvertConstructorToMemberInitializers

namespace ActiveDirectoryQuerier.Tests;

/// <remarks>
/// 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.
/// </remarks>
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
31 changes: 18 additions & 13 deletions ActiveDirectoryQuerier.Tests/ComboBoxParameterViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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");
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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);
}
}
Loading

0 comments on commit 5ef9f5c

Please sign in to comment.