Skip to content

Commit 954fe40

Browse files
committed
Update Backup-SqlDscRSEncryptionKey tests to include Credential parameter and improve path handling
1 parent 95a0838 commit 954fe40

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

tests/Unit/Public/Backup-SqlDscRSEncryptionKey.Tests.ps1

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
4848
It 'Should have the correct parameters in parameter set <ExpectedParameterSetName>' -ForEach @(
4949
@{
5050
ExpectedParameterSetName = '__AllParameterSets'
51-
ExpectedParameters = '[-Configuration] <Object> [-Password] <securestring> [-Path] <string> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]'
51+
ExpectedParameters = '[-Configuration] <Object> [-Path] <string> [-Password] <securestring> [[-Credential] <pscredential>] [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]'
5252
}
5353
) {
5454
$result = (Get-Command -Name 'Backup-SqlDscRSEncryptionKey').ParameterSets |
@@ -73,25 +73,27 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
7373

7474
Mock -CommandName Invoke-RsCimMethod -MockWith {
7575
return @{
76-
KeyFile = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent'))
76+
KeyFile = [System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent')
7777
}
7878
}
79-
80-
Mock -CommandName Set-Content
8179
}
8280

8381
It 'Should backup encryption key without errors' {
84-
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -Confirm:$false } | Should -Not -Throw
82+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
83+
84+
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -Confirm:$false } | Should -Not -Throw
8585

8686
Should -Invoke -CommandName Invoke-RsCimMethod -ParameterFilter {
8787
$MethodName -eq 'BackupEncryptionKey'
8888
} -Exactly -Times 1
8989

90-
Should -Invoke -CommandName Set-Content -Exactly -Times 1
90+
$testPath | Should -Exist
9191
}
9292

9393
It 'Should not return anything by default' {
94-
$result = $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -Confirm:$false
94+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey2.snk'
95+
96+
$result = $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -Confirm:$false
9597

9698
$result | Should -BeNullOrEmpty
9799
}
@@ -107,15 +109,15 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
107109

108110
Mock -CommandName Invoke-RsCimMethod -MockWith {
109111
return @{
110-
KeyFile = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent'))
112+
KeyFile = [System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent')
111113
}
112114
}
113-
114-
Mock -CommandName Set-Content
115115
}
116116

117117
It 'Should return the configuration CIM instance' {
118-
$result = $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -PassThru -Confirm:$false
118+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
119+
120+
$result = $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -PassThru -Confirm:$false
119121

120122
$result | Should -Not -BeNullOrEmpty
121123
$result.InstanceName | Should -Be 'SSRS'
@@ -132,15 +134,15 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
132134

133135
Mock -CommandName Invoke-RsCimMethod -MockWith {
134136
return @{
135-
KeyFile = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent'))
137+
KeyFile = [System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent')
136138
}
137139
}
138-
139-
Mock -CommandName Set-Content
140140
}
141141

142142
It 'Should backup encryption key without confirmation' {
143-
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -Force } | Should -Not -Throw
143+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
144+
145+
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -Force } | Should -Not -Throw
144146

145147
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
146148
}
@@ -160,7 +162,9 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
160162
}
161163

162164
It 'Should throw a terminating error' {
163-
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -Confirm:$false } | Should -Throw -ErrorId 'BSREK0001,Backup-SqlDscRSEncryptionKey'
165+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
166+
167+
{ $mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -Confirm:$false } | Should -Throw -ErrorId 'BSRSEK0001,Backup-SqlDscRSEncryptionKey'
164168
}
165169
}
166170

@@ -173,14 +177,16 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
173177
$mockPassword = ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force
174178

175179
Mock -CommandName Invoke-RsCimMethod
176-
Mock -CommandName Set-Content
177180
}
178181

179182
It 'Should not call Invoke-RsCimMethod' {
180-
$mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -WhatIf
183+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
184+
185+
$mockCimInstance | Backup-SqlDscRSEncryptionKey -Password $mockPassword -Path $testPath -WhatIf
181186

182187
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 0
183-
Should -Invoke -CommandName Set-Content -Exactly -Times 0
188+
189+
$testPath | Should -Not -Exist
184190
}
185191
}
186192

@@ -194,15 +200,15 @@ Describe 'Backup-SqlDscRSEncryptionKey' {
194200

195201
Mock -CommandName Invoke-RsCimMethod -MockWith {
196202
return @{
197-
KeyFile = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent'))
203+
KeyFile = [System.Text.Encoding]::UTF8.GetBytes('MockKeyFileContent')
198204
}
199205
}
200-
201-
Mock -CommandName Set-Content
202206
}
203207

204208
It 'Should backup encryption key' {
205-
{ Backup-SqlDscRSEncryptionKey -Configuration $mockCimInstance -Password $mockPassword -Path 'C:\Backup\RSKey.snk' -Confirm:$false } | Should -Not -Throw
209+
$testPath = Join-Path -Path $TestDrive -ChildPath 'RSKey.snk'
210+
211+
{ Backup-SqlDscRSEncryptionKey -Configuration $mockCimInstance -Password $mockPassword -Path $testPath -Confirm:$false } | Should -Not -Throw
206212

207213
Should -Invoke -CommandName Invoke-RsCimMethod -Exactly -Times 1
208214
}

0 commit comments

Comments
 (0)