Sha256: 5503cb2064e8978d9b450f8920e8ce6604fb4df0e2c06a037e262953dd15933f

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

require 'test_helper'
require 'minitest/mock'

class FilteredContext
  extend Surrounded::Context
  protect_triggers
  
  initialize :user, :other_user
  
  trigger :if_ready do
    'ready'
  end
  
  disallow :if_ready do
    user.name != 'Amy'
  end
  
  trigger :check_disallow_behavior do
    # used for disallow check
  end
  
  disallow :check_disallow_behavior do
    user.special
  end
  
  role :user do
    def special
      'special user method'
    end
  end
end

describe Surrounded::Context, 'access control' do
  let(:user){ User.new("Jim") }
  let(:other_user){ User.new("Guille") }
  let(:context){ FilteredContext.new(user, other_user) }
  
  it 'includes triggers when allowed' do
    context.stub(:disallow_if_ready?, false) do
      assert context.triggers.include?(:if_ready)
    end
  end

  it 'excludes triggers when not allowed' do
    refute context.triggers.include?(:if_ready)
  end
  
  it 'raises errors when trigger method not allowed' do
    error = assert_raises(::Surrounded::Context::AccessError){
      context.if_ready
    }
    assert_match(/access to FilteredContext#if_ready is not allowed/i, error.message)
  end
  
  it 'applies roles in disallow blocks' do
    assert_equal 'special user method', context.disallow_check_disallow_behavior?
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
surrounded-0.8.1 test/context_access_test.rb
surrounded-0.8.0 test/context_access_test.rb
surrounded-0.7.3 test/context_access_test.rb
surrounded-0.7.2 test/context_access_test.rb