Skip to content

Instantly share code, notes, and snippets.

@dsandstrom
Last active December 12, 2020 06:07
Show Gist options
  • Select an option

  • Save dsandstrom/917a9b6fff195704dc90bef9f4574bb7 to your computer and use it in GitHub Desktop.

Select an option

Save dsandstrom/917a9b6fff195704dc90bef9f4574bb7 to your computer and use it in GitHub Desktop.

Revisions

  1. dsandstrom revised this gist Dec 12, 2020. 2 changed files with 17 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions authorization.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # spec/support/authorizaton.rb

    module TestHelpers
    module Authorization
    def expect_to_be_unauthorized(response)
    expect(response).to redirect_to(:unauthorized)
    end

    def enable_devise_user(controller, user = nil)
    without_partial_double_verification do
    @request.env['devise.mapping'] = Devise.mappings[:user]
    allow(controller).to receive(:resource) { user } if user
    end
    end
    end
    end
    1 change: 1 addition & 0 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@
    RSpec.configure do |config|
    ..
    config.include TestHelpers::Authentication, type: :view
    config.include TestHelpers::Authorization, type: :controller
    end
  2. dsandstrom revised this gist Dec 12, 2020. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions authentication.rb
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@

    module TestHelpers
    module Authentication
    # set the current_user
    def enable_can(view, user)
    without_partial_double_verification do
    allow(view).to receive(:can?) do |action, record|
    @@ -20,5 +21,15 @@ def enable_can(view, user)
    allow(view).to receive(:current_user) { user }
    end
    end

    # setup a guest user for devise
    def enable_devise_user(view)
    without_partial_double_verification do
    allow(view).to receive(:resource) { User.new }
    allow(view).to receive(:resource_class) { User }
    allow(view).to receive(:resource_name) { :user }
    allow(view).to receive(:devise_mapping) { Devise.mappings[:user] }
    end
    end
    end
    end
  3. dsandstrom revised this gist Oct 28, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion authentication.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@
    # before { enable_can(view, admin) }
    # ```


    module TestHelpers
    module Authentication
    def enable_can(view, user)
  4. dsandstrom revised this gist Oct 28, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions authentication.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,14 @@
    # spec/support/authentication.rb

    # Enable can? method in view specs for cancancan gem
    #
    # Example:
    #
    # ```
    # let(:admin) { Fabricate(:user_admin) }
    # before { enable_can(view, admin) }
    # ```


    module TestHelpers
    module Authentication
  5. dsandstrom revised this gist Oct 28, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,5 @@

    RSpec.configure do |config|
    ..
    config.include TestHelpers::Authentication, type: :controller
    config.include TestHelpers::Authentication, type: :view
    end
  6. dsandstrom created this gist Oct 28, 2020.
    17 changes: 17 additions & 0 deletions authentication.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # spec/support/authentication.rb

    # Enable can? method in view specs for cancancan gem

    module TestHelpers
    module Authentication
    def enable_can(view, user)
    without_partial_double_verification do
    allow(view).to receive(:can?) do |action, record|
    ability = Ability.new(user)
    ability.can?(action, record)
    end
    allow(view).to receive(:current_user) { user }
    end
    end
    end
    end
    10 changes: 10 additions & 0 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # spec/rails_helper.rb

    ..

    Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }

    RSpec.configure do |config|
    ..
    config.include TestHelpers::Authentication, type: :controller
    end