Skip to content

Commit

Permalink
Adds Invoke-CommentRedaction and other comment function updates
Browse files Browse the repository at this point in the history
  • Loading branch information
RobFaie committed Feb 19, 2020
1 parent 284c179 commit c861d94
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
1 change: 1 addition & 0 deletions PwshZendesk.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
'Get-UserRelated', 'Get-ZendeskUserRelated'
'Hide-Comment', 'Hide-ZendeskComment'
'Import-Ticket', 'Import-ZendeskTicket'
'Invoke-CommentRedaction', 'Invoke-ZendeskCommentRedaction'
'Invoke-Method', 'Invoke-ZendeskMethod'
'Merge-Ticket', 'Merge-ZendeskTicket'
'Merge-User', 'Merge-ZendeskUser'
Expand Down
2 changes: 2 additions & 0 deletions functions/Get-Comment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function Get-Comment {
$Context = $null
)

Assert-IsAgent -Context $Context

$path = "/api/v2/tickets/$TicketId/comments.json"

$result = Invoke-Method -Context $Context -Path $path -Verbose:$VerbosePreference
Expand Down
10 changes: 7 additions & 3 deletions functions/Hide-Comment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function Hide-Comment {

[OutputType([String])]
[CmdletBinding()]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# Unique Id of the comment to hide
Expand All @@ -24,8 +24,12 @@ function Hide-Comment {
$Context = $null
)

Assert-IsAgent -Context $Context

$path = "/api/v2/tickets/$TicketId/comments/$Id/make_private.json"

$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference
$result
if ($PSCmdlet.ShouldProcess($Id, 'Make comment private')) {
$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference
$result
}
}
44 changes: 44 additions & 0 deletions functions/Invoke-CommentRedaction.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

function Invoke-CommentRedaction {

[OutputType([PSCustomObject])]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Param (

# Unique Id of the ticket the comment belongs to
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$TicketId,

# Unique Id of the comment to redact text in
[Parameter(Mandatory = $true)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$Id,

# Text to redact
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$Text,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

Assert-IsAgent -Context $Context

$path = "/api/v2/tickets/$TicketId/comments/$Id/redact.json"
$body = @{
text = $Text
}

if ($PSCmdlet.ShouldProcess($Id, 'Redact comment text')) {
$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Body $body -Verbose:$VerbosePreference
$result
}
}
117 changes: 117 additions & 0 deletions tests/Routes-TicketComments.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
Param()

Import-Module "$PSScriptRoot/../PwshZendesk.psm1" -Force

Describe 'Groups Routes' {

InModuleScope PwshZendesk {

$IsInteractive = [Environment]::GetCommandLineArgs() -join ' ' -notmatch '-NonI'

$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
}
}

}

}

0 comments on commit c861d94

Please sign in to comment.