Sha256: 2761e05f0edce58e93497c598e419b49e3484c363cdaea93704baf328303b8c5

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 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_successful
  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_successful
  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).to redirect_to '/admin'
  end

  private

  def auth
    ActiveAdmin::Authorization
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin-rails-1.7.2 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-rails-1.7.1 spec/unit/authorization/controller_authorization_spec.rb
activeadmin-rails-1.7.0 spec/unit/authorization/controller_authorization_spec.rb