Private/Tests/ConvertTo-SecretKey.Tests.ps1

<#
 
# ConvertTo-SecretKey (UTest)
 
Ohne Beschreibung
 
- **Hashtags** UTest Pester
- **Version** 2019.09.09
 
#>

# ? TITEL
# ? DESCRIPTION
# ? TAGS
# ? VERSION

using Module Microsoft.WSMan.Management
using Module Microsoft.PowerShell.Security
using Module CimCmdlets
using Module Pester
using namespace System
using namespace System.Runtime.InteropServices
using namespace System.Management.Automation
$ErrorActionPreference = [ActionPreference]::Continue
Set-StrictMode -Version Latest

$CmdletRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$CmdletName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.ps1', [String]::Empty
Set-Location -Path (Join-Path -Path $CmdletRoot -ChildPath "..\..\Public")
. ".\$CmdletName.ps1"

Describe "Teste $CmdletName.ps1" {

    Context "Parameters Test" {
        It "Parameter 'Key' => Mandatory => True" {
            $target = (Get-Command -Name ConvertTo-SecretKey).Parameters['Key'].Attributes.Where({$_ -is [ParameterAttribute]}).Mandatory
            $target | Should -Be $true 
        }
        
        It "Parameter 'Key' => Attribute 'ValidateLength' => MinLength = 16" {
            $target = (Get-Command ConvertTo-SecretKey).Parameters['Key'].Attributes.Where({$_ -is [ValidateLengthAttribute]})
            $target.MinLength | Should -Be 16 
        }
    
        It "Parameter 'Key' => Attribute 'ValidateLength' => MaxLength = 32" {
            $target = (Get-Command ConvertTo-SecretKey).Parameters['Key'].Attributes.Where({$_ -is [ValidateLengthAttribute]})
            $target.MaxLength | Should -Be 32 
        }
    
        It "Parameter 'Key' => throws exception => Argument -lt MinLength" {
            { ConvertTo-SecretKey -Key 123456789012345 } | Should -Throw -ErrorId 'ParameterArgumentValidationError'
        }
    
        It "Parameter 'Key' => throws exception => Argument -gt MaxLength" {
            { ConvertTo-SecretKey -Key 123456789012345678901234567890123 } | Should -Throw -ErrorId 'ParameterArgumentValidationError'
        }
    
        It "Parameter 'Key' => Argument -ge MinLength -and -le MaxLength Test1" {
            ConvertTo-SecretKey -Key '12345678901234567' | Should be @(49,50,51,52,53,54,55,56,57,48,49,50,51,52,53,54,55,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48)
        }
        
        It "Parameter 'Key' => Argument -ge MinLength -and -le MaxLength Test2" {
            ConvertTo-SecretKey -Key '12345678901234567890123456789012' | Should be @(49,50,51,52,53,54,55,56,57,48,49,50,51,52,53,54,55,56,57,48,49,50,51,52,53,54,55,56,57,48,49,50)
        }
    }

    Context "Content Test" {
        It "Return correct Byte-Array for Password-Phrase ('P@ssw0rt-Phrase!')" {
            $key = "P@ssw0rt-Phrase!"
            
            $pad = 32 - $key.Length
            $encoding = New-Object -TypeName System.Text.ASCIIEncoding
            $expected = $encoding.GetBytes($key + "0" * $pad)
            
            $target = ConvertTo-SecretKey -Key $key
            
            $target | Should be $expected
        }
    }

    Context 'ScriptAnalyzer Tests' {
        [Array]$testCase = @()
        $testCase += Invoke-ScriptAnalyzer -Path ".\$CmdletName.ps1" -Severity Error, Warning -ExcludeRule PSUseSingularNouns | 
            Foreach-Object {
                @{
                    Line       = $_.Line
                    RuleName   = $_.RuleName
                    Message    = $_.Message
                }
            }

            It "Ist die Skript-Datei frei von Regelverletzungen?" {
            $testCase.Count | Should -Be 0
        }

        if ($testCase.Count -gt 0) {
            It "REGEL-Verletzung: LINE <Line> | RULE <RuleName> | MESSAGE <Message>" -TestCases $testCase {
                param(
                    $Line,
                    $RuleName,
                    $Message
                )
                $RuleName | Should BeNullOrEmpty
            }
        }
    }
}