Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Fix rails console login method returning inconsistent results
Browse files Browse the repository at this point in the history
Change-Id: I62a4451b1a7988906f7eede74776036cd1a8a829
  • Loading branch information
Jennifer Hickey committed May 8, 2012
1 parent 0a459dc commit f78d029
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
15 changes: 8 additions & 7 deletions lib/cli/console_helper.rb
Expand Up @@ -50,13 +50,14 @@ def console_login(auth_info, port)
5.times do
begin
results = @telnet_client.login("Name"=>auth_info["username"],
"Password"=>auth_info["password"]) {|line|
if line =~ /[$%#>] \z/n
prompt = line
elsif line =~ /Login failed/
err_msg = line
end
}
"Password"=>auth_info["password"])
lines = results.split("\n")
last_line = lines.pop
if last_line =~ /[$%#>] \z/n
prompt = last_line
elsif last_line =~ /Login failed/
err_msg = last_line
end
break
rescue TimeoutError
sleep 1
Expand Down
26 changes: 13 additions & 13 deletions spec/unit/console_helper_spec.rb
Expand Up @@ -68,7 +68,7 @@

it 'should start console and process a command if authentication succeeds' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Switch to inspect mode\nirb():001:0> ")
exit_console "irb():001:0> "
start_local_console(3344,'foo')
end
Expand All @@ -86,7 +86,7 @@

it 'should exit if authentication fails' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("Login failed.")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Login failed.")
@telnet_client.should_receive(:close)
errmsg = nil
begin
Expand All @@ -100,7 +100,7 @@
it 'should retry authentication on timeout' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_raise(TimeoutError)
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Switch to inspect mode\nirb():001:0> ")
exit_console "irb():001:0> "
start_local_console(3344,'foo')
end
Expand All @@ -109,14 +109,14 @@
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_raise(EOFError)
@telnet_client.should_receive(:close)
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
exit_console "irb():001:0> "
start_local_console(3344,'foo')
end

it 'should operate console interactively' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
Readline.should_receive(:readline).with("irb():001:0> ",true).and_return("puts 'hi'")
@telnet_client.should_receive(:cmd).with("puts 'hi'").and_return("nil" + "\n" + "irb():002:0> ")
exit_console "irb():002:0> "
Expand All @@ -125,7 +125,7 @@

it 'should not crash if command times out' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
Readline.should_receive(:readline).with("irb():001:0> ",true).and_return("puts 'hi'")
@telnet_client.should_receive(:cmd).with("puts 'hi'").and_raise(TimeoutError)
exit_console "irb():001:0> "
Expand All @@ -134,7 +134,7 @@

it 'should exit with error message if an EOF is received' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Switch to inspect mode\nirb():001:0> ")
Readline.should_receive(:readline).with("irb():001:0> ",true).and_return("puts 'hi'")
@telnet_client.should_receive(:cmd).with("puts 'hi'").and_raise(EOFError)
errmsg = nil
Expand All @@ -149,7 +149,7 @@

it 'should not process blank input lines' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
Readline.should_receive(:readline).with("irb():001:0> ",true).and_return("")
Readline::HISTORY.should_receive(:pop)
exit_console "irb():001:0> "
Expand All @@ -158,7 +158,7 @@

it 'should not keep identical commands in history' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
Readline::HISTORY.should_receive(:to_a).and_return(["puts 'hi'","puts 'hi'"])
Readline::HISTORY.should_receive(:to_a).and_return(["puts 'hi'"])
Readline.should_receive(:readline).with("irb():001:0> ",true).and_return("puts 'hi'")
Expand All @@ -170,7 +170,7 @@

it 'should return remote tab completion data' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Switch to inspect mode\nirb():001:0> ")
@telnet_client.should_receive(:cmd).with({"String"=>"app.\t", "Match"=>/\S*\n$/, "Timeout"=>10}).and_return("to_s,nil?\n")
exit_console "irb():001:0> "
start_local_console(3344,'foo')
Expand All @@ -179,7 +179,7 @@

it 'should return remote tab completion data on receipt of empty completion string' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
@telnet_client.should_receive(:cmd).with({"String"=>"app.\t", "Match"=>/\S*\n$/, "Timeout"=>10}).and_return("\n")
exit_console "irb():001:0> "
start_local_console(3344,'foo')
Expand All @@ -188,7 +188,7 @@

it 'should not crash on timeout of remote tab completion data' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("Switch to inspect mode\nirb():001:0> ")
@telnet_client.should_receive(:cmd).with({"String"=>"app.\t", "Match"=>/\S*\n$/, "Timeout"=>10}).and_raise(TimeoutError)
exit_console "irb():001:0> "
start_local_console(3344,'foo')
Expand All @@ -197,7 +197,7 @@

it 'should properly initialize Readline for tab completion' do
@client.should_receive(:app_files).with("foo", '/app/cf-rails-console/.consoleaccess', '0').and_return(IO.read(spec_asset('console_access.txt')))
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_yield("irb():001:0> ")
@telnet_client.should_receive(:login).with({"Name"=>"cfuser", "Password"=>"testpw"}).and_return("irb():001:0> ")
Readline.should_receive(:respond_to?).with("basic_word_break_characters=").and_return(true)
Readline.should_receive(:basic_word_break_characters=).with(" \t\n`><=;|&{(")
Readline.should_receive(:completion_append_character=).with(nil)
Expand Down

0 comments on commit f78d029

Please sign in to comment.