MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]5param()6 7Describe "Get-Credential Test" -Tag "CI" {8 BeforeAll {9 $th = New-TestHost10 $th.UI.StringForSecureString = "This is a test"11 $th.UI.UserNameForCredential = "John"12 $rs = [runspacefactory]::Createrunspace($th)13 $rs.open()14 $ps = [powershell]::Create()15 $ps.Runspace = $rs16 $ps.Commands.Clear()17 }18 AfterAll {19 $rs.Close()20 $rs.Dispose()21 $ps.Dispose()22 }23 AfterEach {24 $ps.Commands.Clear()25 }26 It "Get-Credential with message, produces a credential object" {27 $cred = $ps.AddScript("Get-Credential -UserName Joe -Message Foo").Invoke() | Select-Object -First 128 $cred | Should -BeOfType System.Management.Automation.PSCredential29 $netcred = $cred.GetNetworkCredential()30 $netcred.UserName | Should -Be "Joe"31 $netcred.Password | Should -Be "this is a test"32 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:Foo"33 }34 It "Get-Credential with title, produces a credential object" {35 $cred = $ps.AddScript("Get-Credential -UserName Joe -Title CustomTitle").Invoke() | Select-Object -First 136 $cred | Should -BeOfType System.Management.Automation.PSCredential37 $netcred = $cred.GetNetworkCredential()38 $netcred.UserName | Should -Be "Joe"39 $netcred.Password | Should -Be "this is a test"40 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:CustomTitle:[^:]+"41 }42 It "Get-Credential with only username, produces a credential object" {43 $cred = $ps.AddScript("Get-Credential -UserName Joe").Invoke() | Select-Object -First 144 $cred | Should -BeOfType System.Management.Automation.PSCredential45 $netcred = $cred.GetNetworkCredential()46 $netcred.UserName | Should -Be "Joe"47 $netcred.Password | Should -Be "this is a test"48 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"49 }50 It "Get-Credential with title and message, produces a credential object" {51 $cred = $ps.AddScript("Get-Credential -UserName Joe -Message Foo -Title CustomTitle").Invoke() | Select-Object -First 152 $cred | Should -BeOfType System.Management.Automation.PSCredential53 $netcred = $cred.GetNetworkCredential()54 $netcred.UserName | Should -Be "Joe"55 $netcred.Password | Should -Be "this is a test"56 $th.ui.Streams.Prompt[-1] | Should -Be "Credential:CustomTitle:Foo"57 }58 It "Get-Credential without parameters" {59 $cred = $ps.AddScript("Get-Credential").Invoke() | Select-Object -First 160 $cred | Should -BeOfType System.Management.Automation.PSCredential61 $netcred = $cred.GetNetworkCredential()62 $netcred.UserName | Should -Be "John"63 $netcred.Password | Should -Be "This is a test"64 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"65 }66 It "Get-Credential `$null" {67 $cred = $ps.AddScript("Get-Credential `$null").Invoke() | Select-Object -First 168 $cred | Should -BeOfType System.Management.Automation.PSCredential69 $netcred = $cred.GetNetworkCredential()70 $netcred.UserName | Should -Be "John"71 $netcred.Password | Should -Be "This is a test"72 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"73 }74 It "Get-Credential -Credential `$null" {75 $cred = $ps.AddScript("Get-Credential -Credential `$null").Invoke() | Select-Object -First 176 $cred | Should -BeOfType System.Management.Automation.PSCredential77 $netcred = $cred.GetNetworkCredential()78 $netcred.UserName | Should -Be "John"79 $netcred.Password | Should -Be "This is a test"80 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"81 }82 It "Get-Credential Joe" {83 $cred = $ps.AddScript("Get-Credential Joe").Invoke() | Select-Object -First 184 $cred | Should -BeOfType System.Management.Automation.PSCredential85 $netcred = $cred.GetNetworkCredential()86 $netcred.UserName | Should -Be "Joe"87 $netcred.Password | Should -Be "This is a test"88 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"89 }90 It "Get-Credential -Credential Joe" {91 $cred = $ps.AddScript("Get-Credential Joe").Invoke() | Select-Object -First 192 $cred | Should -BeOfType System.Management.Automation.PSCredential93 $netcred = $cred.GetNetworkCredential()94 $netcred.UserName | Should -Be "Joe"95 $netcred.Password | Should -Be "This is a test"96 $th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"97 }98 It "Get-Credential `$credential" {99 #[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]100 $password = ConvertTo-SecureString -String "CredTest" -AsPlainText -Force101 $credential = [pscredential]::new("John", $password)102 103 $cred = Get-Credential $credential104 $cred | Should -BeOfType System.Management.Automation.PSCredential105 $netcred = $cred.GetNetworkCredential()106 $netcred.UserName | Should -Be "John"107 $netcred.Password | Should -Be "CredTest"108 }109}110 