ref: refs/heads/master
commit d7162862eda9ddeda8963cabdb0be58fea9fd106
Author: Stefan Schubert
Date: Thu Aug 13 17:22:17 2009 +0200
code cleanup; added testcases
---
.../app/controllers/permissions_controller.rb | 25 ++--
.../test/functional/permission_controller_test.rb | 161 ++++++++++++++++++++
plugins/permissions/test/permissions_test.rb | 8 -
plugins/permissions/test/test_helper.rb | 55 +++++++-
4 files changed, 229 insertions(+), 20 deletions(-)
diff --git a/plugins/permissions/app/controllers/permissions_controller.rb b/plugins/permissions/app/controllers/permissions_controller.rb
index cee9a5e..48887d6 100644
--- a/plugins/permissions/app/controllers/permissions_controller.rb
+++ b/plugins/permissions/app/controllers/permissions_controller.rb
@@ -8,14 +8,14 @@ class PermissionsController < ApplicationController
def client_permissions
login = YaST::ServiceResource::Session.login
perm_resource = OpenStruct.new(:href => '/permissions', :singular => false, :interface => 'org.opensuse.yast.webservice.permissions')
- @proxy = YaST::ServiceResource.class_for_resource(perm_resource)
- unless @proxy
+ proxy = YaST::ServiceResource.class_for_resource(perm_resource)
+ unless proxy
flash[:notice] = _("Invalid session, please login again.")
- redirect_to( logout_path ) and return
+ redirect_to( logout_path ) and return nil
end
@right_set_permissions = false
@right_get_permissions = false
- permissions = @proxy.find(:all, :params => { :user_id => login, :filter => 'org.opensuse.yast.webservice' })
+ permissions = proxy.find(:all, :params => { :user_id => login, :filter => 'org.opensuse.yast.webservice' })
permissions.each do |permission|
if permission.name == "org.opensuse.yast.webservice.write-permissions" &&
permission.grant
@@ -26,6 +26,7 @@ class PermissionsController < ApplicationController
@right_get_permissions = true
end
end
+ return proxy
end
# Initialize GetText and Content-Type.
@@ -117,11 +118,11 @@ class PermissionsController < ApplicationController
return data_list
end
- def get_permissions(user, get_perm_from_server)
+ def get_permissions(user, get_perm_from_server, proxy)
@current_user = nil
if get_perm_from_server
begin
- @permissions = @proxy.find(:all, :params => { :user_id => user })
+ @permissions = proxy.find(:all, :params => { :user_id => user })
rescue ActiveResource::ClientError => e
return YaST::ServiceResource.error(e)
rescue Exception => e
@@ -146,8 +147,9 @@ class PermissionsController < ApplicationController
def set
- return unless client_permissions
- error = get_permissions(params[:user].rstrip, true)
+ proxy = client_permissions
+ return unless proxy
+ error = get_permissions(params[:user].rstrip, true, proxy)
if !error.blank?
flash[:error] = error
render :action => "index" and return
@@ -174,14 +176,15 @@ class PermissionsController < ApplicationController
break unless response
end
flash[:notice] = _("Permissions have been set.") if response
- error = get_permissions(params[:user].rstrip, true)
+ error = get_permissions(params[:user].rstrip, true, proxy)
flash[:error] = error if !error.blank?
render :action => "index"
end
def search
- return unless client_permissions
- flash[:error] = get_permissions(params[:user].rstrip, true)
+ proxy = client_permissions
+ return unless proxy
+ flash[:error] = get_permissions(params[:user].rstrip, true, proxy)
render :action => "index"
end
diff --git a/plugins/permissions/test/functional/permission_controller_test.rb b/plugins/permissions/test/functional/permission_controller_test.rb
new file mode 100644
index 0000000..8de37a5
--- /dev/null
+++ b/plugins/permissions/test/functional/permission_controller_test.rb
@@ -0,0 +1,161 @@
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
+require 'test/unit'
+require File.expand_path( File.join("test","validation_assert"), RailsParent.parent )
+require 'rubygems'
+require 'mocha'
+
+
+
+class PermissionsControllerTest < ActionController::TestCase
+ # fixtures :accounts
+
+ class Proxy
+ attr_accessor :result, :permissions, :timeout
+ def find dummy1, dummy2
+ return result.permissions
+ end
+ end
+
+ class Permission
+ attr_accessor :name, :grant
+ def initialize (name, grant)
+ @grant = grant
+ @name = name
+ end
+ end
+
+ class Result
+ attr_accessor :permissions
+
+ def fill
+ @permissions = [Permission.new("org.opensuse.yast.webservice.read-permissions", true),
+ Permission.new("org.opensuse.yast.webservice.write-permissions", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.setcurrentlanguage", true),
+ Permission.new("org.opensuse.yast.system.network.read", true),
+ Permission.new("org.opensuse.yast.language.read-firstlanguage", true),
+ Permission.new("org.opensuse.yast.scr.error", false),
+ Permission.new("org.opensuse.yast.services.execute-commands-gpm", true),
+ Permission.new("org.opensuse.yast.scr.registeragent", false),
+ Permission.new("org.opensuse.yast.language.read", true),
+ Permission.new("org.opensuse.yast.systemtime.write-timezone", true),
+ Permission.new("org.opensuse.yast.services.read-config-ntp-manualserver", true),
+ Permission.new("org.opensuse.yast.system.services.execute", true),
+ Permission.new("org.opensuse.yast.system.users.write", true),
+ Permission.new("org.opensuse.yast.services.write-config", true),
+ Permission.new("org.opensuse.yast.system.users.delete", true),
+ Permission.new("org.opensuse.yast.system.patches.read", true),
+ Permission.new("org.opensuse.yast.system.time.read", true),
+ Permission.new("org.opensuse.yast.language.read-secondlanguages", true),
+ Permission.new("org.opensuse.yast.services.read", true),
+ Permission.new("org.opensuse.yast.system.users.read", true),
+ Permission.new("org.opensuse.yast.system.patches.install", true),
+ Permission.new("org.opensuse.yast.permissions.write", true),
+ Permission.new("org.opensuse.yast.language.write", true),
+ Permission.new("org.opensuse.yast.modules.yapi.users.useradd", true),
+ Permission.new("org.opensuse.yast.scr.unregisteragent", false),
+ Permission.new("org.opensuse.yast.services.read-config-ntp-enabled", true),
+ Permission.new("org.opensuse.yast.scr.execute", false),
+ Permission.new("org.opensuse.yast.modules.yapi.language.setutf8", true),
+ Permission.new("org.opensuse.yast.scr.unregisterallagents", false),
+ Permission.new("org.opensuse.yast.services.write-config-ntp", true),
+ Permission.new("org.opensuse.yast.module-manager.import", true),
+ Permission.new("org.opensuse.yast.modules.yapi.time.write", true),
+ Permission.new("org.opensuse.yast.services.execute-commands", true),
+ Permission.new("org.opensuse.yast.scr.dir", false),
+ Permission.new("org.opensuse.yast.modules.yapi.language.getlanguages", true),
+ Permission.new("org.opensuse.yast.systemtime.write", true),
+ Permission.new("org.opensuse.yast.system.status.writelimits", true),
+ Permission.new("org.opensuse.yast.system.time.write", true),
+ Permission.new("org.opensuse.yast.language.read-available", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-random", true),
+ Permission.new("org.opensuse.yast.modules.yapi.users.usersget", true),
+ Permission.new("org.opensuse.yast.system.security.write", true),
+ Permission.new("org.opensuse.yast.permissions.read", true),
+ Permission.new("org.opensuse.yast.system.services.read", true),
+ Permission.new("org.opensuse.yast.services.execute", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-ntp", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-smbfs", true),
+ Permission.new("org.opensuse.yast.services.write-config-ntp-enabled", true),
+ Permission.new("org.opensuse.yast.scr.read", false),
+ Permission.new("org.opensuse.yast.modules.yapi.language.getcurrentlanguage", true),
+ Permission.new("org.opensuse.yast.systemtime.read", true),
+ Permission.new("org.opensuse.yast.modules.yapi.time.read", true),
+ Permission.new("org.opensuse.yast.scr.write", false),
+ Permission.new("org.opensuse.yast.patch.install", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.isutf8", true),
+ Permission.new("org.opensuse.yast.system.services.write", true),
+ Permission.new("org.opensuse.yast.services.write-config-ntp-manualserver", true),
+ Permission.new("org.opensuse.yast.system.language.read", true),
+ Permission.new("org.opensuse.yast.services.write", true),
+ Permission.new("org.opensuse.yast.scr.registernewagents", false),
+ Permission.new("org.opensuse.yast.language.write-secondlanguages", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.write", true),
+ Permission.new("org.opensuse.yast.system.status.read", true),
+ Permission.new("org.opensuse.yast.system.users.new", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-sshd", true),
+ Permission.new("org.opensuse.yast.systemtime.read-validtimezones", true),
+ Permission.new("org.opensuse.yast.systemtime.read-isutc", true),
+ Permission.new("org.opensuse.yast.scr.unmountagent", false),
+ Permission.new("org.opensuse.yast.language.write-firstlanguage", true),
+ Permission.new("org.opensuse.yast.module-manager.lock", true),
+ Permission.new("org.opensuse.yast.modules.yapi.users.userdelete", true),
+ Permission.new("org.opensuse.yast.services.read-config-ntp-userandomserverw", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.setrootlang", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.read", true),
+ Permission.new("org.opensuse.yast.system.network.writelimits", true),
+ Permission.new("org.opensuse.yast.systemtime.write-isutc", true),
+ Permission.new("org.opensuse.yast.systemtime.write-currenttime", true),
+ Permission.new("org.opensuse.yast.modules.yapi.users.userget", true),
+ Permission.new("org.opensuse.yast.patch.read", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-cups", true),
+ Permission.new("org.opensuse.yast.system.security.read", true),
+ Permission.new("org.opensuse.yast.systemtime.read-currenttime", true),
+ Permission.new("org.opensuse.yast.system.language.write", true),
+ Permission.new("org.opensuse.yast.modules.yapi.language.getrootlang", true),
+ Permission.new("org.opensuse.yast.systemtime.read-timezone", true),
+ Permission.new("org.opensuse.yast.services.execute-commands-cron", true),
+ Permission.new("org.opensuse.yast.services.read-config-ntp", true),
+ Permission.new("org.opensuse.yast.services.write-config-ntp-userandomserverw", true),
+ Permission.new("org.opensuse.yast.services.read-config", true),
+ Permission.new("org.opensuse.yast.modules.yapi.users.usermodify", true)
+
+ ];
+ end
+
+ def save
+ @saved = true
+ end
+ end
+
+ def setup
+ PermissionsController.any_instance.stubs(:login_required)
+ @controller = PermissionsController.new
+ @request = ActionController::TestRequest.new
+ # http://railsforum.com/viewtopic.php?id=1719
+ @request.session[:account_id] = 1 # defined in fixtures
+ @right_set_permissions = true
+ @right_get_permissions = true
+ @result = Result.new
+ @result.fill
+ @proxy = Proxy.new
+ @proxy.permissions = @permissions
+ @proxy.result = @result
+ PermissionsController.any_instance.stubs(:client_permissions).with().returns(@proxy)
+ end
+
+ def test_access_index
+ get :index
+
+ #check if everything is correctly setted
+ assert_response :success
+ end
+
+ def test_access_search
+ get :search, {:user =>"test" }
+
+ #check if everything is correctly setted
+ assert_response :success
+ end
+
+
+end
\ No newline at end of file
diff --git a/plugins/permissions/test/permissions_test.rb b/plugins/permissions/test/permissions_test.rb
deleted file mode 100644
index 08fcc0b..0000000
--- a/plugins/permissions/test/permissions_test.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-require 'test_helper'
-
-class PermissionTest < ActiveSupport::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
- end
-end
diff --git a/plugins/permissions/test/test_helper.rb b/plugins/permissions/test/test_helper.rb
index cf148b8..ca844a2 100644
--- a/plugins/permissions/test/test_helper.rb
+++ b/plugins/permissions/test/test_helper.rb
@@ -1,3 +1,56 @@
+# find the rails parent
+require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
+# first config rails
+require File.expand_path( File.join("config","environment"), RailsParent.parent )
+# then enable testing, this will get the routing right
+ENV["RAILS_ENV"] = "test"
+require 'test_help'
+
require 'rubygems'
require 'active_support'
-require 'active_support/test_case'
\ No newline at end of file
+require 'active_support/test_case'
+
+class ActiveSupport::TestCase
+ # Transactional fixtures accelerate your tests by wrapping each test method
+ # in a transaction that's rolled back on completion. This ensures that the
+ # test database remains unchanged so your fixtures don't have to be reloaded
+ # between every test method. Fewer database queries means faster tests.
+ #
+ # Read Mike Clark's excellent walkthrough at
+ # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
+ #
+ # Every Active Record database supports transactions except MyISAM tables
+ # in MySQL. Turn off transactional fixtures in this case; however, if you
+ # don't care one way or the other, switching from MyISAM to InnoDB tables
+ # is recommended.
+ #
+ # The only drawback to using transactional fixtures is when you actually
+ # need to test transactions. Since your test is bracketed by a transaction,
+ # any transactions started in your code will be automatically rolled back.
+ self.use_transactional_fixtures = true
+
+ # Instantiated fixtures are slow, but give you @david where otherwise you
+ # would need people(:david). If you don't want to migrate your existing
+ # test cases which use the @david style and don't mind the speed hit (each
+ # instantiated fixtures translates to a database query per test method),
+ # then set this back to true.
+ self.use_instantiated_fixtures = false
+
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
+ #
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
+ # -- they do not yet inherit this setting
+ fixtures :all
+
+ # Add more helper methods to be used by all tests here...
+
+ # See http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/
+ def clean_backtrace(&block)
+ yield
+ rescue ActiveSupport::TestCase::Assertion => error
+ framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions"))
+ error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path }
+ raise
+ end
+
+end
\ No newline at end of file
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org