merged cont.
[opensuse:yast-rest-service.git] / webyast / test / unit / permission_test.rb
1 #--
2 # Webyast Webservice framework
3 #
4 # Copyright (C) 2009, 2010 Novell, Inc. 
5 #   This library is free software; you can redistribute it and/or modify
6 # it only under the terms of version 2.1 of the GNU Lesser General Public
7 # License as published by the Free Software Foundation. 
8 #
9 #   This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
12 # details. 
13 #
14 #   You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software 
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #++
18
19 require File.dirname(__FILE__) + '/../test_helper'
20 require 'polkit'
21
22 # Test Permission class
23
24 class PermissionTest < ActiveSupport::TestCase
25 TEST_DATA_ACTIONS = <<EOF
26 org.opensuse.yast.modules.ysr.statelessregister
27 org.opensuse.yast.modules.ysr.getregistrationconfig
28 org.opensuse.yast.modules.ysr.setregistrationconfig
29 org.freedesktop.network-manager-settings.system.modify
30 org.opensuse.yast.module-manager.import
31 org.opensuse.yast.module-manager.lock
32 org.opensuse.yast.scr.read
33 org.opensuse.yast.modules.yapi.users.usersget
34 org.opensuse.yast.modules.yapi.users.userget
35 org.opensuse.yast.modules.yapi.users.usermodify
36 org.opensuse.yast.modules.yapi.users.useradd
37 org.opensuse.yast.modules.yapi.users.userdelete
38 org.opensuse.yast.permissions.read
39 org.opensuse.yast.permissions.write
40 EOF
41
42 TEST_DATA_GRANT = [
43 "org.opensuse.yast.modules.ysr.statelessregister",
44 "org.opensuse.yast.modules.ysr.getregistrationconfig",
45 "org.freedesktop.network-manager-settings.system.modify",
46 "org.opensuse.yast.module-manager.import"]
47
48
49   def setup
50     Permission.stubs(:all_actions).returns(TEST_DATA_ACTIONS)
51     PolKit.stubs(:polkit_check).with(){ |p,u| TEST_DATA_GRANT.include? p.to_s}.returns(:yes)
52     PolKit.stubs(:polkit_check).with(){ |p,u| !TEST_DATA_GRANT.include?(p.to_s)}.returns(:no)
53   end
54
55   def test_find_all
56     perm = Permission.find(:all)
57 #test all yast perm is loaded
58     assert_equal 10,perm.size
59 #test that all have not granted
60     perm.each do |p|
61       assert !p[:granted]
62       assert !p[:id].blank?
63     end
64   end
65
66   def test_find_for_user
67     perm = Permission.find(:all,{:user_id => "test"})
68 #test all loaded
69     assert_equal 10,perm.size
70 #check if is granted
71     perm.each do |p|
72       if p[:id]=="org.opensuse.yast.modules.ysr.statelessregister"
73         assert p[:granted] 
74       end
75       assert !p[:granted] if p[:id]=="org.opensuse.yast.modules.ysr.setregisterconfig"
76     end
77   end
78
79   def test_find_with_filter
80     perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.permissions.write"})
81
82 #test all loaded
83     assert_equal 1,perm.size
84 #check if is granted
85     perm.each do |p|
86       assert p[:id] == "org.opensuse.yast.permissions.write"
87     end
88   end
89
90   def test_serialization
91     perm = Permission.find(:all)
92     assert_not_nil perm.to_xml
93     assert_not_nil perm.to_json
94     perm = Permission.find(:all,{:user_id => "test"})
95     assert_not_nil perm.to_xml
96     assert_not_nil perm.to_json
97   end
98
99 GENERIC_EXCEPTION_MESSAGE = "Polkit not run"
100   def test_exception_handling
101     PolKit.stubs(:polkit_check).raises(RuntimeError.new("PolicyKit exception: test does not exist"))
102     assert_raise (InvalidParameters.new( :user_id => "UNKNOWN")) do
103       perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.permissions.write"})
104     end
105
106     PolKit.stubs(:polkit_check).raises(RuntimeError.new(GENERIC_EXCEPTION_MESSAGE))
107     assert_raise (PolicyKitException.new(GENERIC_EXCEPTION_MESSAGE,"test","org.opensuse.yast.permissions.write")) do
108       perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.permissions.write"})
109     end
110   end
111 end