From 353bbe668a6985e13c01688feb76333b4d4e05ca Mon Sep 17 00:00:00 2001 From: Joe Beaudry Date: Fri, 27 May 2016 08:22:37 -0400 Subject: [PATCH] Added Firewall Test (#9) * Added Firewall Test * Small Tweaks and things I missed --- Public/Firewall.ps1 | 41 ++++++++++++++++++++++++++++++++++++++++ ReleaseNotes.md | 5 +++++ Tests/poshspec.Tests.ps1 | 13 +++++++++++++ poshspecdemo.ps1 | 7 +++++++ 4 files changed, 66 insertions(+) create mode 100644 Public/Firewall.ps1 diff --git a/Public/Firewall.ps1 b/Public/Firewall.ps1 new file mode 100644 index 0000000..83ff826 --- /dev/null +++ b/Public/Firewall.ps1 @@ -0,0 +1,41 @@ +<# +.SYNOPSIS + Firewall Settings +.DESCRIPTION + Used To Determine if Firewall is Running Desired Settings +.PARAMETER Target + The name of the Firewall DisplayName to be Tested +.PARAMETER Property + The name of the Property of the Firewall Object to be Tested +.PARAMETER Should + A Script Block defining a Pester Assertion. +.EXAMPLE + Firewall putty.exe Enabled { Should be "$True" } +.EXAMPLE + Firewall putty.exe Action { Should be 'Allow' } +.EXAMPLE + Firewall putty.exe Private { Should be 'Public' } +.NOTES + Assertions: Be +#> + function Firewall{ + [CmdletBinding()] + param( + [Parameter(Mandatory, Position=1)] + [Alias('Name')] + [string]$Target, + + [Parameter(Position=2)] + [ValidateSet("Name","DisplayName","Description","DisplayGroup","Group","Enabled","Profile","Direction","Action","EdgeTraversalPolicy","LooseSourceMapping","LocalOnlyMapping","PrimaryStatus","Status","EnforcementStatus","PolicyStoreSource","PolicyStoreSourceType")] + [string]$Property, + + [Parameter(Mandatory, Position=3)] + [scriptblock]$Should + ) + + $expression = {Get-NetFirewallRule -DisplayName '$Target' -ErrorAction SilentlyContinue } + + $params = Get-PoshspecParam -TestName Firewall -TestExpression $expression @PSBoundParameters + + Invoke-PoshspecExpression @params +} \ No newline at end of file diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 1d892b9..305a2da 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,3 +1,7 @@ +# Version 2.1.1 +* Added Functions for + * Firewall + # Version 2.1.0 * Broke Down PSM1 to Many Different Functions in their own files [No change in functionality] * Added Functions for @@ -5,6 +9,7 @@ * CheckAppPool * WebSite * SoftwareProduct + # Version 1.2.2 * Merged PR including 5 new functions diff --git a/Tests/poshspec.Tests.ps1 b/Tests/poshspec.Tests.ps1 index 335df88..eee6219 100644 --- a/Tests/poshspec.Tests.ps1 +++ b/Tests/poshspec.Tests.ps1 @@ -313,6 +313,19 @@ Describe 'Test Functions' { $results.Expression | Should Be "Test-Path -Path `"IIS:\AppPools\TestSite`" -ErrorAction SilentlyContinue | Should be `$true" } } + + Context 'Firewall' { + $results = Firewall putty.exe Action { Should be 'Allow' } + + It "Should return the correct test Name" { + $results.Name | Should Be "Firewall property 'Action' for 'putty.exe' Should be 'Allow'" + } + + It "Should return the correct test Expression" { + $results.Expression | Should Be "Get-NetFirewallRule -DisplayName 'putty.exe' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'Action' | Should be 'Allow'" + } + } + Context 'SoftwareProduct' { diff --git a/poshspecdemo.ps1 b/poshspecdemo.ps1 index 75f2957..ac9f597 100644 --- a/poshspecdemo.ps1 +++ b/poshspecdemo.ps1 @@ -40,3 +40,10 @@ Describe 'WebSite' { CheckAppPool TestSite { Should be $True } AppPoolState TestSite { Should be Started } } + +Describe 'Firewall' { + Firewall putty.exe Enabled { Should be "$True" } + Firewall putty.exe Action { Should be 'Allow' } + Firewall putty.exe Profile { Should be 'Private' } +} +