|
| 1 | +# Copyright (c) 2019 TeamViewer GmbH |
| 2 | +# See file LICENSE.txt |
| 3 | + |
| 4 | +$here = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 5 | +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' |
| 6 | +. "$here\$sut" -ApiToken "test" -InformationAction SilentlyContinue |
| 7 | + |
| 8 | +Describe 'Get-TeamViewerPolicy' { |
| 9 | + Mock Invoke-WebRequest { @{Content = '{"policies": [ |
| 10 | + {"policy_id": "foo", "name": "bar", "settings": {}}, |
| 11 | + {"policy_id": "hello", "name": "world", "settings": {}} |
| 12 | + ]}'} } |
| 13 | + |
| 14 | + It 'Should call the "teamviewerpolicies" Web API endpoint' { |
| 15 | + Get-TeamViewerPolicy 'TestToken' |
| 16 | + Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter { |
| 17 | + $Uri -And [System.Uri]$Uri.PathAndQuery -eq '/api/v1/teamviewerpolicies' -And |
| 18 | + $Method -And $Method -eq 'Get' -And -Not $Body |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + It 'Should set the authorization header' { |
| 23 | + Get-TeamViewerPolicy 'TestToken' |
| 24 | + Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter { |
| 25 | + $Headers -And $Headers.ContainsKey('authorization') -And ` |
| 26 | + $Headers.authorization -eq 'Bearer TestToken' |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + It 'Should filter-out policy ID and name' { |
| 31 | + $result = (Get-TeamViewerPolicy 'TestToken') |
| 32 | + $result | Should -HaveCount 2 |
| 33 | + $result[0].PSObject.Properties | Should -HaveCount 2 |
| 34 | + $result[0].policy_id | Should -Be "foo" |
| 35 | + $result[0].name | Should -Be "bar" |
| 36 | + $result[1].PSObject.Properties | Should -HaveCount 2 |
| 37 | + $result[1].policy_id | Should -Be "hello" |
| 38 | + $result[1].name | Should -Be "world" |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +Describe 'ConvertTo-TeamViewerRestError' { |
| 43 | + It 'Should convert from JSON' { |
| 44 | + $result = ('{"foo": "bar"}' | ConvertTo-TeamViewerRestError) |
| 45 | + $result.foo | Should -Be 'bar' |
| 46 | + } |
| 47 | + |
| 48 | + It 'Should return input object for invalid JSON' { |
| 49 | + $result = ('garbage' | ConvertTo-TeamViewerRestError) |
| 50 | + $result | Should -Be 'garbage' |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +Describe 'Invoke-TeamViewerRestMethod' { |
| 55 | + Mock Invoke-WebRequest { @{Content = '{"foo": "bar"}'} } |
| 56 | + |
| 57 | + It 'Should call Invoke-WebRequest and convert the result from JSON' { |
| 58 | + $result = Invoke-TeamViewerRestMethod -Uri 'http://example.test' |
| 59 | + $result.foo | Should -Be 'bar' |
| 60 | + Assert-MockCalled Invoke-WebRequest ` |
| 61 | + -ParameterFilter { $Uri -eq 'http://example.test' }-Times 1 -Scope It |
| 62 | + } |
| 63 | +} |
0 commit comments