<commit_before>$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
    Context "Validate parameters" {
        [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
        [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Login', 'ExcludeLogin', 'Database', 'Path', 'FilePath', 'NoClobber', 'Append', 'ExcludeDatabases', 'ExcludeJobs', 'EnableException', 'ExcludeGoBatchSeparator', 'DestinationVersion', 'InputObject', 'DefaultDatabase'
        $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
        It "Should only contain our specific parameters" {
            (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
        }
    }
}


$outputFile = "dbatoolsci_exportdbalogin.sql"

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
    BeforeAll {
        try {
            $random = Get-Random
            $dbname1 = "dbatoolsci_exportdbalogin1$random"
            $login1 = "dbatoolsci_exportdbalogin_login1$random"
            $user1 = "dbatoolsci_exportdbalogin_user1$random"

            $dbname2 = "dbatoolsci_exportdbalogin2$random"
            $login2 = "dbatoolsci_exportdbalogin_login2$random"
            $user2 = "dbatoolsci_exportdbalogin_user2$random"

            $server = Connect-DbaInstance -SqlInstance $script:instance2
            $null = $server.Query("CREATE DATABASE [$dbname1]")
            $null = $server.Query("CREATE LOGIN [$login1] WITH PASSWORD = '<PASSWORD>!'")
            $server.Databases[$dbname1].ExecuteNonQuery("CREATE USER [$user1] FOR LOGIN [$login1]")

            $null = $server.Query("CREATE DATABASE [$dbname2]")
            $null = $server.Query("CREATE LOGIN [$login2] WITH PASSWORD = '<PASSWORD>!'")
            $null = $server.Query("ALTER LOGIN [$login2] DISABLE")
            $null = $server.Query("DENY CONNECT SQL TO [$login2]")

            if ($server.VersionMajor -lt 11) {
                $null = $server.Query("EXEC sys.sp_addsrvrolemember @rolename=N'dbcreator', @loginame=N'$login2'")
            } else {
                $null = $server.Query("ALTER SERVER ROLE [dbcreator] ADD MEMBER [$login2]")
            }
            $null = $server.Query("GRANT SELECT ON sys.databases TO [$login2] WITH GRANT OPTION")
            $server.Databases[$dbname2].ExecuteNonQuery("CREATE USER [$user2] FOR LOGIN [$login2]")
        } catch {
            $_
        }
    }
    AfterAll {
        Remove-DbaDatabase -SqlInstance $script:instance2 -Database $dbname1 -Confirm:$false
        Remove-DbaLogin -SqlInstance $script:instance2 -Login $login1 -Confirm:$false

        Remove-DbaDatabase -SqlInstance $script:instance2 -Database $dbname2 -Confirm:$false
        Remove-DbaLogin -SqlInstance $script:instance2 -Login $login2 -Confirm:$false

        Remove-Item -Path $outputFile
    }

    It "Doesn't include database details when using NoDatabase" {
        $output = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -WarningAction SilentlyContinue

        ([regex]::matches($output, 'USE \[.*?\]')).Count | Should Be 0
    }

    $output = Export-DbaLogin -SqlInstance $script:instance2 -WarningAction SilentlyContinue

    It -Skip "Doesn't filter specific databases" {
        ([regex]::matches($output, 'USE \[.*?\]').Value | Select-Object -Unique).Count | Should BeGreaterThan 0
    }

    It -Skip "Exports disabled logins" {
        [regex]::matches($output, "ALTER LOGIN \[.*?\] DISABLE").Count | Should BeGreaterThan 0
    }

    It -Skip "Exports deny connects" {
        [regex]::matches($output, "DENY CONNECT SQL TO \[.*?\]").Count | Should BeGreaterThan 0
    }

    It -Skip "Exports system role memberships" {
        if ($server.VersionMajor -lt 11) {
            [regex]::matches($output, "EXEC sys.sp_addsrvrolemember @rolename=N'dbcreator', @loginame=N'$login2'").Count | Should BeGreaterThan 0
        } else {
            [regex]::matches($output, "ALTER SERVER ROLE \[.*?\] ADD MEMBER \[.*?\]").Count | Should BeGreaterThan 0
        }
    }

    It "Exports to the specified file" {
        Export-DbaLogin -SqlInstance $script:instance2 -FilePath $outputFile -WarningAction SilentlyContinue

        Test-Path -Path $outputFile | Should Be $true
    }
}<commit_msg>Export-DbaLogin - Integration Tests (#5630)<commit_after>$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
    Context "Validate parameters" {
        [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
        [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Login', 'ExcludeLogin', 'Database', 'Path', 'FilePath', 'NoClobber', 'Append', 'ExcludeDatabases', 'ExcludeJobs', 'EnableException', 'ExcludeGoBatchSeparator', 'DestinationVersion', 'InputObject', 'DefaultDatabase'
        $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
        It "Should only contain our specific parameters" {
            (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
        }
    }
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
    BeforeAll {
        $DefaultExportPath = Get-DbatoolsConfigValue -FullName path.dbatoolsexport
        $AltExportPath = "$env:USERPROFILE\Documents"
        try {
            $random = Get-Random
            $dbname1 = "dbatoolsci_exportdbalogin1$random"
            $login1 = "dbatoolsci_exportdbalogin_login1$random"
            $user1 = "dbatoolsci_exportdbalogin_user1$random"

            $dbname2 = "dbatoolsci_exportdbalogin2$random"
            $login2 = "dbatoolsci_exportdbalogin_login2$random"
            $user2 = "dbatoolsci_exportdbalogin_user2$random"

            $server = Connect-DbaInstance -SqlInstance $script:instance2
            $null = $server.Query("CREATE DATABASE [$dbname1]")
            $null = $server.Query("CREATE LOGIN [$login1] WITH PASSWORD = '<PASSWORD>!'")
            $server.Databases[$dbname1].ExecuteNonQuery("CREATE USER [$user1] FOR LOGIN [$login1]")

            $null = $server.Query("CREATE DATABASE [$dbname2]")
            $null = $server.Query("CREATE LOGIN [$login2] WITH PASSWORD = '<PASSWORD>!'")
            $null = $server.Query("ALTER LOGIN [$login2] DISABLE")
            $null = $server.Query("DENY CONNECT SQL TO [$login2]")

            if ($server.VersionMajor -lt 11) {
                $null = $server.Query("EXEC sys.sp_addsrvrolemember @rolename=N'dbcreator', @loginame=N'$login2'")
            } else {
                $null = $server.Query("ALTER SERVER ROLE [dbcreator] ADD MEMBER [$login2]")
            }
            $null = $server.Query("GRANT SELECT ON sys.databases TO [$login2] WITH GRANT OPTION")
            $server.Databases[$dbname2].ExecuteNonQuery("CREATE USER [$user2] FOR LOGIN [$login2]")
        } catch {
            $_
        }
    }
    AfterAll {
        try {
            Remove-DbaDatabase -SqlInstance $script:instance2 -Database $dbname1 -Confirm:$false
            Remove-DbaLogin -SqlInstance $script:instance2 -Login $login1 -Confirm:$false

            Remove-DbaDatabase -SqlInstance $script:instance2 -Database $dbname2 -Confirm:$false
            Remove-DbaLogin -SqlInstance $script:instance2 -Login $login2 -Confirm:$false
        } catch { }
        $timenow = (Get-Date -uformat "%m%d%Y%H")
        $ExportedCredential = Get-ChildItem $DefaultExportPath, $AltExportPath | Where-Object { $_.Name -match "$timenow\d{4}-login.sql|Dbatoolsci_login_CustomFile.sql" }
        $null = Remove-Item -Path $($ExportedCredential.FullName) -ErrorAction SilentlyContinue
    }

    Context "Executes with Exclude Parameters" {
        It "Should exclude databases when exporting" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -WarningAction SilentlyContinue
            $results = Get-Content -Path $file -Raw
            $allfiles += $file.FullName
            $results | Should Match '\nGo\r'
        }
        It "Should exclude Jobs when exporting" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeJobs -WarningAction SilentlyContinue
            $results = Get-Content -Path $file -Raw
            $allfiles += $file.FullName
            $results | Should Not Match 'Job'
        }
        It "Should exclude Go when exporting" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -ExcludeGoBatchSeparator -WarningAction SilentlyContinue
            $results = Get-Content -Path $file -Raw
            $allfiles += $file.FullName
            $results | Should Not Match 'Go'
        }
        It "Should exclude a specific login" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeLogin $login1 -WarningAction SilentlyContinue
            $results = Get-Content -Path $file -Raw
            $allfiles += $file.FullName
            $results | Should Not Match "$login1"
        }
    }
    Context "Executes for various users, databases, and environments" {
        It "Should Export a specific user" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -Login $login1 -Database $dbname1 -DefaultDatabase master -WarningAction SilentlyContinue
            $results = Get-Content -Path $file -Raw
            $allfiles += $file.FullName
            $results | Should Not Match "$login2|$dbname2"
            $results | Should Match "$login1|$dbname1"
        }
        foreach ($version in $((Get-Command $CommandName).Parameters.DestinationVersion.attributes.validvalues)) {
            It "Should Export for the SQLVersion $version" {
                $file = Export-DbaLogin -SqlInstance $script:instance2 -Login $login2 -Database $dbname2 -DestinationVersion $version -WarningAction SilentlyContinue
                $results = Get-Content -Path $file -Raw
                $allfiles += $file.FullName
                $results | Should Match "$login2|$dbname2"
                $results | Should Not Match "$login1|$dbname1"
            }
        }
    }
    Context "Exports file to random and specified paths" {
        It "Should export file to the configured path" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -WarningAction SilentlyContinue
            $results = $file.DirectoryName
            $allfiles += $file.FullName
            $results | Should Be $DefaultExportPath
        }
        It "Should export file to custom folder path" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -Path $AltExportPath -ExcludeDatabases -WarningAction SilentlyContinue
            $results = $file.DirectoryName
            $allfiles += $file.FullName
            $results | Should Be $AltExportPath
        }
        It "Should export file to custom file path" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -ExcludeDatabases -WarningAction SilentlyContinue
            $results = $file.Name
            $allfiles += $file.FullName
            $results | Should Be "Dbatoolsci_login_CustomFile.sql"
        }
        It "Should export file to custom file path and Append" {
            $file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -Append -ExcludeDatabases -WarningAction SilentlyContinue
            $allfiles += $file.FullName
            $file.CreationTimeUtc.Ticks | Should BeLessThan $file.LastWriteTimeUtc.Ticks
        }
        It "Should not export file to custom file path with NoClobber" {
            { Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -NoClobber -WarningAction SilentlyContinue } | Should Throw
        }
    }
}
{'fraction_non_alphanumeric': 0.11805951895637995, 'fraction_numerical': 0.010110069302894415, 'mean_word_length': 2.8403256105197245, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5694252', 'n_tokens_mistral': 3666, 'n_tokens_neox': 3505, 'n_words': 835}