Skip to content

Commit

Permalink
ADObjectPermissionEntry: Update Assert-ADPSDrive with PSProvider Chec…
Browse files Browse the repository at this point in the history
…ks (#528)

- Changes to ADObjectPermissionEntry
  - Updated Assert-ADPSDrive with PSProvider Checks (issue #527).
  • Loading branch information
X-Guardian authored and johlju committed Nov 16, 2019
1 parent 29f7bad commit 6942331
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Resource Integration tests added.
- Changes to ADKDSKey
- Added Integration testing ([issue #351](https://github.com/PowerShell/ActiveDirectoryDsc/issues/351))
- Changes to ADObjectPermissionEntry
- Updated Assert-ADPSDrive with PSProvider Checks ([issue #527](https://github.com/PowerShell/ActiveDirectoryDsc/issues/527)).

## 4.2.0.0

Expand Down
36 changes: 35 additions & 1 deletion Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,38 @@ function Test-DscPropertyState
return $returnValue
}

<#
.SYNOPSIS
Asserts if the AD PS Provider has been installed.
.NOTES
Attempts to force import the ActiveDirectory module if the AD PS Provider has not been installed
and throws an exception if the AD PS Provider cannot be installed.
#>

function Assert-ADPSProvider
{
[CmdletBinding()]
param()

$activeDirectoryPSProvider = Get-PSProvider -PSProvider 'ActiveDirectory' -ErrorAction SilentlyContinue

if ($null -eq $activeDirectoryPSProvider)
{
Write-Verbose -Message $script:localizedData.AdPsProviderNotFound -Verbose
Import-Module -Name 'ActiveDirectory' -Force
try
{
$activeDirectoryPSProvider = Get-PSProvider -PSProvider 'ActiveDirectory'
}
catch
{
$errorMessage = $script:localizedData.AdPsProviderInstallFailureError
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
}
}

<#
.SYNOPSIS
Asserts if the AD PS Drive has been created, and creates one if not.
Expand All @@ -1900,7 +1932,9 @@ function Assert-ADPSDrive
$Root = '//RootDSE/'
)

Assert-Module -ModuleName 'ActiveDirectory' -ImportModule
Assert-Module -ModuleName 'ActiveDirectory'

Assert-ADPSProvider

$activeDirectoryPSDrive = Get-PSDrive -Name AD -ErrorAction SilentlyContinue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ ConvertFrom-StringData @'
SearchingForDomainControllerInSite = Searching for a domain controller in the site '{0}' in the domain '{1}'. (ADCOMMON0053)
IgnoreCredentialError = Suppressing the credential error '{0}' with the message '{1}'. (ADCOMMON0054)
NoObjectFoundInRecycleBin = Did not find a restorable object in the recycle bin. (ADCOMMON0055)
AdPsProviderNotFound = The Active Directory PS Provider was not found, Forcing import of the ActiveDirectory module. (ADCOMMON0056)
AdPsProviderInstallFailureError = Error installing the Active Directory PS Provider. (ADCOMMON0057)
'@
163 changes: 126 additions & 37 deletions Tests/Unit/ActiveDirectory.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2171,69 +2171,158 @@ InModuleScope 'ActiveDirectoryDsc.Common' {
}
}

Describe 'ActiveDirectoryDsc.Common\Assert-ADPSDrive' {
Mock -CommandName Assert-Module
Describe 'ActiveDirectoryDsc.Common\Assert-ADPSProvider' {
BeforeAll {

Context 'When the AD PS Drive does not exist and the New-PSDrive function is successful' {
Mock -CommandName Get-PSDrive -MockWith { $null }
Mock -CommandName New-PSDrive
}

It 'Should not throw' {
{ Assert-ADPSDrive } | Should -Not -Throw
}
Context 'When the AD PS Provider is installed' {
BeforeAll {
$mockPSProviderResult = @{
Name = 'ActiveDirectory'
}

It 'Should have called Assert-Module' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1 -Scope Context
Mock -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' } `
-MockWith { $mockPSProviderResult }
Mock -CommandName Import-Module
}

It 'Should have called Get-PSDrive only once' {
Assert-MockCalled -CommandName Get-PSDrive -Exactly -Times 1 -Scope Context
It 'Should not throw' {
{ Assert-ADPSProvider -Verbose } | Should -Not -Throw
}

It 'Should have called New-PSDrive only once' {
Assert-MockCalled -CommandName New-PSDrive -Exactly -Times 1 -Scope Context
It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' } `
-Exactly -Times 1
Assert-MockCalled -CommandName Import-Module -Exactly -Times 0
}
}

Context 'When the AD PS Drive does not exist and the New-PSDrive function is not successful' {
Mock -CommandName Get-PSDrive -MockWith { $null }
Mock -CommandName New-PSDrive -MockWith { throw }
Context 'When the AD PS Provider is not installed' {

It 'Should throw the correct error' {
{ Assert-ADPSDrive } | Should -Throw $script:localizedString.CreatingNewADPSDriveError
Context 'When the AD PS Provider is successfully installed by Import-Module' {
BeforeAll {
$mockPSProviderResult = @{
Name = 'ActiveDirectory'
}

Mock -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' }
Mock -CommandName Import-Module
Mock -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -ne 'SilentlyContinue' } `
-MockWith { $mockPSProviderResult }
}

It 'Should not throw' {
{ Assert-ADPSProvider } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' } `
-Exactly -Times 1
Assert-MockCalled -CommandName Import-Module -Exactly -Times 1
Assert-MockCalled -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -ne 'SilentlyContinue' } `
-Exactly -Times 1
}
}

It 'Should call Assert-Module' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1 -Scope Context
Context 'When the AD PS Provider is not successfully installed by Import-Module' {
BeforeAll {
Mock -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' }
Mock -CommandName Import-Module
Mock -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -ne 'SilentlyContinue' } `
-MockWith { Throw 'Error'}
}

It 'Should throw the correct exception' {
{ Assert-ADPSProvider } | Should -Throw $script:localizedData.AdPsProviderInstallFailureError
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -eq 'SilentlyContinue' } `
-Exactly -Times 1
Assert-MockCalled -CommandName Import-Module -Exactly -Times 1
Assert-MockCalled -CommandName Get-PSProvider `
-ParameterFilter { $PSBoundParameters['ErrorAction'] -ne 'SilentlyContinue' } `
-Exactly -Times 1
}
}
}
}

It 'Should call Get-PSDrive once' {
Assert-MockCalled -CommandName Get-PSDrive -Exactly -Times 1 -Scope Context
Describe 'ActiveDirectoryDsc.Common\Assert-ADPSDrive' {
BeforeAll {
$defaultPSDriveRoot = '//RootDSE/'
Mock -CommandName Assert-Module
Mock -CommandName Assert-ADPSProvider
}

Context 'When the AD PS Drive does not exist' {
BeforeAll {
Mock -CommandName Get-PSDrive
}

It 'Should call New-PSDrive once' {
Assert-MockCalled -CommandName New-PSDrive -Exactly -Times 1 -Scope Context
Context 'When the New-PSDrive function is successful' {
BeforeAll {
Mock -CommandName New-PSDrive
}

It 'Should not throw' {
{ Assert-ADPSDrive } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1
Assert-MockCalled -CommandName Get-PSDrive -Exactly -Times 1
Assert-MockCalled -CommandName New-PSDrive `
-ParameterFilter { $Root -eq $defaultPSDriveRoot } `
-Exactly -Times 1
}
}
}

Context 'When the AD PS Drive already exists' {
Mock -CommandName Get-PSDrive -MockWith { New-MockObject -Type System.Management.Automation.PSDriveInfo }
Mock -CommandName New-PSDrive
Context 'When the New-PSDrive function is not successful' {
BeforeAll {
Mock -CommandName New-PSDrive -MockWith { throw }
}

It 'Should not throw' {
{ Assert-ADPSDrive } | Should -Not -Throw
It 'Should throw the correct error' {
{ Assert-ADPSDrive } | Should -Throw $script:localizedString.CreatingNewADPSDriveError
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1
Assert-MockCalled -CommandName Get-PSDrive -Exactly -Times 1
Assert-MockCalled -CommandName New-PSDrive `
-ParameterFilter { $Root -eq $defaultPSDriveRoot } `
-Exactly -Times 1
}
}
}

It 'Should call Assert-Module only once' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1 -Scope Context
Context 'When the AD PS Drive already exists' {
BeforeAll {
Mock -CommandName Get-PSDrive -MockWith { New-MockObject -Type System.Management.Automation.PSDriveInfo }
Mock -CommandName New-PSDrive
}

It 'Should call Get-PSDrive only once' {
Assert-MockCalled -CommandName Get-PSDrive -Exactly -Times 1 -Scope Context
It 'Should not throw' {
{ Assert-ADPSDrive } | Should -Not -Throw
}

It 'Should not call New-PSDrive' {
Assert-MockCalled -CommandName New-PSDrive -Exactly -Times 0 -Scope Context
It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Assert-Module -Exactly -Times 1
Assert-MockCalled -CommandName Get-PSDrive `
-ParameterFilter { $Name -eq 'AD' } `
-Exactly -Times 1
Assert-MockCalled -CommandName New-PSDrive -Exactly -Times 0
}
}
}
Expand Down

0 comments on commit 6942331

Please sign in to comment.