Last active
June 30, 2017 20:30
-
-
Save wrkrb33/326c49358b2f57e155b789651c98522c to your computer and use it in GitHub Desktop.
test for CanCan abilities with ActiveRecord subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require "bundler/inline" | |
rescue LoadError => e | |
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" | |
raise e | |
end | |
gemfile(true) do | |
source "https://rubygems.org" | |
# Activate the gem you are reporting the issue against. | |
gem "activerecord", "4.2.9" | |
gem "sqlite3" | |
gem "cancancan", "1.17.0" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
require "cancancan" | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :things, force: true do |t| | |
end | |
end | |
class Thing < ActiveRecord::Base | |
end | |
class SubThing < Thing | |
attr_accessor :editable | |
def editable? | |
@editable | |
end | |
end | |
class ThingOnlyAbility | |
include CanCan::Ability | |
def initialize | |
can :update, Thing | |
end | |
end | |
class BothPositiveAbilities | |
include CanCan::Ability | |
def initialize | |
can :update, Thing | |
can :update, SubThing do |st| | |
st.editable? | |
end | |
end | |
end | |
class NegativeSubthingAbility | |
include CanCan::Ability | |
def initialize | |
can :update, Thing | |
cannot :update, SubThing do |st| | |
!st.editable? | |
end | |
end | |
end | |
class BugTest < Minitest::Test | |
# Establishes that SubThing is updateable, even when abilities are only defined for Thing. | |
def test_thingonly_ability | |
ability = ThingOnlyAbility.new | |
subthing1 = SubThing.new | |
assert_equal true, ability.can?(:update, subthing1) | |
end | |
# Shows that positively-stated SubThing-specific ability _appears_ | |
# to work when editable? == true, though in fact it works because of | |
# the inherited ability from Thing. | |
def test_both_positive_abilities_editable | |
ability = BothPositiveAbilities.new | |
subthing = SubThing.new editable: true | |
assert_equal true, ability.can?(:update, subthing) | |
end | |
# Shows that positively-stated SubThing-specific ability does not work when editable? == false | |
def test_both_positive_abilities_uneditable | |
ability = BothPositiveAbilities.new | |
subthing = SubThing.new editable: false | |
assert_equal false, ability.can?(:update, subthing) | |
end | |
# This is essentially the same as `test_both_positive_abilities_editable` | |
def test_negative_subthing_ability_editable | |
ability = NegativeSubthingAbility.new | |
subthing = SubThing.new editable: true | |
assert_equal true, ability.can?(:update, subthing) | |
end | |
# Shows that negatively-stated SubThing-specific ability actually works. | |
def test_negative_subthing_ability_uneditable | |
ability = NegativeSubthingAbility.new | |
subthing = SubThing.new editable: false | |
assert_equal false, ability.can?(:update, subthing) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment