Skip to content

Commit

Permalink
Add more tests (do Export-DbaUser)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioESSilva committed Feb 5, 2020
1 parent 261e2cb commit 7aa32c6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/Export-DbaUser.Tests.ps1
Expand Up @@ -16,31 +16,39 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$AltExportPath = "$env:USERPROFILE\Documents"
$outputPath = "$AltExportPath\Dbatoolsci_user_CustomFolder"
$outputFile = "$AltExportPath\Dbatoolsci_user_CustomFile.sql"
$outputFile2 = "$AltExportPath\Dbatoolsci_user_CustomFile2.sql"
try {
$dbname = "dbatoolsci_exportdbauser"
$login = "dbatoolsci_exportdbauser_login"
$login2 = "dbatoolsci_exportdbauser_login2"
$user = "dbatoolsci_exportdbauser_user"
$user2 = "dbatoolsci_exportdbauser_user2"
$table = "dbatoolsci_exportdbauser_table"
$server = Connect-DbaInstance -SqlInstance $script:instance1
$null = $server.Query("CREATE DATABASE [$dbname]")

$securePassword = $(ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force)
$null = New-DbaLogin -SqlInstance $script:instance1 -Login $login -Password $securePassword
$null = New-DbaLogin -SqlInstance $script:instance1 -Login $login2 -Password $securePassword

$db = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname
$null = $db.Query("CREATE USER [$user] FOR LOGIN [$login]")
$null = $db.Query("CREATE USER [$user2] FOR LOGIN [$login2]")

$null = $db.Query("CREATE TABLE $table (C1 INT);")
$null = $db.Query("GRANT SELECT ON OBJECT::$table TO [$user];")
$null = $db.Query("GRANT SELECT ON OBJECT::$table TO [$user2];")
} catch { } # No idea why appveyor can't handle this
}
AfterAll {
Remove-DbaDatabase -SqlInstance $script:instance1 -Database $dbname -Confirm:$false
Remove-DbaLogin -SqlInstance $script:instance1 -Login $login -Confirm:$false
Remove-DbaLogin -SqlInstance $script:instance1 -Login $login2 -Confirm:$false
(Get-ChildItem $outputFile -ErrorAction SilentlyContinue) | Remove-Item -ErrorAction SilentlyContinue
(Get-ChildItem $outputFile2 -ErrorAction SilentlyContinue) | Remove-Item -ErrorAction SilentlyContinue
Remove-Item -Path $outputPath -Recurse -ErrorAction SilentlyContinue -Confirm:$false
}

Context "Check if output file was created" {
Expand Down Expand Up @@ -79,4 +87,17 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
(Get-ChildItem $outputFile2 -ErrorAction SilentlyContinue) | Remove-Item -ErrorAction SilentlyContinue
}
}

Context "Check if one output file per user was created" {
$null = Export-DbaUser -SqlInstance $script:instance1 -Database $dbname -Path $outputPath
It "Exports two files to the path" {
(Get-ChildItem $outputPath).Count | Should Be 2
}
It "Exported file name contains username '$user'" {
Get-ChildItem $outputPath | Where-Object Name -like ('*' + $User + '*') | Should BeTrue
}
It "Exported file name contains username '$user2'" {
Get-ChildItem $outputPath | Where-Object Name -like ('*' + $User2 + '*') | Should BeTrue
}
}
}

0 comments on commit 7aa32c6

Please sign in to comment.