Skip to content

Commit

Permalink
Merge pull request #17 from Readify/search
Browse files Browse the repository at this point in the history
Adds role assertions and route tests for search functions
  • Loading branch information
RobFaie authored Feb 19, 2020
2 parents bc72a59 + 23cf96f commit 0f6cd09
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions functions/Get-SearchCount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function Get-SearchCount {
$Context = $null
)

Assert-IsAgent -Context $Context

Write-Debug -Message "Query: $Query"
$Query = [uri]::EscapeDataString($Query)
Write-Debug -Message "Escaped Query: $Query"
Expand Down
4 changes: 3 additions & 1 deletion functions/Search-.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ function Search- {
$Context = $null
)

Assert-IsAgent -Context $Context

Write-Debug -Message "Query: $Query"
$Query = [uri]::EscapeDataString($Query)
Write-Debug -Message "Escaped Query: $Query"

$results = Invoke-Method -Context $Context -Path "/api/v2/search.json?query=$Query"
$results = Invoke-Method -Context $Context -Path "/api/v2/search.json?query=$Query" -Verbose:$VerbosePreference
$results.results

}
89 changes: 89 additions & 0 deletions tests/Routes-Search.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
Param()

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

Describe 'Search 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]@{ results = $null; count = 0 } }

Context 'List Search Results' {

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

$context.User.role = 'agent'

{ Search- -Context $context -Query 'type:ticket' } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/search\.json\?query=' } -Scope It
}

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

{ Search- -Context $context -Query 'type:ticket' } | Should -Throw 'Authenticated user must have role'
}

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

{ Search- -Context $context -Query 'type:ticket' } | Should -Not -Throw
}

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

{ Search- -Context $context -Query 'type:ticket' } | Should -Not -Throw
}

}

Context 'Show Results Count' {

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

$context.User.role = 'agent'

{ Get-SearchCount -Context $context -Query 'type:ticket' } | Should -Not -Throw
Assert-MockCalled Invoke-RestMethod -Exactly 1 -ParameterFilter { $Method -eq 'Get' -and $Uri -match '/api/v2/search/count\.json\?query=' } -Scope It
}

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

{ Get-SearchCount -Context $context -Query 'type:ticket' } | Should -Throw 'Authenticated user must have role'
}

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

{ Get-SearchCount -Context $context -Query 'type:ticket' } | Should -Not -Throw
}

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

{ Get-SearchCount -Context $context -Query 'type:ticket' } | Should -Not -Throw
}
}

}

}

0 comments on commit 0f6cd09

Please sign in to comment.