@@ -31,6 +31,7 @@ Describe $CommandName -Tag UnitTests {
3131 " NoPrefix" ,
3232 " Passthru" ,
3333 " ObjectLevel" ,
34+ " IncludeRolePermissions" ,
3435 " EnableException"
3536 )
3637 Compare-Object - ReferenceObject $expectedParameters - DifferenceObject $hasParameters | Should - BeNullOrEmpty
@@ -73,6 +74,21 @@ Describe $CommandName -Tag IntegrationTests {
7374 $login3 = " dbatoolsci_exportdbalogin_login3$random "
7475 $server.Query (" CREATE LOGIN [$login3 ] WITH PASSWORD = 'GoodPass1234!'" )
7576 $db1.Query (" CREATE USER [$login3 ] WITHOUT LOGIN" )
77+
78+ # login with a custom role that has granted permissions (for IncludeRolePermissions tests)
79+ $login4 = " dbatoolsci_exportdbalogin_login4$random "
80+ $user4 = " dbatoolsci_exportdbalogin_user4$random "
81+ $role4 = " dbatoolsci_exportdbalogin_role4$random "
82+ $null = $server.Query (" CREATE LOGIN [$login4 ] WITH PASSWORD = 'GoodPass1234!'" )
83+ $db1.Query (" CREATE USER [$user4 ] FOR LOGIN [$login4 ]" )
84+ $db1.Query (" CREATE ROLE [$role4 ]" )
85+ $db1.Query (" GRANT SELECT ON SCHEMA::dbo TO [$role4 ]" )
86+ $db1.Query (" GRANT EXECUTE ON SCHEMA::dbo TO [$role4 ]" )
87+ if ($server.VersionMajor -lt 11 ) {
88+ $db1.Query (" EXEC sp_addrolemember @rolename = N'$role4 ', @membername = N'$user4 '" )
89+ } else {
90+ $db1.Query (" ALTER ROLE [$role4 ] ADD MEMBER [$user4 ]" )
91+ }
7692 }
7793 AfterAll {
7894 Remove-DbaDatabase - SqlInstance $TestConfig.InstanceSingle - Database $dbname1
@@ -87,6 +103,7 @@ Describe $CommandName -Tag IntegrationTests {
87103 }
88104
89105 Remove-DbaLogin - SqlInstance $TestConfig.InstanceSingle - Login $login3
106+ Remove-DbaLogin - SqlInstance $TestConfig.InstanceSingle - Login $login4
90107 }
91108
92109 Context " Executes with Exclude Parameters" {
@@ -159,6 +176,24 @@ Describe $CommandName -Tag IntegrationTests {
159176 $results | Should -Match ([regex ]::Escape(" IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'$user1 ')" ))
160177 }
161178 }
179+ Context " Executes with IncludeRolePermissions" {
180+ It " Should include role permissions in non-ObjectLevel export" {
181+ $results = Export-DbaLogin - SqlInstance $server - Login $login4 - Database $dbname1 - IncludeRolePermissions - Passthru - WarningAction SilentlyContinue
182+ $results | Should -Match " GRANT SELECT ON SCHEMA::\[dbo\]"
183+ $results | Should -Match " GRANT EXECUTE ON SCHEMA::\[dbo\]"
184+ $results | Should -Match ([regex ]::Escape(" [$role4 ]" ))
185+ }
186+ It " Should include role permissions in ObjectLevel export" {
187+ $results = Export-DbaLogin - SqlInstance $server - Login $login4 - Database $dbname1 - ObjectLevel - IncludeRolePermissions - Passthru - WarningAction SilentlyContinue
188+ $results | Should -Match " GRANT SELECT ON SCHEMA::\[dbo\]"
189+ $results | Should -Match " GRANT EXECUTE ON SCHEMA::\[dbo\]"
190+ $results | Should -Match ([regex ]::Escape(" [$role4 ]" ))
191+ }
192+ It " Should not include role permissions without the switch" {
193+ $results = Export-DbaLogin - SqlInstance $server - Login $login4 - Database $dbname1 - Passthru - WarningAction SilentlyContinue
194+ $results | Should -Not -Match " GRANT SELECT ON SCHEMA::\[dbo\]"
195+ }
196+ }
162197 Context " Exports file to random and specified paths" {
163198 It " Should export file to the configured path" {
164199 $file = Export-DbaLogin - SqlInstance $TestConfig.InstanceSingle - ExcludeDatabase - WarningAction SilentlyContinue
0 commit comments