tests/New-DbaDatabaseCertificate.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
$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 Master -Password $(ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force) -Confirm:$false
            $null = New-DbaDatabaseMasterKey -SqlInstance $script:instance1 -Database TempDb -Password $(ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force) -Confirm:$false
            $certificateName1 = "Cert_$(Get-random)"
            $certificateName2 = "Cert_$(Get-random)"
        }
        AfterAll {
            $null = Remove-DbaDatabaseCertificate -SqlInstance $script:instance1 -Certificate $certificateName1 -database Master -Confirm:$false
            $null = Remove-DbaDatabaseCertificate -SqlInstance $script:instance1 -Certificate $certificateName2 -database TempDb -Confirm:$false
            if($masterKey) { $null = Remove-DbaDatabaseMasterKey -SqlInstance $script:instance1 -Database Master }
            $null = Remove-DbaDatabaseMasterKey -SqlInstance $script:instance1 -Database TempDb
        }
                
        $results = New-DbaDatabaseCertificate -SqlInstance $script:instance1 -Name $certificateName1
        
        It "Successfully creates a new database certificate in default, master database" {
            "$($results.name)" -match $certificateName1 | Should Be $true
        }
                
        $results = New-DbaDatabaseCertificate -SqlInstance $script:instance1 -Name $certificateName2 -Database TempDb
        It "Successfully creates a new database certificate in the TempDb database" {
            "$($results.Database)" -match "TempDb"  | Should Be $true
        }
    }
}