Mailinglist Archive: yast-commit (883 mails)

< Previous Next >
[yast-commit] <rest-service> master : Cleanup sessions_controller and bring test coverage to 100%
  • From: Klaus Kämpf <kkaempf@xxxxxxx>
  • Date: Tue, 7 Jul 2009 15:33:15 +0200
  • Message-id: <E1MOAnQ-00027R-2N@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit cb6d15249fda2df8cfaa882ff2cf8a141d4df4a5
Author: Klaus Kämpf <kkaempf@xxxxxxx>
Date: Tue Jul 7 15:33:15 2009 +0200

Cleanup sessions_controller and bring test coverage to 100%
---
webservice/app/controllers/sessions_controller.rb | 56 +++--------------
webservice/app/views/sessions/create.html.erb | 2 +
webservice/app/views/sessions/create.json.erb | 1 +
webservice/app/views/sessions/create.xml.erb | 1 +
webservice/app/views/sessions/destroy.html.erb | 2 +
webservice/app/views/sessions/destroy.json.erb | 1 +
webservice/app/views/sessions/destroy.xml.erb | 1 +
webservice/app/views/sessions/show.html.erb | 2 +
webservice/app/views/sessions/show.json.erb | 1 +
webservice/app/views/sessions/show.xml.erb | 1 +
.../test/functional/sessions_controller_test.rb | 66 ++++++++++++++++++++
11 files changed, 87 insertions(+), 47 deletions(-)

diff --git a/webservice/app/controllers/sessions_controller.rb
b/webservice/app/controllers/sessions_controller.rb
index 3b33c99..44b63b0 100644
--- a/webservice/app/controllers/sessions_controller.rb
+++ b/webservice/app/controllers/sessions_controller.rb
@@ -18,18 +18,13 @@ class SessionsController < ApplicationController
end

def show
- ret = Hash.new
- ret[:hash] = Hash.new
- ret[:hash][:login] = 'foo'
- respond_to do |format|
- format.xml { render :xml => ret.to_xml, :location => "none" }
- format.json { render :json => ret.to_json, :location => "none" }
- format.html { render :html => ret.to_xml, :location => "none" }
- end
+ @ret = Hash.new
+ @ret[:hash] = Hash.new
+ @ret[:hash][:login] = 'nobody'
end

def create
- if params["hash"] != nil
+ if params["hash"].is_a? Hash
#checking if the session description is hosted in a own Hash
params["hash"].each do |name,value|
params[name] = value
@@ -38,40 +33,18 @@ class SessionsController < ApplicationController
if params.has_key?(:login) && params[:password]
self.current_account = Account.authenticate(params[:login],
params[:password])
end
+ @cmd_ret = Hash.new
if logged_in?
- if params.has_key?(:remember_me) && params[:remember_me] == true
+ auth_token = { :value => self.current_account.remember_token , :expires
=> self.current_account.remember_token_expires_at }
+ if params[:remember_me]
current_account.remember_me unless current_account.remember_token?
- cookies[:auth_token] = { :value => self.current_account.remember_token
, :expires => self.current_account.remember_token_expires_at }
+ cookies[:auth_token] = auth_token
end

- @cmd_ret = Hash.new
@cmd_ret["login"] = "granted"
- @cmd_ret["auth_token"] = { :value => self.current_account.remember_token
, :expires => self.current_account.remember_token_expires_at }
- respond_to do |format|
- format.xml do
- render :xml => @cmd_ret.to_xml, :location => "none"
- end
- format.json do
- render :json => @cmd_ret.to_json, :location => "none"
- end
- format.html do
- render :xml => @cmd_ret.to_xml, :location => "none"
- end
- end
+ @cmd_ret["auth_token"] = auth_token
else
- @cmd_ret = Hash.new
@cmd_ret["login"] = "denied"
- respond_to do |format|
- format.xml do
- render :xml => @cmd_ret.to_xml, :location => "none"
- end
- format.json do
- render :json => @cmd_ret.to_json, :location => "none"
- end
- format.html do
- render :xml => @cmd_ret.to_xml, :location => "none" #only XML will be
returned
- end
- end
end
end

