Skip to content

Commit

Permalink
Merge pull request #166 from pspete/feature_session-info
Browse files Browse the repository at this point in the history
Add Session Functions
  • Loading branch information
pspete committed Jun 17, 2019
2 parents 867b82a + e815ed1 commit b991be9
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Tests/Add-PASAccount.Tests.ps1
Expand Up @@ -118,6 +118,7 @@ Describe $FunctionName {
"remoteMachines" = "someMachine"
"accessRestrictedToRemoteMachines" = $false
}
$Script:ExternalVersion = "0.0"

}

Expand Down Expand Up @@ -238,7 +239,6 @@ Describe $FunctionName {
}

It "throws error if version requirement not met" {
$Script:ExternalVersion = "1.0"

$Script:ExternalVersion = "1.0"
{ $InputObjV10 | Add-PASAccount } | Should Throw
Expand Down Expand Up @@ -272,6 +272,7 @@ $Script:ExternalVersion = "1.0"
"automaticManagementEnabled" = $true
"remoteMachines" = "someMachine"
}
$Script:ExternalVersion = "0.0"

}

Expand Down
4 changes: 4 additions & 0 deletions Tests/Close-PASSession.Tests.ps1
Expand Up @@ -42,6 +42,10 @@ Describe $FunctionName {

}

Mock Clear-Variable -MockWith {

}

$response = Close-PASSession -UseV9API -verbose

