Skip to content

Commit 35b8bad

Browse files
Refactor the Credential related tests (#9737)
1 parent cef2878 commit 35b8bad

File tree

3 files changed

+40
-45
lines changed

3 files changed

+40
-45
lines changed

tests/Copy-DbaCredential.Tests.ps1

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
1616

1717
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
1818
BeforeAll {
19-
$logins = "dbatoolsci_thor", "dbatoolsci_thorsmomma", "dbatoolsci_thor_crypto"
19+
$logins = "thor", "thorsmomma", "thor_crypto"
2020
$plaintext = "BigOlPassword!"
2121
$password = ConvertTo-SecureString $plaintext -AsPlainText -Force
2222

@@ -26,7 +26,6 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
2626
# Add user
2727
foreach ($login in $logins) {
2828
$null = Invoke-Command2 -ScriptBlock { net user $args[0] $args[1] /add *>&1 } -ArgumentList $login, $plaintext -ComputerName $TestConfig.instance2
29-
$null = Invoke-Command2 -ScriptBlock { net user $args[0] $args[1] /add *>&1 } -ArgumentList $login, $plaintext -ComputerName $TestConfig.instance3
3029
}
3130

3231
<#
@@ -64,43 +63,41 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
6463
$cryptoProvider = ($instance2CryptoProviders | Where-Object { $_.name -eq $instance3CryptoProviders.name } | Select-Object -First 1).name
6564
}
6665
AfterAll {
67-
(Get-DbaCredential -SqlInstance $server2 -Identity dbatoolsci_thor, dbatoolsci_thorsmomma, dbatoolsci_thor_crypto -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
68-
(Get-DbaCredential -SqlInstance $server3 -Identity dbatoolsci_thor, dbatoolsci_thorsmomma, dbatoolsci_thor_crypto -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
66+
Remove-DbaCredential -SqlInstance $server2, $server3 -Identity thor, thorsmomma, thor_crypto -Confirm:$false
6967

7068
foreach ($login in $logins) {
7169
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
72-
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance3
7370
}
7471
}
7572

7673
Context "Create new credential" {
7774
It "Should create new credentials with the proper properties" {
78-
$results = New-DbaCredential -SqlInstance $server2 -Name dbatoolsci_thorcred -Identity dbatoolsci_thor -Password $password
79-
$results.Name | Should Be "dbatoolsci_thorcred"
80-
$results.Identity | Should Be "dbatoolsci_thor"
75+
$results = New-DbaCredential -SqlInstance $server2 -Name thorcred -Identity thor -Password $password
76+
$results.Name | Should Be "thorcred"
77+
$results.Identity | Should Be "thor"
8178

82-
$results = New-DbaCredential -SqlInstance $server2 -Identity dbatoolsci_thorsmomma -Password $password
83-
$results.Name | Should Be "dbatoolsci_thorsmomma"
84-
$results.Identity | Should Be "dbatoolsci_thorsmomma"
79+
$results = New-DbaCredential -SqlInstance $server2 -Identity thorsmomma -Password $password
80+
$results.Name | Should Be "thorsmomma"
81+
$results.Identity | Should Be "thorsmomma"
8582

8683
if ($cryptoProvider) {
87-
$results = New-DbaCredential -SqlInstance $server2 -Identity dbatoolsci_thor_crypto -Password $password -MappedClassType CryptographicProvider -ProviderName $cryptoProvider
88-
$results.Name | Should Be "dbatoolsci_thor_crypto"
89-
$results.Identity | Should Be "dbatoolsci_thor_crypto"
84+
$results = New-DbaCredential -SqlInstance $server2 -Identity thor_crypto -Password $password -MappedClassType CryptographicProvider -ProviderName $cryptoProvider
85+
$results.Name | Should Be "thor_crypto"
86+
$results.Identity | Should Be "thor_crypto"
9087
$results.ProviderName | Should -Be $cryptoProvider
9188
}
9289
}
9390
}
9491

9592
Context "Copy Credential with the same properties." {
9693
It "Should copy successfully" {
97-
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name dbatoolsci_thorcred
94+
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name thorcred
9895
$results.Status | Should Be "Successful"
9996
}
10097

10198
It "Should retain its same properties" {
102-
$Credential1 = Get-DbaCredential -SqlInstance $server2 -Name dbatoolsci_thor -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
103-
$Credential2 = Get-DbaCredential -SqlInstance $server3 -Name dbatoolsci_thor -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
99+
$Credential1 = Get-DbaCredential -SqlInstance $server2 -Name thor -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
100+
$Credential2 = Get-DbaCredential -SqlInstance $server3 -Name thor -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
104101

105102
# Compare its value
106103
$Credential1.Name | Should Be $Credential2.Name
@@ -110,25 +107,25 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
110107

111108
Context "No overwrite" {
112109
It "does not overwrite without force" {
113-
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name dbatoolsci_thorcred
110+
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name thorcred
114111
$results.Status | Should Be "Skipping"
115112
}
116113
}
117114

118115
# See https://github.com/dataplat/dbatools/issues/7896 and comments above in BeforeAll
119116
Context "Crypto provider cred" {
120117
It -Skip:(-not $cryptoProvider) "ensure copied credential is using the same crypto provider" {
121-
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name dbatoolsci_thor_crypto
118+
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name thor_crypto
122119
$results.Status | Should Be Successful
123-
$results = Get-DbaCredential -SqlInstance $server3 -Name dbatoolsci_thor_crypto
124-
$results.Name | Should -Be dbatoolsci_thor_crypto
120+
$results = Get-DbaCredential -SqlInstance $server3 -Name thor_crypto
121+
$results.Name | Should -Be thor_crypto
125122
$results.ProviderName | Should -Be $cryptoProvider
126123
}
127124

128125
It -Skip:(-not $cryptoProvider) "check warning message if crypto provider is not configured/enabled on destination" {
129-
Remove-DbaCredential -SqlInstance $server3 -Credential dbatoolsci_thor_crypto -Confirm:$false
126+
Remove-DbaCredential -SqlInstance $server3 -Credential thor_crypto -Confirm:$false
130127
$server3.Query("ALTER CRYPTOGRAPHIC PROVIDER $cryptoProvider DISABLE")
131-
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name dbatoolsci_thor_crypto
128+
$results = Copy-DbaCredential -Source $server2 -Destination $server3 -Name thor_crypto
132129
$server3.Query("ALTER CRYPTOGRAPHIC PROVIDER $cryptoProvider ENABLE")
133130
$results.Status | Should Be Failed
134131
$results.Notes | Should -Match "The cryptographic provider $cryptoProvider needs to be configured and enabled on"

tests/Get-DbaCredential.Tests.ps1

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
1717

1818
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
1919
BeforeAll {
20-
$logins = "dbatoolsci_thor", "dbatoolsci_thorsmomma"
20+
$logins = "thor", "thorsmomma"
2121
$plaintext = "BigOlPassword!"
2222
$password = ConvertTo-SecureString $plaintext -AsPlainText -Force
2323

@@ -26,33 +26,32 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
2626
$null = Invoke-Command2 -ScriptBlock { net user $args[0] $args[1] /add *>&1 } -ArgumentList $login, $plaintext -ComputerName $TestConfig.instance2
2727
}
2828

29-
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Name dbatoolsci_thorcred -Identity dbatoolsci_thor -Password $password
30-
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thorsmomma -Password $password
29+
$null = New-DbaCredential -SqlInstance $TestConfig.instance2 -Name thorcred -Identity thor -Password $password
30+
$null = New-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thorsmomma -Password $password
3131
}
3232
AfterAll {
3333
try {
34-
(Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thor, dbatoolsci_thorsmomma -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
34+
(Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thor, thorsmomma -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
3535
} catch { }
3636

3737
foreach ($login in $logins) {
3838
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
39-
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
4039
}
4140
}
4241

4342
Context "Get credentials" {
4443
It "Should get just one credential with the proper properties when using Identity" {
45-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thorsmomma
46-
$results.Name | Should Be "dbatoolsci_thorsmomma"
47-
$results.Identity | Should Be "dbatoolsci_thorsmomma"
44+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thorsmomma
45+
$results.Name | Should Be "thorsmomma"
46+
$results.Identity | Should Be "thorsmomma"
4847
}
4948
It "Should get just one credential with the proper properties when using Name" {
50-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Name dbatoolsci_thorsmomma
51-
$results.Name | Should Be "dbatoolsci_thorsmomma"
52-
$results.Identity | Should Be "dbatoolsci_thorsmomma"
49+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Name thorsmomma
50+
$results.Name | Should Be "thorsmomma"
51+
$results.Identity | Should Be "thorsmomma"
5352
}
5453
It "gets more than one credential" {
55-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thor, dbatoolsci_thorsmomma
54+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thor, thorsmomma
5655
$results.count -gt 1
5756
}
5857
}

tests/New-DbaCredential.Tests.ps1

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
1616

1717
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
1818
BeforeAll {
19-
$logins = "dbatoolsci_thor", "dbatoolsci_thorsmomma"
19+
$logins = "thor", "thorsmomma"
2020
$plaintext = "BigOlPassword!"
2121
$password = ConvertTo-SecureString $plaintext -AsPlainText -Force
2222

@@ -27,29 +27,28 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
2727
}
2828
AfterAll {
2929
try {
30-
(Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thor, dbatoolsci_thorsmomma -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
30+
(Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thor, thorsmomma -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
3131
(Get-DbaCredential -SqlInstance $TestConfig.instance2 -Name "https://mystorageaccount.blob.core.windows.net/mycontainer" -ErrorAction Stop -WarningAction SilentlyContinue).Drop()
3232
} catch { }
3333

3434
foreach ($login in $logins) {
3535
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
36-
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
3736
}
3837
}
3938

4039
Context "Create a new credential" {
4140
It "Should create new credentials with the proper properties" {
42-
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Name dbatoolsci_thorcred -Identity dbatoolsci_thor -Password $password
43-
$results.Name | Should Be "dbatoolsci_thorcred"
44-
$results.Identity | Should Be "dbatoolsci_thor"
41+
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Name thorcred -Identity thor -Password $password
42+
$results.Name | Should Be "thorcred"
43+
$results.Identity | Should Be "thor"
4544

46-
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thorsmomma -Password $password
45+
$results = New-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thorsmomma -Password $password
4746
$results | Should Not Be $null
4847
}
4948
It "Gets the newly created credential" {
50-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity dbatoolsci_thorsmomma
51-
$results.Name | Should Be "dbatoolsci_thorsmomma"
52-
$results.Identity | Should Be "dbatoolsci_thorsmomma"
49+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thorsmomma
50+
$results.Name | Should Be "thorsmomma"
51+
$results.Identity | Should Be "thorsmomma"
5352
}
5453
}
5554

0 commit comments

Comments
 (0)