unify checking permissions (bnc#555208)
[opensuse:yast-rest-service.git] / webservice / test / unit / permission_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 # Test Permission class
4
5 class PermissionTest < ActiveSupport::TestCase
6 TEST_DATA_ACTIONS = <<EOF
7 org.opensuse.yast.modules.ysr.statelessregister
8 org.opensuse.yast.modules.ysr.getregistrationconfig
9 org.opensuse.yast.modules.ysr.setregistrationconfig
10 org.freedesktop.network-manager-settings.system.modify
11 org.opensuse.yast.module-manager.import
12 org.opensuse.yast.module-manager.lock
13 org.opensuse.yast.modules.yapi.users.usersget
14 org.opensuse.yast.modules.yapi.users.userget
15 org.opensuse.yast.modules.yapi.users.usermodify
16 org.opensuse.yast.modules.yapi.users.useradd
17 org.opensuse.yast.modules.yapi.users.userdelete
18 org.opensuse.yast.permissions.read
19 org.opensuse.yast.permissions.write
20 EOF
21
22 TEST_NONEXIST = <<EOF
23   polkit-auth: cannot look up uid for user 'nonexist'
24 EOF
25
26   def setup
27     Permission.any_instance.stubs(:all_actions).returns(TEST_DATA_ACTIONS)
28     PolKit.stubs(:polkit_check).returns(:no)
29     ["org.opensuse.yast.modules.ysr.statelessregister",
30      "org.opensuse.yast.modules.ysr.getregistrationconfig",
31      "org.freedesktop.network-manager-settings.system.modify",
32      "org.opensuse.yast.module-manager.import"].each do |perm|
33       PolKit.stubs(:polkit_check).with(perm,"test").returns(:yes)
34     end
35   end
36
37   def test_find_all
38     perm = Permission.find(:all)
39 #test all yast perm is loaded
40     assert_equal 12,perm.permissions.size
41 #test that all have not granted
42     perm.permissions.each do |p|
43       assert !p[:granted]
44       assert !p[:id].blank?
45     end
46   end
47
48   def test_find_for_user
49     perm = Permission.find(:all,{:user_id => "test"})
50 #test all loaded
51     assert_equal 12,perm.permissions.size
52 #check if is granted
53     perm.permissions.each do |p|
54       if p[:id]=="org.opensuse.yast.modules.ysr.statelessregister"
55         assert p[:granted] 
56       end
57       assert !p[:granted] if p[:id]=="org.opensuse.yast.modules.ysr.setregisterconfig"
58     end
59   end
60
61   def test_find_with_filter
62     perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.module-manager.import"})
63
64 #test all loaded
65     assert_equal 1,perm.permissions.size
66 #check if is granted
67     perm.permissions.each do |p|
68       assert p[:id] == "org.opensuse.yast.module-manager.import"
69     end
70   end
71 end