-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialize-Blogger.Tests.ps1
More file actions
207 lines (169 loc) · 6.33 KB
/
Copy pathInitialize-Blogger.Tests.ps1
File metadata and controls
207 lines (169 loc) · 6.33 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
Describe "Initialize-Blogger" {
BeforeEach {
Import-Module $PSScriptRoot\..\PSBlogger.psm1 -Force
}
Context "User provides AuthCode" {
BeforeEach {
InModuleScope -ModuleName PSBlogger {
# simulate running as admin
Mock Test-IsAdmin { $true }
Mock Test-ChocolateyInstalled { $true }
Mock Test-PandocInstalled { $true }
# simulate valid auth token
Mock Get-GoogleAccessToken { return @{ refresh_token = "refresh_token" } }
# simulate valid offline token
Mock Update-GoogleAccessToken { return @{ access_token = "access_token" } }
# simulate auth-flow in browser
Mock Wait-GoogleAuthApiToken { "simulatedcode" }
# prevent browser from being launched
Mock Start-Process { }
}
}
It "Should persist credentials" {
# test variable inside loaded module
InModuleScope -ModuleName PSBlogger {
# arrange
$credentialCache = "TestDrive:\credentialcache.json"
$BloggerSession.CredentialCache = $credentialCache
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
}
# act
Initialize-Blogger @initArgs
# assert
$credentials = Get-Content -Path $credentialCache | ConvertFrom-Json
$credentials.access_token | Should -Be "access_token"
}
}
It "Should reset previous auth tokens" {
# test variable inside loaded module
InModuleScope -ModuleName PSBlogger {
# arrange
$credentialCache = "TestDrive:\credentialcache.json"
$BloggerSession.CredentialCache = $credentialCache
$BloggerSession.AccessToken = "invalid"
$BloggerSession.RefreshToken = "invalid"
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
}
# act
Initialize-Blogger @initArgs
# assert
$BloggerSession.AccessToken | Should -Be $null
$BloggerSession.RefreshToken | Should -Be $null
}
}
}
Context "Running as non-admin" {
BeforeEach {
InModuleScope -ModuleName PSBlogger {
# simulate running as non-admin
Mock Test-IsAdmin { $false }
# ensure that we don't launch browser or admin features
Mock Start-Process { throw "Unexpected call to start-process"}
}
}
It "Should show warning and exit when not admin" {
InModuleScope -ModuleName PSBlogger {
# arrange
$initArgs = @{ ClientId="dummy"; ClientSecret="dummy" }
Mock Write-Warning {} -Verifiable
# act & assert
{ Initialize-Blogger @initArgs } | Should -Not -Throw
# The function should exit early, so we can verify it doesn't try to do auth
Assert-MockCalled Test-IsAdmin -Times 1
Should -InvokeVerifiable
}
}
}
Context "Pandoc Detection and Installation" {
BeforeEach {
InModuleScope -ModuleName PSBlogger {
# simulate running as admin
Mock Test-IsAdmin { $true }
# Mock the authentication flow to avoid complications
Mock Get-GoogleAccessToken { return @{ refresh_token = "refresh_token" } }
Mock Update-GoogleAccessToken { return @{ access_token = "access_token" } }
Mock Wait-GoogleAuthApiToken { "simulatedcode" }
Mock Start-Process { }
Mock Set-CredentialCache { }
}
}
It "Should continue normally when Pandoc is already installed" {
InModuleScope -ModuleName PSBlogger {
# arrange
Mock Test-PandocInstalled { $true }
Mock Test-ChocolateyInstalled { throw "Should not be called when Pandoc is installed" }
Mock Install-PandocWithChocolatey { throw "Should not be called when Pandoc is installed" }
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
Confirm = $false # Prevent any prompting
}
# act & assert
{ Initialize-Blogger @initArgs } | Should -Not -Throw
# verify pandoc installation was not attempted
Assert-MockCalled Test-PandocInstalled -Times 1
Assert-MockCalled Install-PandocWithChocolatey -Times 0
}
}
It "Should show warning when Pandoc is not installed and Chocolatey is not available" {
InModuleScope -ModuleName PSBlogger {
# arrange
Mock Test-PandocInstalled { $false }
Mock Test-ChocolateyInstalled { $false }
Mock Install-PandocWithChocolatey { throw "Should not be called when Chocolatey is not available" }
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
Confirm = $false # Prevent any prompting
}
# act
Initialize-Blogger @initArgs
# assert
Should -InvokeVerifiable
Assert-MockCalled Install-PandocWithChocolatey -Times 0
}
}
It "Should automatically install Pandoc when Confirm is false and Chocolatey is available" {
InModuleScope -ModuleName PSBlogger {
# arrange
Mock Test-PandocInstalled { $false }
Mock Test-ChocolateyInstalled { $true }
Mock Install-PandocWithChocolatey { } -Verifiable
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
Confirm = $false # This should bypass confirmation and install automatically
}
# act
Initialize-Blogger @initArgs
# assert
Should -InvokeVerifiable
}
}
It "Should call pandoc and chocolatey check functions in correct order" {
InModuleScope -ModuleName PSBlogger {
# arrange
Mock Test-PandocInstalled { $false } -Verifiable
Mock Test-ChocolateyInstalled { $false } -Verifiable
Mock Write-Warning { }
$initArgs = @{
ClientId = "dummy"
ClientSecret = "dummy"
Confirm = $false # Prevent any prompting
}
# act
Initialize-Blogger @initArgs
# assert
Should -InvokeVerifiable
}
}
# Note: Additional confirmation behavior tests can be performed manually:
# - Initialize-Blogger (with ConfirmPreference = 'Medium' or 'High') should prompt
# - Initialize-Blogger -Confirm:$false should install without prompting
# - Initialize-Blogger -WhatIf should show what would happen without installing
}
}