]> git.openstreetmap.org Git - rails.git/blob - test/models/abilities_test.rb
fix and improve ability coverage to account for tokens
[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     ability = Ability.new create(:user), tokens
51
52     [:read, :read_one, :update, :update_one, :delete_one].each do |act|
53       assert ability.cannot? act, UserPreference
54     end
55
56     ability = Ability.new user, tokens(:allow_read_prefs)
57
58     [:update, :update_one, :delete_one].each do |act|
59       assert ability.cannot? act, UserPreference
60     end
61
62     [:read, :read_one].each do |act|
63       assert ability.can? act, UserPreference
64     end
65
66     ability = Ability.new user, tokens(:allow_write_prefs)
67     [:read, :read_one].each do |act|
68       assert ability.cannot? act, UserPreference
69     end
70
71     [:update, :update_one, :delete_one].each do |act|
72       assert ability.can? act, UserPreference
73     end
74   end
75 end
76
77 class AdministratorAbilityTest < AbilityTest
78
79   test "Diary for an administrator" do
80     ability = Ability.new create(:administrator_user), tokens
81     [:list, :rss, :view, :comments, :create, :edit, :comment, :subscribe, :unsubscribe, :hide, :hidecomment].each do |action|
82       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
83     end
84
85     [:hide, :hidecomment].each do |action|
86       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment"
87     end
88   end
89
90   test "administrator does not auto-grant user preferences" do
91     ability = Ability.new create(:administrator_user), tokens
92
93     [:read, :read_one, :update, :update_one, :delete_one].each do |act|
94       assert ability.cannot? act, UserPreference
95     end
96   end
97
98
99 end