Skip to content

Commit

Permalink
ADUser: Refactor Module and Unit Tests (#569)
Browse files Browse the repository at this point in the history
- ADUser
  - Improve Try/Catch blocks to only cover cmdlet calls.
  - Move the Test-Password function to the ActiveDirectoryDsc.Common module and add unit tests.
  - Reformat code to keep line lengths to less than 120 characters.
  - Fix Password parameter processing when PasswordNeverResets is $true.
  - Remove unnecessary Enabled parameter check.
  - Remove unnecessary Clear explicit parameter check.
  - Add check to only call Set-ADUser if there are properties to change.
  - Refactored Unit Tests - (issue #467)
  • Loading branch information
X-Guardian authored Mar 9, 2020
1 parent ff7d92f commit 1d91e81
Show file tree
Hide file tree
Showing 11 changed files with 2,548 additions and 2,178 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md)
2012 R2.
- ADForestProperties
- Refactored unit tests.
- ADUser
- Improve Try/Catch blocks to only cover cmdlet calls.
- Move the Test-Password function to the ActiveDirectoryDsc.Common module and add unit tests.
- Reformat code to keep line lengths to less than 120 characters.
- Fix Password parameter processing when PasswordNeverResets is $true.
- Remove unnecessary Enabled parameter check.
- Remove unnecessary Clear explicit parameter check.
- Add check to only call Set-ADUser if there are properties to change.
- Refactored Unit Tests - ([issue #467](https://github.com/dsccommunity/ActiveDirectoryDsc/issues/467))

## [5.0.0] - 2020-01-14

Expand Down
251 changes: 35 additions & 216 deletions Tests/Integration/MSFT_ADUser.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
<#
.SYNOPSIS
Pester integration test for the ADUser Resource of the ActiveDirectoryDsc Module
.DESCRIPTION
Verbose/Debug output can be set by running:
Invoke-pester -Script @{Path='.\MSFT_ADUser.Integration.Tests.ps1';Parameters=@{Verbose=$true;Debug=$true}}
#>

[CmdletBinding()]
param ()

Set-StrictMode -Version 1.0

$script:dscModuleName = 'ActiveDirectoryDsc'
$script:dscResourceFriendlyName = 'ADUser'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"

try
{
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop'
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' -Verbose:$false
}
catch [System.IO.FileNotFoundException]
{
Expand All @@ -26,251 +41,55 @@ try
BeforeAll {
$resourceId = "[$($script:dscResourceFriendlyName)]Integration_Test"
}
foreach ($testName in $ConfigurationData.AllNodes.Tests.Keys)
{
$configurationName = "$($script:dscResourceName)_$($testName)_Config"

$configurationName = "$($script:dscResourceName)_CreateUser1_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
Context ('When using configuration {0}' -f $configurationName) {
BeforeAll {
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters
& $configurationName @configurationParameters -Debug:$false

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.UserName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.UserPrincipalName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.DisplayName | Should -Be $ConfigurationData.AllNodes.DisplayName1
$resourceCurrentState.Credential | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

$configurationName = "$($script:dscResourceName)_UpdateThumbnailPhotoUsingBase64_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
It 'Should compile and apply the MOF without throwing' {
{ Start-DscConfiguration @startDscConfigurationParameters } |
Should -Not -Throw
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.UserName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.ThumbnailPhoto | Should -Be $ConfigurationData.AllNodes.ThumbnailPhotoBase64
$resourceCurrentState.ThumbnailPhotoHash | Should -Be $ConfigurationData.AllNodes.ThumbnailPhotoHash
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

$configurationName = "$($script:dscResourceName)_RemoveThumbnailPhoto_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
It 'Should be able to call Get-DscConfiguration without throwing' {
{ $script:currentConfiguration = Get-DscConfiguration -ErrorAction Stop } |
Should -Not -Throw
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.UserName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.ThumbnailPhoto | Should -BeNullOrEmpty
$resourceCurrentState.ThumbnailPhotoHash | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

$configurationName = "$($script:dscResourceName)_UpdateThumbnailPhotoFromFile_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.UserName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.ThumbnailPhoto | Should -Be $ConfigurationData.AllNodes.ThumbnailPhotoBase64
$resourceCurrentState.ThumbnailPhotoHash | Should -Be $ConfigurationData.AllNodes.ThumbnailPhotoHash
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
}
}

$configurationName = "$($script:dscResourceName)_RemoveUser1_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
foreach ($property in $ConfigurationData.AllNodes.Tests.$testName.Keys)
{
$configurationParameters = @{
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
It "Should have set the correct '$property' property" {
$resourceCurrentState.$property | Sort-Object |
Should -Be ($ConfigurationData.AllNodes.Tests.$testName.$property | Sort-Object)
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop
} | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Absent'
$resourceCurrentState.UserName | Should -Be $ConfigurationData.AllNodes.UserName1
$resourceCurrentState.UserPrincipalName | Should -BeNullOrEmpty
$resourceCurrentState.DisplayName | Should -BeNullOrEmpty
$resourceCurrentState.Credential | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be 'True'
It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration | Should -Be 'True'
}
}
}
}
Expand Down
Loading

0 comments on commit 1d91e81

Please sign in to comment.