]> git.openstreetmap.org Git - rails.git/blob - test/models/abilities_test.rb
Update capabilities check to actually reflect the existing logic
[rails.git] / test / models / abilities_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class AbilityTest < ActiveSupport::TestCase
6
7   def tokens(*toks)
8     AccessToken.new do |token|
9       toks.each do |t|
10         token.public_send("#{t}=", true)
11       end
12     end
13   end
14
15 end
16
17 class GuestAbilityTest < AbilityTest
18
19   test "diary permissions for a guest" do
20     ability = Ability.new nil, tokens
21     [:list, :rss, :view, :comments].each do |action|
22       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
23     end
24
25     [:create, :edit, :comment, :subscribe, :unsubscribe, :hide, :hidecomment].each do |action|
26       assert ability.cannot?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
27       assert ability.cannot?(action, DiaryComment), "should be able to #{action} DiaryEntries"
28     end
29   end
30
31 end
32
33 class UserAbilityTest < AbilityTest
34
35   test "Diary permissions" do
36     ability = Ability.new create(:user), tokens
37
38     [:list, :rss, :view, :comments, :create, :edit, :comment, :subscribe, :unsubscribe].each do |action|
39       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
40     end
41
42     [:hide, :hidecomment].each do |action|
43       assert ability.cannot?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
44       assert ability.cannot?(action, DiaryComment), "should be able to #{action} DiaryEntries"
45     end
46   end
47
48   test "user preferences" do
49     user = create(:user)
50
51     # a user with no tokens
52     ability = Ability.new create(:user), nil
53     [:read, :read_one, :update, :update_one, :delete_one].each do |act|
54       assert ability.can? act, UserPreference
55     end
56
57     # A user with empty tokens
58     ability = Ability.new create(:user), tokens
59
60     [:read, :read_one, :update, :update_one, :delete_one].each do |act|
61       assert ability.cannot? act, UserPreference
62     end
63
64     ability = Ability.new user, tokens(:allow_read_prefs)
65
66     [:update, :update_one, :delete_one].each do |act|
67       assert ability.cannot? act, UserPreference
68     end
69
70     [:read, :read_one].each do |act|
71       assert ability.can? act, UserPreference
72     end
73
74     ability = Ability.new user, tokens(:allow_write_prefs)
75     [:read, :read_one].each do |act|
76       assert ability.cannot? act, UserPreference
77     end
78
79     [:update, :update_one, :delete_one].each do |act|
80       assert ability.can? act, UserPreference
81     end
82   end
83 end
84
85 class AdministratorAbilityTest < AbilityTest
86
87   test "Diary for an administrator" do
88     ability = Ability.new create(:administrator_user), tokens
89     [:list, :rss, :view, :comments, :create, :edit, :comment, :subscribe, :unsubscribe, :hide, :hidecomment].each do |action|
90       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
91     end
92
93     [:hide, :hidecomment].each do |action|
94       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment"
95     end
96   end
97
98   test "administrator does not auto-grant user preferences" do
99     ability = Ability.new create(:administrator_user), tokens
100
101     [:read, :read_one, :update, :update_one, :delete_one].each do |act|
102       assert ability.cannot? act, UserPreference
103     end
104   end
105
106
107 end