tests/Restore-DbaDbCertificate.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
$commandname = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
    Context "Can create a database certificate" {
        BeforeAll {
            $masterkey = New-DbaDatabaseMasterKey -SqlInstance $script:instance1 -Database tempdb -Password $(ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force) -Confirm:$false
        }
        AfterAll {
            $null = $masterkey | Remove-DbaDatabaseMasterKey -Confirm:$false
        }

        $password = ConvertTo-SecureString -AsPlainText "GoodPass1234!!" -force
        $cert = New-DbaDbCertificate -SqlInstance $script:instance1 -Database tempdb
        $backup = Backup-DbaDbCertificate -SqlInstance $script:instance1 -Database tempdb -EncryptionPassword $password
        $null = Remove-DbaDbCertificate -SqlInstance $script:instance1 -Certificate $cert.Name -Database tempdb -Confirm:$false
        $results = Restore-DbaDbCertificate -SqlInstance $script:instance1 -Path $backup.ExportPath -Password $password -Database tempdb

        It "restores the db cert" {
            $results.Parent.Name -eq 'tempdb'
            $null -ne $results.Name
            $results.PrivateKeyEncryptionType -eq "Password"
        }

        $results | Remove-DbaDbCertificate -Confirm:$false
    }
}