-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow testing of the role of the authenticated user +semver: feature
- Loading branch information
Showing
17 changed files
with
330 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
|
||
function Connect- { | ||
<# | ||
.SYNOPSIS | ||
Connects to a Zendesk instance. | ||
.DESCRIPTION | ||
Connects to a Zendesk instance. Overriding any existing connection established previously with this function. | ||
.EXAMPLE | ||
PS C:\> Connect-Zendesk -Organization 'company' -Username '[email protected]' -ApiKey $ApiKey | ||
Connects to the 'company' Zendesk instance as the user '[email protected]' | ||
#> | ||
[OutputType([Boolean])] | ||
[CmdletBinding()] | ||
Param ( | ||
|
@@ -23,13 +33,7 @@ function Connect- { | |
$ApiKey | ||
) | ||
|
||
$context = Get-Connection @PSBoundParameters | ||
|
||
if (Test-Connection -context $context) { | ||
$Script:Context = $context | ||
} else { | ||
throw $Script:InvalidConnection | ||
} | ||
$Script:Context = Get-Connection @PSBoundParameters | ||
|
||
$true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
|
||
function Get-Connection { | ||
<# | ||
.SYNOPSIS | ||
Returns a Zendesk connection context | ||
.DESCRIPTION | ||
Returns an object describing a connection to a Zendesk instance | ||
.EXAMPLE | ||
PS C:\> $context = Get-ZendeskConnection -Organization 'company' -Username '[email protected]' -ApiKey $ApiKey | ||
Sets $context to a connection context for the 'company' Zendesk instance as the user '[email protected]' | ||
#> | ||
[OutputType([PSCustomObject])] | ||
[CMDletBinding()] | ||
Param ( | ||
|
@@ -27,9 +36,15 @@ function Get-Connection { | |
Organization = $Organization | ||
BaseUrl = "https://$Organization.zendesk.com" | ||
Credential = [System.Management.Automation.PSCredential]::New("$Username/token", $ApiKey) | ||
User = $null | ||
} | ||
|
||
$context | Add-Member -TypeName 'ZendeskContext' | ||
|
||
if (-not (Test-Connection -context $context)) { | ||
throw $Script:InvalidConnection | ||
} | ||
|
||
$context | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function Assert-IsAdmin { | ||
<# | ||
.SYNOPSIS | ||
Asserts that the current user is an admin. | ||
.DESCRIPTION | ||
Asserts that the current user is an admin. | ||
.EXAMPLE | ||
PS C:\> Assert-IsAdmin -Context $Context | ||
Raises and exception if current user is not an admin | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
if (-not (Test-IsAdmin -Context $Context)) { | ||
throw ($Script:InvalidRoleMessage -f 'admin', $Context.User.Role) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function Assert-IsAgent { | ||
<# | ||
.SYNOPSIS | ||
Asserts that the current user is an agent or admin. | ||
.DESCRIPTION | ||
Asserts that the current user is an agent or admin. | ||
.EXAMPLE | ||
PS C:\> Assert-IsAgent -Context $Context | ||
Raises and exception if current user is not an agent or an admin | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
if (-not (Test-IsAgent -Context $Context) -and -not (Test-IsAdmin -Context $Context)) { | ||
throw ($Script:InvalidRoleMessage -f 'agent/admin', $Context.User.Role) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function Assert-IsEndUser { | ||
<# | ||
.SYNOPSIS | ||
Asserts that the current user is an end user. | ||
.DESCRIPTION | ||
Asserts that the current user is an end user. | ||
.EXAMPLE | ||
PS C:\> Assert-IsEndUSer -Context $Context | ||
Raises and exception if current user is not an end user | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
if (-not (Test-IsEndUser -Context $Context)) { | ||
throw ($Script:InvalidRoleMessage -f 'end-user', $Context.User.Role) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function Test-IsAdmin { | ||
<# | ||
.SYNOPSIS | ||
Tests if the current user is an admin. | ||
.DESCRIPTION | ||
Tests if the current user is an admin. | ||
.EXAMPLE | ||
PS C:\> Test-IsAdmin -Context $Context | ||
Returns `$true` if the current user is an admin or `$false` otherwise. | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
# Determine the context | ||
if ($null -eq $Context) { | ||
if (Test-Path Variable:\Script:Context) { | ||
$Context = $Script:Context | ||
} else { | ||
throw $Script:NotConnectedMessage | ||
} | ||
} | ||
|
||
$Context.User.Role -eq 'admin' | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function Test-IsAgent { | ||
<# | ||
.SYNOPSIS | ||
Tests if the current user is an agent. | ||
.DESCRIPTION | ||
Tests if the current user is an agent. | ||
.EXAMPLE | ||
PS C:\> Test-IsAgent -Context $Context | ||
Returns `$true` if the current user is an agent or `$false` otherwise. | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
# Determine the context | ||
if ($null -eq $Context) { | ||
if (Test-Path Variable:\Script:Context) { | ||
$Context = $Script:Context | ||
} else { | ||
throw $Script:NotConnectedMessage | ||
} | ||
} | ||
|
||
$Context.User.Role -eq 'agent' | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function Test-IsEndUser { | ||
<# | ||
.SYNOPSIS | ||
Tests if the current user is an end user. | ||
.DESCRIPTION | ||
Tests if the current user is an end user. | ||
.EXAMPLE | ||
PS C:\> Test-IsEndUser -Context $Context | ||
Returns `$true` if the current user is an end user or `$false` otherwise. | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
# Zendesk Connection Context from `Get-ZendeskConnection` | ||
[Parameter(Mandatory = $false)] | ||
[PSTypeName('ZendeskContext')] | ||
[PSCustomObject] | ||
$Context = $null | ||
) | ||
|
||
# Determine the context | ||
if ($null -eq $Context) { | ||
if (Test-Path Variable:\Script:Context) { | ||
$Context = $Script:Context | ||
} else { | ||
throw $Script:NotConnectedMessage | ||
} | ||
} | ||
|
||
$Context.User.Role -eq 'end-user' | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] | ||
Param() | ||
|
||
Import-Module "$PSScriptRoot/../PwshZendesk.psm1" -Force | ||
|
||
Describe 'Users Routes' { | ||
|
||
InModuleScope PwshZendesk { | ||
|
||
$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' | ||
|
||
Context 'Admin' { | ||
|
||
$context.User.role = 'admin' | ||
|
||
It 'Test-IsAdmin => $true' { | ||
Test-IsAdmin -Context $context | Should -Be $true | ||
} | ||
|
||
It 'Test-IsAgent => $false' { | ||
Test-IsAgent -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Test-IsEndUser => $false' { | ||
Test-IsEndUser -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Assert-IsAdmin passes' { | ||
{ Assert-IsAdmin -Context $context } | Should -Not -Throw | ||
} | ||
|
||
It 'Assert-IsAgent passes' { | ||
{ Assert-IsAgent -Context $context } | Should -Not -Throw | ||
} | ||
|
||
It 'Assert-IsEndUser throws' { | ||
{ Assert-IsEndUser -Context $context } | Should -Throw | ||
} | ||
|
||
} | ||
|
||
Context 'Agent' { | ||
|
||
$context.User.role = 'agent' | ||
|
||
It 'Test-IsAdmin => $false' { | ||
Test-IsAdmin -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Test-IsAgent => $true' { | ||
Test-IsAgent -Context $context | Should -Be $true | ||
} | ||
|
||
It 'Test-IsEndUser => $false' { | ||
Test-IsEndUser -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Assert-IsAdmin throw' { | ||
{ Assert-IsAdmin -Context $context } | Should -Throw | ||
} | ||
|
||
It 'Assert-IsAgent passes' { | ||
{ Assert-IsAgent -Context $context } | Should -Not -Throw | ||
} | ||
|
||
It 'Assert-IsEndUser throws' { | ||
{ Assert-IsEndUser -Context $context } | Should -Throw | ||
} | ||
|
||
} | ||
|
||
Context 'End User' { | ||
|
||
$context.User.role = 'end-user' | ||
|
||
It 'Test-IsAdmin => $false' { | ||
Test-IsAdmin -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Test-IsAgent => $false' { | ||
Test-IsAgent -Context $context | Should -Be $false | ||
} | ||
|
||
It 'Test-IsEndUser => $true' { | ||
Test-IsEndUser -Context $context | Should -Be $true | ||
} | ||
|
||
It 'Assert-IsAdmin throws' { | ||
{ Assert-IsAdmin -Context $context } | Should -Throw | ||
} | ||
|
||
It 'Assert-IsAgent throws' { | ||
{ Assert-IsAgent -Context $context } | Should -Throw | ||
} | ||
|
||
It 'Assert-IsEndUser passes' { | ||
{ Assert-IsEndUser -Context $context } | Should -Not -Throw | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.