From 6b05af2db427d464cc56fc4944d658230bec4a52 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 16 Jul 2019 14:52:22 -0400 Subject: [PATCH] Update Invoke-ZARestRequest.Tests.ps1 --- Tests/Private/Invoke-ZARestRequest.Tests.ps1 | 106 ++++++++++++++++--- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/Tests/Private/Invoke-ZARestRequest.Tests.ps1 b/Tests/Private/Invoke-ZARestRequest.Tests.ps1 index 984d682..b76f320 100644 --- a/Tests/Private/Invoke-ZARestRequest.Tests.ps1 +++ b/Tests/Private/Invoke-ZARestRequest.Tests.ps1 @@ -1,17 +1,95 @@ -$filePath = (Split-Path -Parent $MyInvocation.MyCommand.Path) -replace 'Tests', 'ZertoApiWrapper' -$fileName = (Split-Path -Leaf $MyInvocation.MyCommand.Path ) -replace '.Tests.', '.' -$file = Get-ChildItem "$filePath\$fileName" -. $file.FullName +#Requires -Modules Pester +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag Unit { - it "file should exist" { - $file.FullName | should exist - } +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file.FullName -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 + InModuleScope -ModuleName ZertoApiWrapper { + + 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 + } + + 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 "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 "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" + } + + 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" { + $parameterInfo = ( Get-Command Invoke-ZARestRequest ).Parameters['method'] + $parameterInfo.Attributes.Where{ $_ -is [ValidateSet] }.Count | Should -Be 1 + $validateSet = $parameterInfo.Attributes.Where{ $_ -is [ValidateSet] } + $validateSet.ValidValues -contains 'GET' | Should -BeTrue + $validateSet.ValidValues -contains 'PUT' | Should -BeTrue + $validateSet.ValidValues -contains 'POST' | Should -BeTrue + $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" { + + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-RestMethod { + "Ran Command" + } + + BeforeEach { + Set-Variable -Name zaHeaders -Scope Script -Value (@{ "Accept" = "application/json" }) + Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks + } + + 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 zaHeaders variable does not exits" { + Remove-Variable -Name zaHeaders -Scope Script + { Invoke-ZARestRequest -uri "myuri" } | Should Throw + } + + it "throws when the zaLastActionTime variable does not exits" { + Remove-Variable -Name zaLastActionTime -Scope Script + { Invoke-ZARestRequest -uri "myuri" } | Should Throw + } + + Assert-MockCalled -CommandName Invoke-RestMethod -ModuleName ZertoApiWrapper -Exactly 1 + } } -} \ No newline at end of file +} + +Remove-Variable -Name function -Scope Global +Remove-Variable -Name here -Scope Global