Context "Input" {
Expand Down
71 changes: 71 additions & 0 deletions Tests/Get-PASSession.Tests.ps1
@@ -0,0 +1,71 @@
#Get Current Directory
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path

#Get Function Name
$FunctionName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1"

#Assume ModuleName from Repository Root folder
$ModuleName = Split-Path (Split-Path $Here -Parent) -Leaf

#Resolve Path to Module Directory
$ModulePath = Resolve-Path "$Here\..\$ModuleName"

#Define Path to Module Manifest
$ManifestPath = Join-Path "$ModulePath" "$ModuleName.psd1"

if ( -not (Get-Module -Name $ModuleName -All)) {

Import-Module -Name "$ManifestPath" -ArgumentList $true -Force -ErrorAction Stop

}

BeforeAll {

$Script:RequestBody = $null
$Script:BaseURI = "https://SomeURL/SomeApp"
$Script:ExternalVersion = "0.0"
$Script:WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession

}

AfterAll {

$Script:RequestBody = $null

}

Describe $FunctionName {

InModuleScope $ModuleName {

Mock Get-PASLoggedOnUser -MockWith {
[PSCustomObject]@{"Username" = "SomeUser"; "Prop2" = "Val2" }
}

$response = Get-PASSession

Context "Standard Operation" {

it "provides output" {

$response | Should not BeNullOrEmpty

}

It "has output with expected number of properties" {

($response | Get-Member -MemberType NoteProperty).length | Should Be 4

}

it "outputs object with expected typename" {

$response | get-member | select-object -expandproperty typename -Unique | Should Be psPAS.CyberArk.Vault.Session

}

}

}

}
2 changes: 1 addition & 1 deletion Tests/Remove-PASAccount.Tests.ps1
Expand Up @@ -97,7 +97,7 @@ Describe $FunctionName {
}

Context "Input V10 API" {

$Script:ExternalVersion = "0.0"
$response = $InputObj | Remove-PASAccount

It "sends request" {
Expand Down
80 changes: 40 additions & 40 deletions Tests/Remove-PASDirectory.Tests.ps1
Expand Up @@ -13,7 +13,7 @@ $ModulePath = Resolve-Path "$Here\..\$ModuleName"
#Define Path to Module Manifest
$ManifestPath = Join-Path "$ModulePath" "$ModuleName.psd1"

if( -not (Get-Module -Name $ModuleName -All)) {
if ( -not (Get-Module -Name $ModuleName -All)) {

Import-Module -Name "$ManifestPath" -ArgumentList $true -Force -ErrorAction Stop

Expand Down Expand Up @@ -48,76 +48,76 @@ Describe $FunctionName {

(Get-Command Remove-PASDirectory).Parameters["$Parameter"].Attributes.Mandatory | Should Be $true

}

}
}

Context "Input" {
}

BeforeEach {
Context "Input" {

Mock Invoke-PASRestMethod -MockWith { }
BeforeEach {
$Script:ExternalVersion = "0.0"
Mock Invoke-PASRestMethod -MockWith { }

$response = Remove-PASDirectory -id SomeDir
$response = Remove-PASDirectory -id SomeDir

}
}

It "sends request" {
It "sends request" {

Assert-MockCalled Invoke-PASRestMethod -Times 1 -Exactly -Scope It
Assert-MockCalled Invoke-PASRestMethod -Times 1 -Exactly -Scope It

}
}

It "sends request to expected endpoint" {
It "sends request to expected endpoint" {

Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter {

$URI -eq "$($Script:BaseURI)/api/Configuration/LDAP/Directories/SomeDir"
$URI -eq "$($Script:BaseURI)/api/Configuration/LDAP/Directories/SomeDir"

} -Times 1 -Exactly -Scope It
} -Times 1 -Exactly -Scope It

}
}

It "uses expected method" {
It "uses expected method" {

Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { $Method -match 'DELETE' } -Times 1 -Exactly -Scope It
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { $Method -match 'DELETE' } -Times 1 -Exactly -Scope It

}
}

It "sends request with no body" {
It "sends request with no body" {

Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { $Body -eq $null } -Times 1 -Exactly -Scope It
Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { $Body -eq $null } -Times 1 -Exactly -Scope It

}
}

It "throws error if version requirement not met" {
$Script:ExternalVersion = "1.0"
{ Get-PASDirectory } | Should Throw
$Script:ExternalVersion = "0.0"
}
It "throws error if version requirement not met" {
$Script:ExternalVersion = "1.0"
{ Get-PASDirectory } | Should Throw
$Script:ExternalVersion = "0.0"
}

}
}

Context "Output" {
Context "Output" {

BeforeEach {
BeforeEach {
$Script:ExternalVersion = "0.0"
Mock Invoke-PASRestMethod -MockWith { }

Mock Invoke-PASRestMethod -MockWith { }
$response = Remove-PASDirectory -id SomeDir

$response = Remove-PASDirectory -id SomeDir
}

}
it "provides no output" {

it "provides no output" {
$response | Should BeNullOrEmpty

$response | Should BeNullOrEmpty

}
}



}
}

}
}

}
2 changes: 1 addition & 1 deletion Tests/Remove-PASGroupMember.Tests.ps1
Expand Up @@ -58,7 +58,7 @@ Describe $FunctionName {
BeforeEach {

Mock Invoke-PASRestMethod -MockWith { }

$Script:ExternalVersion = "0.0"
}

It "sends request" {
Expand Down
64 changes: 64 additions & 0 deletions Tests/Use-PASSession.Tests.ps1
@@ -0,0 +1,64 @@
#Get Current Directory
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path

#Get Function Name
$FunctionName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1"

#Assume ModuleName from Repository Root folder
$ModuleName = Split-Path (Split-Path $Here -Parent) -Leaf

#Resolve Path to Module Directory
$ModulePath = Resolve-Path "$Here\..\$ModuleName"

#Define Path to Module Manifest
$ManifestPath = Join-Path "$ModulePath" "$ModuleName.psd1"

if ( -not (Get-Module -Name $ModuleName -All)) {

Import-Module -Name "$ManifestPath" -ArgumentList $true -Force -ErrorAction Stop

}

BeforeAll {

$Script:RequestBody = $null

}

AfterAll {

$Script:RequestBody = $null

}

Describe $FunctionName {

InModuleScope $ModuleName {

Context "Standard Operation" {

BeforeEach {
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Headers["Test"] = "SomeValue"

$InputObject = [PSCustomObject]@{
PSTypeName = 'psPAS.CyberArk.Vault.Session'
User = "SomeUser"
BaseURI = "SomeURL"
ExternalVersion = "6.6"
WebSession = $session
}

}

it "invokes set-variable the expected number of times" {
Mock Set-Variable -MockWith { }
Use-PASSession -Session $InputObject
Assert-MockCalled Set-Variable -Times 3 -Exactly -Scope It
}

}

}

}
7 changes: 6 additions & 1 deletion psPAS/Functions/Authentication/Close-PASSession.ps1
Expand Up @@ -108,5 +108,10 @@

}#process

END { }#end
END {
#Clear Module scope variables on logoff
Clear-Variable -Name ExternalVersion -Scope Script -ErrorAction SilentlyContinue
Clear-Variable -Name BaseURI -Scope Script -ErrorAction SilentlyContinue
Clear-Variable -Name WebSession -Scope Script -ErrorAction SilentlyContinue
}#end
}
52 changes: 52 additions & 0 deletions psPAS/Functions/Authentication/Get-PASSession.ps1
@@ -0,0 +1,52 @@
function Get-PASSession {
<#
.SYNOPSIS
Returns information related to the authenticated session
.DESCRIPTION
For the current session, returns data from the module scope:
- Username relating to the session.
- BaseURI: URL value used for sending requests to the API.
- ExternalVersion: PAS version information.
- Websession: Contains Authorization Header, Cookie & Certificate data related to the current session.
The session information can be saved a variable accessible outside of the module scope for use in requests outside of psPAS.
.EXAMPLE
Show current session related information
Get-PASSession
.EXAMPLE
Save current session related information
$session = Get-PASSession
.EXAMPLE
Use session information for Invoke-RestMethod command
$session = Get-PASSession
Invoke-RestMethod
Invoke-RestMethod -Method GET -Uri "$session.BaseURI/SomePath" -WebSession $session.WebSession
#>
[CmdletBinding()]
param( )

BEGIN { }#begin

PROCESS {

[PSCustomObject]@{
User = Get-PASLoggedOnUser -ErrorAction Stop | Select-Object -ExpandProperty Username
BaseURI = $Script:BaseURI
ExternalVersion = $Script:ExternalVersion
WebSession = $Script:WebSession
} | Add-ObjectDetail -typename psPAS.CyberArk.Vault.Session

}#process

END { }#end

}

0 comments on commit b991be9

Please sign in to comment.