ref: refs/heads/master
commit 2a09409c06af62f3f00702c8b56c48f28ad6edff
Author: Klaus Kämpf
Date: Fri Jul 10 15:10:35 2009 +0200
Harden session controller test
---
webclient/app/controllers/sessions_controller.rb | 59 ++++++++----------
webclient/app/views/sessions/new.rhtml | 4 +-
.../test/functional/sessions_controller_test.rb | 63 +++++++++++++++-----
3 files changed, 77 insertions(+), 49 deletions(-)
diff --git a/webclient/app/controllers/sessions_controller.rb b/webclient/app/controllers/sessions_controller.rb
index 2ca0bc4..8e95708 100644
--- a/webclient/app/controllers/sessions_controller.rb
+++ b/webclient/app/controllers/sessions_controller.rb
@@ -52,32 +52,31 @@ class SessionsController < ApplicationController
if params[:hostname].blank?
flash[:warning] = _("You need to specify the hostname")
redirect_to :action => "new"
- return
elsif params[:password].blank?
flash[:warning] = _("No password specified")
redirect_to :action => "new", :hostname => params[:hostname]
- return
else
# otherwise, we have all the data, try to login
begin
self.current_account, auth_token = Account.authenticate(params[:login],
params[:password],
params[:hostname])
- # error handling when loggin in to the service is pretty
- # important to get meanful error messages to the user
+ # error handling when login to the service is pretty
+ # important to get meaningful error messages to the user
rescue Errno::ECONNREFUSED => e
- flash[:warning] = _("Can't connect to host at #{params[:hostname]}, make sure the host is up and that the YaST web service is running.")
- #redirect_to :action => :login, :hostname => params[:hostname]
- redirect_to new_session_path(:hostname => params[:hostname])
+ flash[:error] = _("Can't connect to host at #{params[:hostname]}, make sure the host is up and that the YaST web service is running.")
+ redirect_to :action => "new"
return
rescue Exception => e
logger.warn e.to_s
logger.info e.backtrace.join("\n")
- flash[:warning] = _("Error when trying to login: #{e.to_s}")
- redirect_to new_session_path(:hostname => params[:hostname])
+ flash[:error] = _("Error when trying to login: #{e.to_s}")
+ redirect_to :action => "new", :hostname => params[:hostname]
#redirect_to :action => :login, :hostname => params[:hostname]
return
end
+
+ # Now check if the authentication was successful
if logged_in?
session[:auth_token] = auth_token
@@ -92,42 +91,38 @@ class SessionsController < ApplicationController
@short_host_name = @short_host_name[@short_host_name.index("://")+3, @short_host_name.length-1] #extract "http(s)://"
end
- # success, go to the main menu
+ # success, go to the main controller
logger.info "Login success."
redirect_to "/"
- return
else
- session[:user] = nil
- session[:host] = nil
+ session[:user] = session[:host] = nil
#show # getting hosts again
flash[:warning] = _("Login incorrect. Check your username and password.")
- redirect_to new_session_path(:hostname => params[:hostname])
- return
+ redirect_to :action => "new", :hostname => params[:hostname]
end
end
end
-
+
def destroy
# remove session data
[:user, :host].each do |k|
session[k] = nil
end
- if logged_in?
- ret = YaST::ServiceResource::Logout.create rescue nil
- if (ret and ret.attributes["logout"])
- logger.debug "Logout: #{ret.attributes["logout"]}"
- else
- logger.debug "Logout: Error"
- end
- self.current_account.forget_me
- session[:auth_token] = nil
- end
-
- cookies.delete :auth_token
- reset_session
- flash[:notice] = _("You have been logged out.") unless flash[:notice]
- redirect_to new_session_path
- return
+ if logged_in?
+ ret = YaST::ServiceResource::Logout.create rescue nil
+ if (ret and ret.attributes["logout"])
+ logger.debug "Logout: #{ret.attributes["logout"]}"
+ else
+ logger.debug "Logout: Error"
+ end
+ self.current_account.forget_me
+ session[:auth_token] = nil
+ end
+
+ cookies.delete :auth_token
+ reset_session
+ flash[:notice] = _("You have been logged out.") unless flash[:notice]
+ redirect_to new_session_path
end
end
diff --git a/webclient/app/views/sessions/new.rhtml b/webclient/app/views/sessions/new.rhtml
index 4db83f5..97ccb90 100644
--- a/webclient/app/views/sessions/new.rhtml
+++ b/webclient/app/views/sessions/new.rhtml
@@ -1,7 +1,7 @@
<div class="content">
<% form_tag( :action => 'create' ) do %>
- <span id="title">Login</span>
+ <span id="content_title">Login</span>
<p><%= _("Enter login credentials for host #{@hostname}.") %></p>
@@ -13,7 +13,7 @@
<p><label for="password"><%= _("Password:") %></label><br>
<%= password_field_tag 'password', @password %></p>
- <%= submit_tag(_('Login'), :class => "button") %>
+ <%= submit_tag(_('Login'), :class => "button", :id => "login_button") %>
<% end %>
</div>
diff --git a/webclient/test/functional/sessions_controller_test.rb b/webclient/test/functional/sessions_controller_test.rb
index ae7c768..2620aea 100644
--- a/webclient/test/functional/sessions_controller_test.rb
+++ b/webclient/test/functional/sessions_controller_test.rb
@@ -1,5 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'
+require 'mocha'
+
require 'active_resource/http_mock'
# Re-raise errors caught by the controller.
@@ -23,6 +25,12 @@ class SessionsControllerTest < ActionController::TestCase
@logout_granted = "<hash><logout>Goodbye!</logout></hash>"
@hostname = "http://localhost:8000"
+ current_account = Account.new
+ auth_token = "abcdef"
+ Account.stubs(:authenticate).with("quentin","test",@hostname).returns([current_account, auth_token])
+ Account.stubs(:authenticate).with("quentin","bad password",@hostname).returns([nil,nil])
+ Account.stubs(:authenticate).with("quentin","exception","exception").raises(RuntimeError)
+ Account.stubs(:authenticate).with("quentin","bad host","bad").raises(Errno::ECONNREFUSED)
YaST::ServiceResource::Session.site = @hostname
ActiveResource::Base.site = @hostname
end
@@ -43,35 +51,60 @@ class SessionsControllerTest < ActionController::TestCase
# new with hostname, must show login
def test_new_shows_login
get :new, :hostname => @hostname
- assert_select "form input", 2
- assert_select "title", "Login"
+ assert_select "form input", 4 # hostname, username, password, submit
end
# without a service hostname to to login, we should
# be redirected to web service choosing...
- def test_should_redirect_to_select_hostname
- get :new, :login => 'quentin', :password => 'test'
- assert_redirected_to :controller => :webservices
+ def test_create_should_redirect_to_select_hostname
+ post :create
+ assert flash[:warning]
+ assert_redirected_to :action => :new
+ end
+
+ # create with blank password
+ def test_create_with_blank_password
+ post :create, :password => "", :hostname => @hostname
+ assert flash[:warning]
+ assert_redirected_to :action => :new, :hostname => @hostname
end
- def test_should_login_and_redirect
+ def test_create_successful_login
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/login.xml", {}, @login_granted
end
post :create, :hostname => @hostname,
:login => 'quentin', :password => 'test'
- # success ful login sends you to control panel
- # FIXME however login is not working in tests
- # assert_redirected_to :controller => :controlpanel
+ assert_nil flash[:warning]
+ assert_nil flash[:error]
+ assert session[:auth_token]
+ assert session[:user]
+ assert session[:host]
+ assert_redirected_to "/"
end
- def test_should_fail_login_and_send_to_new
- ActiveResource::HttpMock.respond_to do |mock|
- mock.post "/login.xml", {}, @login_denied
- end
-
- post :create, :login => 'quentin', :password => 'bad password'
+ def test_create_with_authentication_failure
+ post :create, :login => 'quentin', :password => 'bad password', :hostname => @hostname
assert_nil session[:account_id]
+ assert flash[:warning]
+ assert_nil flash[:error]
+ # we should be at the login form again
+ assert_redirected_to :controller => :sessions, :action => :new, :hostname => @hostname
+ end
+
+ def test_create_with_connection_refused
+ post :create, :login => 'quentin', :password => 'bad host', :hostname => "bad"
+ assert_nil session[:account_id]
+ assert_nil flash[:warning]
+ assert flash[:error]
+ # we should be at the login form again
+ assert_redirected_to :controller => :sessions, :action => :new
+ end
+
+ def test_create_with_exception_raised
+ post :create, :login => 'quentin', :password => 'exception', :hostname => "exception"
+ assert_nil flash[:warning]
+ assert flash[:error]
# we should be at the login form again
assert_redirected_to :controller => :sessions, :action => :new
end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org