-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPat-TokenChecker.Tests.ps1
More file actions
228 lines (179 loc) · 8.5 KB
/
Pat-TokenChecker.Tests.ps1
File metadata and controls
228 lines (179 loc) · 8.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
BeforeAll {
. $PSScriptRoot\Pat-TokenChecker.ps1 -PAT "dummy" -Organization "test" -WhatIf
}
Describe "Pat-TokenChecker Parameter Validation" {
Context "When validating required parameters" {
It "Should require PAT parameter" {
{ & $PSScriptRoot\Pat-TokenChecker.ps1 -Organization "test" } | Should -Throw
}
It "Should require Organization parameter" {
{ & $PSScriptRoot\Pat-TokenChecker.ps1 -PAT "test" } | Should -Throw
}
It "Should accept valid PAT and Organization" {
{ & $PSScriptRoot\Pat-TokenChecker.ps1 -PAT "test" -Organization "test" -WhatIf } | Should -Not -Throw
}
It "Should accept optional Project parameter" {
{ & $PSScriptRoot\Pat-TokenChecker.ps1 -PAT "test" -Organization "test" -Project "testproject" -WhatIf } | Should -Not -Throw
}
It "Should accept optional OutputPath parameter" {
{ & $PSScriptRoot\Pat-TokenChecker.ps1 -PAT "test" -Organization "test" -OutputPath "C:\temp" -WhatIf } | Should -Not -Throw
}
}
}
Describe "Authorization Header Construction" {
Context "When building authentication headers" {
It "Should create correct Authorization header format" {
$testPAT = "testtoken123"
$expectedAuth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$testPAT"))
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$testPAT"))
}
$headers.Authorization | Should -Be $expectedAuth
}
It "Should use correct header name (Authorization not Authorisation)" {
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":testtoken"))
}
$headers.ContainsKey("Authorization") | Should -Be $true
$headers.ContainsKey("Authorisation") | Should -Be $false
}
It "Should base64 encode PAT correctly" {
$testPAT = "mytoken"
$expectedEncoding = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$testPAT"))
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$testPAT"))
}
$headers.Authorization | Should -Be "Basic $expectedEncoding"
}
}
}
Describe "Invoke-SafeRestMethod Function" {
Context "When making REST API calls" {
It "Should return success object structure" {
Mock Invoke-RestMethod { return @{ test = "data" } }
$result = Invoke-SafeRestMethod -Uri "https://test.com" -Headers @{} -Method "GET"
$result.Success | Should -Be $true
$result.Data | Should -Not -Be $null
$result.Error | Should -Be $null
}
It "Should return error object structure on exception" {
Mock Invoke-RestMethod { throw "Test error" }
$result = Invoke-SafeRestMethod -Uri "https://test.com" -Headers @{} -Method "GET"
$result.Success | Should -Be $false
$result.Data | Should -Be $null
$result.Error | Should -Not -Be $null
}
It "Should default to GET method when not specified" {
Mock Invoke-RestMethod { return @{} } -ParameterFilter { $Method -eq "GET" }
$result = Invoke-SafeRestMethod -Uri "https://test.com" -Headers @{}
Assert-MockCalled Invoke-RestMethod -ParameterFilter { $Method -eq "GET" }
}
}
}
Describe "URL Construction" {
Context "When building Azure DevOps API URLs" {
It "Should construct profile URL correctly" {
$organization = "myorg"
$expectedUrl = "https://dev.azure.com/$organization/_apis/profile/profiles/me?api-version=7.1-preview.3"
$profileUrl = "https://dev.azure.com/$organization/_apis/profile/profiles/me?api-version=7.1-preview.3"
$profileUrl | Should -Be $expectedUrl
}
It "Should construct projects URL correctly" {
$organization = "myorg"
$expectedUrl = "https://dev.azure.com/$organization/_apis/projects?api-version=7.1"
$projectUrl = "https://dev.azure.com/$organization/_apis/projects?api-version=7.1"
$projectUrl | Should -Be $expectedUrl
}
It "Should handle special characters in organization name" {
$organization = "my-org_123"
$profileUrl = "https://dev.azure.com/$organization/_apis/profile/profiles/me?api-version=7.1-preview.3"
$profileUrl | Should -Match "my-org_123"
}
}
}
Describe "Output File Generation" {
Context "When generating report files" {
It "Should create JSON report filename with timestamp" {
$organization = "testorg"
$timestamp = "20231201_143000"
$outputPath = "."
$expectedPath = Join-Path $outputPath "PAT_Security_Assessment_$($organization)_$timestamp.json"
$reportPath = Join-Path $outputPath "PAT_Security_Assessment_$($organization)_$timestamp.json"
$reportPath | Should -Be $expectedPath
}
It "Should create summary report filename with timestamp" {
$organization = "testorg"
$timestamp = "20231201_143000"
$outputPath = "."
$expectedPath = Join-Path $outputPath "PAT_Summary_$($organization)_$timestamp.txt"
$summaryPath = Join-Path $outputPath "PAT_Summary_$($organization)_$timestamp.txt"
$summaryPath | Should -Be $expectedPath
}
}
}
Describe "Directory Creation" {
Context "When output directory doesn't exist" {
BeforeEach {
$testPath = Join-Path $TestDrive "nonexistent_dir"
}
It "Should create directory if it doesn't exist" {
$testPath | Should -Not -Exist
if (-not (Test-Path $testPath)) {
New-Item -ItemType Directory -Path $testPath -Force | Out-Null
}
$testPath | Should -Exist
(Get-Item $testPath).PSIsContainer | Should -Be $true
}
It "Should not fail if directory already exists" {
New-Item -ItemType Directory -Path $testPath -Force | Out-Null
$testPath | Should -Exist
{ New-Item -ItemType Directory -Path $testPath -Force | Out-Null } | Should -Not -Throw
$testPath | Should -Exist
}
It "Should handle nested directory creation" {
$nestedPath = Join-Path $testPath "nested\subdirectory"
if (-not (Test-Path $nestedPath)) {
New-Item -ItemType Directory -Path $nestedPath -Force | Out-Null
}
$nestedPath | Should -Exist
}
}
}
Describe "Findings Object Structure" {
Context "When initializing findings object" {
It "Should contain all required properties" {
$findings = @{
PATOwner = $null
PATScopes = @()
AccessibleProjects = @()
RepositoryAccess = @()
ServiceConnections = @()
PipelineAccess = @()
UserEnumeration = @()
VariableGroups = @()
SecretScanning = @()
BuildHistory = @()
AuditLogs = @()
Extensions = @()
SecurityNamespaces = @()
CrossProjectAccess = @()
AdvancedPermissions = @()
HighRiskFindings = @()
}
$findings.ContainsKey("PATOwner") | Should -Be $true
$findings.ContainsKey("AccessibleProjects") | Should -Be $true
$findings.ContainsKey("RepositoryAccess") | Should -Be $true
$findings.ContainsKey("HighRiskFindings") | Should -Be $true
}
It "Should initialize arrays as empty" {
$findings = @{
PATScopes = @()
AccessibleProjects = @()
HighRiskFindings = @()
}
$findings.PATScopes.Count | Should -Be 0
$findings.AccessibleProjects.Count | Should -Be 0
$findings.HighRiskFindings.Count | Should -Be 0
}
}
}