Mailinglist Archive: yast-commit (883 mails)

< Previous Next >
[yast-commit] <rest-service> master : Cleanup for better testability
  • From: Klaus Kämpf <kkaempf@xxxxxxx>
  • Date: Mon, 6 Jul 2009 16:33:40 +0200
  • Message-id: <E1MNpGf-0007ds-Qj@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit 2551238b0ea7c9cbbac3c0c79b6bf5415a5b8f68
Author: Klaus Kämpf <kkaempf@xxxxxxx>
Date: Mon Jul 6 16:33:40 2009 +0200

Cleanup for better testability

- use 'Rails.logger' instead of 'logger'
---
webservice/lib/yast_roles.rb | 38 ++++++++++++-----------------
webservice/test/unit/yast_roles_test.rb | 40 +++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/webservice/lib/yast_roles.rb b/webservice/lib/yast_roles.rb
index 5f0f201..7cf1cf8 100644
--- a/webservice/lib/yast_roles.rb
+++ b/webservice/lib/yast_roles.rb
@@ -5,16 +5,14 @@ module YastRoles

private
def user_roles(user)
- return session['services'] if session['user_roles']

- return [] unless File.exists?(USER_ROLES_CONFIG)
-
IO.foreach( USER_ROLES_CONFIG ) do |line|
line = line.chomp
next if line[0] == "#"
roles = line.split(/[\s,]+/)
return roles if ( roles.size > 1 and roles[0] == user )
- end
+ end if File.exists?(USER_ROLES_CONFIG)
+
return []
end

@@ -24,28 +22,24 @@ public
return true if ENV["RAILS_ENV"] == "test"
return false if self.current_account==nil ||
self.current_account.login.size == 0

- begin
- if polkit_check( action, self.current_account.login) == :yes
- logger.debug "Action: #{action} User: #{self.current_account.login}
Result: ok"
- return true
- end
- rescue #caused by poolkit
- logger.error "ruby-polkit has returned an exception"
+ if polkit_check( action, self.current_account.login) == :yes
+ Rails.logger.debug "Action: #{action} User:
#{self.current_account.login} Result: ok"
+ return true
end
#checking roles
- roles = user_roles(self.current_account.login)
+ roles = session['user_roles'] ? session['services'] :
user_roles(self.current_account.login)
roles.each do |role|
- begin
- if ( role != self.current_account.login and
- polkit_check( action, role) == :yes)
- logger.debug "Action: #{action} User:
#{self.current_account.login} WITH role #{role} Result: ok"
- return true
- end
- rescue #caused by poolkit
- logger.error "ruby-polkit has returned an exception"
- end
+ if ( role != self.current_account.login and
+ polkit_check( action, role) == :yes)
+ Rails.logger.debug "Action: #{action} User:
#{self.current_account.login} WITH role #{role} Result: ok"
+ return true
+ end
end
- logger.debug "Action: #{action} User: #{self.current_account.login}
Result: NOT granted"
+ Rails.logger.debug "Action: #{action} User: #{self.current_account.login}
Result: NOT granted"
+ return false
+ rescue Exception => e
+ Rails.logger.error "permission_check() exception: #{$!}"
+ Rails.logger.debug $@.join("\n")
return false
end
end
diff --git a/webservice/test/unit/yast_roles_test.rb
b/webservice/test/unit/yast_roles_test.rb
new file mode 100644
index 0000000..4932b18
--- /dev/null
+++ b/webservice/test/unit/yast_roles_test.rb
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class CurrentLogin
+ attr_reader :login
+
+ def initialize login
+ @login = login
+ end
+end
+
+class YastRolesTest < ActiveSupport::TestCase
+ include YastRoles
+
+ session = Hash.new
+
+ attr_reader :current_account
+
+ def setup
+ ENV["RAILS_ENV"] = ""
+ @current_account = CurrentLogin.new "test"
+ end
+
+ def test_permission_check_trivial
+ ENV["RAILS_ENV"] = "test"
+ assert permission_check(nil)
+ end
+
+ def test_permission_check_no_account
+ @current_account = nil
+ assert !permission_check(nil)
+ end
+
+ def test_action_nil
+ assert !permission_check(nil)
+ end
+
+ def test_action_dummy
+ assert !permission_check("dummy")
+ 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