PSAdsi.Tests.ps1

<#
    .SYNOPSIS
        TEST TDD RDS
    .DESCRIPTION
        Unblock-File
    .EXAMPLE
        While ($true) {$test = Invoke-Pester -CodeCoverage .\RDS.Tests.ps1 -PassThru; $test.FailedCount; start-sleep -s 15}
    .EXAMPLE
        https://github.com/pester/Pester/wiki/Should
 
        Be
        BeExactly
        BeGreaterThan
        BeGreaterOrEqual
        BeIn
        BeLessThan
        BeLessOrEqual
        BeLike
        BeLikeExactly
        BeOfType
        BeTrue
        BeFalse
        HaveCount
        Contain
        Exist
        FileContentMatch
        FileContentMatchExactly
        FileContentMatchMultiline
        Match
        MatchExactly
        Throw
        BeNullOrEmpty
 
    .NOTES
        Alopez 2017
        Em@il : alban.lopez@gmail.com
 
        require https://github.com/pester/Pester
 
        https://github.com/pester/Pester/wiki/Should
        Get-ChildItem C:\Users\alopez\Documents\WindowsPowerShell\Modules\Pester\ -Recurse | Unblock-File
 #>


$psd1 = $MyInvocation.MyCommand.Source -replace ('\.Tests\.ps1', '.psd1')
$psd1 | Import-Module -wa 0 -Force

#Get-ChildItem $("$($MyInvocation.MyCommand.Path)\RDS.Tests.ps1" -replace('\.Tests\.ps1', '*.psm1')) | ForEach-Object {Import-Module $_.FullName -Force}
write-center $psd1 -fore Red
function    global:Write-LogStep {}
Describe "Get-AllADs" {
    Context 'Standard' {
        It 'Get-AllADs not throw' {
            {Get-AllADs} | Should -Not -Throw
        }
        $names = Get-AllADs
        It 'Get-AllADs <Name>'  -TestCase @(
            @{Name = 'open.adds'},
            @{Name = 'cirrus.adds'},
            @{Name = 'acdgroupe.adds'},
            @{Name = 'coaxis-asp.com'},
            @{Name = 'wendel.adds'}
        ) {
            param ($name)
            $names | Should -Contain $name
        }
    }
}
Describe "Get-ADSIUser" {
    Context 'Standard' {
        It 'Get-ADSIUser empty on missing Args' {
            Get-ADSIUser | Should -BeNullOrEmpty
        }
        # It 'Get-ADSIUser empty on missing user' {
        # Get-ADSIUser 'cirrus\bobdylan' | Should -BeNullOrEmpty
        # }
        It 'Get-ADSIUser empty on missin AD' {
            Get-ADSIUser 'fakeAd\bobdylan' | Should -BeNullOrEmpty
        }
        It "Get-ADSIUser not throw 'open\jdoe'" {
            {Get-ADSIUser 'open\jdoe'} | Should -Not -Throw
        }
        It "Get-ADSIUser not throw 'jdoe@open.adds'" {
            {Get-ADSIUser 'jdoe@open.adds'} | Should -Not -Throw
        }
        It 'Get-ADSIUser 1 User' {
            Get-ADSIUser 'open\jdoe' | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIUser Path' {
            (Get-ADSIUser -NtAccountName 'open\jdoe').Path | Should -BeLike 'LDAP://CN=John Doe,OU=Sotrima,DC=open,DC=adds'
        }
    }
    Context 'Pipeline' {
        It 'Get-ADSIUser many User pattern' {
            'open\*doe' | Get-ADSIUser | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIUser Administrateur User pattern' {
            ('b*\Administrateur' | Get-ADSIUser).count | Should -BeGreaterThan 2
        }
    }
}
Describe "Get-ADSIMemberOf" {
    $jdoe = Get-ADSIUser 'open\jdoe'
    Context 'Pipeline' {
        $MemberOf = $jdoe | Get-ADSIMemberOf -Recurse
        It 'Convert-AdsiUsers - <Property>' -TestCase @(
            @{Property = 'MemberOf'; Type = [Object]},
            @{Property = 'NestedMemberOf'; Type = [Object]}
        ) {
            param($Property,$Type)
            $MemberOf.$Property | Should -beOfType $Type
        }
        It 'Get-ADSIMemberOf MemberOf' {
            $MemberOf.MemberOf.count | Should -BeGreaterThan 1
        }
        It 'Get-ADSIMemberOf NestedMemberOf' {
            $MemberOf.NestedMemberOf.count | Should -BeGreaterThan 1
        }
        $MemberOf = $jdoe | Get-ADSIMemberOf
        It 'Get-ADSIMemberOf NestedMemberOf' {
            $MemberOf.NestedMemberOf.count | Should -Be 0
        }
        It 'Get-ADSIMemberOf MemberOf' {
            $MemberOf.MemberOf.count | Should -BeGreaterThan 1
        }
    }
}
Describe "Get-ADSIGroup" {
    Context 'Standard' {
        It 'Get-ADSIGroup empty on missing Args' {
            Get-ADSIGroup | Should -BeNullOrEmpty
        }
        It 'Get-ADSIGroup empty on missing Group' {
            Get-ADSIGroup 'cirrus\groupe bidon 2' | Should -BeNullOrEmpty
        }
        It 'Get-ADSIGroup empty on missin AD' {
            Get-ADSIGroup 'fakeAd\groupe bidon 2' | Should -BeNullOrEmpty
        }
        It "Get-ADSIGroup not throw 'open\groupe sotrima global'" {
            {Get-ADSIGroup 'open\groupe sotrima global'} | Should -Not -Throw
        }
        It "Get-ADSIGroup not throw 'groupe sotrima global@open.adds'" {
            {Get-ADSIGroup 'groupe sotrima global@open.adds'} | Should -Not -Throw
        }
        It 'Get-ADSIGroup 1 Group' {
            Get-ADSIGroup 'open\groupe sotrima global' | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIGroup Path' {
            (Get-ADSIGroup -NtAccountName 'open\groupe sotrima global').Path | Should -BeLike 'LDAP://CN=Groupe SOTRIMA Global,OU=Sotrima,DC=open,DC=adds'
        }
    }
    Context 'Pipeline' {
        It 'Get-ADSIGroup many Group pattern' {
            'open\groupe sotrima *' | Get-ADSIGroup | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIGroup Administrateur Group pattern' {
            ('*\groupe s* global' | Get-ADSIGroup).count | Should -BeGreaterThan 50
        }
    }
}

