Sha256: 9bf5091a74c477502c3fd4dd24fa5afacb3a7ad003063a1c7ab950cd211ea4f6

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 KB

Contents

require 'rails_helper'

RSpec.describe "Controller Authorization", type: :controller do

  let(:authorization){ controller.send(:active_admin_authorization) }

  before do
    load_resources { ActiveAdmin.register Post }
    @controller = Admin::PostsController.new
    allow(authorization).to receive(:authorized?)
  end

  it "should authorize the index action" do
    expect(authorization).to receive(:authorized?).with(auth::READ, Post).and_return true
    get :index
    expect(response).to be_success
  end

  it "should authorize the new action" do
    expect(authorization).to receive(:authorized?).with(auth::CREATE, an_instance_of(Post)).and_return true
    get :new
    expect(response).to be_success
  end

  it "should authorize the create action with the new resource" do
    expect(authorization).to receive(:authorized?).with(auth::CREATE, an_instance_of(Post)).and_return true
    post :create
    expect(response).to redirect_to action: 'show', id: Post.last.id
  end

  it "should redirect when the user isn't authorized" do
    expect(authorization).to receive(:authorized?).with(auth::READ, Post).and_return false
    get :index
    expect(response.body).to eq '<html><body>You are being <a href="http://test.host/admin">redirected</a>.</body></html>'
    expect(response).to redirect_to '/admin'
  end

  private

  def auth
    ActiveAdmin::Authorization
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
activeadmin_addons-1.7.1 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.7.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.6.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.5.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.4.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.3.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin_addons-1.2.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/spec/unit/authorization/controller_authorization_spec.rb
activeadmin-1.2.1 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-1.2.0 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-1.1.0 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-1.0.0 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-1.0.0.pre5 spec/unit/authorization/controller_authorization_spec.rb