diff --git a/Tests/Private/Invoke-ZARestRequest.Tests.ps1 b/Tests/Private/Invoke-ZARestRequest.Tests.ps1 index b76f320..78bb6f3 100644 --- a/Tests/Private/Invoke-ZARestRequest.Tests.ps1 +++ b/Tests/Private/Invoke-ZARestRequest.Tests.ps1 @@ -8,33 +8,44 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { Context "$global:function::Parameter Unit Tests" { - it "have a mandatory string parameter for the URI Variable" { - Get-Command Invoke-ZARestRequest | Should -HaveParameter uri -Mandatory -Type String + $testCases = @( + @{ParameterName = 'uri'; Type = 'String'; Mandatory = $true; TestName = 'URI' } + @{ParameterName = 'method'; Type = 'String'; Mandatory = $false; TestName = 'Method' } + @{ParameterName = 'body'; Type = 'String'; Mandatory = $false; TestName = 'Body' } + @{ParameterName = 'contentType'; Type = 'String'; Mandatory = $false; TestName = 'contentType' } + ) + + It "Parameter present and Type test for: " -TestCases $testCases { + param($parameterName, $type, $Mandatory) + Get-Command $global:function | Should -HaveParameter $parameterName -Type $type + if ($Mandatory) { + Get-Command $global:function | Should -HaveParameter $parameterName -Mandatory + } else { + Get-Command $global:function | Should -HaveParameter $parameterName -Not -Mandatory + } } - it "have a non-mandatory string parameter for the Method Variable, default to 'GET'" { - Get-Command Invoke-ZARestRequest | Should -HaveParameter Method -Type String - Get-Command Invoke-ZARestRequest | Should -HaveParameter Method -Not -Mandatory - Get-Command Invoke-ZARestRequest | Should -HaveParameter Method -DefaultValue "GET" + It "Method parameter default is 'GET'" { + Get-Command $global:function | Should -HaveParameter Method -DefaultValue "GET" } - it "have a non-mandatory string parameter for the Body variable" { - Get-Command Invoke-ZARestRequest | Should -HaveParameter Body -Type String - Get-Command Invoke-ZARestRequest | Should -HaveParameter Body -Not -Mandatory + It "ContentType parameter default is 'application/json'" { + Get-Command $global:function | Should -HaveParameter contentType -DefaultValue "application/json" } - it "have a non-mandatory string parameter for the contentType variable" { - Get-Command Invoke-ZARestRequest | Should -HaveParameter contentType -Type String - Get-Command Invoke-ZARestRequest | Should -HaveParameter contentType -Not -Mandatory - Get-Command Invoke-ZARestRequest | Should -HaveParameter contentType -DefaultValue "application/json" + $NotNullOrEmptyTests = @( + @{ParameterName = 'uri'; TestName = 'Uri' } + @{ParameterName = 'body'; TestName = 'Body' } + @{ParameterName = 'contentType'; TestName = 'ContentType' } + ) + + It " parameter does not accecpt a null or empty value" -TestCases $NotNullOrEmptyTests { + param($parameterName) + $parameterInfo = ( Get-Command Invoke-ZARestRequest ).Parameters[$parameterName] + $parameterInfo.Attributes.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 } - it "uri variable does not accecpt a null or empty variable" { - { Invoke-ZARestRequest -uri $null } | Should Throw - { Invoke-ZARestRequest -uri "" } | Should Throw - } - - it "method variable only accecpts 'GET' 'DELETE' 'PUT' 'POST' values" { + It "method parametert only accecpts 'GET' 'DELETE' 'PUT' 'POST' values" { $parameterInfo = ( Get-Command Invoke-ZARestRequest ).Parameters['method'] $parameterInfo.Attributes.Where{ $_ -is [ValidateSet] }.Count | Should -Be 1 $validateSet = $parameterInfo.Attributes.Where{ $_ -is [ValidateSet] } @@ -44,16 +55,6 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { $validateSet.ValidValues -contains 'DELETE' | Should -BeTrue $validateSet.ValidValues.Count | Should -Be 4 } - - it "body variable does not accecpt a null or empty variable" { - { Invoke-ZARestRequest -uri "connect" -body $null } | Should Throw - { Invoke-ZARestRequest -uri "connect" -body "" } | Should Throw - } - - it "accecpt variable does not accecpt a null or empty variable" { - { Invoke-ZARestRequest -uri "connect" -accecpt $null } | Should Throw - { Invoke-ZARestRequest -uri "connect" -accecpt "" } | Should Throw - } } Context "$global:function::Function Unit Tests" { @@ -64,26 +65,26 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { BeforeEach { Set-Variable -Name zaHeaders -Scope Script -Value (@{ "Accept" = "application/json" }) - Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks + Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-Date).Ticks } - it "runs when called" { + It "runs when called" { Invoke-ZARestRequest -uri "myuri" | Should Be "Ran Command" } - it "throws when the last action was over 60 minutes ago" { - $script:zaLastActionTime = (get-date).AddMinutes(-61).Ticks - { Invoke-ZARestRequest -uri "myuri" } | Should Throw + It "throws when the last action was over 60 minutes ago" { + $script:zaLastActionTime = (Get-Date).AddMinutes(-61).Ticks + { Invoke-ZARestRequest -uri "myuri" } | Should Throw "Authorization Token has Expired." } - it "throws when the zaHeaders variable does not exits" { + It "throws when the zaHeaders variable does not exits" { Remove-Variable -Name zaHeaders -Scope Script - { Invoke-ZARestRequest -uri "myuri" } | Should Throw + { Invoke-ZARestRequest -uri "myuri" } | Should Throw "Zerto Analytics Connection does not Exist." } - it "throws when the zaLastActionTime variable does not exits" { + It "throws when the zaLastActionTime variable does not exist" { Remove-Variable -Name zaLastActionTime -Scope Script - { Invoke-ZARestRequest -uri "myuri" } | Should Throw + { Invoke-ZARestRequest -uri "myuri" } | Should Throw "Zerto Analytics Connection does not Exist." } Assert-MockCalled -CommandName Invoke-RestMethod -ModuleName ZertoApiWrapper -Exactly 1