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
This commit is contained in:
parent
4d20a2c96a
commit
91fc65a2e3
5 changed files with 81 additions and 61 deletions
51
test/models/capability_test.rb
Normal file
51
test/models/capability_test.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
# 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
|
Loading…
Add table
Add a link
Reference in a new issue