-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.Tests.ps1
More file actions
323 lines (259 loc) · 12.6 KB
/
Connection.Tests.ps1
File metadata and controls
323 lines (259 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Describe "Connection Module Tests" {
BeforeAll {
# Import required modules
Import-Module "$PSScriptRoot/../../../src/common/Connection.psm1" -Force
# Mock external dependencies for cross-platform testing
Mock Connect-ExchangeOnline { } -ModuleName Connection
Mock Disconnect-ExchangeOnline { } -ModuleName Connection
Mock Connect-IPPSSession { } -ModuleName Connection
Mock Connect-MgGraph { } -ModuleName Connection
Mock Disconnect-MgGraph { } -ModuleName Connection
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = 'test-connection-id'
}
} -ModuleName Connection
Mock Get-MgContext {
[PSCustomObject]@{
Account = 'test@example.com'
Scopes = @('User.Read', 'Mail.Read')
}
} -ModuleName Connection
Mock Get-UserConfirmation { $true } -ModuleName Connection
}
Context "Module Import" {
It "Should import Connection module successfully" {
Get-Module -Name Connection* | Should -Not -BeNullOrEmpty
}
It "Should export expected functions" {
$ExportedFunctions = (Get-Module -Name Connection*).ExportedFunctions.Keys
$ExportedFunctions | Should -Contain 'Connect-Service'
}
}
Context "ExchangeOnline Service Tests" {
BeforeEach {
Mock Get-ConnectionInformation { $null } -ModuleName Connection
}
It "Should handle ExchangeOnline connection" {
Mock Connect-ExchangeOnline { } -ModuleName Connection
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = ange-connection'
}
} -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-ExchangeOnline -Times 1 -ModuleName Connection
}
It "Should handle existing ExchangeOnline connection" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'existing@example.com'
ConnectionId = 'existing-connection'
}
} -ModuleName Connection
Mock Get-UserConfirmation { $true } -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' } | Should -Not -Throw
# Should not call Connect-ExchangeOnline if already connected and user confirms
Should -Invoke Get-UserConfirmation -Times 1 -ModuleName Connection
}
It "Should disconnect and reconnect if user declines" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'existing@example.com'
ConnectionId = 'existing-connection'
}
} -ModuleName Connection
Mock Get-UserConfirmation { $false } -ModuleName Connection
Mock Disconnect-ExchangeOnline { } -ModuleName Connection
Mock Connect-ExchangeOnline { } -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' } | Should -Not -Throw
Should -Invoke Disconnect-ExchangeOnline -Times 1 -ModuleName Connection
Should -Invoke Connect-ExchangeOnline -Times 1 -ModuleName Connection
}
}
Context "SecurityComplience Service Tests" {
BeforeEach {
Mock Get-ConnectionInformation { $null } -ModuleName Connection
}
It "Should handle SecurityComplience connection" {
Mock Connect-IPPSSession { } -ModuleName Connection
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = 'ipps-connection'
}
} -ModuleName Connection
{ Connect-Service -Services 'SecurityComplience' -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-IPPSSession -Times 1 -ModuleName Connection
}
It "Should handle SecurityComplience disconnection" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = 'ipps-connection'
}
} -ModuleName Connection
Mock Disconnect-ExchangeOnline { } -ModuleName Connection
# The disconnect for SecurityComplience uses Disconnect-ExchangeOnline
{ Connect-Service -Services 'SecurityComplience' -CheckOnly } | Should -Not -Throw
}
}
Context "Graph Service Tests" {
BeforeEach {
Mock Get-MgContext { $null } -ModuleName Connection
}
It "Should handle Graph connection without scopes" {
Mock Connect-MgGraph { } -ModuleName Connection
Mock Get-MgContext {
[PSCustomObject]@{
Account = 'test@example.com'
Scopes = @()
}
} -ModuleName Connection
{ Connect-Service -Services 'Graph' -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection
}
It "Should handle Graph connection with scopes" {
Mock Connect-MgGraph { } -ModuleName Connection
Mock Get-MgContext {
[PSCustomObject]@{
Account = 'test@example.com'
Scopes = @('User.Read', 'Mail.Read')
}
} -ModuleName Connection
$Scopes = @('User.Read', 'Mail.Read')
{ Connect-Service -Services 'Graph' -Scopes $Scopes -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection
}
It "Should handle Graph connection with access token" {
Mock Connect-MgGraph { } -ModuleName Connection
Mock Get-MgContext {
[PSCustomObject]@{
Account = 'test@example.com'
Scopes = @('User.Read')
}
} -ModuleName Connection
$SecureToken = ConvertTo-SecureString 'token123' -AsPlainText -Force
{ Connect-Service -Services 'Graph' -AccessToken $SecureToken -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection -ParameterFilter { $AccessToken -ne $null }
}
It "Should handle insufficient scopes in Graph connection" {
Mock Connect-MgGraph { } -ModuleName Connection
Mock Get-MgContext {
[PSCustomObject]@{
Account = 'test@example.com'
Scopes = @('User.Read') # Missing Mail.Read
}
} -ModuleName Connection
Mock Disconnect-MgGraph { } -ModuleName Connection
$RequiredScopes = @('User.Read', 'Mail.Read')
{ Connect-Service -Services 'Graph' -Scopes $RequiredScopes -DontConfirm } | Should -Not -Throw
# Should disconnect due to insufficient scopes
Should -Invoke Disconnect-MgGraph -Times 1 -ModuleName Connection
}
It "Should handle Graph disconnection" {
Mock Disconnect-MgGraph { } -ModuleName Connection
{ Disconnect-MgGraph } | Should -Not -Throw
Should -Invoke Disconnect-MgGraph -Times 1 -ModuleName Connection
}
}
Context 'CheckOnly Parameter Tests' {
It 'Should check connection status without connecting' {
Mock Get-ConnectionInformation { $null } -ModuleName Connection
Mock Invoke-FailedExit { throw 'Not connected' } -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' -CheckOnly } | Should -Throw 'Not connected'
# Should not attempt to connect
Should -Invoke Connect-ExchangeOnline -Times 0 -ModuleName Connection
}
It "Should pass check when already connected" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = 'existing-connection'
}
} -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' -CheckOnly } | Should -Not -Throw
}
}
Context "DontConfirm Parameter Tests" {
It "Should skip confirmation when DontConfirm is used" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'existing@example.com'
ConnectionId = 'existing-connection'
}
} -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' -DontConfirm } | Should -Not -Throw
# Should not call Get-UserConfirmation
Should -Invoke Get-UserConfirmation -Times 0 -ModuleName Connection
}
It "Should prompt for confirmation by default" {
Mock Get-ConnectionInformation {
[PSCustomObject]@{
UserPrincipalName = 'existing@example.com'
ConnectionId = 'existing-connection'
}
} -ModuleName Connection
Mock Get-UserConfirmation { $true } -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' } | Should -Not -Throw
Should -Invoke Get-UserConfirmation -Times 1 -ModuleName Connection
}
}
Context "Error Handling" {
It "Should handle connection failures" {
Mock Get-ConnectionInformation { $null } -ModuleName Connection
Mock Connect-ExchangeOnline { throw "Connection failed" } -ModuleName Connection
Mock Invoke-FailedExit { throw "Failed to connect" } -ModuleName Connection
{ Connect-Service -Services 'ExchangeOnline' -DontConfirm } | Should -Throw
Should -Invoke Invoke-FailedExit -Times 1 -ModuleName Connection
}
It "Should handle disconnection failures" {
Mock Disconnect-ExchangeOnline { throw "Disconnect failed" } -ModuleName Connection
Mock Invoke-FailedExit { throw "Failed to disconnect" } -ModuleName Connection
{ Disconnect-ExchangeOnline } | Should -Throw
}
It "Should handle Graph context retrieval failures" {
Mock Get-MgContext { throw "Graph context error" } -ModuleName Connection
Mock Connect-MgGraph { } -ModuleName Connection
{ Connect-Service -Services 'Graph' -DontConfirm } | Should -Not -Throw
# Should attempt to connect when context retrieval fails
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection
}
}
Context "Multiple Services Integration" {
It "Should handle connecting to multiple services" {
Mock Get-ConnectionInformation { $null } -ModuleName Connection
Mock Get-MgContext { $null } -ModuleName Connection
Mock Connect-ExchangeOnline { } -ModuleName Connection
Mock Connect-MgGraph { } -ModuleName Connection
{ Connect-Service -Services @('ExchangeOnline', 'Graph') -DontConfirm } | Should -Not -Throw
Should -Invoke Connect-ExchangeOnline -Times 1 -ModuleName Connection
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection
}
It "Should handle mixed connection states" {
# ExchangeOnline already connected, Graph not connected
Mock Get-ConnectionInformation {
param($ConnectionId)
if ($ConnectionId) {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = $ConnectionId
}
} else {
[PSCustomObject]@{
UserPrincipalName = 'test@example.com'
ConnectionId = 'exchange-connection'
}
}
} -ModuleName Connection
Mock Get-MgContext { $null } -ModuleName Connection
Mock Connect-MgGraph { } -ModuleName Connection
{ Connect-Service -Services @('ExchangeOnline', 'Graph') -DontConfirm } | Should -Not -Throw
# Should only connect to Graph
Should -Invoke Connect-ExchangeOnline -Times 0 -ModuleName Connection
Should -Invoke Connect-MgGraph -Times 1 -ModuleName Connection
}
}
}