tests/Routes-TicketComments.tests.ps1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] Param() Import-Module "$PSScriptRoot/../PwshZendesk.psm1" -Force Describe 'Groups Routes' { InModuleScope PwshZendesk { $IsInteractive = [Environment]::GetCommandLineArgs() -join ' ' -notmatch '-NonI|\.ps1' $context = @{ Organization = 'company' BaseUrl = 'https://company.testdesk.com' Credential = [System.Management.Automation.PSCredential]::New('email', ('api-key' | ConvertTo-SecureString -AsPlainText -Force)) User = [PSCustomObject]@{ role = '' } } $context | Add-Member -TypeName 'ZendeskContext' Mock -ModuleName PwshZendesk Invoke-RestMethod { [PSCustomObject]@{ comment = $null; comments = $null } } Context 'List Comments' { It 'Matches the endpoint' { if ($IsInteractive) { throw 'Please run test in non-interactive mode' } $context.User.role = 'admin' { Get-Comment -Context $context -TicketId 1 } | Should -Not -Throw Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/tickets/\d+/comments\.json' } -Scope It } It 'Does not allow end users to call' { $context.User.role = 'end-user' { Get-Comment -Context $context -TicketId 1 } | Should -Throw 'Authenticated user must have role' } It 'Allows agents to call' { $context.User.role = 'agent' { Get-Comment -Context $context -TicketId 1 } | Should -Not -Throw } It 'Allows admins to call' { $context.User.role = 'admin' { Get-Comment -Context $context -TicketId 1 } | Should -Not -Throw } } Context 'Redact String in Comment' { It 'Matches the endpoint' { if ($IsInteractive) { throw 'Please run test in non-interactive mode' } $context.User.role = 'admin' { Invoke-CommentRedaction -Context $context -TicketId 1 -Id 2 -Text 'p@ssword' -confirm:$false } | Should -Not -Throw Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Put' -and $Uri -match '/api/v2/tickets/\d+/comments/\d+/redact\.json' } -Scope It } It 'Does not allow end users to call' { $context.User.role = 'end-user' { Invoke-CommentRedaction -Context $context -TicketId 1 -Id 2 -Text 'p@ssword' -confirm:$false } | Should -Throw 'Authenticated user must have role' } It 'Allows agents to call' { $context.User.role = 'agent' { Invoke-CommentRedaction -Context $context -TicketId 1 -Id 2 -Text 'p@ssword' -confirm:$false } | Should -Not -Throw } It 'Allows admins to call' { $context.User.role = 'admin' { Invoke-CommentRedaction -Context $context -TicketId 1 -Id 2 -Text 'p@ssword' -confirm:$false } | Should -Not -Throw } } Context 'Make Comment Private' { It 'Matches the endpoint' { if ($IsInteractive) { throw 'Please run test in non-interactive mode' } $context.User.role = 'admin' { Hide-Comment -Context $context -TicketId 1 -Id 2 -Confirm:$false } | Should -Not -Throw Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Put' -and $Uri -match '/api/v2/tickets/\d+/comments/\d+/make_private.json' } -Scope It } It 'Does not allow end users to call' { $context.User.role = 'end-user' { Hide-Comment -Context $context -TicketId 1 -Id 2 -Confirm:$false } | Should -Throw 'Authenticated user must have role' } It 'Allows agents to call' { $context.User.role = 'agent' { Hide-Comment -Context $context -TicketId 1 -Id 2 -Confirm:$false } | Should -Not -Throw } It 'Allows admins to call' { $context.User.role = 'admin' { Hide-Comment -Context $context -TicketId 1 -Id 2 -Confirm:$false } | Should -Not -Throw } } } } |