|
| 1 | +$ProjectRoot = Resolve-Path "$PSScriptRoot/.." |
| 2 | +$ModulePsd = (Resolve-Path "$ProjectRoot/ServiceNow/ServiceNow.psd1").Path |
| 3 | + |
| 4 | +Get-Module 'ServiceNow' | Remove-Module -Force -ErrorAction SilentlyContinue |
| 5 | +Import-Module $ModulePsd -Force |
| 6 | + |
| 7 | +Describe 'Export-ServiceNowRecord' { |
| 8 | + |
| 9 | + Context 'Export by ID' { |
| 10 | + |
| 11 | + It 'Filters by number and uses the correct export format from the file extension' { |
| 12 | + Mock Get-ServiceNowAuth -ModuleName 'ServiceNow' { @{} } |
| 13 | + Mock Invoke-RestMethod -ModuleName 'ServiceNow' {} |
| 14 | + |
| 15 | + $path = Join-Path $TestDrive 'out.csv' |
| 16 | + Export-ServiceNowRecord -ID 'INC0010001' -Path $path -ServiceNowSession @{ Domain = 'test.service-now.com' } |
| 17 | + |
| 18 | + Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { |
| 19 | + $Uri -eq 'https://test.service-now.com/incident_list.do?CSV' -and |
| 20 | + $Body.sysparm_query -eq 'number=INC0010001' -and |
| 21 | + $OutFile -eq $path |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + Context 'Export by table and filter' { |
| 27 | + |
| 28 | + It 'Builds the query string from the filter and uses PDF format' { |
| 29 | + Mock Get-ServiceNowAuth -ModuleName 'ServiceNow' { @{} } |
| 30 | + Mock Invoke-RestMethod -ModuleName 'ServiceNow' {} |
| 31 | + |
| 32 | + $path = Join-Path $TestDrive 'out.pdf' |
| 33 | + Export-ServiceNowRecord -Table 'incident' -Filter @('state', '-eq', '1') -Path $path -ServiceNowSession @{ Domain = 'test.service-now.com' } |
| 34 | + |
| 35 | + Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { |
| 36 | + $Uri -eq 'https://test.service-now.com/incident_list.do?PDF' -and |
| 37 | + $Body.sysparm_query -eq 'state=1' |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + It 'Converts XLS extension to the EXCEL export format' { |
| 42 | + Mock Get-ServiceNowAuth -ModuleName 'ServiceNow' { @{} } |
| 43 | + Mock Invoke-RestMethod -ModuleName 'ServiceNow' {} |
| 44 | + |
| 45 | + $path = Join-Path $TestDrive 'out.xls' |
| 46 | + Export-ServiceNowRecord -Table 'incident' -Filter @('state', '-eq', '1') -Path $path -ServiceNowSession @{ Domain = 'test.service-now.com' } |
| 47 | + |
| 48 | + Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { |
| 49 | + $Uri -eq 'https://test.service-now.com/incident_list.do?EXCEL' |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + It 'Passes requested properties as lower case sysparm_fields' { |
| 54 | + Mock Get-ServiceNowAuth -ModuleName 'ServiceNow' { @{} } |
| 55 | + Mock Invoke-RestMethod -ModuleName 'ServiceNow' {} |
| 56 | + |
| 57 | + $path = Join-Path $TestDrive 'out.csv' |
| 58 | + Export-ServiceNowRecord -Table 'incident' -Filter @('state', '-eq', '1') -Property 'Number', 'ShortDescription' -Path $path -ServiceNowSession @{ Domain = 'test.service-now.com' } |
| 59 | + |
| 60 | + Should -Invoke Invoke-RestMethod -ModuleName 'ServiceNow' -Times 1 -Exactly -ParameterFilter { |
| 61 | + $Body.sysparm_fields -eq 'number,shortdescription' |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + It 'Throws for an unsupported file extension' { |
| 66 | + $path = Join-Path $TestDrive 'out.txt' |
| 67 | + { Export-ServiceNowRecord -Table 'incident' -Filter @('state', '-eq', '1') -Path $path } | Should -Throw |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments