Merge branch 'master' of git@git.opensuse.org:projects/yast/rest-service
[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_DATA_GRANT = <<EOF
23 org.opensuse.yast.modules.ysr.statelessregister
24 org.opensuse.yast.modules.ysr.getregistrationconfig
25 org.freedesktop.network-manager-settings.system.modify
26 org.opensuse.yast.module-manager.import
27 EOF
28
29 TEST_NONEXIST = <<EOF
30   polkit-auth: cannot look up uid for user 'nonexist'
31 EOF
32
33   def setup
34     Permission.any_instance.stubs(:all_actions).returns(TEST_DATA_ACTIONS)
35     Permission.any_instance.stubs(:actions_for_user).returns(TEST_DATA_GRANT)
36   end
37
38   def test_find_all
39     perm = Permission.find(:all)
40 #test all yast perm is loaded
41     assert_equal 12,perm.permissions.size
42 #test that all have not granted
43     perm.permissions.each do |p|
44       assert !p[:granted]
45       assert !p[:id].blank?
46     end
47   end
48
49   def test_find_for_user
50     perm = Permission.find(:all,{:user_id => "test"})
51 #test all loaded
52     assert_equal 12,perm.permissions.size
53 #check if is granted
54     perm.permissions.each do |p|
55       if p[:id]=="org.opensuse.yast.modules.ysr.statelessregister"
56         assert p[:granted] 
57       end
58       assert !p[:granted] if p[:id]=="org.opensuse.yast.modules.ysr.setregisterconfig"
59     end
60   end
61
62   def test_find_with_filter
63     perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.module-manager.import"})
64
65 #test all loaded
66     assert_equal 1,perm.permissions.size
67 #check if is granted
68     perm.permissions.each do |p|
69       assert p[:id] == "org.opensuse.yast.module-manager.import"
70     end
71   end
72 end