diff --git a/Tests/Private/Invoke-ZertoRestRequest.Tests.ps1 b/Tests/Private/Invoke-ZertoRestRequest.Tests.ps1 index ffcd1dc..df40da6 100644 --- a/Tests/Private/Invoke-ZertoRestRequest.Tests.ps1 +++ b/Tests/Private/Invoke-ZertoRestRequest.Tests.ps1 @@ -10,22 +10,44 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { } Context "$global:function::Parameter Unit Tests" { - $thisCommand = get-command Invoke-ZertoRestRequest - it "has a mandatory string parameter for the uri" { - $thisCommand | Should -HaveParameter uri -Mandatory -Type String + $thisCommand = Get-Command Invoke-ZertoRestRequest + $ParameterNameTestCases = @( + @{ParameterName = "uri"; Type = "String"; Mandatory = $true; DefaultValue = $null; TestName = "URI" } + @{ParameterName = "method"; Type = "String"; Mandatory = $false; DefaultValue = 'GET'; TestName = "Method" } + @{ParameterName = "apiVersion"; Type = "String"; Mandatory = $false; DefaultValue = 'v1'; TestName = "API Version" } + @{ParameterName = "body"; Type = "String"; Mandatory = $false; DefaultValue = $null; TestName = "Body" } + @{ParameterName = "contentType"; Type = "String"; Mandatory = $false; DefaultValue = 'application/json'; TestName = "Content Type" } + @{ParameterName = "credential"; Type = "PSCredential"; Mandatory = $false; DefaultValue = $null; TestName = "Credential" } + @{ParameterName = "returnHeaders"; Type = "Switch"; Mandatory = $false; DefaultValue = $null; TestName = "Return Headers" } + ) + + It " parameter has the right Type, Default Value, and Mandatory Setting" -TestCases $ParameterNameTestCases { + param($ParameterName, $Type, $DefaultValue, $Mandatory) + if ($Mandatory) { + $thisCommand | Should -HaveParameter $ParameterName -Type $Type -Mandatory + } else { + $thisCommand | Should -HaveParameter $ParameterName -Type $Type + $thisCommand | Should -HaveParameter $ParameterName -Not -Mandatory + } + if ($null -ne $DefaultValue) { + $thisCommand | Should -HaveParameter $ParameterName -DefaultValue $DefaultValue + } } - it "uri parameter cannot be null or empty" { - $thisParameter = $thisCommand.Parameters['uri'] + $ParameterValidationTestCases = @( + @{ParameterName = "URI"; TestName = "URI" } + @{ParameterName = "apiVersion"; TestName = "Api Version" } + @{ParameterName = "body"; TestName = "Body" } + @{ParameterName = "contentType"; TestName = "Content Type" } + ) + + It " parameter cannot be null or empty" -TestCases $ParameterValidationTestCases { + param($ParameterName) + $thisParameter = $thisCommand.Parameters[$ParameterName] $thisParameter.Attributes.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should Be 1 } - it "has a non-mandatory string parameter for the method" { - $thisCommand | Should -HaveParameter method -Not -Mandatory - $thisCommand | Should -HaveParameter method -Type String - } - - it "method parameter can only be 'GET', 'POST', 'PUT', 'DELETE'" { + It "Method parameter can only be 'GET', 'POST', 'PUT', 'DELETE'" { $thisParameter = $thisCommand.Parameters['method'] $thisParameter.Attributes.Where{ $_ -is [ValidateSet] }.Count | Should Be 1 $thisParameter.Attributes.Where{ $_ -is [ValidateSet] }.validValues -contains 'GET' | Should -BeTrue @@ -34,49 +56,6 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { $thisParameter.Attributes.Where{ $_ -is [ValidateSet] }.validValues -contains 'DELETE' | Should -BeTrue $thisParameter.Attributes.Where{ $_ -is [ValidateSet] }.validValues.Count | Should -Be 4 } - - it "has a non-mandatory string parameter for the apiVersion, default 'v1'" { - $thisCommand | Should -HaveParameter apiVersion -Not -Mandatory - $thisCommand | Should -HaveParameter apiVersion -Type String - $thisCommand | Should -HaveParameter apiVersion -DefaultValue "v1" - } - - it "apiVersion parameter cannot be null or empty" { - $thisParameter = $thisCommand.Parameters['apiVersion'] - $thisParameter.Attributes.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should Be 1 - } - - it "has a non-mandatory string parameter for the body" { - $thisCommand | Should -HaveParameter body -Not -Mandatory - $thisCommand | Should -HaveParameter body -Type String - } - - it "apiVersion parameter cannot be null or empty" { - $thisParameter = $thisCommand.Parameters['body'] - $thisParameter.Attributes.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should Be 1 - } - - it "has a non-mandatory string parameter for the contentType" { - $thisCommand | Should -HaveParameter contentType -Not -Mandatory - $thisCommand | Should -HaveParameter contentType -Type String - $thisCommand | Should -HaveParameter contentType -DefaultValue "application/json" - } - - it "apiVersion parameter cannot be null or empty" { - $thisParameter = $thisCommand.Parameters['contentType'] - $thisParameter.Attributes.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should Be 1 - } - - it "has a non-mandatory PSCredential parameter for the credential" { - $thisCommand | Should -HaveParameter credential -Type PSCredential - $thisCommand | Should -HaveParameter credential -not -mandatory - } - - it "has a non-mandatory switch parameter to ReturnHeaders" { - $thisCommand | Should -HaveParameter returnHeaders -Type Switch - $thisCommand | Should -HaveParameter returnHeaders -Not -Mandatory - } - } Context "$global:function::Function Unit Tests" { @@ -86,23 +65,23 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { BeforeEach { Set-Variable -Name zvmHeaders -Scope Script -Value (@{ "Accept" = "application/json" }) - Set-Variable -Name zvmLastAction -Scope Script -Value $(Get-date).Ticks + Set-Variable -Name zvmLastAction -Scope Script -Value $(Get-Date).Ticks Set-Variable -Name zvmServer -Scope Script -Value "192.168.1.100" Set-Variable -Name zvmPort -Scope Script -Value 9669 } - it "runs when called" { + It "runs when called" { Invoke-ZertoRestRequest -uri "MyUri" | Should -Be "Ran Command" } - it "throws an error when zvmLastAction was more than 30 minutes ago" { - Set-Variable -Name zvmLastAction -Scope Script -Value $(Get-date).AddMinutes(-31).Ticks - { Invoke-ZertoRestRequest -uri "MyUri" } | Should Throw + It "throws an error when zvmLastAction was more than 30 minutes ago" { + Set-Variable -Name zvmLastAction -Scope Script -Value $(Get-Date).AddMinutes(-31).Ticks + { Invoke-ZertoRestRequest -uri "MyUri" } | Should Throw "Authorization Token has Expired" } - it "throws an error when the zvmServer variable does not exist" { + It "throws an error when the zvmServer variable does not exist" { Remove-Variable -Name zvmServer -Scope Script - { Invoke-ZertoRestRequest -uri "MyUri" } | Should Throw + { Invoke-ZertoRestRequest -uri "MyUri" } | Should Throw "Zerto Connection does not Exist." } Assert-MockCalled -CommandName Invoke-RestMethod -ModuleName ZertoApiWrapper -Exactly 1