|
| 1 | +Describe $($PSCommandPath -Replace '.Tests.ps1') { |
| 2 | + |
| 3 | + BeforeAll { |
| 4 | + #Get Current Directory |
| 5 | + $Here = Split-Path -Parent $PSCommandPath |
| 6 | + |
| 7 | + #Assume ModuleName from Repository Root folder |
| 8 | + $ModuleName = Split-Path (Split-Path $Here -Parent) -Leaf |
| 9 | + |
| 10 | + #Resolve Path to Module Directory |
| 11 | + $ModulePath = Resolve-Path "$Here\..\$ModuleName" |
| 12 | + |
| 13 | + #Define Path to Module Manifest |
| 14 | + $ManifestPath = Join-Path "$ModulePath" "$ModuleName.psd1" |
| 15 | + |
| 16 | + if ( -not (Get-Module -Name $ModuleName -All)) { |
| 17 | + |
| 18 | + Import-Module -Name "$ManifestPath" -ArgumentList $true -Force -ErrorAction Stop |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + $Script:RequestBody = $null |
| 23 | + $psPASSession = [ordered]@{ |
| 24 | + BaseURI = 'https://SomeURL/SomeApp' |
| 25 | + User = $null |
| 26 | + ExternalVersion = [System.Version]'0.0' |
| 27 | + WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession |
| 28 | + StartTime = $null |
| 29 | + ElapsedTime = $null |
| 30 | + LastCommand = $null |
| 31 | + LastCommandTime = $null |
| 32 | + LastCommandResults = $null |
| 33 | + } |
| 34 | + |
| 35 | + New-Variable -Name psPASSession -Value $psPASSession -Scope Script -Force |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + AfterAll { |
| 41 | + |
| 42 | + $Script:RequestBody = $null |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + InModuleScope $(Split-Path (Split-Path (Split-Path -Parent $PSCommandPath) -Parent) -Leaf ) { |
| 47 | + |
| 48 | + BeforeEach { |
| 49 | + Mock Invoke-PASRestMethod -MockWith { } |
| 50 | + |
| 51 | + Rename-PASPlatform -ID 42 -Name 'NewPlatformName' |
| 52 | + } |
| 53 | + |
| 54 | + Context 'Input' { |
| 55 | + |
| 56 | + It 'sends request' { |
| 57 | + |
| 58 | + Assert-MockCalled Invoke-PASRestMethod -Scope It |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + It 'sends request to expected endpoint' { |
| 63 | + |
| 64 | + Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { |
| 65 | + |
| 66 | + $URI -eq "$($Script:psPASSession.BaseURI)/API/Platforms/targets/42" |
| 67 | + |
| 68 | + } -Scope It |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + It 'uses expected method' { |
| 73 | + |
| 74 | + Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { $Method -match 'PUT' } -Times 1 -Exactly -Scope It |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + It 'sends request with expected body' { |
| 79 | + |
| 80 | + Assert-MockCalled Invoke-PASRestMethod -ParameterFilter { |
| 81 | + If ($null -ne $Body) { |
| 82 | + $($Body | ConvertFrom-Json | Select-Object -ExpandProperty Name) -eq 'NewPlatformName' |
| 83 | + } |
| 84 | + } -Scope It -Times 1 |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + It 'throws error if version requirement not met' { |
| 89 | + $psPASSession.ExternalVersion = '1.0' |
| 90 | + { Rename-PASPlatform -ID 42 -Name 'NewPlatformName' } | Should -Throw |
| 91 | + $psPASSession.ExternalVersion = '0.0' |
| 92 | + } |
| 93 | + |
| 94 | + It 'throws error if run against Privilege Cloud' { |
| 95 | + $psPASSession.BaseURI = 'https://something.cyberark.cloud' |
| 96 | + { Rename-PASPlatform -ID 42 -Name 'NewPlatformName' } | Should -Throw |
| 97 | + $psPASSession.BaseURI = 'https://SomeURL/SomeApp' |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + Context 'Output' { |
| 103 | + |
| 104 | + It 'provides no output' { |
| 105 | + Mock Invoke-PASRestMethod -MockWith { } |
| 106 | + |
| 107 | + $response = Rename-PASPlatform -ID 42 -Name 'NewPlatformName' |
| 108 | + |
| 109 | + $response | Should -BeNullOrEmpty |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments