#Import the psd1 $ModuleName = 'PoshCore' $ModuleManifestName = "$ModuleName.psd1" $ModuleManifestPath = "$PSScriptRoot\..\$ModuleManifestName" Import-Module $ModuleManifestPath -Force -ErrorAction Stop #Used to track and clean up tickets at the end $Global:TicketsCreated = @() <# Integration/Acceptance Testing for PoshCore-Ticket.ps1 Invoke-Pester –Script @{'Path' = 'C:\Githubdata\poshcore\Tests\PoshCore-Ticket.Tests.ps1';} Description: Run through all variations of attribute groups and confirm data from core is as expected #> #################### Data to Compare with Output - BEGIN #################### #region <# NA - this test will create, update and close a new test ticket so no prestaged data will be supplied. This is specific to tickets are tickets get achived and will not be in a viable state for testing. All tests are run using: #> #Used for creating the test ticket [Int]$AccountID = 1103359 [Int]$QueueId = 683 [String]$QueueNameImp = "Provisioning UK - OS" [String]$SSO = "mark4808" [Int]$Userid = "1057184" #Martins user ID [String]$UserName = "Martin Howlett" [Int]$SubcategoryId = 35354 $Devices = @(560559,560570) $AddRemoveDevices = @(502664,502667) $Module = "PoshCore" #module your testing, used in ticket creation. $Subject = "Acceptance/integration Test - $Module" $Text = "$Module Test ticket for International Windows Automation Engineers InternationalWindows-auto-engineers@rackspace.com Testing PoshCore" $AssigneeSsoName = $env:username #Tidy up var if it already exists Remove-Variable TicketID,TicketupdateText -force -ErrorAction SilentlyContinue #endregion ##################### Data to Compare with Output - END ##################### #################### Pester Tests - BEGIN #################### #region Describe "Test for Creating and Updating and finding a new ticket" { It 'New-CoreTicket - Creates new core test ticket' { $Params = @{ AccountId = $AccountID Subject = $Subject Text = $Text QueueId = $QueueID SubcategoryId = $SubcategoryId Source = "CORE Automated" Severity = "Standard" Priority = "Low" Classification = "Request for Change" Computers = $Devices AssigneeSsoName = "$AssigneeSsoName" PrivateTicket = $true } $TicketNo = New-CoreTicket @Params $Global:TicketsCreated += $TicketNo #For cleanup at the end $TicketNo | Should Match "^\d{6}-\d{5}$" #Set ticket number constant Set-Variable TicketID "$TicketNo" -Scope Global Write-Verbose "Created Test ticket = $TicketID" } It 'Find-CoreTicket - Confirms core ticket was created' { #Write-Host "TicketID = $TicketID" $Test2 = Find-CoreTicket -Tickets @($TicketID) $($Test2.number) | should be $($TicketID) } It 'Add-CoreTicketUpdate - Update core ticket adding a new message' { Set-Variable TicketupdateText "Test Add-CoreTicketUpdate - adding a private update." -Scope Global $Test3 = Add-CoreTicketUpdate -TicketNumber "$TicketID" -Text "$TicketupdateText" -Source "Other" -PrivateUpdate #Tests that adding out-stringing data works - same as WHAM uses. $Test3a = Add-CoreTicketUpdate -TicketNumber "$TicketID" -Text $($TicketupdateText | Out-String) -Source "Other" -PrivateUpdate $Test3 | should be $true $Test3a | should be $true } It 'Set-CoreTicket - Update attributes of a core ticket' { if ($env:username -notin ('mark4808', 'mart6141', 'mich8638')) { Write-Host -ForegroundColor DarkYellow "PESTER: Standard users do not have access to Provisioning queue - skipping queue change test" } else { #Sets ticket to "Provisioning UK - OS" queue sso of mark4808 and difficulty of 1 $Test3b = Set-CoreTicket -Tickets "$TicketID" -SetQueueName "$QueueNameImp" -SetAsigneeSso "$SSO" -SetDifficulty 1 $Test3b.queue | should be "success" $Test3b.difficulty | should be "success" $Test3b.assignee | should be "success" } #Sets ticket to 683 queueid, userid of 1057184 and difficulty of 2 $Test3b = $Test3b = Set-CoreTicket -Tickets "$TicketID" -SetQueueID "$QueueId" -SetAsigneeSsoId "$Userid" -SetDifficulty 2 $Test3b.queue | should be "success" $Test3b.difficulty | should be "success" $Test3b.assignee | should be "success" #Checks values set based on last change $Test3b = Find-CoreTicket -Tickets "$TicketID" -Attributes misc -CustomAttributes @{"difficulty"="difficulty"} $Test3b.assignee_name | should be "$UserName" $Test3b.queue_id | should be "$QueueId" $Test3b.difficulty | should be 2 #Sets the assigned back to the originanl as used if futher tests $Test3b = Set-CoreTicket -Tickets "$TicketID" -SetAsigneeSso "$AssigneeSsoName" $Test3b.assignee | should be "success" } It 'Find-CoreTicket - Confirms core ticket was updated' { $Test4 = Find-CoreTicket -Tickets @($TicketID) -state open -Attributes @("Messages") $Test4.messages[-1].text | Should MatchExactly "$($TicketupdateText)" } It 'Find-CoreTicket - Searches test account for test ticket, check one attribute from each group is not null' { $Test5 = Find-CoreTicket -Tickets @($TicketID) -state open -Attributes @("Account","Time","Has","Is","Category","Misc","Computers","Messages","Subtickets") $($Test5.number) | should be $TicketID $($Test5.subject) | should not be $null $($Test5.support_level) | should not be $null $($Test5.submitter) | should not be $null $($Test5.account_name) | should not be $null #Account $($Test5.last_response_date) | should not be $null #Time $($Test5.has_windows_servers) | should not be $null #Has $($Test5.is_terminal) | should not be $null #Is $($Test5.classification_name) | should not be $null #Category $($Test5.source_name) | should not be $null #Misc $($Test5.computers[-1].number) | should be $($Devices[-1]) #Computers $($Test5.messages[-1].text) | Should MatchExactly $($TicketupdateText) #Messages #$($Test5.subtickets) | should not be $null #Subtickets - commented out as no subticket function yet. Write-Verbose "Tests run against ticket = $($Test5.number)" } It 'Find-CoreTicket - Checks a search using parameters, account, AssigneeSsos, state, Subject, HasWindowsComputers' { $Test6 = Find-CoreTicket -Accounts $accountid -AssigneeSsos $AssigneeSsoName -state "open" -HasWindowsComputers -Subject $Subject $Test6.number | WHERE {$_ -eq "$TicketID"} | Should Be $TicketID } It 'Find-CoreTicket - checks CustomAttribute are returned for test ticket' { $Test7 = Find-CoreTicket -Tickets @($TicketID) -state open -CustomAttribute @{"subcategory_name"="subcategory.name"} $Test7.number | Should be $TicketID $Test7.subcategory_name | should not be $null } It 'New-CoreSubticket - adds a subticket to test ticket' { $Params = @{ Superticket = $TicketID Subject = $Subject Text = $Text QueueId = $QueueID SubcategoryId = $SubcategoryId Source = "CORE Automated" Severity = "Standard" Priority = "Low" Computers = $Devices AssigneeSsoName = "$AssigneeSsoName" PrivateTicket = $true } $SubticketID = New-CoreSubticket @Params $Global:TicketsCreated += $SubticketID $Test7a = Find-CoreTicket -Tickets @($SubticketID) -Attributes Category,Superticket,Messages,Computers $($Test7a.superticket.number) | should be $($TicketID) $Test7a.subject | Should MatchExactly $Subject $Test7a.messages[0].text | Should MatchExactly $Text $Test7a.subcategory_name | Should Not Be $null $Test7a.queue_id | Should Be 683 $Test7a.severity_name | Should MatchExactly "Standard" $Test7a.priority | Should MatchExactly "Low" [string[]]($Test7a.Computers.number | sort) | Should Be ([string[]]$Devices | sort) [bool]$Test7a.private | Should Be $true #Close now, or the subticket will prevent closing the superticket try { Set-CoreTicketStatus -TicketNumber $SubticketID -Status Closed } catch {} } } Describe "Tests for Find-CoreComputer" { It 'Find-CoreTicket - Searches test account for tickets, checks more than 10 tickets are are returned and values not null' { #Compares the device number in the pso to the one returned from core. [Array]$Test8 = Find-CoreTicket -Accounts @($AccountID) -state open $($Test8.count) -gt 10 | should be $true $($Test8[0].queue_id) | should not be $null $($Test8[0].queue_name) | should not be $null $($Test8[0].submitter) | should not be $null Write-Verbose "Number of tickets found = $($Test8.count)" Write-Verbose "Ticket checked for null values $($Test8[0].number)" } } Describe "Tests for Find-CoreAttributeValues and Find-CoreTicketQueueCategory" { It 'Find-CoreAttributeValues find class product, subclass product and searchname HP memory, ' { $Test9 = Find-CoreAttributeValues -Class "Product" -SubClass "Product" -SearchName "HP Memory" $($Test9.Description) | should not be $null $($Test9.Name) | should not be $null $($Test9.Id) | should not be $null } It 'Find-CoreTicketQueueCategory find id, checks CategoryId and CategoryName have expected values' { $Test10 = Find-CoreTicketQueueCategory -QueueId 683 -SearchID 35352 $($Test10.CategoryId) | should be "5366" $($Test10.CategoryName) | should be "Operating Services" } It 'Find-CoreTicketQueueCategory return all values, count should be greater than 100' { $Test11 = Find-CoreTicketQueueCategory -QueueId 683 -ReturnAll $($Test11.count) -gt 100 | should be $true } } Describe "Tests for Find-CoreQueue" { It 'Find-CoreQueue returns a queue ID from a queue name' { $Test12 = Find-CoreQueue -QueueNames "INTL Windows" $Test12.id | should be 683 } It 'Find-CoreQueue returns a queue name from a queue ID' { $Test13 = Find-CoreQueue -QueueIds 683 $Test13.name | should be "INTL Windows" } } Describe "Tests for Adding and Removing devices to/from a ticket" { It 'Add-CoreTicketComputer - add devices to the ticket' { $Test14 = Add-CoreTicketComputer -TicketNumber $TicketID -Computers $AddRemoveDevices -PrivateUpdate $Test14 | should be $true } It 'Add-CoreTicketComputer - confirm devices have been successfully added to the ticket' { $Test15 = Find-CoreTicket -Tickets @($TicketID) -state open -Attributes Computers #Checks if the devices are actually attached to the ticket (($($Test15.computers.number) -like $AddRemoveDevices[0]) -and ($($Test15.computers.number) -like $AddRemoveDevices[1])) | should be $true } It 'Remove-CoreTicketComputer - remove devices from the ticket' { $Test16 = Remove-CoreTicketComputer -TicketNumber $TicketID -Computers $AddRemoveDevices -PrivateUpdate $Test16 | should be $true } It 'Remove-CoreTicketComputer - confirm devices have been successfully removed from the ticket' { $Test17 = Find-CoreTicket -Tickets @($TicketID) -state open -Attributes Computers #Checks if the devices have been successfully removed from the ticket (($($Test17.computers.number) -notlike $AddRemoveDevices[0]) -and ($($Test17.computers.number) -notlike $AddRemoveDevices[1])) | should be $true } } Describe "Tests for Close-CoreTicket" { $TicketsToClose = Find-CoreTicket -Tickets $Global:TicketsCreated -State Open | select -ExpandProperty number Mock Invoke-ClosureCodeRequest {} $CloseText = "Pester is cleaning up after itself" Close-CoreTicket -Tickets $TicketsToClose -Text $CloseText -ClosureCodeProduct "Scripting" -ClosureCodeArea "Modify Powershell script" $Tickets = Find-CoreTicket -Tickets $TicketsToClose -Attributes Misc, Messages -State All It 'Close-CoreTicket closes ticket' { $Tickets | select -ExpandProperty status_name -Unique | Should Be "Confirm Solved" } It 'Close-CoreTicket adds message' { $Tickets | foreach { $_.messages[-1].text | Should BeLike $CloseText } } It 'Close-CoreTicket logs to Closure Codes' { Assert-MockCalled Invoke-ClosureCodeRequest -Times $TicketsToClose.Count } } #endregion ##################### Pester Tests - END ##################### Write-Host "Tickets created and used for test: $($TicketsCreated -join ', ')" #Cleanup Remove-Variable TicketID,TicketupdateText,TicketsCreated -force -ErrorAction SilentlyContinue #Log to appstats Try { Set-LogtoAppstats -FunctionID "PoshCore-Ticket-Tests" -AppID "PoshCore" } Catch { Write-Warning "Failed to Log to Appstats, Error message: $($_.exception.message))" }