refactor permissions model and add tests for this model (bnc#581744)
[opensuse:yast-rest-service.git] / webservice / test / unit / permission_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'polkit'
3
4 # Test Permission class
5
6 class PermissionTest < ActiveSupport::TestCase
7 TEST_DATA_ACTIONS = <<EOF
8 org.opensuse.yast.modules.ysr.statelessregister
9 org.opensuse.yast.modules.ysr.getregistrationconfig
10 org.opensuse.yast.modules.ysr.setregistrationconfig
11 org.freedesktop.network-manager-settings.system.modify
12 org.opensuse.yast.module-manager.import
13 org.opensuse.yast.module-manager.lock
14 org.opensuse.yast.scr.read
15 org.opensuse.yast.modules.yapi.users.usersget
16 org.opensuse.yast.modules.yapi.users.userget
17 org.opensuse.yast.modules.yapi.users.usermodify
18 org.opensuse.yast.modules.yapi.users.useradd
19 org.opensuse.yast.modules.yapi.users.userdelete
20 org.opensuse.yast.permissions.read
21 org.opensuse.yast.permissions.write
22 EOF
23
24 TEST_DATA_GRANT = [
25 "org.opensuse.yast.modules.ysr.statelessregister",
26 "org.opensuse.yast.modules.ysr.getregistrationconfig",
27 "org.freedesktop.network-manager-settings.system.modify",
28 "org.opensuse.yast.module-manager.import"]
29
30
31   def setup
32     Permission.any_instance.stubs(:all_actions).returns(TEST_DATA_ACTIONS)
33     PolKit.stubs(:polkit_check).with(){ |p,u| TEST_DATA_GRANT.include? p.to_s}.returns(:yes)
34     PolKit.stubs(:polkit_check).with(){ |p,u| !TEST_DATA_GRANT.include?(p.to_s)}.returns(:no)
35   end
36
37   def test_find_all
38     perm = Permission.find(:all)
39 #test all yast perm is loaded
40     assert_equal 10,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 10,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.permissions.write"})
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.permissions.write"
69     end
70   end
71
72   def test_serialization
73     perm = Permission.find(:all)
74     assert_not_nil perm.to_xml
75     assert_not_nil perm.to_json
76     perm = Permission.find(:all,{:user_id => "test"})
77     assert_not_nil perm.to_xml
78     assert_not_nil perm.to_json
79   end
80
81 GENERIC_EXCEPTION_MESSAGE = "Polkit not run"
82   def test_exception_handling
83     PolKit.stubs(:polkit_check).raises(RuntimeError.new("PolicyKit exception: test does not exist"))
84     assert_raise (InvalidParameters.new( :user_id => "UNKNOWN")) do
85       perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.permissions.write"})
86     end
87
88     PolKit.stubs(:polkit_check).raises(RuntimeError.new(GENERIC_EXCEPTION_MESSAGE))
89     assert_raise (PolicyKitException.new(GENERIC_EXCEPTION_MESSAGE,"test","org.opensuse.yast.permissions.write")) do
90       perm = Permission.find(:all,{:user_id => "test",:filter => "org.opensuse.yast.permissions.write"})
91     end
92   end
93 end