diff --git a/plugins/synced_folders/smb/synced_folder.rb b/plugins/synced_folders/smb/synced_folder.rb index 4a95a0da5..f382c36c9 100644 --- a/plugins/synced_folders/smb/synced_folder.rb +++ b/plugins/synced_folders/smb/synced_folder.rb @@ -39,19 +39,27 @@ module VagrantPlugins # If we need auth information, then ask the user. have_auth = false folders.each do |id, data| - if data[:smb_username] && data[:smb_password] - smb_username = data[:smb_username] - smb_password = data[:smb_password] + smb_username = data[:smb_username] if data[:smb_username] + smb_password = data[:smb_password] if data[:smb_password] + if smb_username && smb_password have_auth = true break end end + modify_username = false if !have_auth machine.ui.detail(I18n.t("vagrant_sf_smb.warning_password") + "\n ") retries = 0 while retries < CREDENTIAL_RETRY_MAX do - smb_username = machine.ui.ask("Username: ") + if smb_username + username = machine.ui.ask("Username (#{smb_username}): ") + smb_username = username if username != "" + modify_username = true + else + smb_username = machine.ui.ask("Username: ") + end + smb_password = machine.ui.ask("Password (will be hidden): ", echo: false) auth_success = true @@ -77,7 +85,12 @@ module VagrantPlugins end folders.each do |id, data| - data[:smb_username] ||= smb_username + if modify_username + # Only override original username if user requests to + data[:smb_username] = smb_username + else + data[:smb_username] ||= smb_username + end data[:smb_password] ||= smb_password # Register password as sensitive diff --git a/test/unit/plugins/synced_folders/smb/synced_folder_test.rb b/test/unit/plugins/synced_folders/smb/synced_folder_test.rb index 0b1e9c85d..493a427f5 100644 --- a/test/unit/plugins/synced_folders/smb/synced_folder_test.rb +++ b/test/unit/plugins/synced_folders/smb/synced_folder_test.rb @@ -69,6 +69,36 @@ describe VagrantPlugins::SyncedFolderSMB::SyncedFolder do describe ".prepare" do let(:host_caps){ [:smb_start, :smb_prepare] } + context "with username credentials provided" do + let(:folders){ {'/first/path' => {smb_username: 'smbuser'}} } + + it "should prompt for credentials" do + expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1) + expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1) + + subject.prepare(machine, folders, options) + end + + it "should set credential information into all folder options and override username" do + expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1) + expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1) + + subject.prepare(machine, folders, options) + expect(folders['/first/path'][:smb_username]).to eq('username') + expect(folders['/first/path'][:smb_password]).to eq('password') + end + + + it "will use configured default with no input" do + expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('').at_least(1) + expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1) + + subject.prepare(machine, folders, options) + expect(folders['/first/path'][:smb_username]).to eq('smbuser') + expect(folders['/first/path'][:smb_password]).to eq('password') + end + end + context "without credentials provided" do before do expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1)