#Import the psd1 $ModuleName = 'PoshCore' $ModuleManifestName = "$ModuleName.psd1" $ModuleManifestPath = "$PSScriptRoot\..\$ModuleManifestName" Import-Module $ModuleManifestPath -Force -ErrorAction Stop <# Integration/Acceptance Testing for PoshCore-CoreQuery.ps1 Invoke-Pester –Script @{'Path' = 'C:\Githubdata\poshcore\Tests\PoshCore-CoreQuery.Tests.ps1';} Description: Run through the three variations of data than needs to be exspanded using function Convertfrom-Subattributes which is in PoshCore-CoreQuery.ps1 Note: Help for sku expantion in Convertfrom-Subattributes covers details of how it works and the data it should retrun. #> #################### Data to Compare with Output - BEGIN #################### #region <# Ticket must have subtickets and computers attached. Note: When a methord for adding subtickets is created, we can have this test created an new ticket and assign a subticket. When a methord for closing a ticket is created we can have this test close the ticket ticket after creation. #> $Ticket = "180806-04087" $Device = "560570" #device attached to ticket above $Subticket = "180806-04107" #subticket of ticket above $Account = "1103359" #endregion ##################### Data to Compare with Output - END ##################### #################### Pester Tests - BEGIN #################### #region Describe "Convertfrom-Subattributes - tests to check data is expanded correctly" { It 'Find-CoreTicket - expand subattributes Computers, checks computers returned and device id present' { $Test1 = Find-CoreTicket -State all -Tickets @("$Ticket") -Attributes Computers ($Test1.computers).count -gt 0 | should be $true $Test1.computers.number | where-object {$_ -eq "$device"} | Should be $device } It 'Find-CoreTicket - expand subattributes Subtickets, check device returned and device id present' { $Test2 = Find-CoreTicket -State all -Tickets @("$Ticket") -Attributes Subtickets ($Test2.subtickets).count -gt 0 | should be $true $Test2.subtickets.number | where-object {$_ -eq "$Subticket"} | Should be $Subticket } It 'Find-CoreTicket - expand subattributes Skus, check skus returned and first sku Label and SKU are not null' { $Test3 = Find-CoreComputer -account $Account -Attributes Skus ($Test3[0].skus).count -gt 0 | should be $true $Test3[0].skus[0].Label | should not be $null $Test3[0].skus[0].SKU | should not be $null } } Describe "Encoding and escaping" { Context "Allowable characters" { It "Escapes only newline, '\' and '`"'" { #10 and 13 are the newline chars. 33 up to about 128 are letters and numbers. Mostly, 128 up to 255 is letters with accents. $PrintableAscii = (10, 13, (33..255) | foreach {$_} | foreach {[char]$_}) -join '' $CorrectlyEscapedAscii = $PrintableAscii -replace '\\', '\\' -replace '"', '\"' -replace '\r', '\r' -replace '\n', '\n' $TestHashtable = @{ String = $PrintableAscii; Array = $PrintableAscii.ToCharArray() } Mock Invoke-RestMethod -ModuleName PoshCore -MockWith {return $Body} $Module = Get-Module PoshCore $PassedToApi = & $Module {Invoke-CoreQuery -JsonHashtable $args[0]} $TestHashtable #This executes the scriptblock in the module's scope, which is how we can access a private function. Bear in mind this hits the mock, so we should be getting back the JSON we would be sending to the API $PassedToApi -match '(?<=String":\s*").*(?=",)' | Should Be $true #matches stuff that follows 'String: ' up until ',' $Matches.0 | Should BeExactly $CorrectlyEscapedAscii try { #We convert back from JSON to test round trip fidelity $ConvertedBack = $PassedToApi | ConvertFrom-Json -ErrorAction Stop } catch { #Since Pester will show this to the user, it's possibly helpful to set the values like this $ConvertedBack = @{String="Conversion from JSON failed"; Array = ('F', 'A', 'I', 'L')} } $ConvertedBack.String | Should BeExactly $PrintableAscii $ConvertedBack.Array -join '' | Should BeExactly $PrintableAscii } } } #endregion ##################### Pester Tests - END ##################### #Log to appstats Try { Set-LogtoAppstats -FunctionID "PoshCore-Query-Tests" -AppID "PoshCore" } Catch { Write-Warning "Failed to Log to Appstats, Error message: $($_.exception.message))" }