Skip to content

Commit

Permalink
Merge pull request #1374 from ykuijs/master
Browse files Browse the repository at this point in the history
Fix issue in SPTrustedRootAuthority and simplified running Pester tests
  • Loading branch information
ykuijs authored Jan 18, 2022
2 parents 0dc55bd + 8acb583 commit 5a735fc
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
* @ykuijs
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SharePointDsc
- Added generic unit tests files to quickly run all or a specific unit test
- SPTrustedIdentityTokenIssuer
- Added parameters to support OIDC authentication in SharePoint Server Subscription Edition

### Changed

- SPFarm
- Suppress a useless reboot that was triggered once a server joined the farm
- Suppress a useless 5 minutes sleep triggered once a server joined the farm

### Fixed

- SPTrustedRootAuthority
- Fixed issue where certificates not in the Personal store could not be used

## [5.0.0] - 2021-12-17

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ function Set-TargetResource
else
{
Write-Verbose -Message "Importing certificate from CertificateThumbprint"
$cert = Get-Item -Path "CERT:\LocalMachine\My\$($params.CertificateThumbprint)" `
-ErrorAction SilentlyContinue
$cert = Get-ChildItem -Path "Cert:\LocalMachine\*$($params.CertificateThumbprint)" `
-ErrorAction SilentlyContinue | `
Sort-Object -Property PSParentPath -Descending | `
Select-Object -First 1

if ($null -eq $cert)
{
Expand Down Expand Up @@ -262,8 +264,10 @@ function Set-TargetResource
else
{
Write-Verbose -Message "Importing certificate from CertificateThumbprint"
$cert = Get-Item -Path "CERT:\LocalMachine\My\$($params.CertificateThumbprint)" `
-ErrorAction SilentlyContinue
$cert = Get-ChildItem -Path "Cert:\LocalMachine\*$($params.CertificateThumbprint)" `
-ErrorAction SilentlyContinue | `
Sort-Object -Property PSParentPath -Descending | `
Select-Object -First 1

if ($null -eq $cert)
{
Expand Down
57 changes: 57 additions & 0 deletions tests/RunUnitTestsForAllResources.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#Requires -RunAsAdministrator

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateSet("2013", "2016", "2019", "Subscription")]
[System.String]
$SharePointVersion,

[Parameter()]
[Switch]
$DoNotBuildModule
)

$modulePath = Split-Path -Path $PSScriptRoot

if ($DoNotBuildModule -eq $false)
{
& $modulePath\build.ps1 -Tasks Build
}

$testPath = Join-Path -Path $modulePath -ChildPath "\Tests\Unit\SharePointDsc"

$params = @{
Tasks = 'Test'
}

switch ($SharePointVersion)
{
"2013"
{
$stubPath = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\15.0.4805.1000\Microsoft.SharePoint.PowerShell.psm1'
}
"2016"
{
$stubPath = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.4456.1000\Microsoft.SharePoint.PowerShell.psm1'
}
"2019"
{
$stubPath = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.10337.12109\Microsoft.SharePoint.PowerShell.psm1'
}
"Subscription"
{
$stubPath = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.14326.20450/SharePointServer.psm1'
}
}

$params.PesterScript = @(
@{
Path = $testPath
Parameters = @{
SharePointCmdletModule = $stubPath
}
}
)

& $modulePath\build.ps1 @params
34 changes: 34 additions & 0 deletions tests/RunUnitTestsForResource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Requires -RunAsAdministrator

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.String]
$Resource,

[Parameter()]
[Switch]
$DoNotBuildModule
)

$modulePath = Split-Path -Path $PSScriptRoot

if ($DoNotBuildModule -eq $false)
{
& $modulePath\build.ps1 -Tasks Build
}

$path15 = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\15.0.4805.1000\Microsoft.SharePoint.PowerShell.psm1'
$path16 = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.4456.1000\Microsoft.SharePoint.PowerShell.psm1'
$path19 = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.10337.12109\Microsoft.SharePoint.PowerShell.psm1'
$pathSE = Join-Path -Path $modulePath -ChildPath '\Tests\Unit\Stubs\SharePoint\16.0.14326.20450\SharePointServer.psm1'

$testPath = Join-Path -Path $modulePath -ChildPath ".\Tests\Unit\SharePointDsc\SharePointDsc.$resource.Tests.ps1"
$compiledModulePath = Split-Path -Path (Get-Module SharePointDsc -ListAvailable).Path
$resourcePath = Join-Path -Path $compiledModulePath -ChildPath "\DSCResources\MSFT_$resource\MSFT_$resource.psm1"
Invoke-Pester -Script @(
@{ Path = $testPath; Parameters = @{SharePointCmdletModule = $path15 } },
@{ Path = $testPath; Parameters = @{SharePointCmdletModule = $path16 } },
@{ Path = $testPath; Parameters = @{SharePointCmdletModule = $path19 } },
@{ Path = $testPath; Parameters = @{SharePointCmdletModule = $pathSE } }
) -CodeCoverage $resourcePath
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ try
InModuleScope -ModuleName $script:DSCResourceFullName -ScriptBlock {
Describe -Name $Global:SPDscHelper.DescribeHeader -Fixture {
BeforeAll {
Invoke-Command -ScriptBlock $Global:SPDscHelper.InitializeScript -NoNewScope
Invoke-Command -Scriptblock $Global:SPDscHelper.InitializeScript -NoNewScope

Mock -CommandName Remove-SPTrustedRootAuthority -MockWith { }
Mock -CommandName Set-SPTrustedRootAuthority -MockWith { }
Expand Down Expand Up @@ -166,7 +166,7 @@ try
Ensure = "Present"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -208,7 +208,7 @@ try
Ensure = "Present"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -250,7 +250,7 @@ try
Ensure = "Present"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -396,7 +396,7 @@ try
}
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -440,7 +440,7 @@ try
return $true
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -540,7 +540,7 @@ try
}
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return $null
}
}
Expand Down Expand Up @@ -570,7 +570,7 @@ try
return $null
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return $null
}
}
Expand All @@ -596,7 +596,7 @@ try
Ensure = "Present"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertIdentifier"
Thumbprint = $testParams.CertificateThumbprint
Expand All @@ -618,7 +618,7 @@ try

It "Should create a new trusted root authority in the set method" {
Set-TargetResource @testParams
Assert-MockCalled Get-Item -Times 1
Assert-MockCalled Get-ChildItem -Times 1
Assert-MockCalled New-SPTrustedRootAuthority -Times 1
}
}
Expand All @@ -635,7 +635,7 @@ try
return $true
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -685,7 +685,7 @@ try
return $true
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertName"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -731,7 +731,7 @@ try
Ensure = "Present"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
$retVal = [pscustomobject]@{
Subject = "CN=CertIdentifier"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -780,7 +780,7 @@ try

It "Should create a new Trusted Root Authority in the set method" {
Set-TargetResource @testParams
Assert-MockCalled Get-Item -Times 1
Assert-MockCalled Get-ChildItem -Times 1
Assert-MockCalled Set-SPTrustedRootAuthority -Times 1
Assert-MockCalled New-Object -Times 1
}
Expand All @@ -794,7 +794,7 @@ try
Ensure = "Absent"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertIdentifier"
Thumbprint = $testParams.CertificateThumbprint
Expand Down Expand Up @@ -833,7 +833,7 @@ try
Ensure = "Absent"
}

Mock -CommandName Get-Item -MockWith {
Mock -CommandName Get-ChildItem -MockWith {
return @{
Subject = "CN=CertIdentifier"
Thumbprint = $testParams.CertificateThumbprint
Expand Down

0 comments on commit 5a735fc

Please sign in to comment.