@@ -81,16 +54,5 @@ class SessionsController < ApplicationController
reset_session
@cmd_ret = Hash.new
@cmd_ret["logout"] = "Goodbye!"
- respond_to do |format|
- format.xml do
- render :xml => @cmd_ret.to_xml, :location => "none"
- end
- format.json do
- render :json => @cmd_ret.to_json, :location => "none"
- end
- format.html do
- render :xml => @cmd_ret.to_xml, :location => "none" #only XML will be
returned
- end
- end
end
end
diff --git a/webservice/app/views/sessions/create.html.erb
b/webservice/app/views/sessions/create.html.erb
new file mode 100644
index 0000000..4a87287
--- /dev/null
+++ b/webservice/app/views/sessions/create.html.erb
@@ -0,0 +1,2 @@
+<!-- html renders as xml -->
+<%= @cmd_ret.to_xml %>
diff --git a/webservice/app/views/sessions/create.json.erb
b/webservice/app/views/sessions/create.json.erb
new file mode 100644
index 0000000..75c495d
--- /dev/null
+++ b/webservice/app/views/sessions/create.json.erb
@@ -0,0 +1 @@
+<%= @cmd_ret.to_json %>
diff --git a/webservice/app/views/sessions/create.xml.erb
b/webservice/app/views/sessions/create.xml.erb
new file mode 100644
index 0000000..51ad339
--- /dev/null
+++ b/webservice/app/views/sessions/create.xml.erb
@@ -0,0 +1 @@
+<%= @cmd_ret.to_xml %>
diff --git a/webservice/app/views/sessions/destroy.html.erb
b/webservice/app/views/sessions/destroy.html.erb
new file mode 100644
index 0000000..4a87287
--- /dev/null
+++ b/webservice/app/views/sessions/destroy.html.erb
@@ -0,0 +1,2 @@
+<!-- html renders as xml -->
+<%= @cmd_ret.to_xml %>
diff --git a/webservice/app/views/sessions/destroy.json.erb
b/webservice/app/views/sessions/destroy.json.erb
new file mode 100644
index 0000000..75c495d
--- /dev/null
+++ b/webservice/app/views/sessions/destroy.json.erb
@@ -0,0 +1 @@
+<%= @cmd_ret.to_json %>
diff --git a/webservice/app/views/sessions/destroy.xml.erb
b/webservice/app/views/sessions/destroy.xml.erb
new file mode 100644
index 0000000..51ad339
--- /dev/null
+++ b/webservice/app/views/sessions/destroy.xml.erb
@@ -0,0 +1 @@
+<%= @cmd_ret.to_xml %>
diff --git a/webservice/app/views/sessions/show.html.erb
b/webservice/app/views/sessions/show.html.erb
new file mode 100644
index 0000000..48acaec
--- /dev/null
+++ b/webservice/app/views/sessions/show.html.erb
@@ -0,0 +1,2 @@
+<!-- html renders as xml -->
+<%= @ret.to_xml %>
diff --git a/webservice/app/views/sessions/show.json.erb
b/webservice/app/views/sessions/show.json.erb
new file mode 100644
index 0000000..a16ca56
--- /dev/null
+++ b/webservice/app/views/sessions/show.json.erb
@@ -0,0 +1 @@
+<%= @ret.to_json %>
diff --git a/webservice/app/views/sessions/show.xml.erb
b/webservice/app/views/sessions/show.xml.erb
new file mode 100644
index 0000000..faf2405
--- /dev/null
+++ b/webservice/app/views/sessions/show.xml.erb
@@ -0,0 +1 @@
+<%= @ret.to_xml %>
diff --git a/webservice/test/functional/sessions_controller_test.rb
b/webservice/test/functional/sessions_controller_test.rb
new file mode 100644
index 0000000..1e3bedb
--- /dev/null
+++ b/webservice/test/functional/sessions_controller_test.rb
@@ -0,0 +1,66 @@
+#
+# test/functional/sessions_controller_test.rb
+#
+#
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
+
+class SessionsControllerTest < ActionController::TestCase
+ fixtures :accounts
+
+ def setup
+ # Fake an active session
+ # http://railsforum.com/viewtopic.php?id=1719
+# @request.session[:account_id] = 1 # defined in fixtures
+ end
+
+ test "sessions new" do
+ get :new
+ assert_response 302 # redirect
+ end
+
+ test "sessions show" do
+ get :show
+ assert_response :success
+ end
+
+ test "sessions create" do
+ get :create
+ assert_response :success
+ end
+
+ test "sessions create with hash" do
+ get :create, :hash => { "foo" => "bar" }
+ assert_response :success
+ end
+
+ test "sessions create with login and password" do
+ get :create, :hash => { :login => "test_user", :password =>
"test_password" }
+ assert_response :success
+ end
+
+ test "sessions create remember_me" do
+ @request.session[:account_id] = 1 # defined in fixtures
+ get :create, :remember_me => true
+# FIXME assert cookies[:auth_token]
+ assert_response :success
+ end
+
+ test "sessions destroy" do
+ get :destroy
+ assert_response :success
+ end
+
+ test "output xml format" do
+ get :show, :format => "xml"
+ assert_response :success
+ assert @response.headers['Content-Type'] =~ %r{application/xml}
+ end
+
+ test "output html format" do
+ get :show, :format => "html"
+ assert_response :success
+ assert @response.headers['Content-Type'] =~ %r{text/html}
+ end
+
+
+end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages