Create new test for Get-Credential which requires module to create a test host

This commit is contained in:
James Truher 2016-08-19 13:01:04 -07:00
parent d7b31245d3
commit 5644c4c54b
2 changed files with 303 additions and 0 deletions

View file

@ -0,0 +1,272 @@
$definition = @'
using System;
using System.Collections.Generic;
using System.Threading;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Security;
using System.Collections;
namespace TestHost
{
public class TestHostRawUI : PSHostRawUserInterface
{
private ConsoleColor _backgroundColor = ConsoleColor.Black;
public override ConsoleColor BackgroundColor
{
get { return _backgroundColor; }
set { _backgroundColor = value; }
}
private ConsoleColor _foregroundColor = ConsoleColor.White;
public override ConsoleColor ForegroundColor
{
get { return _foregroundColor; }
set { _foregroundColor = value; }
}
private string _windowTitle = "title";
public override string WindowTitle
{
get { return _windowTitle; }
set { _windowTitle = value; }
}
private Size _bufferSize = new Size(10,10);
public override Size BufferSize
{
get { return _bufferSize; }
set { _bufferSize = value; }
}
private Coordinates _cursorPosition = new Coordinates(0, 0);
public override Coordinates CursorPosition
{
get { return _cursorPosition; }
set { _cursorPosition = value; }
}
private int _cursorSize = 10;
public override int CursorSize
{
get { return _cursorSize; }
set { _cursorSize = value; }
}
private bool _keyAvailable = false;
public override bool KeyAvailable
{
get { return _keyAvailable; }
}
public override Size MaxPhysicalWindowSize
{
get { throw new NotImplementedException(); }
}
public override Size MaxWindowSize
{
get { throw new NotImplementedException(); }
}
private Coordinates _windowPosition = new Coordinates(0, 0);
public override Coordinates WindowPosition
{
get { return _windowPosition; }
set { _windowPosition = value; }
}
private Size _windowSize = new Size(80, 40);
public override Size WindowSize
{
get { return _windowSize; }
set { _windowSize = value; }
}
public override BufferCell[,] GetBufferContents(Rectangle rectangle) { throw new NotImplementedException(); }
public override void FlushInputBuffer() { throw new NotImplementedException(); }
public override KeyInfo ReadKey(ReadKeyOptions options) { throw new NotImplementedException(); }
public override void SetBufferContents(Coordinates origin, BufferCell[,] contents) { throw new NotImplementedException(); }
public override void SetBufferContents(Rectangle rectangle, BufferCell fill) { throw new NotImplementedException(); }
public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill) { throw new NotImplementedException(); }
}
public class Streams
{
public ArrayList ConsoleOutput = new ArrayList();
public ArrayList Output = new ArrayList();
public ArrayList Input = new ArrayList();
public ArrayList Error = new ArrayList();
public ArrayList Verbose = new ArrayList();
public ArrayList Debug = new ArrayList();
public ArrayList Warning = new ArrayList();
public ArrayList Information = new ArrayList();
public ArrayList Progress = new ArrayList();
public ArrayList Prompt = new ArrayList();
}
public class TestPSHostUserInterface : PSHostUserInterface
{
private PSHostRawUserInterface _rawui = new TestHostRawUI();
public string ReadLineData = "This is readline data";
public int PromptedChoice = 0;
public string StringForSecureString = "TEST";
public string promptResponse = "this is a prompt response";
public Streams Streams = new Streams();
public override PSHostRawUserInterface RawUI
{
get { return _rawui; }
}
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
Streams.Prompt.Add(caption + ":" + message);
Dictionary<string, PSObject> d = new Dictionary<string, PSObject>();
d.Add(descriptions[0].ToString(), new PSObject(promptResponse));
return d;
}
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
Streams.Prompt.Add(caption + ":" + message);
return PromptedChoice;
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
return new PSCredential(userName, ss);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
return new PSCredential(userName, ss);
}
public override string ReadLine()
{
return ReadLineData;
}
public override SecureString ReadLineAsSecureString()
{
SecureString ss = new SecureString();
foreach(char c in StringForSecureString.ToCharArray()) { ss.AppendChar(c); }
return ss;
}
public override void Write(string value)
{
Streams.ConsoleOutput.Add(value);
}
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
Streams.ConsoleOutput.Add(value);
}
public override void WriteDebugLine(string message)
{
Streams.Debug.Add(message);
}
public override void WriteErrorLine(string value)
{
Streams.Error.Add(value);
}
public override void WriteLine(string value)
{
Streams.ConsoleOutput.Add(value);
}
public override void WriteProgress(long sourceId, ProgressRecord record)
{
Streams.Progress.Add(record);
}
public override void WriteVerboseLine(string message)
{
Streams.Verbose.Add(message);
}
public override void WriteWarningLine(string message)
{
Streams.Warning.Add(message);
}
}
public class TestHost : PSHost
{
private Guid _instanceId = Guid.NewGuid();
private PSHostUserInterface _ui = new TestPSHostUserInterface();
public string _hostName = "TEST HOST";
public Version _version = new Version(6, 0);
int promptLevel = 0;
public override CultureInfo CurrentCulture
{
get { return Thread.CurrentThread.CurrentCulture; }
}
public override CultureInfo CurrentUICulture
{
get { return Thread.CurrentThread.CurrentUICulture; }
}
public override Guid InstanceId
{
get { return _instanceId; }
}
public override string Name
{
get { return _hostName; }
}
public override PSHostUserInterface UI
{
get { return _ui; }
}
public override Version Version
{
get { return _version; }
}
public override void EnterNestedPrompt()
{
promptLevel++;
}
public override void ExitNestedPrompt()
{
promptLevel--;
}
public override void NotifyBeginApplication()
{
throw new NotImplementedException();
}
public override void NotifyEndApplication()
{
throw new NotImplementedException();
}
public override void SetShouldExit(int exitCode)
{
throw new NotImplementedException();
}
}
}
'@
## '
function New-TestHost
{
$references = "System","mscorlib","System.Management.Automation",
"System.Collections.NonGeneric","System.Console","System.Collections",
"System.Globalization"
if ( ! ("TestHost.TestHost" -as "type" )) {
$t = add-Type -pass $definition -ref $references
}
[TestHost.TestHost]::New()
}

View file

@ -0,0 +1,31 @@
if ( ! (get-module -ea silentlycontinue TestHostCS ))
{
$root = git rev-parse --show-toplevel
$pestertestroot = join-path $root test/powershell
$common = join-path $pestertestroot Common
$hostmodule = join-path $common TestHostCS.psm1
import-module $hostmodule
}
Describe "Get-Credential Test" -tag "CI" {
BeforeAll {
$th = New-TestHost
$th.UI.StringForSecureString = "This is a test"
$rs = [runspacefactory]::Createrunspace($th)
$rs.open()
$ps = [powershell]::Create()
$ps.Runspace = $rs
$ps.Commands.Clear()
}
AfterAll {
$rs.Close()
$rs.Dispose()
$ps.Dispose()
}
It "Get-Credential produces as credential object" {
$cred = $ps.AddScript("Get-Credential -User Joe -Message Foo").Invoke() | Select-Object -First 1
$cred.gettype().FullName | Should Be "System.Management.Automation.PSCredential"
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should be "Joe"
$netcred.Password | Should be "this is a test"
}
}