Tests/STTools.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
$here = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$sut = ((Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.') -replace '\.ps1', '.psm1'
get-module sttools |remove-module
import-module "$here\$sut" -Force

#Describe "Get-STGroupsFromCSV" {
# It "does something useful" {
# $true | Should -Be $false
# }
#}

Describe "Unit Tests for ConvertFrom-STEduHubST" -Tag "Unit" {
    $fakeUser = [PSCustomObject]@{
        FIRST_NAME = "George";
        SURNAME = "Jones";
        STKEY = "JON0001";
        STATUS = "ACTV";
        BIRTHDATE = "15/07/2001 12:00:00 AM";
    }
    $result = ConvertFrom-STEduHubST -UserList $fakeUser -HomeDirBase \\abcd1234\ -Domain blah.com -HomeDrive H:
    It "Produces correct display name" {
        $result.DisplayName |Should -Match "George Jones"
    }
    It "Produces correct email address" {
        $result.EmailAddress |Should -Match "jon0001@blah.com"
    }
    It "Produces correct enablement" {
        $result.Enabled |Should -Match $true
    }
    It "Produces correct given name" {
        $result.GivenName |Should -Match "George"
    }
    It "Produces correct home directory" {
        $result.HomeDirectory |Should -Contain "\\abcd1234\jon0001"
    }
    It "Produces correct home drive" {
        $result.HomeDrive |Should -Match "H:"
    }
    It "Produces correct Name" {
        $result.Name |Should -Match "JON0001"
    }
    It "Produces correct SamAccountName" {
        $result.SamAccountName |Should -Match "jon0001"
    }
    It "Produces correct Surname" {
        $result.Surname |Should -Match "Jones"
    }
    It "Produces correct UPN" {
        $result.UserPrincipalName |Should -Match ""
    }
}


Describe "Select-STAccountsToDisable" {
    Context "Source object Enabled=`$false and target object Enabled=`$true" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser
        It "Select source objects with Enabled=`$false where target Enabled=`$true" {
            $accountstodisable.Enabled | Should -Be $false
        }
    }
    Context "Source object Enabled=`$true and target object Enabled=`$false" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser
        It "Doesn't output accounts that have Enabled=`$true" {
            $accountstodisable | Should -Be $null
        }
    }
    Context "Source object Enabled=`$false and target object Enabled=`$false" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser
        It "Doesn't output accounts that are already disabled in AD" {
            $accountstodisable | Should -Be $null
        }
    }
    Context "Source object Enabled=`$false and no target object" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "notmatchingfaketestuser";
            Enabled = $true
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser
        It "Doesn't output accounts that don't exist in AD" {
            $accountstodisable | Should -Be $null
        }
    }
    Context "IgnoreList passed in" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $false
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser -ignoreList @('faketestuser')
        It "Doesn't output accounts that are in an ignorelist" {
            $accountstodisable | Should -Be $null
        }
    }
    Context "forceInactive list passed in" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser -forceInactiveList @('faketestuser')
        It "Output accounts that are in an forceInactiveList even if they were Enabled" {
            $accountstodisable.SamAccountName | Should -Be "faketestuser"
        }
        It "Sets the account's Enabled status to `$false" {
            $accountstodisable.Enabled | Should -Be $false
        }
    }
    Context "forceInactiveList and ignoreList passed in" {
        $sourceUser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $aduser = [PSCustomObject]@{
            SamAccountName = "faketestuser";
            Enabled = $true
        }
        $accountstodisable = $sourceUser |Select-STAccountsToDisable -ADUsers $aduser -forceInactiveList @('faketestuser') -ignoreList @('faketestuser')
        It "Output accounts that are in an forceInactiveList even if they were Enabled and in ignoreList" {
            $accountstodisable.SamAccountName | Should -Be "faketestuser"
        }
    }
}