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 " testApiToken" - Path " testPath" - InformationAction ' SilentlyContinue'
7
+
8
+ Describe ' Import-TeamViewerUser' {
9
+ Mock Invoke-TeamViewerPing { $true }
10
+ Mock Get-TeamViewerUser { }
11
+ Mock Add-TeamViewerUser { }
12
+ Mock Edit-TeamViewerUser { }
13
+ $testUser = @ {
email = ' [email protected] ' ;
name = ' Test User' }
14
+
15
+ It ' Should check the connection to the TeamViewer API' {
16
+ { Import-TeamViewerUser ' TestAccessToken' } | Should -Not - Throw
17
+ Assert-MockCalled Invoke-TeamViewerPing - Times 1 - Scope It
18
+ }
19
+
20
+ Context ' TeamViewer API not reachable' {
21
+ Mock Invoke-TeamViewerPing { $false }
22
+
23
+ It ' Should abort if TeamViewer API is not reachable' {
24
+ { Import-TeamViewerUser ' TestAccessToken' - ErrorAction Stop } | Should - Throw
25
+ }
26
+ }
27
+
28
+ It ' Should check for existence of a user' {
29
+ $testUser | Import-TeamViewerUser ' TestAccessToken'
30
+ Assert-MockCalled Get-TeamViewerUser - Times 1 - Scope It - ParameterFilter {
31
+ $email -And $email -eq $testUser.email
32
+ }
33
+ }
34
+
35
+ Context ' User already exists' {
36
+ Mock Get-TeamViewerUser { @ { id = 1234 ; email = $testUser.email ; name = ' Old Name' } }
37
+
38
+ It ' Should update the existing user' {
39
+ $result = ($testUser | Import-TeamViewerUser ' TestAccessToken' )
40
+ $result | Should -Not - BeNullOrEmpty
41
+ $result.Updated | Should - Be 1
42
+ $result.Created | Should - Be 0
43
+ Assert-MockCalled Edit-TeamViewerUser - Times 1 - Scope It - ParameterFilter {
44
+ $userId -And $userId -eq 1234 -And
45
+ $user -And $user -eq $testUser
46
+ }
47
+ }
48
+ }
49
+
50
+ Context ' User does NOT exist' {
51
+ It ' Should create a new user' {
52
+ $result = ($testUser | Import-TeamViewerUser ' TestAccessToken' )
53
+ $result | Should -Not - BeNullOrEmpty
54
+ $result.Created | Should - Be 1
55
+ $result.Updated | Should - Be 0
56
+ Assert-MockCalled Add-TeamViewerUser - Times 1 - Scope It - ParameterFilter {
57
+ $user -And $user -eq $testUser
58
+ }
59
+ }
60
+ }
61
+ }
62
+
63
+ Describe ' ConvertTo-TeamViewerRestError' {
64
+ It ' Should convert from JSON' {
65
+ $result = (' {"foo": "bar"}' | ConvertTo-TeamViewerRestError )
66
+ $result.foo | Should - Be ' bar'
67
+ }
68
+
69
+ It ' Should return input object for invalid JSON' {
70
+ $result = (' garbage' | ConvertTo-TeamViewerRestError )
71
+ $result | Should - Be ' garbage'
72
+ }
73
+ }
74
+
75
+ Describe ' Invoke-TeamViewerRestMethod' {
76
+ Mock Invoke-WebRequest { @ {Content = ' {"foo": "bar"}' } }
77
+
78
+ It ' Should call Invoke-WebRequest and convert the result from JSON' {
79
+ $result = Invoke-TeamViewerRestMethod - Uri ' http://example.test'
80
+ $result.foo | Should - Be ' bar'
81
+ Assert-MockCalled Invoke-WebRequest `
82
+ - ParameterFilter { $Uri -eq ' http://example.test' } - Times 1 - Scope It
83
+ }
84
+ }
85
+
86
+ # Redefine `Invoke-TeamViewerRestMethod` for mocking purposes
87
+ function Invoke-TeamViewerRestMethod ($Uri , $Method , $Headers , $Body ) { }
88
+
89
+ Describe ' Invoke-TeamViewerPing' {
90
+ It ' Should call the API ping REST endpoint' {
91
+ Mock Invoke-TeamViewerRestMethod { @ {token_valid = $true } }
92
+ Invoke-TeamViewerPing ' TestAccessToken' | Should - Be $true
93
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
94
+ $Uri -And ([System.Uri ]$Uri ).PathAndQuery -eq ' /api/v1/ping' -And
95
+ $Method -And $Method -eq ' Get'
96
+ }
97
+ }
98
+
99
+ It ' Should return false for invalid tokens' {
100
+ Mock Invoke-TeamViewerRestMethod { @ {token_valid = $false } }
101
+ Invoke-TeamViewerPing ' TestAccessToken' | Should - Be $false
102
+ }
103
+
104
+ It ' Should set the authorization header' {
105
+ Mock Invoke-TeamViewerRestMethod { @ {token_valid = $true } }
106
+ Invoke-TeamViewerPing ' TestAccessToken'
107
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
108
+ $Headers -And $Headers.ContainsKey (' authorization' ) -And $Headers.authorization -eq ' Bearer TestAccessToken'
109
+ }
110
+ }
111
+ }
112
+
113
+ Describe ' Get-TeamViewerUser' {
114
+ Mock Invoke-TeamViewerRestMethod { @ {
115
+ ' users' = @ (
116
+ @ {
email = ' [email protected] ' ;
name = ' Test User1' }
117
+ )
118
+ } }
119
+
120
+ It ' Should call the API users endpoint' {
121
+ Get-TeamViewerUser ' TestAccessToken' ' [email protected] '
122
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
123
+ $Uri -And ([System.Uri ]$Uri ).PathAndQuery -eq ' /api/v1/users' -And
124
+ $Method -And $Method -eq ' Get'
125
+ $Body -And $Body.email -eq ' [email protected] '
126
+ }
127
+ }
128
+
129
+ It ' Should return a single user' {
130
+ $result = (
Get-TeamViewerUser ' TestAccessToken' ' [email protected] ' )
131
+ $result | Should - HaveCount 1
132
+ $result.email | Should
- Be
' [email protected] '
133
+ }
134
+
135
+ It ' Should set the authorization header' {
136
+ Get-TeamViewerUser ' TestAccessToken' ' [email protected] '
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 ' Add-TeamViewerUser' {
144
+ $testUser = @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' }
145
+ $lastMockParams = @ { }
146
+ Mock Invoke-TeamViewerRestMethod {
147
+ $lastMockParams.Body = $Body
148
+ return $testUser
149
+ }
150
+
151
+ It ' Should call the API users endpoint' {
152
+ $input = @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' ;
' language' = ' en' }
153
+ Add-TeamViewerUser ' TestAccessToken' $input | Should - Be $testUser
154
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
155
+ $Uri -And ([System.Uri ]$Uri ).PathAndQuery -eq ' /api/v1/users' -And
156
+ $Method -And $Method -eq ' Post'
157
+ }
158
+ }
159
+
160
+ It ' Should throw if required fields are missing' {
161
+ $inputs = @ (
162
+ @ {
' email' = ' [email protected] ' ;
' language' = ' en' }
, # missing 'name'
163
+ @ { ' name' = ' Test User 1' ; ' language' = ' en' }, # missing 'email'
164
+ @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' }
# missing 'language'
165
+ )
166
+ $inputs | ForEach-Object { { Add-TeamViewerUser ' TestAccessToken' $_ } | Should - Throw }
167
+ }
168
+
169
+ It ' Should encode the payload using UTF-8' {
170
+ $input = @ {
' name' = ' Test User Müller' ;
' email' = ' [email protected] ' ;
' language' = ' en' }
171
+ Add-TeamViewerUser ' TestAccessToken' $input
172
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter { $Body }
173
+ { [System.Text.Encoding ]::UTF8.GetString($lastMockParams.Body ) } | Should -Not - Throw
174
+ { [System.Text.Encoding ]::UTF8.GetString($lastMockParams.Body ) | ConvertFrom-Json } | Should -Not - Throw
175
+ ([System.Text.Encoding ]::UTF8.GetString($lastMockParams.Body ) | ConvertFrom-Json ).name | Should - Be ' Test User Müller'
176
+ }
177
+
178
+ It ' Should set the authorization header' {
179
+ $input = @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' ;
' language' = ' en' }
180
+ Add-TeamViewerUser ' TestAccessToken' $input
181
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
182
+ $Headers -And $Headers.ContainsKey (' authorization' ) -And $Headers.authorization -eq ' Bearer TestAccessToken'
183
+ }
184
+ }
185
+ }
186
+
187
+ Describe ' Edit-TeamViewerUser' {
188
+ $testUser = @ {
' id' = ' 1234' ;
' name' = ' Test User 1' ;
' email' = ' [email protected] ' }
189
+ Mock Invoke-TeamViewerRestMethod { $testUser }
190
+
191
+ It ' Should call the API users endpoint' {
192
+ $input = @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' }
193
+ $result = (Edit-TeamViewerUser ' TestAccessToken' 1234 $input )
194
+ $result.id | Should - Be $testUser.id
195
+ $result.name | Should - Be $testUser.name
196
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
197
+ $Uri -And ([System.Uri ]$Uri ).PathAndQuery -eq ' /api/v1/users/1234' -And
198
+ $Method -And $Method -eq ' Put'
199
+ }
200
+ }
201
+
202
+ It ' Should set the authorization header' {
203
+ $input = @ {
' name' = ' Test User 1' ;
' email' = ' [email protected] ' }
204
+ Edit-TeamViewerUser ' TestAccessToken' 1234 $input
205
+ Assert-MockCalled Invoke-TeamViewerRestMethod - Times 1 - Scope It - ParameterFilter {
206
+ $Headers -And $Headers.ContainsKey (' authorization' ) -And $Headers.authorization -eq ' Bearer TestAccessToken'
207
+ }
208
+ }
209
+ }
0 commit comments