-
Notifications
You must be signed in to change notification settings - Fork 141
/
MSFT_xADGroup.Tests.ps1
702 lines (538 loc) · 35.1 KB
/
MSFT_xADGroup.Tests.ps1
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()
$script:dscModuleName = 'xActiveDirectory'
$script:dscResourceName = 'MSFT_xADGroup'
#region HEADER
# Unit Test Template Version: 1.2.4
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests'))
}
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-ResourceType 'Mof' `
-TestType Unit
#endregion HEADER
function Invoke-TestSetup
{
}
function Invoke-TestCleanup
{
Restore-TestEnvironment -TestEnvironment $TestEnvironment
}
# Begin Testing
try
{
Invoke-TestSetup
InModuleScope $script:dscResourceName {
$testPresentParams = @{
GroupName = 'TestGroup'
GroupScope = 'Global'
Category = 'Security'
Path = 'OU=Fake,DC=contoso,DC=com'
Description = 'Test AD group description'
DisplayName = 'Test display name'
Ensure = 'Present'
Notes = 'This is a test AD group'
ManagedBy = 'CN=User 1,CN=Users,DC=contoso,DC=com'
}
$testAbsentParams = $testPresentParams.Clone()
$testAbsentParams['Ensure'] = 'Absent'
$testPresentParamsMultiDomain = $testPresentParams.Clone()
$testPresentParamsMultiDomain.MembershipAttribute = 'DistinguishedName'
$fakeADGroup = @{
Name = $testPresentParams.GroupName
Identity = $testPresentParams.GroupName
GroupScope = $testPresentParams.GroupScope
GroupCategory = $testPresentParams.Category
DistinguishedName = "CN=$($testPresentParams.GroupName),$($testPresentParams.Path)"
Description = $testPresentParams.Description
DisplayName = $testPresentParams.DisplayName
ManagedBy = $testPresentParams.ManagedBy
Info = $testPresentParams.Notes
}
$fakeADUser1 = [PSCustomObject] @{
DistinguishedName = 'CN=User 1,CN=Users,DC=contoso,DC=com'
ObjectGUID = 'a97cc867-0c9e-4928-8387-0dba0c883b8e'
SamAccountName = 'USER1'
SID = 'S-1-5-21-1131554080-2861379300-292325817-1106'
}
$fakeADUser2 = [PSCustomObject] @{
DistinguishedName = 'CN=User 2,CN=Users,DC=contoso,DC=com'
ObjectGUID = 'a97cc867-0c9e-4928-8387-0dba0c883b8f'
SamAccountName = 'USER2'
SID = 'S-1-5-21-1131554080-2861379300-292325817-1107'
}
$fakeADUser3 = [PSCustomObject] @{
DistinguishedName = 'CN=User 3,CN=Users,DC=contoso,DC=com'
ObjectGUID = 'a97cc867-0c9e-4928-8387-0dba0c883b90'
SamAccountName = 'USER3'
SID = 'S-1-5-21-1131554080-2861379300-292325817-1108'
}
$fakeADUser4 = [PSCustomObject] @{
DistinguishedName = 'CN=User 4,CN=Users,DC=sub,DC=contoso,DC=com'
ObjectGUID = 'ebafa34e-b020-40cd-8652-ee7286419869'
SamAccountName = 'USER4'
SID = 'S-1-5-21-1131554080-2861379300-292325817-1109'
}
$testDomainController = 'TESTDC'
$testCredentials = New-Object System.Management.Automation.PSCredential 'DummyUser', (ConvertTo-SecureString 'DummyPassword' -AsPlainText -Force)
#region Function Get-TargetResource
Describe 'xADGroup\Get-TargetResource' {
Mock -CommandName Assert-Module -ParameterFilter { $ModuleName -eq 'ActiveDirectory' }
It 'Calls "Assert-Module" to check AD module is installed' {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$result = Get-TargetResource @testPresentParams; # -DomainName $correctDomainName
Assert-MockCalled -CommandName Assert-Module -ParameterFilter { $ModuleName -eq 'ActiveDirectory' } -Scope It
}
It "Returns 'Ensure' is 'Present' when group exists" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
(Get-TargetResource @testPresentParams).Ensure | Should -Be 'Present'
}
It "Returns 'Ensure' is 'Absent' when group does not exist" {
Mock -CommandName Get-ADGroup { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
(Get-TargetResource @testPresentParams).Ensure | Should -Be 'Absent'
}
It "Calls 'Get-ADGroup' with 'Server' parameter when 'DomainController' specified" {
Mock -CommandName Get-ADGroup -ParameterFilter { $Server -eq $testDomainController } -MockWith { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
Get-TargetResource @testPresentParams -DomainController $testDomainController
Assert-MockCalled -CommandName Get-ADGroup -ParameterFilter { $Server -eq $testDomainController } -Scope It
}
It "Calls 'Get-ADGroup' with 'Credential' parameter when specified" {
Mock -CommandName Get-ADGroup -ParameterFilter { $Credential -eq $testCredentials } -MockWith { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
Get-TargetResource @testPresentParams -Credential $testCredentials
Assert-MockCalled -CommandName Get-ADGroup -ParameterFilter { $Credential -eq $testCredentials } -Scope It
}
It "Calls 'Get-ADGroupMember' with 'Server' parameter when 'DomainController' specified" {
Mock -CommandName Get-ADGroup -MockWith { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember -ParameterFilter { $Server -eq $testDomainController } -MockWith { return @($fakeADUser1, $fakeADUser2); }
Get-TargetResource @testPresentParams -DomainController $testDomainController
Assert-MockCalled -CommandName Get-ADGroupMember -ParameterFilter { $Server -eq $testDomainController } -Scope It
}
It "Calls 'Get-ADGroupMember' with 'Credential' parameter when specified" {
Mock -CommandName Get-ADGroup -MockWith { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember -ParameterFilter { $Credential -eq $testCredentials } -MockWith { return @($fakeADUser1, $fakeADUser2); }
Get-TargetResource @testPresentParams -Credential $testCredentials
Assert-MockCalled -CommandName Get-ADGroupMember -ParameterFilter { $Credential -eq $testCredentials } -Scope It
}
}
#end region
#region Function Test-TargetResource
Describe 'xADGroup\Test-TargetResource' {
Mock -CommandName Assert-Module -ParameterFilter { $ModuleName -eq 'ActiveDirectory' }
foreach ($attribute in @('SamAccountName','DistinguishedName','ObjectGUID','SID'))
{
It "Passes when group 'Members' match using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser1.$attribute, $fakeADUser2.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $true
}
It "Fails when group membership counts do not match using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1); }
$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser2.$attribute, $fakeADUser3.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $false
}
It "Fails when group 'Members' do not match using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -Members $fakeADUser2.$attribute, $fakeADUser3.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $false
}
It "Passes when specified 'MembersToInclude' match using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -MembersToInclude $fakeADUser2.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $true
}
It "Fails when specified 'MembersToInclude' are missing using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -MembersToInclude $fakeADUser3.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $false
}
It "Passes when specified 'MembersToExclude' are missing using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -MembersToExclude $fakeADUser3.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $true
}
It "Fails when when specified 'MembersToExclude' match using '$attribute'" {
Mock -CommandName Get-ADGroup { return $fakeADGroup; }
Mock -CommandName Get-ADGroupMember { return @($fakeADUser1, $fakeADUser2); }
$targetResource = Test-TargetResource @testPresentParams -MembersToExclude $fakeADUser2.$attribute -MembershipAttribute $attribute
$targetResource | Should -Be $false
}
} #end foreach attribute
It "Fails when group does not exist and 'Ensure' is 'Present'" {
Mock -CommandName Get-TargetResource -MockWith { return $testAbsentParams }
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'Scope' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['GroupScope'] = 'Universal'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'Category' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['Category'] = 'Distribution'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'Path' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['Path'] = 'OU=WrongPath,DC=contoso,DC=com'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'Description' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['Description'] = 'Test AD group description is wrong'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'DisplayName' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['DisplayName'] = 'Wrong display name'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'ManagedBy' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['ManagedBy'] = $fakeADUser3.DistinguishedName
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists, 'Ensure' is 'Present' but 'Notes' is wrong" {
Mock -CommandName Get-TargetResource -MockWith {
$duffADGroup = $testPresentParams.Clone()
$duffADGroup['Notes'] = 'These notes are clearly wrong'
return $duffADGroup
}
Test-TargetResource @testPresentParams | Should -Be $false
}
It "Fails when group exists and 'Ensure' is 'Absent'" {
Mock -CommandName Get-TargetResource -MockWith { return $testPresentParams }
Test-TargetResource @testAbsentParams | Should -Be $false
}
It "Passes when group exists, target matches and 'Ensure' is 'Present'" {
Mock -CommandName Get-TargetResource -MockWith { return $testPresentParams }
Test-TargetResource @testPresentParams | Should -Be $true
}
It "Passes when group does not exist and 'Ensure' is 'Absent'" {
Mock -CommandName Get-TargetResource -MockWith { return $testAbsentParams }
Test-TargetResource @testAbsentParams | Should -Be $true
}
}
#end region
#region Function Set-TargetResource
Describe 'xADGroup\Set-TargetResource' {
Mock -CommandName Assert-Module -ParameterFilter { $ModuleName -eq 'ActiveDirectory' }
It "Calls 'New-ADGroup' when 'Ensure' is 'Present' and the group does not exist" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName New-ADGroup -Scope It
}
$testProperties = @{
Description = 'Test AD Group description is wrong'
ManagedBy = $fakeADUser3.DistinguishedName
DisplayName = 'Test DisplayName'
}
foreach ($property in $testProperties.Keys)
{
It "Calls 'Set-ADGroup' when 'Ensure' is 'Present' and '$property' is specified" {
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember
Mock -CommandName Get-ADGroup -MockWith {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup[$property] = $testProperties.$property
return $duffADGroup
}
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName Set-ADGroup -Scope It -Exactly 1
}
}
It "Calls 'Set-ADGroup' when 'Ensure' is 'Present' and 'Category' is specified" {
Mock -CommandName Set-ADGroup -ParameterFilter { $GroupCategory -eq $testPresentParams.Category }
Mock -CommandName Get-ADGroupMember
Mock -CommandName Get-ADGroup -MockWith {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup['GroupCategory'] = 'Distribution'
return $duffADGroup
}
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName Set-ADGroup -ParameterFilter { $GroupCategory -eq $testPresentParams.Category } -Scope It -Exactly 1
}
It "Calls 'Set-ADGroup' when 'Ensure' is 'Present' and 'Notes' is specified" {
Mock -CommandName Set-ADGroup -ParameterFilter { $Replace -ne $null }
Mock -CommandName Get-ADGroupMember
Mock -CommandName Get-ADGroup {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup['Info'] = 'My test note..'
return $duffADGroup
}
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName Set-ADGroup -ParameterFilter { $Replace -ne $null } -Scope It -Exactly 1
}
It "Calls 'Set-ADGroup' twice when 'Ensure' is 'Present', the group exists but the 'Scope' has changed" {
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember
Mock -CommandName Get-ADGroup -MockWith {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup['GroupScope'] = 'DomainLocal'
return $duffADGroup
}
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName Set-ADGroup -Scope It -Exactly 2
}
It "Adds group members when 'Ensure' is 'Present', the group exists and 'Members' are specified" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName Add-ADCommonGroupMember
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Set-TargetResource @testPresentParams -Members @($fakeADUser1.SamAccountName, $fakeADUser2.SamAccountName)
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It
}
It "Tries to resolve the domain names for all groups in the same domain when the 'MembershipAttribute' property is set to distinguishedName" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName Add-ADCommonGroupMember
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Get-DomainName -MockWith { return 'contoso.com' }
Mock -CommandName Get-ADDomainNameFromDistinguishedName -MockWith { return 'contoso.com' }
Mock -CommandName Write-Verbose -ParameterFilter { $Message -and $Message -match 'Group membership objects are in .* different AD Domains.'}
Set-TargetResource @testPresentParamsMultiDomain -Members @($fakeADUser1.distinguishedName, $fakeADUser2.distinguishedName)
Assert-MockCalled -CommandName Get-ADDomainNameFromDistinguishedName
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It
Assert-MockCalled -CommandName Write-Verbose -ParameterFilter { $Message -and $Message -match 'Group membership objects are in .* different AD Domains.'} -Exactly -Times 0
}
It "Tries to resolve the domain names for all groups in different domains when the 'MembershipAttribute' property is set to distinguishedName" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName Add-ADCommonGroupMember
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Get-DomainName -MockWith {return 'contoso.com'}
Mock -CommandName Get-ADDomainNameFromDistinguishedName -MockWith {
param (
[Parameter()]
[string]
$DistinguishedName
)
if ($DistinguishedName -match 'DC=sub')
{
return 'sub.contoso.com'
}
else
{
return 'contoso.com'
}
}
Mock -CommandName Write-Verbose -ParameterFilter { $Message -and $Message -match 'Group membership objects are in .* different AD Domains.'}
Set-TargetResource @testPresentParamsMultiDomain -Members @($fakeADUser1.distinguishedName, $fakeADUser4.distinguishedName)
Assert-MockCalled -CommandName Get-ADDomainNameFromDistinguishedName
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It
Assert-MockCalled -CommandName Write-Verbose -ParameterFilter { $Message -and $Message -match 'Group membership objects are in .* different AD Domains.'}
}
It "Adds group members when 'Ensure' is 'Present', the group exists and 'MembersToInclude' are specified" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName Add-ADCommonGroupMember
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Set-TargetResource @testPresentParams -MembersToInclude @($fakeADUser1.SamAccountName, $fakeADUser2.SamAccountName)
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It
}
It "Moves group when 'Ensure' is 'Present', the group exists but the 'Path' has changed" {
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember
Mock -CommandName Move-ADObject
Mock -CommandName Get-ADGroup -MockWith {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup['DistinguishedName'] = "CN=$($testPresentParams.GroupName),OU=WrongPath,DC=contoso,DC=com"
return $duffADGroup
}
Set-TargetResource @testPresentParams
Assert-MockCalled -CommandName Move-ADObject -Scope It
}
It "Resets group membership when 'Ensure' is 'Present' and 'Members' is incorrect" {
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember -MockWith { return @($fakeADUser1, $fakeADUser2); }
Mock -CommandName Add-ADCommonGroupMember
Mock -CommandName Remove-ADGroupMember
Set-TargetResource @testPresentParams -Members $fakeADuser1.SamAccountName
Assert-MockCalled -CommandName Remove-ADGroupMember -Scope It -Exactly 1
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It -Exactly 1
}
It "Does not reset group membership when 'Ensure' is 'Present' and existing group is empty" {
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember
Mock -CommandName Remove-ADGroupMember
Set-TargetResource @testPresentParams -MembersToExclude $fakeADuser1.SamAccountName
Assert-MockCalled -CommandName Remove-ADGroupMember -Scope It -Exactly 0
}
It "Removes members when 'Ensure' is 'Present' and 'MembersToExclude' is incorrect" {
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember -MockWith { return @($fakeADUser1, $fakeADUser2); }
Mock -CommandName Remove-ADGroupMember
Set-TargetResource @testPresentParams -MembersToExclude $fakeADuser1.SamAccountName
Assert-MockCalled -CommandName Remove-ADGroupMember -Scope It -Exactly 1
}
It "Adds members when 'Ensure' is 'Present' and 'MembersToInclude' is incorrect" {
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember -MockWith { return @($fakeADUser1, $fakeADUser2); }
Mock -CommandName Add-ADCommonGroupMember
Set-TargetResource @testPresentParams -MembersToInclude $fakeADuser3.SamAccountName
Assert-MockCalled -CommandName Add-ADCommonGroupMember -Scope It -Exactly 1
}
It "Removes group when 'Ensure' is 'Absent' and group exists" {
Mock -CommandName Get-ADGroup -MockWith { return $fakeADGroup; }
Mock -CommandName Remove-ADGroup
Set-TargetResource @testAbsentParams
Assert-MockCalled -CommandName Remove-ADGroup -Scope It
}
It "Calls 'Set-ADGroup' with credentials when 'Ensure' is 'Present' and the group exists (#106)" {
Mock -CommandName Get-ADGroup -MockWith { return $fakeADGroup; }
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Get-ADGroupMember
Mock -CommandName Set-ADGroup -ParameterFilter { $Credential -eq $testCredentials }
Set-TargetResource @testPresentParams -Credential $testCredentials
Assert-MockCalled -CommandName Set-ADGroup -ParameterFilter { $Credential -eq $testCredentials } -Scope It
}
It "Calls 'Set-ADGroup' with credentials when 'Ensure' is 'Present' and the group does not exist (#106)" {
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup -ParameterFilter { $Credential -eq $testCredentials }
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Set-TargetResource @testPresentParams -Credential $testCredentials
Assert-MockCalled -CommandName Set-ADGroup -ParameterFilter { $Credential -eq $testCredentials } -Scope It
}
It "Calls 'Move-ADObject' with credentials when specified (#106)" {
Mock -CommandName Set-ADGroup
Mock -CommandName Get-ADGroupMember
Mock -CommandName Move-ADObject -ParameterFilter { $Credential -eq $testCredentials }
Mock -CommandName Get-ADGroup -MockWith {
$duffADGroup = $fakeADGroup.Clone()
$duffADGroup['DistinguishedName'] = "CN=$($testPresentParams.GroupName),OU=WrongPath,DC=contoso,DC=com"
return $duffADGroup
}
Set-TargetResource @testPresentParams -Credential $testCredentials
Assert-MockCalled -CommandName Move-ADObject -ParameterFilter { $Credential -eq $testCredentials } -Scope It
}
# tests for issue 183
It "Doesn't reset to 'Global' when not specifying a 'Scope' and updating group membership" {
$testUniversalPresentParams = $testPresentParams.Clone()
$testUniversalPresentParams['GroupScope'] = 'Universal'
$fakeADUniversalGroup = $fakeADGroup.Clone()
$fakeADUniversalGroup['GroupScope'] = 'Universal'
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADUniversalGroup }
Mock -CommandName Set-ADGroup -ParameterFilter { $Identity -eq $fakeADUniversalGroup.Identity -and -not $PSBoundParameters.ContainsKey('GroupScope') }
Mock -CommandName Add-ADCommonGroupMember
Set-TargetResource -GroupName $testUniversalPresentParams.GroupName -Members @($fakeADUser1.SamAccountName, $fakeADUser2.SamAccountName)
Assert-MockCalled -CommandName Set-ADGroup -Times 1 -Scope It
}
# tests for issue 183
It "Doesn't reset to 'Security' when not specifying a 'Category' and updating group membership" {
$testUniversalPresentParams = $testPresentParams.Clone()
$testUniversalPresentParams['Category'] = 'Distribution'
$fakeADUniversalGroup = $fakeADGroup.Clone()
$fakeADUniversalGroup['GroupCategory'] = 'Distribution'
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADUniversalGroup }
Mock -CommandName Set-ADGroup -ParameterFilter { $Identity -eq $fakeADUniversalGroup.Identity -and -not $PSBoundParameters.ContainsKey('GroupCategory') }
Mock -CommandName Add-ADCommonGroupMember
Set-TargetResource -GroupName $testUniversalPresentParams.GroupName -Members @($fakeADUser1.SamAccountName, $fakeADUser2.SamAccountName)
Assert-MockCalled -CommandName Set-ADGroup -Times 1 -Scope It
}
# tests for issue 183
It "Doesn't reset to 'Global' when not specifying a 'Scope' and testing display name" {
$testUniversalPresentParams = $testPresentParams.Clone()
$testUniversalPresentParams['GroupScope'] = 'Universal'
$fakeADUniversalGroup = $fakeADGroup.Clone()
$fakeADUniversalGroup['GroupScope'] = 'Universal'
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADUniversalGroup }
Mock -CommandName Set-ADGroup -ParameterFilter { $Identity -eq $fakeADUniversalGroup.Identity -and -not $PSBoundParameters.ContainsKey('GroupScope') }
Mock -CommandName Add-ADCommonGroupMember
$universalGroupInCompliance = Test-TargetResource -GroupName $testUniversalPresentParams.GroupName -DisplayName $testUniversalPresentParams.DisplayName
$universalGroupInCompliance | Should -Be $true
}
# tests for issue 183
It "Doesn't reset to 'Security' when not specifying a 'Category' and testing display name" {
$testUniversalPresentParams = $testPresentParams.Clone()
$testUniversalPresentParams['Category'] = 'Distribution'
$fakeADUniversalGroup = $fakeADGroup.Clone()
$fakeADUniversalGroup['GroupCategory'] = 'Distribution'
Mock -CommandName Get-ADGroup -MockWith { return [PSCustomObject] $fakeADUniversalGroup }
Mock -CommandName Set-ADGroup -ParameterFilter { $Identity -eq $fakeADUniversalGroup.Identity -and -not $PSBoundParameters.ContainsKey('GroupScope') }
Mock -CommandName Add-ADCommonGroupMember
$universalGroupInCompliance = Test-TargetResource -GroupName $testUniversalPresentParams.GroupName -DisplayName $testUniversalPresentParams.DisplayName
$universalGroupInCompliance | Should -Be $true
}
It "Calls Restore-AdCommonObject when RestoreFromRecycleBin is used" {
$restoreParam = $testPresentParams.Clone()
$restoreParam.RestoreFromRecycleBin = $true
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Restore-ADCommonObject -MockWith { return [PSCustomObject] $fakeADGroup;}
Set-TargetResource @restoreParam
Assert-MockCalled -CommandName Restore-AdCommonObject -Scope It
Assert-MockCalled -CommandName New-ADGroup -Scope It -Exactly -Times 0
Assert-MockCalled -CommandName Set-ADGroup -Scope It
}
It "Calls New-ADGroup when RestoreFromRecycleBin is used and if no object was found in the recycle bin" {
$restoreParam = $testPresentParams.Clone()
$restoreParam.RestoreFromRecycleBin = $true
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Restore-ADCommonObject
Set-TargetResource @restoreParam
Assert-MockCalled -CommandName Restore-AdCommonObject -Scope It
Assert-MockCalled -CommandName New-ADGroup -Scope It
}
It "Throws if the object cannot be restored" {
$restoreParam = $testPresentParams.Clone()
$restoreParam.RestoreFromRecycleBin = $true
Mock -CommandName Get-ADGroup -MockWith { throw New-Object Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException }
Mock -CommandName Set-ADGroup
Mock -CommandName New-ADGroup -MockWith { return [PSCustomObject] $fakeADGroup; }
Mock -CommandName Restore-ADCommonObject -MockWith { throw (New-Object -TypeName System.InvalidOperationException)}
{Set-TargetResource @restoreParam;} | Should -Throw
Assert-MockCalled -CommandName Restore-AdCommonObject -Scope It
Assert-MockCalled -CommandName New-ADGroup -Scope It -Exactly -Times 0
Assert-MockCalled -CommandName Set-ADGroup -Scope It -Exactly -Times 0
}
}
#end region
}
#end region
}
finally
{
Invoke-TestCleanup
}