1 # frozen_string_literal: true
5 class AbilityTest < ActiveSupport::TestCase
8 class GuestAbilityTest < AbilityTest
9 test "search permissions for a guest" do
10 ability = Ability.new nil
12 [:create, :show].each do |action|
13 assert ability.can?(action, :search), "should be able to #{action} searches"
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"
23 [:index].each do |action|
24 assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
27 [:create, :edit, :subscribe, :unsubscribe, :hide, :unhide].each do |action|
28 assert ability.cannot?(action, DiaryEntry), "should not be able to #{action} DiaryEntries"
31 [:create, :hide, :unhide].each do |action|
32 assert ability.cannot?(action, DiaryComment), "should not be able to #{action} DiaryComments"
36 test "note permissions for a guest" do
37 ability = Ability.new nil
39 [:index].each do |action|
40 assert ability.can?(action, Note), "should be able to #{action} Notes"
44 test "user roles permissions for a guest" do
45 ability = Ability.new nil
47 [:create, :destroy].each do |action|
48 assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles"
53 class UserAbilityTest < AbilityTest
54 test "Diary permissions" do
55 ability = Ability.new create(:user)
57 [:index, :rss, :show, :create, :edit, :subscribe, :unsubscribe].each do |action|
58 assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
61 [:index, :create].each do |action|
62 assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
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"
70 [:index, :show, :resolve, :ignore, :reopen].each do |action|
71 assert ability.cannot?(action, Issue), "should not be able to #{action} Issues"
76 class ModeratorAbilityTest < AbilityTest
77 test "Issue permissions" do
78 ability = Ability.new create(:moderator_user)
80 [:index, :show, :resolve, :ignore, :reopen].each do |action|
81 assert ability.can?(action, Issue), "should be able to #{action} Issues"
85 test "User Roles permissions" do
86 ability = Ability.new create(:moderator_user)
88 [:create, :destroy].each do |action|
89 assert ability.cannot?(action, UserRole), "should not be able to #{action} UserRoles"
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"
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)
103 creator_ability = Ability.new creator_user
104 assert creator_ability.can?(:edit, block)
105 assert creator_ability.can?(:update, block)
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)
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)
117 creator_ability = Ability.new creator_user
118 assert creator_ability.can?(:edit, block)
119 assert creator_ability.can?(:update, block)
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)
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)
132 creator_ability = Ability.new creator_user
133 assert creator_ability.can?(:edit, block)
134 assert creator_ability.can?(:update, block)
136 revoker_ability = Ability.new revoker_user
137 assert revoker_ability.can?(:edit, block)
138 assert revoker_ability.can?(:update, block)
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)
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"
153 [:index, :create, :hide, :unhide].each do |action|
154 assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComments"
158 test "User Roles permissions for an administrator" do
159 ability = Ability.new create(:administrator_user)
161 [:create, :destroy].each do |action|
162 assert ability.can?(action, UserRole), "should be able to #{action} UserRoles"