openstreetmap-website/test/models/capability_test.rb
Chris Flipse 91fc65a2e3 separate ability and capability
These are asking fundamentally different questions;

Abilities are asking the application if the user has a role that allows
the user to take a certain action
Capabilities are asking if the user has granted the application to
perform a certain type of action

CanCanCan makes no distinction, however, so the `granted_capabilities`
method is provided as a point that can be checked in rescue methods, so
that one can _attempt_ to continue to provide the more informative error
messages around permission refusals
2018-06-17 13:57:32 -04:00

51 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class CapabilityTest < ActiveSupport::TestCase
def tokens(*toks)
AccessToken.new do |token|
toks.each do |t|
token.public_send("#{t}=", true)
end
end
end
end
class UserCapabilityTest < CapabilityTest
test "user preferences" do
user = create(:user)
# a user with no tokens
capability = Capability.new create(:user), nil
[:read, :read_one, :update, :update_one, :delete_one].each do |act|
assert capability.can? act, UserPreference
end
# A user with empty tokens
capability = Capability.new create(:user), tokens
[:read, :read_one, :update, :update_one, :delete_one].each do |act|
assert capability.cannot? act, UserPreference
end
capability = Capability.new user, tokens(:allow_read_prefs)
[:update, :update_one, :delete_one].each do |act|
assert capability.cannot? act, UserPreference
end
[:read, :read_one].each do |act|
assert capability.can? act, UserPreference
end
capability = Capability.new user, tokens(:allow_write_prefs)
[:read, :read_one].each do |act|
assert capability.cannot? act, UserPreference
end
[:update, :update_one, :delete_one].each do |act|
assert capability.can? act, UserPreference
end
end
end