-
Is there a way to override the Ability class and inject a new one for system testing? I'd like to be able to inject two different files depending on what I'm doing. can :manage, :all or cannot :manage, :all Then I can run my system tests without worrying about slow tests because most of my abilities are in the database, and they are already tested elsewhere. Plus I really don't want to set all that up in fixtures. I just want system level tests where I can test authentication and authorization, and make sure the views are rendering what I expect. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Well, I figured it out. Here is what I did if anyone is interested. require 'application_system_test_case'
require 'minitest-matchers'
class AdminAdditionalCoveragesTest < ApplicationSystemTestCase
driven_by :selenium, using: :headless_chrome
class CanAbility
include CanCan::Ability
def initialize(current_user)
can :manage, :all
end
end
class CannotAbility
include CanCan::Ability
def initialize(current_user)
cannot :manage, :all
end
end
test 'authorized index' do
# authenticated and authorized
user = users(:user)
sign_in user
Admin::ApplicationController.class_eval do
def current_ability
@current_ability = CanAbility.new(current_user)
end
end
visit admin_additional_coverages_url
assert_selector '.container-fluid'
assert_selector '.page-header', text: 'Additional Coverage'
end
test 'unauthorized index' do
# authenticated but unauthorized
user = users(:user)
sign_in user
Admin::ApplicationController.class_eval do
def current_ability
@current_ability = CannotAbility.new(current_user)
end
end
visit admin_additional_coverages_url
assert_selector '.container-fluid'
assert_selector '#flash_alert', text: 'You are not authorized to access this page.'
end
end |
Beta Was this translation helpful? Give feedback.
Well, I figured it out. Here is what I did if anyone is interested.