Skip to content

Commit

Permalink
Merge pull request #16 from Readify/suspended-tickets
Browse files Browse the repository at this point in the history
Added role assertions, route tests, and bug fixes for suspended tickets
  • Loading branch information
RobFaie authored Feb 19, 2020
2 parents c797fc0 + fc355ef commit 5d95f7b
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 5 deletions.
10 changes: 6 additions & 4 deletions functions/Get-SuspendedTicket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ function Get-SuspendedTicket {
$Context = $null
)

if ($PSBoundParameters.containsKey('UserId')) {
$path = "/api/v2/suspended_tickets/{id}.json"
$key = 'ticket'
Assert-IsAgent -Context $Context

if ($PSBoundParameters.containsKey('Id')) {
$path = "/api/v2/suspended_tickets/$Id.json"
$key = 'suspended_ticket'
} else {
$path = '/api/v2/suspended_tickets.json'
$key = 'tickets'
$key = 'suspended_tickets'
}

$result = Invoke-Method -Context $Context -Path $path -Verbose:$VerbosePreference
Expand Down
4 changes: 3 additions & 1 deletion functions/Remove-SuspendedTicket.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function Get-SuspendedTicket {
function Remove-SuspendedTicket {

[OutputType([PSCustomObject])]
[CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
Expand All @@ -18,6 +18,8 @@ function Get-SuspendedTicket {
$Context = $null
)

Assert-IsAgent -Context $Context

if ($Id.Count -gt 1) {
$ids = $Id -join ','
$path = "/api/v2/suspended_tickets/destroy_many.json?ids=$ids"
Expand Down
2 changes: 2 additions & 0 deletions functions/Restore-SuspendedTicket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function Restore-SuspendedTicket {
$Context = $null
)

Assert-IsAgent -Context $Context

if ($Id.Count -gt 1) {
$ids = $Id -join ','
$path = "/api/v2/suspended_tickets/recover_many.json?ids=$ids"
Expand Down
210 changes: 210 additions & 0 deletions tests/Routes-SuspendedTickets.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
[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]@{ suspended_ticket = $null; suspended_tickets = $null } }

Context 'List Suspended Tickets' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Get-SuspendedTicket -Context $context } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/suspended_tickets.json' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Get-SuspendedTicket -Context $context } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Get-SuspendedTicket -Context $context } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Get-SuspendedTicket -Context $context } | Should -Not -Throw
}
} # GET /api/v2/suspended_tickets.json

Context 'Show Suspended Ticket' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Get-SuspendedTicket -Context $context -Id 1 } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/suspended_tickets/\d+\.json' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Get-SuspendedTicket -Context $context -Id 1 } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Get-SuspendedTicket -Context $context -Id 1 } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Get-SuspendedTicket -Context $context -Id 1 } | Should -Not -Throw
}
} # GET /api/v2/suspended_tickets/{id}.json

Context 'Recover Suspended Ticket' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Restore-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Put' -and $Uri -match '/api/v2/suspended_tickets/\d+/recover\.json' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Restore-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Restore-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Restore-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
}
} # PUT /api/v2/suspended_tickets/{id}/recover.json

Context 'Recover Multiple Suspended Tickets' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Restore-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Put' -and $Uri -match '/api/v2/suspended_tickets/recover_many\.json\?ids=' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Restore-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Restore-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Restore-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
}
} # PUT /api/v2/suspended_tickets/recover_many.json?ids={id1},{id2}

Context 'Delete Suspended Ticket' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Remove-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Delete' -and $Uri -match '/api/v2/suspended_tickets/\d+\.json' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Remove-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Remove-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Remove-SuspendedTicket -Context $context -Id 1 -Confirm:$false } | Should -Not -Throw
}
} # DELETE /api/v2/suspended_tickets/{id}.json

Context 'Delete Multiple Suspended Tickets' {
It 'Matches the endpoint' {
if ($IsInteractive) {
throw 'Please run test in non-interactive mode'
}

$context.User.role = 'admin'

{ Remove-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Delete' -and $Uri -match '/api/v2/suspended_tickets/destroy_many\.json\?ids=' } -Scope It
}

It 'Does not allow end users to call' {
$context.User.role = 'end-user'

{ Remove-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Throw 'Authenticated user must have role'
}

It 'Allows agents to call' {
$context.User.role = 'agent'

{ Remove-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
}

It 'Allows admins to call' {
$context.User.role = 'admin'

{ Remove-SuspendedTicket -Context $context -Id @(1..5) -Confirm:$false } | Should -Not -Throw
}
} # DELETE /api/v2/suspended_tickets/destroy_many.json?ids={id1},{id2}

}

}

0 comments on commit 5d95f7b

Please sign in to comment.