def test_pshost_ui_not_implemented(self): # tests that methods we have marked as not implemented raise the error ui = PSHostUserInterface() with pytest.raises(NotImplementedError): ui.ReadLine(None, None) with pytest.raises(NotImplementedError): ui.ReadLineAsSecureString(None, None) with pytest.raises(NotImplementedError): ui.Prompt(None, None, None, None, None) with pytest.raises(NotImplementedError): ui.PromptForCredential1(None, None, None, None, None, None) with pytest.raises(NotImplementedError): ui.PromptForCredential2(None, None, None, None, None, None, None, None) with pytest.raises(NotImplementedError): ui.PromptForChoice(None, None, None, None, None, None)
def test_psrp_pshost_ui_mocked_methods(self, wsman_conn, monkeypatch): # This tests that the args from an actual host call match up with our # definitions monkeypatch.setattr('cryptography.hazmat.primitives.asymmetric.rsa.' 'generate_private_key', gen_rsa_keypair) mock_read_line = MagicMock(return_value="ReadLine response") mock_read_line_as_ss = MagicMock() mock_write1 = MagicMock(return_value=None) mock_write2 = MagicMock(return_value=None) mock_write_line1 = MagicMock(return_value=None) mock_write_line2 = MagicMock(return_value=None) mock_write_line3 = MagicMock(return_value=None) mock_write_error = MagicMock(return_value=None) mock_write_debug = MagicMock(return_value=None) mock_write_progress = MagicMock(return_value=None) mock_write_verbose = MagicMock(return_value=None) mock_write_warning = MagicMock(return_value=None) mock_prompt = MagicMock(return_value={ "prompt field": "prompt response", }) mock_prompt_credential = MagicMock() mock_prompt_choice = MagicMock(return_value=1) host_ui = PSHostUserInterface() host_ui.ReadLine = mock_read_line host_ui.ReadLineAsSecureString = mock_read_line_as_ss host_ui.Write1 = mock_write1 host_ui.Write2 = mock_write2 host_ui.WriteLine1 = mock_write_line1 host_ui.WriteLine2 = mock_write_line2 host_ui.WriteLine3 = mock_write_line3 host_ui.WriteErrorLine = mock_write_error host_ui.WriteDebugLine = mock_write_debug host_ui.WriteProgress = mock_write_progress host_ui.WriteVerboseLine = mock_write_verbose host_ui.WriteWarningLine = mock_write_warning host_ui.Prompt = mock_prompt # seems like PS never calls PromptForCredential1 so we will skip that host_ui.PromptForCredential2 = mock_prompt_credential host_ui.PromptForChoice = mock_prompt_choice host = PSHost(None, None, False, None, None, host_ui, None) with RunspacePool(wsman_conn, host=host) as pool: pool.exchange_keys() mock_read_line_as_ss.return_value = pool.serialize( u"ReadLineAsSecureString response", ObjectMeta("SS") ) mock_ps_credential = PSCredential(username="******", password=u"password") mock_prompt_credential.return_value = mock_ps_credential ps = PowerShell(pool) ps.add_script('''$host.UI.ReadLine() $host.UI.ReadLineAsSecureString() $host.UI.Write("Write1") $host.UI.Write([System.ConsoleColor]::Blue, [System.ConsoleColor]::White, "Write2") $host.UI.WriteLine() $host.UI.WriteLine("WriteLine2") $host.UI.WriteLine([System.ConsoleColor]::Gray, [System.ConsoleColor]::Green, "WriteLine3") $host.UI.WriteErrorLine("WriteErrorLine") $host.UI.WriteDebugLine("WriteDebugLine") $host.UI.WriteProgress(1, (New-Object -TypeName System.Management.Automation.ProgressRecord -ArgumentList 2, "activity", "description")) $host.UI.WriteVerboseLine("WriteVerboseLine") $host.UI.WriteWarningLine("WriteWarningLine") $prompt_field = New-Object -TypeName System.Management.Automation.Host.FieldDescription -ArgumentList @("prompt field") $prompt_field.Label = "PromptLabel" $host.UI.Prompt("Prompt caption", "Prompt message", $prompt_field) $host.UI.PromptForCredential("PromptForCredential caption", "PromptForCredential message", "PromptForCredential user", "PromptForCredential target") $choice_field1 = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList "Prompt1 label", "Prompt1 help" $choice_field2 = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList "Prompt2 label", "Prompt2 help" $host.UI.PromptForChoice("PromptForChoice caption", "PromptForChoice message", @($choice_field1, $choice_field2), 0)''') actual = ps.invoke() assert len(actual) == 5 assert actual[0] == "ReadLine response" assert mock_read_line.call_count == 1 assert isinstance(mock_read_line.call_args[0][0], RunspacePool) assert isinstance(mock_read_line.call_args[0][1], PowerShell) assert actual[1] == "ReadLineAsSecureString response" assert mock_read_line_as_ss.call_count == 1 assert isinstance(mock_read_line_as_ss.call_args[0][0], RunspacePool) assert isinstance(mock_read_line_as_ss.call_args[0][1], PowerShell) assert mock_write1.call_count == 1 assert isinstance(mock_write1.call_args[0][0], RunspacePool) assert isinstance(mock_write1.call_args[0][1], PowerShell) assert mock_write1.call_args[0][2] == "Write1" assert mock_write2.call_count == 1 assert isinstance(mock_write2.call_args[0][0], RunspacePool) assert isinstance(mock_write2.call_args[0][1], PowerShell) assert mock_write2.call_args[0][2] == Color.BLUE assert mock_write2.call_args[0][3] == Color.WHITE assert mock_write2.call_args[0][4] == "Write2" assert mock_write_line1.call_count == 1 assert isinstance(mock_write_line1.call_args[0][0], RunspacePool) assert isinstance(mock_write_line1.call_args[0][1], PowerShell) assert mock_write_line2.call_count == 1 assert isinstance(mock_write_line2.call_args[0][0], RunspacePool) assert isinstance(mock_write_line2.call_args[0][1], PowerShell) assert mock_write_line2.call_args[0][2] == "WriteLine2" assert mock_write_line3.call_count == 1 assert isinstance(mock_write_line3.call_args[0][0], RunspacePool) assert isinstance(mock_write_line3.call_args[0][1], PowerShell) assert mock_write_line3.call_args[0][2] == Color.GRAY assert mock_write_line3.call_args[0][3] == Color.GREEN assert mock_write_line3.call_args[0][4] == "WriteLine3" assert mock_write_error.call_count == 1 assert isinstance(mock_write_error.call_args[0][0], RunspacePool) assert isinstance(mock_write_error.call_args[0][1], PowerShell) assert mock_write_error.call_args[0][2] == "WriteErrorLine" assert mock_write_debug.call_count == 1 assert isinstance(mock_write_debug.call_args[0][0], RunspacePool) assert isinstance(mock_write_debug.call_args[0][1], PowerShell) assert mock_write_debug.call_args[0][2] == "WriteDebugLine" # On PSv5 a progress record is always sent, we still sent one # ourselves to ensure it works so we verify we received at least # one and assert the last assert mock_write_progress.call_count > 0 progress_args = mock_write_progress.call_args_list[-1] assert isinstance(progress_args[0][0], RunspacePool) assert isinstance(progress_args[0][1], PowerShell) assert progress_args[0][2] == 1 progress_record = pool._serializer.deserialize( progress_args[0][3], ObjectMeta("Obj", object=ProgressRecord) ) assert progress_record.activity == "activity" assert progress_record.activity_id == 2 assert progress_record.description == "description" assert mock_write_verbose.call_count == 1 assert isinstance(mock_write_verbose.call_args[0][0], RunspacePool) assert isinstance(mock_write_verbose.call_args[0][1], PowerShell) assert mock_write_verbose.call_args[0][2] == "WriteVerboseLine" assert mock_write_warning.call_count == 1 assert isinstance(mock_write_warning.call_args[0][0], RunspacePool) assert isinstance(mock_write_warning.call_args[0][1], PowerShell) assert mock_write_warning.call_args[0][2] == "WriteWarningLine" assert actual[2] == {"prompt field": "prompt response"} assert mock_prompt.call_count == 1 assert isinstance(mock_prompt.call_args[0][0], RunspacePool) assert isinstance(mock_prompt.call_args[0][1], PowerShell) assert mock_prompt.call_args[0][2] == "Prompt caption" assert mock_prompt.call_args[0][3] == "Prompt message" assert isinstance(mock_prompt.call_args[0][4], list) assert len(mock_prompt.call_args[0][4]) == 1 assert mock_prompt.call_args[0][4][0].extended_properties['name'] == \ 'prompt field' assert mock_prompt.call_args[0][4][0].extended_properties['label'] == \ 'PromptLabel' assert isinstance(actual[3], PSCredential) assert actual[3].username == "username" assert actual[3].password == "password" assert mock_prompt_credential.call_count == 1 assert isinstance(mock_prompt_credential.call_args[0][0], RunspacePool) assert isinstance(mock_prompt_credential.call_args[0][1], PowerShell) assert mock_prompt_credential.call_args[0][2] == \ "PromptForCredential caption" assert mock_prompt_credential.call_args[0][3] == \ "PromptForCredential message" assert mock_prompt_credential.call_args[0][4] == \ "PromptForCredential user" assert mock_prompt_credential.call_args[0][5] == \ "PromptForCredential target" assert mock_prompt_credential.call_args[0][6] == 3 assert mock_prompt_credential.call_args[0][7] == 1 assert actual[4] == 1 assert mock_prompt_choice.call_count == 1 assert isinstance(mock_prompt_choice.call_args[0][0], RunspacePool) assert isinstance(mock_prompt_choice.call_args[0][1], PowerShell) assert mock_prompt_choice.call_args[0][2] == "PromptForChoice caption" assert mock_prompt_choice.call_args[0][3] == "PromptForChoice message" assert isinstance(mock_prompt_choice.call_args[0][4], list) assert len(mock_prompt_choice.call_args[0][4]) == 2 assert mock_prompt_choice.call_args[0][4][0].extended_properties[ 'label'] == "Prompt1 label" assert mock_prompt_choice.call_args[0][4][0].extended_properties[ 'helpMessage'] == "Prompt1 help" assert mock_prompt_choice.call_args[0][4][1].extended_properties[ 'label'] == "Prompt2 label" assert mock_prompt_choice.call_args[0][4][1].extended_properties[ 'helpMessage'] == "Prompt2 help"