Skip to content

Commit

Permalink
Merge pull request #23 from Readify/job-statuses
Browse files Browse the repository at this point in the history
Adds role assertion and route tests for Job Status function
  • Loading branch information
RobFaie authored Feb 20, 2020
2 parents bbe8b58 + 33a9a2b commit 5c99379
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions functions/Get-JobStatus.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function Get-JobStatus {
$Context = $null
)

Assert-IsAgent -Context $Context

$key = 'job_statuses'

if ($PSBoundParameters.ContainsKey('Id')) {
Expand Down
117 changes: 117 additions & 0 deletions tests/Routes-JobStatuses.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 'Job Status 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]@{ job_status = $null; job_statuses = $null } }

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

$context.User.role = 'admin'

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

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

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

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

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

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

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

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

$context.User.role = 'admin'

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

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

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

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

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

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

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

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

$context.User.role = 'admin'

{ Get-JobStatus -Context $context -Id @(1..5) } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/job_statuses/show_many\.json\?ids=' } -Scope It
}

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

{ Get-JobStatus -Context $context -Id @(1..5) } | Should -Throw 'Authenticated user must have role'
}

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

{ Get-JobStatus -Context $context -Id @(1..5) } | Should -Not -Throw
}

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

{ Get-JobStatus -Context $context -Id @(1..5) } | Should -Not -Throw
}
}

}

}

0 comments on commit 5c99379

Please sign in to comment.