<s> import os <EOL> import mock <EOL> from awscli . customizations . configure import configure , ConfigValue , NOT_SET <EOL> from awscli . testutils import unittest <EOL> from . import FakeSession <EOL> class TestConfigureCommand ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . writer = mock . Mock ( ) <EOL> self . global_args = mock . Mock ( ) <EOL> self . global_args . profile = None <EOL> self . precanned = PrecannedPrompter ( value = '<STR_LIT>' ) <EOL> self . session = FakeSession ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . configure = configure . ConfigureCommand ( self . session , <EOL> prompter = self . precanned , <EOL> config_writer = self . writer ) <EOL> def assert_credentials_file_updated_with ( self , new_values ) : <EOL> called_args = self . writer . update_config . call_args_list <EOL> credentials_file_call = called_args [ <NUM_LIT:0> ] <EOL> expected_creds_file = os . path . expanduser ( '<STR_LIT>' ) <EOL> self . assertEqual ( credentials_file_call , <EOL> mock . call ( new_values , expected_creds_file ) ) <EOL> def test_configure_command_sends_values_to_writer ( self ) : <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . assert_credentials_file_updated_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . writer . update_config . assert_called_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> def test_same_values_are_not_changed ( self ) : <EOL> self . configure = configure . ConfigureCommand ( self . session , <EOL> prompter = EchoPrompter ( ) , <EOL> config_writer = self . writer ) <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . assertFalse ( self . writer . update_config . called ) <EOL> def test_none_values_are_not_changed ( self ) : <EOL> user_presses_enter = None <EOL> precanned = PrecannedPrompter ( value = user_presses_enter ) <EOL> self . configure = configure . ConfigureCommand ( self . session , <EOL> prompter = precanned , <EOL> config_writer = self . writer ) <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . assertFalse ( self . writer . update_config . called ) <EOL> def test_create_configure_cmd_session_only ( self ) : <EOL> self . configure = configure . ConfigureCommand ( self . session ) <EOL> self . assertIsInstance ( self . configure , configure . ConfigureCommand ) <EOL> def test_some_values_changed ( self ) : <EOL> responses = { <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> prompter = KeyValuePrompter ( responses ) <EOL> self . configure = configure . ConfigureCommand ( self . session , prompter = prompter , <EOL> config_writer = self . writer ) <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . writer . update_config . assert_called_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> def test_section_name_can_be_changed_for_profiles ( self ) : <EOL> self . global_args . profile = '<STR_LIT>' <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . assert_credentials_file_updated_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . writer . update_config . assert_called_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> def test_session_says_profile_does_not_exist ( self ) : <EOL> session = FakeSession ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> profile_does_not_exist = True ) <EOL> self . configure = configure . ConfigureCommand ( session , <EOL> prompter = self . precanned , <EOL> config_writer = self . writer ) <EOL> self . global_args . profile = '<STR_LIT>' <EOL> self . configure ( args = [ ] , parsed_globals = self . global_args ) <EOL> self . assert_credentials_file_updated_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . writer . update_config . assert_called_with ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> class TestInteractivePrompter ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . patch = mock . patch ( <EOL> '<STR_LIT>' ) <EOL> self . mock_raw_input = self . patch . start ( ) <EOL> def tearDown ( self ) : <EOL> self . patch . stop ( ) <EOL> def test_access_key_is_masked ( self ) : <EOL> self . mock_raw_input . return_value = '<STR_LIT:foo>' <EOL> prompter = configure . InteractivePrompter ( ) <EOL> response = prompter . get_value ( <EOL> current_value = '<STR_LIT>' , config_name = '<STR_LIT>' , <EOL> prompt_text = '<STR_LIT>' ) <EOL> self . assertEqual ( response , '<STR_LIT:foo>' ) <EOL> prompt_text = self . mock_raw_input . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertNotIn ( '<STR_LIT>' , prompt_text ) <EOL> self . assertRegexpMatches ( prompt_text , r'<STR_LIT>' ) <EOL> def test_access_key_not_masked_when_none ( self ) : <EOL> self . mock_raw_input . return_value = '<STR_LIT:foo>' <EOL> prompter = configure . InteractivePrompter ( ) <EOL> response = prompter . get_value ( <EOL> current_value = None , config_name = '<STR_LIT>' , <EOL> prompt_text = '<STR_LIT>' ) <EOL> self . assertEqual ( response , '<STR_LIT:foo>' ) <EOL> prompt_text = self . mock_raw_input . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertIn ( '<STR_LIT>' , prompt_text ) <EOL> def test_secret_key_is_masked ( self ) : <EOL> prompter = configure . InteractivePrompter ( ) <EOL> prompter . get_value ( <EOL> current_value = '<STR_LIT>' , <EOL> config_name = '<STR_LIT>' , <EOL> prompt_text = '<STR_LIT>' ) <EOL> prompt_text = self . mock_raw_input . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertNotIn ( '<STR_LIT>' , prompt_text ) <EOL> self . assertRegexpMatches ( prompt_text , r'<STR_LIT>' ) <EOL> def test_non_secret_keys_are_not_masked ( self ) : <EOL> prompter = configure . InteractivePrompter ( ) <EOL> prompter . get_value ( <EOL> current_value = '<STR_LIT>' , config_name = '<STR_LIT>' , <EOL> prompt_text = '<STR_LIT>' ) <EOL> prompt_text = self . mock_raw_input . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . assertIn ( '<STR_LIT>' , prompt_text ) <EOL> self . assertRegexpMatches ( prompt_text , r'<STR_LIT>' ) <EOL> def test_user_hits_enter_returns_none ( self ) : <EOL> self . mock_raw_input . return_value = '<STR_LIT>' <EOL> prompter = configure . InteractivePrompter ( ) <EOL> response = prompter . get_value ( <EOL> current_value = None , config_name = '<STR_LIT>' , <EOL> prompt_text = '<STR_LIT>' ) <EOL> self . assertIsNone ( response ) <EOL> class TestConfigValueMasking ( unittest . TestCase ) : <EOL> def test_config_value_is_masked ( self ) : <EOL> config_value = ConfigValue ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( config_value . value , '<STR_LIT>' ) <EOL> config_value . mask_value ( ) <EOL> self . assertEqual ( config_value . value , '<STR_LIT>' ) <EOL> def test_dont_mask_unset_value ( self ) : <EOL> no_config = ConfigValue ( NOT_SET , None , None ) <EOL> self . assertEqual ( no_config . value , NOT_SET ) <EOL> no_config . mask_value ( ) <EOL> self . assertEqual ( no_config . value , NOT_SET ) <EOL> class PrecannedPrompter ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . _value = value <EOL> def get_value ( self , current_value , logical_name , prompt_text = '<STR_LIT>' ) : <EOL> return self . _value <EOL> class EchoPrompter ( object ) : <EOL> def get_value ( self , current_value , logical_name , prompt_text = '<STR_LIT>' ) : <EOL> return current_value <EOL> class KeyValuePrompter ( object ) : <EOL> def __init__ ( self , mapping ) : <EOL> self . mapping = mapping <EOL> def get_value ( self , current_value , config_name , prompt_text = '<STR_LIT>' ) : <EOL> return self . mapping . get ( prompt_text ) </s>
