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

Several improvements and fixes #105

Merged
merged 48 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2385398
Rename class to more accurately represent purpose
StrangeRanger Mar 19, 2024
06134cb
Improve class, method, and variable names
StrangeRanger Mar 19, 2024
8fda393
Refactoring
StrangeRanger Mar 19, 2024
703e4e6
Fix async Task problem + more
StrangeRanger Mar 20, 2024
f06621a
Update property names
StrangeRanger Mar 20, 2024
8579f1c
Fix checkmark not checking
StrangeRanger Mar 20, 2024
342ca4c
Refactor, renaming, etc.
StrangeRanger Mar 21, 2024
8e3f463
Slight refactoring
StrangeRanger Mar 21, 2024
4cb30dd
Update comments and slight refactor
StrangeRanger Mar 21, 2024
800adf0
Several changes to nullability
StrangeRanger Mar 21, 2024
ce1fe26
Fix functionality regarding editing button + property renaming
StrangeRanger Mar 21, 2024
5b54d3c
Prevent manually checking editing box
StrangeRanger Mar 21, 2024
d5dd689
If query selected for editing is deleted, it will no longer be...
StrangeRanger Mar 21, 2024
df8e43f
Fix 'System.ArgumentOutOfRangeException' when deleting a query while...
StrangeRanger Mar 22, 2024
44ad702
Massive refactor from MainWindowViewModel to QueryExecutor
StrangeRanger Mar 22, 2024
b0d523e
CSV formatting is now done at the powershell level
StrangeRanger Mar 22, 2024
00cae57
Revert some changes and fix a bug
StrangeRanger Mar 23, 2024
5315779
Re-organizing method structure
StrangeRanger Mar 23, 2024
05db28d
Update version number and Repo URL
StrangeRanger Mar 23, 2024
f8bdb6e
Add development prerequisites and instructions
StrangeRanger Mar 23, 2024
42993d2
Merge branch 'dev' of https://github.com/StrangeRanger/FAFB-PowerShel…
StrangeRanger Mar 23, 2024
ade8862
Format using clang-format
StrangeRanger Mar 23, 2024
d24883b
Merge branch 'main' into dev
StrangeRanger Mar 23, 2024
ad7ac5b
Improve async functionality
StrangeRanger Mar 27, 2024
3eb42c3
Format code and update comments
StrangeRanger Mar 27, 2024
dcf023c
Fix property visibility and data binding issue
StrangeRanger Mar 27, 2024
6e82d80
Remove unused usings
StrangeRanger Mar 27, 2024
ae79128
Improve thread-safety and nullability handling
StrangeRanger Mar 27, 2024
dec2a3a
Format with clang-format
StrangeRanger Mar 27, 2024
ec0766a
Newline is now added to end of console output
StrangeRanger Mar 27, 2024
9184172
Remove redundant and inefficient code
StrangeRanger Mar 27, 2024
3390070
Can now make tests against methods in MainWindowViewModel
StrangeRanger Mar 27, 2024
eede5e2
New test
StrangeRanger Mar 27, 2024
539cf0b
Format with clang-format
StrangeRanger Mar 27, 2024
a29c211
Fix failed exception handling
StrangeRanger Mar 27, 2024
784b2e1
Rename method to reflect the use of async
StrangeRanger Mar 27, 2024
37b8c95
Multiple changes to improve testing
StrangeRanger Mar 28, 2024
5463071
New tests
StrangeRanger Mar 28, 2024
adca693
Update formatting and comments
StrangeRanger Mar 28, 2024
11fca84
Remove redundant comments
StrangeRanger Mar 28, 2024
bca6d32
Update comments
StrangeRanger Mar 28, 2024
8382b44
Update comments
StrangeRanger Mar 28, 2024
98a702a
Rename method
StrangeRanger Mar 28, 2024
0651e0a
Format with clang-format
StrangeRanger Mar 28, 2024
16b82df
Refactor to remove redundant code
StrangeRanger Mar 28, 2024
80850dd
Rename parameter of CheckBoxChecked
StrangeRanger Mar 28, 2024
c9f2274
Add disable pragmas
StrangeRanger Mar 28, 2024
1a32af1
Revert "Add disable pragmas"
StrangeRanger Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading