Sha256: 830b41db5faf370366854a6e1feb9b89c0388037f3c7e3673828dba0b0b49115

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 KB

Contents

require 'test_helper'
require 'rails_ops/authorization_backend/can_can_can'
require 'cancancan'

class RailsOps::Operation::UpdateLazyAuthTest < ActiveSupport::TestCase
  include TestHelper

  BASIC_OP = Class.new(RailsOps::Operation::Model::Update) do
    model ::Group

    model_authorization_action :update

    def perform
      fail osparams.exception if osparams.exception
      @done = true
    end
  end

  ABILITY = Class.new do
    include CanCan::Ability

    def initialize(read: false, update: false)
      can :read, Group if read
      can :update, Group if update
    end
  end

  setup do
    Group.create!(id: 1, name: 'Group')
    RailsOps.config.authorization_backend = 'RailsOps::AuthorizationBackend::CanCanCan'
  end

  def test_unpermitted_read
    ctx = RailsOps::Context.new(ability: ABILITY.new(read: true))
    assert_raises CanCan::AccessDenied do
      BASIC_OP.new(ctx, id: 1)
    end
  end

  def test_permitted_read
    ctx = RailsOps::Context.new(ability: ABILITY.new(read: true))
    assert_nothing_raised do
      BASIC_OP.new(ctx, id: 1)
    end
  end

  def test_unpermitted_update
    ctx = RailsOps::Context.new(ability: ABILITY.new(read: true))
    op = BASIC_OP.new(ctx, id: 1)
    assert_raises CanCan::AccessDenied do
      op.run!
    end
  end

  def test_permitted_update
    ctx = RailsOps::Context.new(ability: ABILITY.new(read: true, update: true))
    op = BASIC_OP.new(ctx, id: 1)
    assert_nothing_raised do
      op.run!
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
rails_ops-1.4.1 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.4.0 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.3.0 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.2.3 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.2.2 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.2.1 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.2.0 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.31 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.30 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.29 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.28 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.27 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.26 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.25 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.24 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.23 test/unit/rails_ops/operation/update_auth_test.rb
rails_ops-1.1.22 test/unit/rails_ops/operation/update_auth_test.rb