]> git.openstreetmap.org Git - rails.git/blob - test/abilities/abilities_test.rb
Rename integration login test
[rails.git] / test / abilities / abilities_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class AbilityTest < ActiveSupport::TestCase
6 end
7
8 class GuestAbilityTest < AbilityTest
9   test "search permissions for a guest" do
10     ability = Ability.new nil
11
12     [:create, :show].each do |action|
13       assert ability.can?(action, :search), "should be able to #{action} searches"
14     end
15   end
16
17   test "diary permissions for a guest" do
18     ability = Ability.new nil
19     [:index, :rss, :show].each do |action|
20       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
21     end
22
23     [:index].each do |action|
24       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
25     end
26
27     [:create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action|
28       assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries"
29     end
30
31     [:create, :hide, :unhide].each do |action|
32       assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComments"
33     end
34   end
35
36   test "note permissions for a guest" do
37     ability = Ability.new nil
38
39     [:index].each do |action|
40       assert ability.can?(action, Note), "should be able to #{action} Notes"
41     end
42   end
43
44   test "user roles permissions for a guest" do
45     ability = Ability.new nil
46
47     [:create, :destroy].each do |action|
48       assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles"
49     end
50   end
51 end
52
53 class UserAbilityTest < AbilityTest
54   test "Diary permissions" do
55     ability = Ability.new create(:user)
56
57     [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe].each do |action|
58       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
59     end
60
61     [:index, :create].each do |action|
62       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
63     end
64
65     [:hide, :unhide].each do |action|
66       assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries"
67       assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComment"
68     end
69
70     [:index, :show, :resolve, :ignore, :reopen].each do |action|
71       assert ability.cannot?(action, Issue), "should not be able to #{action} Issues"
72     end
73   end
74 end
75
76 class ModeratorAbilityTest < AbilityTest
77   test "Issue permissions" do
78     ability = Ability.new create(:moderator_user)
79
80     [:index, :show, :resolve, :ignore, :reopen].each do |action|
81       assert ability.can?(action, Issue), "should be able to #{action} Issues"
82     end
83   end
84
85   test "User Roles permissions" do
86     ability = Ability.new create(:moderator_user)
87
88     [:create, :destroy].each do |action|
89       assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles"
90     end
91
92     [:hide, :unhide].each do |action|
93       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
94       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment"
95     end
96   end
97
98   test "Active block update permissions" do
99     creator_user = create(:moderator_user)
100     other_moderator_user = create(:moderator_user)
101     block = create(:user_block, :creator => creator_user)
102
103     creator_ability = Ability.new creator_user
104     assert creator_ability.can?(:edit, block)
105     assert creator_ability.can?(:update, block)
106
107     other_moderator_ability = Ability.new other_moderator_user
108     assert other_moderator_ability.can?(:edit, block)
109     assert other_moderator_ability.can?(:update, block)
110   end
111
112   test "Expired block update permissions" do
113     creator_user = create(:moderator_user)
114     other_moderator_user = create(:moderator_user)
115     block = create(:user_block, :expired, :creator => creator_user)
116
117     creator_ability = Ability.new creator_user
118     assert creator_ability.can?(:edit, block)
119     assert creator_ability.can?(:update, block)
120
121     other_moderator_ability = Ability.new other_moderator_user
122     assert other_moderator_ability.cannot?(:edit, block)
123     assert other_moderator_ability.cannot?(:update, block)
124   end
125
126   test "Revoked block update permissions" do
127     creator_user = create(:moderator_user)
128     revoker_user = create(:moderator_user)
129     other_moderator_user = create(:moderator_user)
130     block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
131
132     creator_ability = Ability.new creator_user
133     assert creator_ability.can?(:edit, block)
134     assert creator_ability.can?(:update, block)
135
136     revoker_ability = Ability.new revoker_user
137     assert revoker_ability.can?(:edit, block)
138     assert revoker_ability.can?(:update, block)
139
140     other_moderator_ability = Ability.new other_moderator_user
141     assert other_moderator_ability.cannot?(:edit, block)
142     assert other_moderator_ability.cannot?(:update, block)
143   end
144 end
145
146 class AdministratorAbilityTest < AbilityTest
147   test "Diary for an administrator" do
148     ability = Ability.new create(:administrator_user)
149     [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action|
150       assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
151     end
152
153     [:index, :create, :hide, :unhide].each do |action|
154       assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
155     end
156   end
157
158   test "User Roles permissions for an administrator" do
159     ability = Ability.new create(:administrator_user)
160
161     [:create, :destroy].each do |action|
162       assert ability.can?(action, UserRole), "should be able to #{action} UserRoles"
163     end
164   end
165 end