|
| 1 | +$here = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 2 | +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' |
| 3 | +. "$here\$sut" -ApiToken "testApiToken" -MappingFilePath "testMappingFilePath" -IgnoreSourceGroup -NoLogFile -InformationAction 'SilentlyContinue' |
| 4 | + |
| 5 | +Describe 'Invoke-TeamViewerGroupPerUserSync' { |
| 6 | + Mock Invoke-WebRequest { } |
| 7 | + Mock Invoke-TeamViewerRestMethod { } |
| 8 | + Mock Invoke-TeamViewerPing { $true } |
| 9 | + Mock Get-TeamViewerUser { @() } |
| 10 | + Mock Get-TeamViewerGroup { @() } |
| 11 | + Mock Add-TeamViewerGroup { } |
| 12 | + Mock Add-TeamViewerGroupShare { } |
| 13 | + Mock Get-TeamViewerDevice { @() } |
| 14 | + Mock Edit-TeamViewerDevice { } |
| 15 | + |
| 16 | + It 'Should check the connection to the TeamViewer API' { |
| 17 | + { Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName '' -ignoreSourceGroup $true -mappingData @() } | Should -Not -Throw |
| 18 | + Assert-MockCalled Invoke-TeamViewerPing -Times 1 -Scope It |
| 19 | + } |
| 20 | + |
| 21 | + Context 'TeamViewer API not reachable' { |
| 22 | + Mock Invoke-TeamViewerPing { $false } |
| 23 | + |
| 24 | + It 'Should abort if TeamViewer API is not reachable' { |
| 25 | + { Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName '' -ignoreSourceGroup $true -mappingData @() } | Should -Throw |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + It 'Should get users, groups and devices' { |
| 30 | + { Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName '' -ignoreSourceGroup $true -mappingData @() } | Should -Not -Throw |
| 31 | + Assert-MockCalled Get-TeamViewerUser -Times 1 -Scope It |
| 32 | + Assert-MockCalled Get-TeamViewerGroup -Times 1 -Scope It |
| 33 | + Assert-MockCalled Get-TeamViewerDevice -Times 1 -Scope It |
| 34 | + } |
| 35 | + |
| 36 | + It 'Should abort if source group does not exist' { |
| 37 | + { Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName 'Non-existing group' -ignoreSourceGroup $false -mappingData @() } | Should -Throw |
| 38 | + } |
| 39 | + |
| 40 | + Context 'Happy Path' { |
| 41 | + Mock Get-TeamViewerUser { @( |
| 42 | + @{ id = 'TestUser1'; email = '[email protected]' } |
| 43 | + ) } |
| 44 | + Mock Get-TeamViewerDevice { @( |
| 45 | + @{device_id = '007'; alias = "TestDevice1"; remotecontrol_id = "r1234" } |
| 46 | + ) } |
| 47 | + Mock Add-TeamViewerGroup { @{ |
| 48 | + id = 'newgroupid1'; name = 'Devices of [email protected]' |
| 49 | + } } |
| 50 | + |
| 51 | + It 'Should create the group, add the device to the group and share the group with the user' { |
| 52 | + $mappingData = @( |
| 53 | + @{ email = '[email protected]'; device = 'TestDevice1' } |
| 54 | + ) |
| 55 | + $result = Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName '' -ignoreSourceGroup $true -mappingData $mappingData |
| 56 | + $result.Statistics.Failed | Should -Be 0 |
| 57 | + $result.Statistics.Unchanged | Should -Be 0 |
| 58 | + $result.Statistics.Updated | Should -Be 1 |
| 59 | + |
| 60 | + Assert-MockCalled Add-TeamViewerGroup -Times 1 -Scope It -ParameterFilter { |
| 61 | + $group -And $group.name -Eq 'Devices of [email protected]' |
| 62 | + } |
| 63 | + Assert-MockCalled Edit-TeamViewerDevice -Times 1 -Scope It -ParameterFilter { |
| 64 | + $deviceId -Eq '007' -And $device -And $device.groupid -Eq 'newgroupid1' |
| 65 | + } |
| 66 | + Assert-MockCalled Add-TeamViewerGroupShare -Times 1 -Scope It -ParameterFilter { |
| 67 | + $groupId -Eq 'newgroupid1' -And $user -And $user.userid -Eq 'TestUser1' -And $user.permissions -Eq 'full' |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + It 'Should do the lookup via TeamViewer ID' { |
| 72 | + $mappingData = @( |
| 73 | + @{ email = '[email protected]'; teamviewerid = '1234' } |
| 74 | + ) |
| 75 | + $result = Invoke-TeamViewerGroupPerUserSync -apiToken 'TestAccessToken' -sourceGroupName '' -ignoreSourceGroup $true -mappingData $mappingData |
| 76 | + $result.Statistics.Failed | Should -Be 0 |
| 77 | + $result.Statistics.Unchanged | Should -Be 0 |
| 78 | + $result.Statistics.Updated | Should -Be 1 |
| 79 | + |
| 80 | + Assert-MockCalled Add-TeamViewerGroup -Times 1 -Scope It -ParameterFilter { |
| 81 | + $group -And $group.name -Eq 'Devices of [email protected]' |
| 82 | + } |
| 83 | + Assert-MockCalled Edit-TeamViewerDevice -Times 1 -Scope It -ParameterFilter { |
| 84 | + $deviceId -Eq '007' -And $device -And $device.groupid -Eq 'newgroupid1' |
| 85 | + } |
| 86 | + Assert-MockCalled Add-TeamViewerGroupShare -Times 1 -Scope It -ParameterFilter { |
| 87 | + $groupId -Eq 'newgroupid1' -And $user -And $user.userid -Eq 'TestUser1' -And $user.permissions -Eq 'full' |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +Describe 'ConvertTo-TeamViewerRestError' { |
| 94 | + It 'Should convert from JSON' { |
| 95 | + $result = ('{"foo": "bar"}' | ConvertTo-TeamViewerRestError) |
| 96 | + $result.foo | Should -Be 'bar' |
| 97 | + } |
| 98 | + |
| 99 | + It 'Should return input object for invalid JSON' { |
| 100 | + $result = ('garbage' | ConvertTo-TeamViewerRestError) |
| 101 | + $result | Should -Be 'garbage' |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +Describe 'Invoke-TeamViewerRestMethod' { |
| 106 | + Mock Invoke-WebRequest { @{Content = '{"foo": "bar"}' } } |
| 107 | + |
| 108 | + It 'Should call Invoke-WebRequest and convert the result from JSON' { |
| 109 | + $result = Invoke-TeamViewerRestMethod -Uri 'http://example.test' |
| 110 | + $result.foo | Should -Be 'bar' |
| 111 | + Assert-MockCalled Invoke-WebRequest ` |
| 112 | + -ParameterFilter { $Uri -eq 'http://example.test' } -Times 1 -Scope It |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +# Redefine `Invoke-TeamViewerRestMethod` for mocking purposes |
| 117 | +function Invoke-TeamViewerRestMethod($Uri, $Method, $Headers, $ContentType, $Body) { } |
| 118 | + |
| 119 | +Describe 'Invoke-TeamViewerPing' { |
| 120 | + It 'Should call the API ping REST endpoint' { |
| 121 | + Mock Invoke-TeamViewerRestMethod { @{token_valid = $true } } |
| 122 | + Invoke-TeamViewerPing 'TestAccessToken' | Should -Be $true |
| 123 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 124 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/ping' -And |
| 125 | + $Method -And $Method -eq 'Get' |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + It 'Should return false for invalid tokens' { |
| 130 | + Mock Invoke-TeamViewerRestMethod { @{token_valid = $false } } |
| 131 | + Invoke-TeamViewerPing 'TestAccessToken' | Should -Be $false |
| 132 | + } |
| 133 | + |
| 134 | + It 'Should set the authorization header' { |
| 135 | + Mock Invoke-TeamViewerRestMethod { @{token_valid = $true } } |
| 136 | + Invoke-TeamViewerPing 'TestAccessToken' |
| 137 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 138 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +Describe 'Get-TeamViewerUser' { |
| 144 | + Mock Invoke-TeamViewerRestMethod { @{ |
| 145 | + 'users' = @( |
| 146 | + @{ email = '[email protected]'; name = 'Test User1' } |
| 147 | + ) |
| 148 | + } } |
| 149 | + |
| 150 | + It 'Should call the API users endpoint' { |
| 151 | + Get-TeamViewerUser 'TestAccessToken' |
| 152 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 153 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/users' -And |
| 154 | + $Method -And $Method -eq 'Get' |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + It 'Should return a single user' { |
| 159 | + $result = (Get-TeamViewerUser 'TestAccessToken') |
| 160 | + $result | Should -HaveCount 1 |
| 161 | + $result.email | Should -Be '[email protected]' |
| 162 | + } |
| 163 | + |
| 164 | + It 'Should set the authorization header' { |
| 165 | + Get-TeamViewerUser 'TestAccessToken' |
| 166 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 167 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +Describe 'Get-TeamViewerGroup' { |
| 173 | + Mock Invoke-TeamViewerRestMethod { @{ |
| 174 | + 'groups' = @( |
| 175 | + @{ id = 'testgroup123'; name = 'Test Group1' }, |
| 176 | + @{ id = 'testgroup456'; name = 'Test Group2' } |
| 177 | + ) |
| 178 | + } } |
| 179 | + |
| 180 | + It 'Should call the API groups endpoint' { |
| 181 | + Get-TeamViewerGroup 'TestAccessToken' |
| 182 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 183 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/groups' -And |
| 184 | + $Method -And $Method -eq 'Get' |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + It 'Should return the group objects' { |
| 189 | + $result = (Get-TeamViewerGroup 'TestAccessToken') |
| 190 | + $result | Should -HaveCount 2 |
| 191 | + } |
| 192 | + |
| 193 | + It 'Should set the authorization header' { |
| 194 | + Get-TeamViewerGroup 'TestAccessToken' |
| 195 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 196 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +Describe 'Add-TeamViewerGroup' { |
| 202 | + $testGroup = @{ 'name' = 'Test Group 1'; 'id' = '007' } |
| 203 | + $lastMockParams = @{ } |
| 204 | + Mock Invoke-TeamViewerRestMethod { |
| 205 | + $lastMockParams.Body = $Body |
| 206 | + return $testGroup |
| 207 | + } |
| 208 | + |
| 209 | + It 'Should call the API groups endpoint' { |
| 210 | + Add-TeamViewerGroup 'TestAccessToken' @{ 'name' = 'Test Group 1' } |
| 211 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 212 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/groups' -And |
| 213 | + $Method -And $Method -eq 'Post' |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + It 'Should throw if required fields are missing' { |
| 218 | + { { Add-TeamViewerGroup'TestAccessToken' @{ } } | Should -Throw } |
| 219 | + } |
| 220 | + |
| 221 | + It 'Should encode the payload using UTF-8' { |
| 222 | + $input = @{ 'name' = 'Test Group Müller' } |
| 223 | + Add-TeamViewerGroup 'TestAccessToken' $input |
| 224 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { $Body } |
| 225 | + { [System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) } | Should -Not -Throw |
| 226 | + { [System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) | ConvertFrom-Json } | Should -Not -Throw |
| 227 | + ([System.Text.Encoding]::UTF8.GetString($lastMockParams.Body) | ConvertFrom-Json).name | Should -Be 'Test Group Müller' |
| 228 | + } |
| 229 | + |
| 230 | + It 'Should set the authorization header' { |
| 231 | + $input = @{ 'name' = 'Test Group 1' } |
| 232 | + Add-TeamViewerGroup 'TestAccessToken' $input |
| 233 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 234 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +Describe 'Add-TeamViewerGroupShare' { |
| 240 | + Mock Invoke-TeamViewerRestMethod { } |
| 241 | + |
| 242 | + It 'Should call the API share_group endpoint' { |
| 243 | + Add-TeamViewerGroupShare 'TestAccessToken' 'TestGroupId1' @{ userid = 'TestUser1'; permissions = 'full' } |
| 244 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 245 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/groups/TestGroupId1/share_group' -And |
| 246 | + $Method -And $Method -eq 'Post' |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + It 'Should throw if required fields are missing' { |
| 251 | + { { Add-TeamViewerGroupShare'TestAccessToken' 'TestGroupId1' @{ } } | Should -Throw } |
| 252 | + } |
| 253 | + |
| 254 | + It 'Should set the authorization header' { |
| 255 | + Add-TeamViewerGroupShare 'TestAccessToken' 'TestGroupId1' @{ userid = 'TestUser1'; permissions = 'full' } |
| 256 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 257 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 258 | + } |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +Describe 'Get-TeamViewerDevice' { |
| 263 | + Mock Invoke-TeamViewerRestMethod { @{ |
| 264 | + 'devices' = @( |
| 265 | + @{ device_id = 'testdevice1'; alias = 'Test Device1'; groupid = "TestGroup1" }, |
| 266 | + @{ device_id = 'testdevice2'; alias = 'Test Device2'; groupid = "TestGroup1" } |
| 267 | + ) |
| 268 | + } } |
| 269 | + |
| 270 | + It 'Should call the API devices endpoint' { |
| 271 | + Get-TeamViewerDevice 'TestAccessToken' |
| 272 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 273 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/devices' -And |
| 274 | + $Method -And $Method -eq 'Get' |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + It 'Should return the devices list' { |
| 279 | + $result = (Get-TeamViewerDevice 'TestAccessToken') |
| 280 | + $result | Should -HaveCount 2 |
| 281 | + $result[0].device_id | Should -Be 'testdevice1' |
| 282 | + } |
| 283 | + |
| 284 | + It 'Should set the authorization header' { |
| 285 | + Get-TeamViewerDevice 'TestAccessToken' |
| 286 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 287 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +Describe 'Edit-TeamViewerDevice' { |
| 293 | + $lastMockParams = @{ } |
| 294 | + Mock Invoke-TeamViewerRestMethod { |
| 295 | + $lastMockParams.Body = $Body |
| 296 | + } |
| 297 | + |
| 298 | + It 'Should call the API devices endpoint' { |
| 299 | + Edit-TeamViewerDevice 'TestAccessToken' 'TestDeviceId' @{groupid = 'TestGroup' } |
| 300 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 301 | + $Uri -And ([System.Uri]$Uri).PathAndQuery -eq '/api/v1/devices/TestDeviceId' -And |
| 302 | + $Method -And $Method -eq 'Put' |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + It 'Should set the authorization header' { |
| 307 | + Edit-TeamViewerDevice 'TestAccessToken' 'TestDeviceId' @{groupid = 'TestGroup' } |
| 308 | + Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter { |
| 309 | + $Headers -And $Headers.ContainsKey('authorization') -And $Headers.authorization -eq 'Bearer TestAccessToken' |
| 310 | + } |
| 311 | + } |
| 312 | +} |
0 commit comments