Describe "Get-ADSIComputer" {
    Context 'Standard' {
        It 'Get-ADSIComputer empty on missing Args' {
            Get-ADSIComputer | Should -BeNullOrEmpty
        }
        It 'Get-ADSIComputer empty on missing Computer' {
            Get-ADSIComputer 'open\vtsebroX' | Should -BeNullOrEmpty
        }
        It 'Get-ADSIComputer empty on missin AD' {
            Get-ADSIComputer 'fakeAd\vtseX' | Should -BeNullOrEmpty
        }
        It "Get-ADSIComputer not throw 'open\vcbrot2'" {
            {Get-ADSIComputer 'open\vcbrot2'} | Should -Not -Throw
        }
        It "Get-ADSIComputer not throw 'vcbrot2@open.adds'" {
            {Get-ADSIComputer 'vcbrot2@open.adds'} | Should -Not -Throw
        }
        It 'Get-ADSIComputer 1 computer' {
            Get-ADSIComputer 'open\vcbrot2' | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIComputer Path' {
            (Get-ADSIComputer -ComputerName 'open\vcbrot2').Path | Should -BeLike 'LDAP://CN=VCBROT2,OU=Brotte_C,OU=Serveurs,DC=open,DC=adds'
        }
    }
    Context 'Pipeline' {
        It 'Get-ADSIComputer many Group pattern' {
            'open\vcbr*' | Get-ADSIComputer | Should -BeOfType [System.DirectoryServices.SearchResult]
        }
        It 'Get-ADSIComputer Administrateur Group pattern' {
            ('ovf*' | Get-ADSIComputer).count | Should -BeGreaterThan 5
        }
    }
}
Describe 'Set-ADSIProperties' {
    $jdoe = [adsi]('open\jdoe' | Get-ADSIUser).path
    Context 'Set-ADSIProperties' {
        It 'Set-ADSIProperties not throw' {
            {Set-ADSIProperties} | Should -not -Throw 
        }
        It 'Set-ADSIProperties - none' {
            $jdoe | Set-ADSIProperties | Should -BeNullOrEmpty
        }
        It 'Set-ADSIProperties - <Property>' -TestCase @(
                @{Property = 'description'; value = 'Hello !'},
                @{Property = 'telephonenumber'; value = '123.456.789'}
            ) {
                param($Property,$value)
                $param = @{$Property = $value}
                ($jdoe | Set-ADSIProperties @param -PassThru).$Property | Should -be $value
        }
        It 'Set-ADSIProperties - <Property>' -TestCase @(
                @{Property = 'description'; value = 'Compte de test Sotrima'},
                @{Property = 'telephonenumber'; value = '01.02.03.04.05'}
            ) {
                param($Property,$value)
                $param = @{$Property = $value}
                ($jdoe | Set-ADSIProperties @param -PassThru).$Property | Should -be $value
        }
        It 'Set-ADSIProperties -password 5h0rt Echec' {
            $jdoe | Set-ADSIProperties -Password '5h0rt' -PassThru | Should -BeNullOrEmpty
        }
        It 'Set-ADSIProperties -password random ' {
            # $Previous = $($jdoe)
            $Previous = $([int64]$jdoe.ConvertLargeIntegerToInt64($jdoe.properties.pwdLastSet.value))
            $jdoe = $jdoe | Set-ADSIProperties -Password (Calcul-RandomPassword -Length 20 -Other $null) -PassThru
            $jdoe.ConvertLargeIntegerToInt64($jdoe.properties.pwdLastSet.value) | Should -BeGreaterThan $Previous
        }
    }
}
Describe "Convert-ADSIUser" {
    $jdoe = 'open\jdoe' | Get-ADSIUser
    Context 'convert' {
        $AdsiUsers = $jdoe | convert-AdsiUsers
        It 'Convert-AdsiUsers - <Property>' -TestCase @(
            @{Property = 'NtAccountName'; Type = [string]},
            @{Property = 'DisplayName'; Type = [string]},
            @{Property = 'Sid'; Type = [string]},
            @{Property = 'email'; Type = [string]},
            @{Property = 'Type'; Type = [string]},
            @{Property = 'State'; Type = [string]},
            @{Property = 'Phone'; Type = [string]},
            @{Property = 'Description'; Type = [string]},
            @{Property = 'Facturation'; Type = [string]},
            # @{Property = 'PasswordType'; Type = [string]},
            @{Property = 'PasswordDate'; Type = [datetime]},
            @{Property = 'ExpireDate'; Type = [datetime]},
            # @{Property = 'Extern'; Type = [string]},
            @{Property = 'Groupes'; Type = [string]}
        ) {
            param($Property,$Type)
            $AdsiUsers.$Property | Should -beOfType $Type
        }
        
        $AdsiUserToMail = $jdoe | convert-AdsiUserToMail
        It 'Convert-AdsiUserToMail - <Property>' -TestCase @(
            @{Property = 'NtAccountName'; Type = [string]},
            @{Property = 'DisplayName'; Type = [string]},
            @{Property = 'email'; Type = [string]},
            @{Property = 'State'; Type = [string]},
            # @{Property = 'ExtensionAttribute1'; Type = [string]},
            @{Property = 'AllAddresses'; Type = [string]},
            # @{Property = 'msexchhomeservername'; Type = [string]},
            @{Property = 'msExchServerName'; Type = [string]},
            @{Property = 'msExchDbName'; Type = [string]},
            @{Property = 'msExchQuotaGb'; Type = [int]},
            # @{Property = 'msExchDelegateBal'; Type = [string]},
            # @{Property = 'msExchArchiveName'; Type = [string]},
            @{Property = 'msExchArchDbName'; Type = [string]},
            @{Property = 'msExchArchPolicy'; Type = [string]},
            @{Property = 'msExchArchiveQuotaGb'; Type = [int]}
        ) {
            param($Property,$Type)
            $AdsiUserToMail.$Property | Should -beOfType $Type
        }
        
        $AdsiFactUsers = $jdoe | convert-AdsiFactUsers
        It 'Convert-AdsiFactUsers - <Property>' -TestCase @(
            @{Property = 'sid'; Type = [string]},
            @{Property = 'Enabled'; Type = [bool]},
            @{Property = 'SamAccountName'; Type = [string]},
            @{Property = 'NtAccountName'; Type = [string]},
            @{Property = 'email'; Type = [string]},
            @{Property = 'TsAllowLogon'; Type = [bool]},
            # @{Property = 'Facturation'; Type = [string]},
            @{Property = 'Externe'; Type = [bool]}
        ) {
            param($Property,$Type)
            $AdsiFactUsers.$Property | Should -beOfType $Type
        }
    }
}
Describe "Convert-AdsiComputer" {
    $jdoe = 'coaxis-asp\vdiv05' | Get-ADSIComputer
    Context 'convert' {
        $AdsiComputers = $jdoe | convert-AdsiComputers
        It 'Convert-AdsiComputers - <Property>' -TestCase @(
            @{Property = 'Name'; Type = [string]},
            @{Property = 'NtAccountName'; Type = [string]},
            @{Property = 'Sid'; Type = [string]},
            @{Property = 'DnsHostName'; Type = [string]},
            @{Property = 'operatingsystem'; Type = [string]},
            @{Property = 'operatingsystemversion'; Type = [string]},
            @{Property = 'whencreated'; Type = [datetime]},
            # @{Property = 'memberof'; Type = [string]},
            @{Property = 'adspath'; Type = [string]}
        ) {
            param($Property,$Type)
            $AdsiComputers.$Property | Should -beOfType $Type
        }
    }
}







ipmo PsWrite -Function Write-LogStep -Force

# Build-Readme.md '.\PSAdsi*.psm1' -MarkDownFileName .\README.md