Tests/Unit/MSFT_UserResource.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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
# To run these tests, the currently logged on user must have rights to create a user [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param () $errorActionPreference = 'Stop' Set-StrictMode -Version 'Latest' $script:testFolderPath = Split-Path -Path $PSScriptRoot -Parent $script:testHelpersPath = Join-Path -Path $script:testFolderPath -ChildPath 'TestHelpers' Import-Module -Name (Join-Path -Path $script:testHelpersPath -ChildPath 'CommonTestHelper.psm1') $script:testEnvironment = Enter-DscResourceTestEnvironment ` -DSCResourceModuleName 'PSDscResources' ` -DSCResourceName 'MSFT_UserResource' ` -TestType 'Unit' try { Import-Module -Name (Join-Path -Path $script:testHelpersPath ` -ChildPath 'MSFT_UserResource.TestHelper.psm1') # get rid of this if-check once the fix for this is released if (Test-IsNanoServer) { Import-Module -Name 'Microsoft.Powershell.LocalAccounts' } # Commented out until the fix is released #Import-Module -Name 'Microsoft.Powershell.LocalAccounts' InModuleScope 'MSFT_UserResource' { $script:disposableObjects = @() try { $existingUserName = 'TestUserName12345' $existingUserPassword = 'StrongOne7.' $existingUserSecurePassword = ConvertTo-SecureString -String $existingUserPassword -AsPlainText -Force $existingUserTestCredential = New-Object PSCredential ($existingUserName, $existingUserSecurePassword) # Mock user object and properties used for most tests $existingUserValues = @{ Name = $existingUserName Password = $existingUserTestCredential Description = 'Some Description' DisplayName = 'Test Existing User' FullName = 'Test Existing User' Enabled = $true PasswordNeverExpires = $false PasswordExpires = '01/03/2018 17:04:20' UserCannotChangePassword = $false UserMayChangePassword = $true PasswordChangeRequired = $false } $newUserName = 'NewTestUserName12345' $newUserPassword = 'NewStrongOne123.' $newUserSecurePassword = ConvertTo-SecureString -String $newUserPassword -AsPlainText -Force $newUserCredential = New-Object PSCredential ($newUserName, $newUserSecurePassword) # Mock user object and properties mainly used for checking updating values $newUserValues = @{ Name = $newUserName Password = $newUserCredential Description = 'Some other Description' DisplayName = 'Test New User1' FullName = 'Test New User1' Enabled = $true PasswordNeverExpires = $true PasswordExpires = $null UserCannotChangePassword = $true UserMayChangePassword = $false PasswordChangeRequired = $false } $modifiableUserName = 'newUser1234' $modifiableUserPassword = 'ThisIsAStrongPassword543!' $modifiableUserSecurePassword = ConvertTo-SecureString -String $modifiableUserPassword -AsPlainText -Force $modifiableUserCredential = New-Object PSCredential ($modifiableUserName, $modifiableUserSecurePassword) # Mock user object that gets modified by the Set-TargetResourceOnNanoServer tests $modifiableUserValues = @{ Name = $modifiableUserName Password = $modifiableUserCredential Description = 'Another Description' DisplayName = 'Test New User2' FullName = 'Another Description' Enabled = $false PasswordNeverExpires = $true PasswordExpires = $null UserCannotChangePassword = $true UserMayChangePassword = $false PasswordChangeRequired = $true } if (Test-IsNanoServer) { $script:UserObject = New-Object -TypeName 'System.Management.Automation.SecurityAccountsManager.LocalUser' ` -ArgumentList @( $existingUserName ) $script:disposableObjects += $script:UserObject # Properties that are only on Nano Server user object $script:UserObject.FullName = $existingUserValues.FullName $script:UserObject.UserMayChangePassword = $existingUserValues.UserMayChangePassword $script:UserObject.PasswordExpires = $existingUserValues.PasswordExpires } else { $script:testPrincipalContext = New-Object -TypeName 'System.DirectoryServices.AccountManagement.PrincipalContext' ` -ArgumentList @( [System.DirectoryServices.AccountManagement.ContextType]::Machine ) $script:disposableObjects += $script:testPrincipalContext $script:UserObject = New-Object -TypeName 'System.DirectoryServices.AccountManagement.UserPrincipal' ` -ArgumentList @( $testPrincipalContext ) $script:disposableObjects += $script:UserObject # Properties that are only on FullSku user object $script:UserObject.DisplayName = $existingUserValues.DisplayName $script:UserObject.UserCannotChangePassword = $existingUserValues.UserCannotChangePassword $script:UserObject.PasswordNeverExpires = $existingUserValues.PasswordNeverExpires } # Properties that are used on both Nano and Full Sku $script:UserObject.Name = $existingUserValues.Name $script:UserObject.Description = $existingUserValues.Description $script:UserObject.Enabled = $existingUserValues.Enabled Describe 'UserResource/Get-TargetResource' { Context 'Tests on FullSKU' { Mock -CommandName Test-IsNanoServer -MockWith { return $false } Mock -CommandName Remove-UserOnFullSku -MockWith {} Mock -CommandName Remove-DisposableObject -MockWith {} It 'Should return the user as Present' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $existingUserValues } $getTargetResourceResult = Get-TargetResource -UserName $existingUserValues.Name Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $getTargetResourceResult['UserName'] | Should Be $existingUserValues.Name $getTargetResourceResult['Ensure'] | Should Be 'Present' $getTargetResourceResult['Description'] | Should Be $existingUserValues.Description $getTargetResourceResult['FullName'] | Should Be $existingUserValues.DisplayName $getTargetResourceResult['PasswordChangeRequired'] | Should Be $null $getTargetResourceResult['PasswordChangeNotAllowed'] | Should Be $existingUserValues.UserCannotChangePassword $getTargetResourceResult['PasswordNeverExpires'] | Should Be $existingUserValues.PasswordNeverExpires $getTargetResourceResult['Disabled'] | Should Be (-not $existingUserValues.Enabled) } It 'Should return the user as Absent' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $null } $getTargetResourceResult = Get-TargetResource -UserName 'NotAUserName' Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $getTargetResourceResult['UserName'] | Should Be 'NotAUserName' $getTargetResourceResult['Ensure'] | Should Be 'Absent' } It 'Should throw an Invalid Operaion exception' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { Throw } Mock -CommandName New-InvalidOperationException -MockWith {} Get-TargetResource -UserName 'DuplicateUser' Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName New-InvalidOperationException -Exactly 1 -Scope It } } Context 'Tests on Nano Server' { Mock -CommandName Test-IsNanoServer -MockWith { return $true } It 'Should return the user as Present on Nano Server' { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $existingUserValues } $getTargetResourceResult = Get-TargetResource -UserName $existingUserValues.Name Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It $getTargetResourceResult['UserName'] | Should Be $existingUserValues.Name $getTargetResourceResult['Ensure'] | Should Be 'Present' $getTargetResourceResult['Description'] | Should Be $existingUserValues.Description $getTargetResourceResult['FullName'] | Should Be $existingUserValues.FullName $getTargetResourceResult['PasswordChangeRequired'] | Should Be $null $getTargetResourceResult['PasswordChangeNotAllowed'] | Should Be (-not $existingUserValues.UserMayChangePassword) $getTargetResourceResult['PasswordNeverExpires'] | Should Be (-not $existingUserValues.PasswordExpires) $getTargetResourceResult['Disabled'] | Should Be (-not $existingUserValues.Enabled) } It 'Should return a different user as Present on Nano Server' { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $newUserValues } $getTargetResourceResult = Get-TargetResource -UserName $newUserValues.Name Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It $getTargetResourceResult['UserName'] | Should Be $newUserValues.Name $getTargetResourceResult['Ensure'] | Should Be 'Present' $getTargetResourceResult['Description'] | Should Be $newUserValues.Description $getTargetResourceResult['FullName'] | Should Be $newUserValues.FullName $getTargetResourceResult['PasswordChangeRequired'] | Should Be $null $getTargetResourceResult['PasswordChangeNotAllowed'] | Should Be (-not $newUserValues.UserMayChangePassword) $getTargetResourceResult['PasswordNeverExpires'] | Should Be (-not $newUserValues.PasswordExpires) $getTargetResourceResult['Disabled'] | Should Be (-not $newUserValues.Enabled) } It 'Should return the user as Absent' { Mock -CommandName Find-UserByNameOnNanoServer ` -MockWith { Write-Error -Message 'Test error message' -ErrorId 'UserNotFound' } $getTargetResourceResult = Get-TargetResource -UserName 'NotAUserName' Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It $getTargetResourceResult['UserName'] | Should Be 'NotAUserName' $getTargetResourceResult['Ensure'] | Should Be 'Absent' } It 'Should throw an Invalid Operation exception' { $exception = New-Object -TypeName 'InvalidOperationException' ` -ArgumentList @($null) $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` -ArgumentList @($exception, 'MachineStateIncorrect', 'InvalidOperation', $null) Mock -CommandName Find-UserByNameOnNanoServer -MockWith { Throw } { Get-TargetResource -UserName 'DuplicateUser' } | Should -Throw -ExpectedMessage $errorRecord Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It } } } Describe 'UserResource/Set-TargetResource' { Context 'Tests on FullSKU' { Mock -CommandName Test-IsNanoServer -MockWith { return $false } Mock -CommandName Add-UserOnFullSku -MockWith { return $script:UserObject } Mock -CommandName Remove-UserOnFullSku -MockWith {} Mock -CommandName Save-UserOnFullSku -MockWith {} Mock -CommandName Set-UserPasswordOnFullSku -MockWith {} Mock -CommandName Revoke-UserPassword -MockWith {} Mock -CommandName Remove-DisposableObject -MockWith {} It 'Should remove the user' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $script:UserObject } Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Absent' Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 1 -Scope It } It 'Should add a new user with a password' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $null } Set-TargetResource -UserName $existingUserValues.Name ` -Password $existingUserValues.Password ` -Ensure 'Present' Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $script:UserObject.Name | Should Be $existingUserValues.Name $script:UserObject.DisplayName | Should Be '' } It 'Should update the user' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $script:UserObject } $disabled = $true $passwordNeverExpires = $true $passwordChangeRequired = $false $passwordChangeNotAllowed = $true Set-TargetResource -UserName $existingUserValues.Name ` -Password $existingUserValues.Password ` -Ensure 'Present' ` -FullName $newUserValues.DisplayName ` -Description $newUserValues.Description ` -Disabled $disabled ` -PasswordNeverExpires $passwordNeverExpires ` -PasswordChangeRequired $passwordChangeRequired ` -PasswordChangeNotAllowed $passwordChangeNotAllowed Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-UserPasswordOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Revoke-UserPassword -Exactly 0 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $script:UserObject.Name | Should Be $existingUserValues.Name $script:UserObject.DisplayName | Should Be $newUserValues.DisplayName $script:UserObject.UserCannotChangePassword | Should Be $passwordChangeNotAllowed $script:UserObject.PasswordNeverExpires | Should Be $passwordNeverExpires $script:UserObject.Description | Should Be $newUserValues.Description $script:UserObject.Enabled | Should Be (-not $disabled) } It 'Should update the user again with different values' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $script:UserObject } $disabled = $false $passwordNeverExpires = $false $passwordChangeRequired = $true $passwordChangeNotAllowed = $false Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Present' ` -FullName $modifiableUserValues.DisplayName ` -Description $modifiableUserValues.Description ` -Disabled $disabled ` -PasswordNeverExpires $passwordNeverExpires ` -PasswordChangeRequired $passwordChangeRequired ` -PasswordChangeNotAllowed $passwordChangeNotAllowed Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-UserPasswordOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Revoke-UserPassword -Exactly 1 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $script:UserObject.Name | Should Be $existingUserValues.Name $script:UserObject.DisplayName | Should Be $modifiableUserValues.DisplayName $script:UserObject.UserCannotChangePassword | Should Be $passwordChangeNotAllowed $script:UserObject.PasswordNeverExpires | Should Be $passwordNeverExpires $script:UserObject.Description | Should Be $modifiableUserValues.Description $script:UserObject.Enabled | Should Be (-not $disabled) } It 'Should not update the user if no new values are passed in' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $script:UserObject } Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Present' Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-UserPasswordOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Revoke-UserPassword -Exactly 0 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $script:UserObject.Name | Should Be $existingUserValues.Name $script:UserObject.DisplayName | Should Be $modifiableUserValues.DisplayName $script:UserObject.Description | Should Be $modifiableUserValues.Description } It 'Should not update the user if existing values are passed in' { Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $script:UserObject } Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Present' ` -FullName $modifiableUserValues.DisplayName ` -Description $modifiableUserValues.Description Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Add-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-UserPasswordOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Revoke-UserPassword -Exactly 0 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-DisposableObject -Exactly 1 -Scope It $script:UserObject.Name | Should Be $existingUserValues.Name $script:UserObject.DisplayName | Should Be $modifiableUserValues.DisplayName $script:UserObject.Description | Should Be $modifiableUserValues.Description } It 'Should throw an Invalid Operation exception' { $exception = New-Object -TypeName 'InvalidOperationException' ` -ArgumentList @($null) $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` -ArgumentList @($exception, 'MachineStateIncorrect', 'InvalidOperation', $null) Mock -CommandName Find-UserByNameOnFullSku -MockWith { Throw } { Set-TargetResource -UserName 'DuplicateUser' } | Should -Throw -ExpectedMessage $errorRecord Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Save-UserOnFullSku -Exactly 0 -Scope It } } Context 'Tests on Nano Server' { Mock -CommandName Test-IsNanoServer -MockWith { return $true } Mock -CommandName Test-CredentialsValidOnNanoServer -MockWith { return $true } # Mock -CommandName New-LocalUser -MockWith {} # Mock -CommandName Remove-LocalUser -MockWith {} # Mock -CommandName Disable-LocalUser -MockWith {} # Mock -CommandName Enable-LocalUser -MockWith {} It 'Should add a new user' -Skip:$true { Mock -CommandName Set-LocalUser -MockWith { $modifiableUserValues.FullName = [String]::Empty} Mock -CommandName Find-UserByNameOnNanoServer ` -MockWith { Write-Error -Message 'Test error message' -ErrorId 'UserNotFound' } Set-TargetResource -UserName $modifiableUserValues.Name ` -Ensure 'Present' # Set-LocalUser only called to set FullName Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 1 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 1 -Scope It $modifiableUserValues.Name | Should Be $modifiableUserValues.Name $modifiableUserValues.FullName | Should Be '' } It 'Should remove the user' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $script:UserObject } Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Absent' Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 1 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 0 -Scope It } It 'Should not remove the user if the user does not exist' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer ` -MockWith { Write-Error -Message 'Test error message' -ErrorId 'UserNotFound' } Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName 'NotAUserName' ` -Ensure 'Absent' Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 0 -Scope It } It 'Should update all values of the user' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $existingUserValues } Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName $existingUserValues.Name ` -Password $newUserValues.Password ` -Ensure 'Present' ` -FullName $newUserValues.FullName ` -Description $newUserValues.Description ` -Disabled $existingUserValues.Enabled ` -PasswordNeverExpires (-not $existingUserValues.PasswordNeverExpires) ` -PasswordChangeRequired (-not $existingUserValues.PasswordChangeRequired) ` -PasswordChangeNotAllowed $existingUserValues.UserMayChangePassword <# Set-LocalUser called to: Set the Password Change the FullName Change the Description Change PasswordNeverExpires Set PasswordChangeRequired (which sets the password to expire 'Now') Set UserMayChangePassword #> Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 6 -Scope It Assert-MockCalled -CommandName Disable-LocalUser -Exactly 1 -Scope It Assert-MockCalled -CommandName Enable-LocalUser -Exactly 0 -Scope It } It 'Should update the user with different values' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $modifiableUserValues } $modifiableUserValues.FullName = 'new full name' Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName $modifiableUserValues.Name ` -Password $modifiableUserValues.Password ` -Ensure 'Present' ` -FullName $null ` -Description $null ` -Disabled $modifiableUserValues.Enabled ` -PasswordNeverExpires (-not $modifiableUserValues.PasswordNeverExpires) ` -PasswordChangeRequired (-not $modifiableUserValues.PasswordChangeRequired) ` -PasswordChangeNotAllowed $modifiableUserValues.UserMayChangePassword <# Set-LocalUser called to: Set the Password (even though it's the same) Change the FullName Change the Description Change PasswordNeverExpires Set UserMayChangePassword (Set-LocalUser will not be called to set PasswordChangeRequired if it is set to false, ) #> Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 5 -Scope It Assert-MockCalled -CommandName Disable-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Enable-LocalUser -Exactly 1 -Scope It } It 'Should not update the user if no new values are passed in' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $existingUserValues } Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Present' Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Disable-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Enable-LocalUser -Exactly 0 -Scope It } It 'Should not update the user if existing values are passed in' -Skip:$true { Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $existingUserValues } Mock -CommandName Set-LocalUser -MockWith {} Set-TargetResource -UserName $existingUserValues.Name ` -Ensure 'Present' ` -FullName $existingUserValues.FullName ` -Description $existingUserValues.Description ` -Disabled (-not $existingUserValues.Enabled) ` -PasswordNeverExpires $existingUserValues.PasswordNeverExpires ` -PasswordChangeRequired $existingUserValues.PasswordChangeRequired ` -PasswordChangeNotAllowed (-not $existingUserValues.UserMayChangePassword) Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Disable-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Enable-LocalUser -Exactly 0 -Scope It } It 'Should throw an Invalid Operation exception' -Skip:$true { $exception = New-Object -TypeName 'InvalidOperationException' ` -ArgumentList @($null) $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` -ArgumentList @($exception, 'MachineStateIncorrect', 'InvalidOperation', $null) Mock -CommandName Find-UserByNameOnNanoServer -MockWith { Throw } { Set-TargetResource -UserName 'DuplicateUser' } | Should -Throw -ExpectedMessage $errorRecord Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName New-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Remove-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Set-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Disable-LocalUser -Exactly 0 -Scope It Assert-MockCalled -CommandName Enable-LocalUser -Exactly 0 -Scope It } } } Describe 'UserResource/Test-TargetResource' { Context 'Tests on FullSKU' { Mock -CommandName Test-IsNanoServer -MockWith { return $false } Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $existingUserValues } ` -ParameterFilter { $UserName -eq $existingUserName } $absentUserName = 'AbsentUserUserName123456789' Mock -CommandName Find-UserByNameOnFullSku -MockWith { return $null } ` -ParameterFilter { $UserName -eq $absentUserName } It 'Should return true when user Present and correct values' { Mock -CommandName Test-UserPasswordOnFullSku -MockWith { return $true } $testTargetResourceResult = Test-TargetResource -UserName $existingUserValues.Name ` -Description $existingUserValues.Description ` -Password $existingUserValues.Password ` -Disabled (-not $existingUserValues.Enabled) ` -PasswordNeverExpires $existingUserValues.PasswordNeverExpires ` -PasswordChangeNotAllowed $existingUserValues.UserCanNotChangePassword $testTargetResourceResult | Should -BeTrue Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Test-UserPasswordOnFullSku -Exactly 1 -Scope It } It 'Should return true when user Absent and Ensure = Absent' { $testTargetResourceResult = Test-TargetResource -UserName $absentUserName ` -Ensure 'Absent' $testTargetResourceResult | Should -BeTrue } It 'Should return false when user Absent and Ensure = Present' { $testTargetResourceResult = Test-TargetResource -UserName $absentUserName ` -Ensure 'Present' $testTargetResourceResult | Should -BeFalse } It 'Should return false when user Present and Ensure = Absent' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Ensure 'Absent' $testTargetResourceResult | Should -BeFalse } It 'Should return false when Password is wrong' { Mock -CommandName Test-UserPasswordOnFullSku -MockWith { return $false } $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Password $newUserValues.Password $testTargetResourceResult | Should -BeFalse Assert-MockCalled -CommandName Find-UserByNameOnFullSku -Exactly 1 -Scope It Assert-MockCalled -CommandName Test-UserPasswordOnFullSku -Exactly 1 -Scope It } It 'Should return false when user Present and wrong Description' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Description 'Wrong description' $testTargetResourceResult | Should -BeFalse } It 'Should return false when FullName is incorrect' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -FullName 'Wrong FullName' $testTargetResourceResult | Should -BeFalse } It 'Should return false when Disabled is incorrect' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Disabled $existingUserValues.Enabled $testTargetResourceResult | Should -BeFalse } It 'Should return false when PasswordNeverExpires is incorrect' { $testTargetResourceResult = Test-TargetResource ` -UserName $existingUserName ` -PasswordNeverExpires (-not $existingUserValues.PasswordNeverExpires) $testTargetResourceResult | Should -BeFalse } It 'Should return false when PasswordChangeNotAllowed is incorrect' { $testTargetResourceResult = Test-TargetResource ` -UserName $existingUserName ` -PasswordChangeNotAllowed $existingUserValues.UserMayChangePassword $testTargetResourceResult | Should -BeFalse } } Context 'Tests on Nano Server' { Mock -CommandName Test-IsNanoServer -MockWith { return $true } Mock -CommandName Find-UserByNameOnNanoServer -MockWith { return $existingUserValues } ` -ParameterFilter { $UserName -eq $existingUserName } $absentUserName = 'AbsentUserUserName123456789' Mock -CommandName Find-UserByNameOnNanoServer ` -MockWith { Write-Error -Message 'Test error message' -ErrorId 'UserNotFound' } ` -ParameterFilter { $UserName -eq $absentUserName } $duplicateUserName = 'DuplicateUserName' Mock -CommandName Find-UserByNameOnNanoServer -MockWith { Throw } ` -ParameterFilter { $UserName -eq $duplicateUserName } It 'Should return true when user Present and correct values' { Mock -CommandName Test-CredentialsValidOnNanoServer { return $true } $testTargetResourceResult = Test-TargetResource -UserName $existingUserValues.Name ` -Description $existingUserValues.Description ` -Password $existingUserValues.Password ` -Disabled (-not $existingUserValues.Enabled) ` -PasswordNeverExpires $existingUserValues.PasswordNeverExpires ` -PasswordChangeNotAllowed $existingUserValues.UserCanNotChangePassword $testTargetResourceResult | Should -BeTrue Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName Test-CredentialsValidOnNanoServer -Exactly 1 -Scope It } It 'Should return true when user Absent and Ensure = Absent' { $testTargetResourceResult = Test-TargetResource -UserName $absentUserName ` -Ensure 'Absent' $testTargetResourceResult | Should -BeTrue } It 'Should return false when user Absent and Ensure = Present' { $testTargetResourceResult = Test-TargetResource -UserName $absentUserName ` -Ensure 'Present' $testTargetResourceResult | Should -BeFalse } It 'Should return false when user Present and Ensure = Absent' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Ensure 'Absent' $testTargetResourceResult | Should -BeFalse } It 'Should return false when Password is wrong' { Mock -CommandName Test-CredentialsValidOnNanoServer { return $false } $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Password $newUserValues.Password $testTargetResourceResult | Should -BeFalse Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It Assert-MockCalled -CommandName Test-CredentialsValidOnNanoServer -Exactly 1 -Scope It } It 'Should return false when user Present and wrong Description' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Description 'Wrong description' $testTargetResourceResult | Should -BeFalse } It 'Should return false when FullName is incorrect' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -FullName 'Wrong FullName' $testTargetResourceResult | Should -BeFalse } It 'Should return false when Disabled is incorrect' { $testTargetResourceResult = Test-TargetResource -UserName $existingUserName ` -Disabled $existingUserValues.Enabled $testTargetResourceResult | Should -BeFalse } It 'Should return false when PasswordNeverExpires is incorrect' { $testTargetResourceResult = Test-TargetResource ` -UserName $existingUserName ` -PasswordNeverExpires (-not $existingUserValues.PasswordNeverExpires) $testTargetResourceResult | Should -BeFalse } It 'Should return false when PasswordChangeNotAllowed is incorrect' { $testTargetResourceResult = Test-TargetResource ` -UserName $existingUserName ` -PasswordChangeNotAllowed $existingUserValues.UserMayChangePassword $testTargetResourceResult | Should -BeFalse } It 'Should throw an Invalid Operation exception when there are multiple users with the given name' { $exception = New-Object -TypeName 'InvalidOperationException' ` -ArgumentList @($null) $errorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord ` -ArgumentList @($exception, 'MachineStateIncorrect', 'InvalidOperation', $null) { Test-TargetResource -UserName $duplicateUserName } | Should -Throw -ExpectedMessage $errorRecord Assert-MockCalled -CommandName Find-UserByNameOnNanoServer -Exactly 1 -Scope It } } } Describe 'UserResource/Assert-UserNameValid' { It 'Should not throw when username contains all valid chars' { { Assert-UserNameValid -UserName 'abc123456!f_t-l098s' } | Should Not Throw } It 'Should throw InvalidArgumentError when username contains only whitespace and dots' { $invalidName = ' . .. . ' $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument $errorId = 'UserNameHasOnlyWhiteSpacesAndDots' $errorMessage = "The name $invalidName cannot be used." $exception = New-Object System.ArgumentException $errorMessage; $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $null { Assert-UserNameValid -UserName $invalidName } | Should -Throw -ExpectedMessage $errorRecord } It 'Should throw InvalidArgumentError when username contains an invalid char' { $invalidName = 'user|name' $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument $errorId = 'UserNameHasInvalidCharachter' $errorMessage = "The name $invalidName cannot be used." $exception = New-Object System.ArgumentException $errorMessage; $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $null { Assert-UserNameValid -UserName $invalidName } | Should -Throw -ExpectedMessage $errorRecord } } } finally { foreach ($disposableObject in $script:disposableObjects) { $disposableObject.Dispose() } } } } finally { Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment } |