Initial Commit
This commit is contained in:
27
tests/Unit/Private/Assert-ModuleAvailability.tests.ps1
Normal file
27
tests/Unit/Private/Assert-ModuleAvailability.tests.ps1
Normal file
@@ -0,0 +1,27 @@
|
||||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
|
||||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
|
||||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
|
||||
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
|
||||
}).BaseName
|
||||
|
||||
|
||||
Import-Module $ProjectName
|
||||
|
||||
InModuleScope $ProjectName {
|
||||
Describe Get-PrivateFunction {
|
||||
Context 'Default' {
|
||||
BeforeEach {
|
||||
$return = Get-PrivateFunction -PrivateData 'string'
|
||||
}
|
||||
|
||||
It 'Returns a single object' {
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'Returns a string based on the parameter PrivateData' {
|
||||
$return | Should -Be 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
tests/Unit/Private/Connect-M365Suite.tests.ps1
Normal file
27
tests/Unit/Private/Connect-M365Suite.tests.ps1
Normal file
@@ -0,0 +1,27 @@
|
||||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
|
||||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
|
||||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
|
||||
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
|
||||
}).BaseName
|
||||
|
||||
|
||||
Import-Module $ProjectName
|
||||
|
||||
InModuleScope $ProjectName {
|
||||
Describe Get-PrivateFunction {
|
||||
Context 'Default' {
|
||||
BeforeEach {
|
||||
$return = Get-PrivateFunction -PrivateData 'string'
|
||||
}
|
||||
|
||||
It 'Returns a single object' {
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'Returns a string based on the parameter PrivateData' {
|
||||
$return | Should -Be 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
tests/Unit/Private/Disconnect-M365Suite.tests.ps1
Normal file
27
tests/Unit/Private/Disconnect-M365Suite.tests.ps1
Normal file
@@ -0,0 +1,27 @@
|
||||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
|
||||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
|
||||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
|
||||
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
|
||||
}).BaseName
|
||||
|
||||
|
||||
Import-Module $ProjectName
|
||||
|
||||
InModuleScope $ProjectName {
|
||||
Describe Get-PrivateFunction {
|
||||
Context 'Default' {
|
||||
BeforeEach {
|
||||
$return = Get-PrivateFunction -PrivateData 'string'
|
||||
}
|
||||
|
||||
It 'Returns a single object' {
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'Returns a string based on the parameter PrivateData' {
|
||||
$return | Should -Be 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
tests/Unit/Private/Get-PrivateFunction.tests.ps1
Normal file
31
tests/Unit/Private/Get-PrivateFunction.tests.ps1
Normal file
@@ -0,0 +1,31 @@
|
||||
BeforeAll {
|
||||
$script:dscModuleName = 'M365FoundationsCISReport'
|
||||
|
||||
Import-Module -Name $script:dscModuleName
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
# Unload the module being tested so that it doesn't impact any other tests.
|
||||
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
|
||||
}
|
||||
|
||||
Describe Get-PrivateFunction {
|
||||
Context 'When calling the function with string value' {
|
||||
It 'Should return a single object' {
|
||||
InModuleScope -ModuleName $dscModuleName {
|
||||
$return = Get-PrivateFunction -PrivateData 'string'
|
||||
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
}
|
||||
|
||||
It 'Should return a string based on the parameter PrivateData' {
|
||||
InModuleScope -ModuleName $dscModuleName {
|
||||
$return = Get-PrivateFunction -PrivateData 'string'
|
||||
|
||||
$return | Should -Be 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
91
tests/Unit/Public/Get-Something.tests.ps1
Normal file
91
tests/Unit/Public/Get-Something.tests.ps1
Normal file
@@ -0,0 +1,91 @@
|
||||
BeforeAll {
|
||||
$script:dscModuleName = 'M365FoundationsCISReport'
|
||||
|
||||
Import-Module -Name $script:dscModuleName
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
# Unload the module being tested so that it doesn't impact any other tests.
|
||||
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
|
||||
}
|
||||
|
||||
Describe Get-Something {
|
||||
BeforeAll {
|
||||
Mock -CommandName Get-PrivateFunction -MockWith {
|
||||
# This return the value passed to the Get-PrivateFunction parameter $PrivateData.
|
||||
$PrivateData
|
||||
} -ModuleName $dscModuleName
|
||||
}
|
||||
|
||||
Context 'When passing values using named parameters' {
|
||||
It 'Should call the private function once' {
|
||||
{ Get-Something -Data 'value' } | Should -Not -Throw
|
||||
|
||||
Should -Invoke -CommandName Get-PrivateFunction -Exactly -Times 1 -Scope It -ModuleName $dscModuleName
|
||||
}
|
||||
|
||||
It 'Should return a single object' {
|
||||
$return = Get-Something -Data 'value'
|
||||
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
|
||||
It 'Should return the correct string value' {
|
||||
$return = Get-Something -Data 'value'
|
||||
|
||||
$return | Should -Be 'value'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When passing values over the pipeline' {
|
||||
It 'Should call the private function two times' {
|
||||
{ 'value1', 'value2' | Get-Something } | Should -Not -Throw
|
||||
|
||||
Should -Invoke -CommandName Get-PrivateFunction -Exactly -Times 2 -Scope It -ModuleName $dscModuleName
|
||||
}
|
||||
|
||||
It 'Should return an array with two items' {
|
||||
$return = 'value1', 'value2' | Get-Something
|
||||
|
||||
$return.Count | Should -Be 2
|
||||
}
|
||||
|
||||
It 'Should return an array with the correct string values' {
|
||||
$return = 'value1', 'value2' | Get-Something
|
||||
|
||||
$return[0] | Should -Be 'value1'
|
||||
$return[1] | Should -Be 'value2'
|
||||
}
|
||||
|
||||
It 'Should accept values from the pipeline by property name' {
|
||||
$return = 'value1', 'value2' | ForEach-Object {
|
||||
[PSCustomObject]@{
|
||||
Data = $_
|
||||
OtherProperty = 'other'
|
||||
}
|
||||
} | Get-Something
|
||||
|
||||
$return[0] | Should -Be 'value1'
|
||||
$return[1] | Should -Be 'value2'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When passing WhatIf' {
|
||||
It 'Should support the parameter WhatIf' {
|
||||
(Get-Command -Name 'Get-Something').Parameters.ContainsKey('WhatIf') | Should -Be $true
|
||||
}
|
||||
|
||||
It 'Should not call the private function' {
|
||||
{ Get-Something -Data 'value' -WhatIf } | Should -Not -Throw
|
||||
|
||||
Should -Invoke -CommandName Get-PrivateFunction -Exactly -Times 0 -Scope It -ModuleName $dscModuleName
|
||||
}
|
||||
|
||||
It 'Should return $null' {
|
||||
$return = Get-Something -Data 'value' -WhatIf
|
||||
|
||||
$return | Should -BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
71
tests/Unit/Public/Invoke-M365SecurityAudit.tests.ps1
Normal file
71
tests/Unit/Public/Invoke-M365SecurityAudit.tests.ps1
Normal file
@@ -0,0 +1,71 @@
|
||||
BeforeAll {
|
||||
$script:moduleName = '<% $PLASTER_PARAM_ModuleName %>'
|
||||
|
||||
# If the module is not found, run the build task 'noop'.
|
||||
if (-not (Get-Module -Name $script:moduleName -ListAvailable))
|
||||
{
|
||||
# Redirect all streams to $null, except the error stream (stream 2)
|
||||
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
|
||||
}
|
||||
|
||||
# Re-import the module using force to get any code changes between runs.
|
||||
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
|
||||
|
||||
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName
|
||||
$PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName
|
||||
$PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
$PSDefaultParameterValues.Remove('Mock:ModuleName')
|
||||
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
|
||||
$PSDefaultParameterValues.Remove('Should:ModuleName')
|
||||
|
||||
Remove-Module -Name $script:moduleName
|
||||
}
|
||||
|
||||
Describe Get-Something {
|
||||
|
||||
Context 'Return values' {
|
||||
BeforeEach {
|
||||
$return = Get-Something -Data 'value'
|
||||
}
|
||||
|
||||
It 'Returns a single object' {
|
||||
($return | Measure-Object).Count | Should -Be 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Context 'Pipeline' {
|
||||
It 'Accepts values from the pipeline by value' {
|
||||
$return = 'value1', 'value2' | Get-Something
|
||||
|
||||
$return[0] | Should -Be 'value1'
|
||||
$return[1] | Should -Be 'value2'
|
||||
}
|
||||
|
||||
It 'Accepts value from the pipeline by property name' {
|
||||
$return = 'value1', 'value2' | ForEach-Object {
|
||||
[PSCustomObject]@{
|
||||
Data = $_
|
||||
OtherProperty = 'other'
|
||||
}
|
||||
} | Get-Something
|
||||
|
||||
|
||||
$return[0] | Should -Be 'value1'
|
||||
$return[1] | Should -Be 'value2'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'ShouldProcess' {
|
||||
It 'Supports WhatIf' {
|
||||
(Get-Command Get-Something).Parameters.ContainsKey('WhatIf') | Should -Be $true
|
||||
{ Get-Something -Data 'value' -WhatIf } | Should -Not -Throw
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user