1 # frozen_string_literal: true
5 class AbilityTest < ActiveSupport::TestCase
8 AccessToken.new do |token|
10 token.public_send("#{t}=", true)
17 class GuestAbilityTest < AbilityTest
19 test "geocoder permission for a guest" do
20 ability = Ability.new nil, tokens
22 [:search, :search_latlon, :search_ca_postcode, :search_osm_nominatim,
23 :search_geonames, :search_osm_nominatim_reverse, :search_geonames_reverse].each do |action|
24 assert ability.can?(action, :geocoder), "should be able to #{action} geocoder"
28 test "diary permissions for a guest" do
29 ability = Ability.new nil, tokens
30 [:list, :rss, :view, :comments].each do |action|
31 assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
34 [:create, :edit, :comment, :subscribe, :unsubscribe, :hide, :hidecomment].each do |action|
35 assert ability.cannot?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
36 assert ability.cannot?(action, DiaryComment), "should be able to #{action} DiaryEntries"
42 class UserAbilityTest < AbilityTest
44 test "Diary permissions" do
45 ability = Ability.new create(:user), tokens
47 [:list, :rss, :view, :comments, :create, :edit, :comment, :subscribe, :unsubscribe].each do |action|
48 assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
51 [:hide, :hidecomment].each do |action|
52 assert ability.cannot?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
53 assert ability.cannot?(action, DiaryComment), "should be able to #{action} DiaryEntries"
57 test "user preferences" do
60 # a user with no tokens
61 ability = Ability.new create(:user), nil
62 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
63 assert ability.can? act, UserPreference
66 # A user with empty tokens
67 ability = Ability.new create(:user), tokens
69 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
70 assert ability.cannot? act, UserPreference
73 ability = Ability.new user, tokens(:allow_read_prefs)
75 [:update, :update_one, :delete_one].each do |act|
76 assert ability.cannot? act, UserPreference
79 [:read, :read_one].each do |act|
80 assert ability.can? act, UserPreference
83 ability = Ability.new user, tokens(:allow_write_prefs)
84 [:read, :read_one].each do |act|
85 assert ability.cannot? act, UserPreference
88 [:update, :update_one, :delete_one].each do |act|
89 assert ability.can? act, UserPreference
94 class AdministratorAbilityTest < AbilityTest
96 test "Diary for an administrator" do
97 ability = Ability.new create(:administrator_user), tokens
98 [:list, :rss, :view, :comments, :create, :edit, :comment, :subscribe, :unsubscribe, :hide, :hidecomment].each do |action|
99 assert ability.can?(action, DiaryEntry), "should be able to #{action} DiaryEntries"
102 [:hide, :hidecomment].each do |action|
103 assert ability.can?(action, DiaryComment), "should be able to #{action} DiaryComment"
107 test "administrator does not auto-grant user preferences" do
108 ability = Ability.new create(:administrator_user), tokens
110 [:read, :read_one, :update, :update_one, :delete_one].each do |act|
111 assert ability.cannot? act, UserPreference