Sha256: efe91fbf76fab5294940985e607f48c4dcf071709dc5e25808b8841f85891c8c

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 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 error specific to the context class when trigger method not allowed' do
    error = assert_raises(::FilteredContext::AccessError){
      context.if_ready
    }
    assert_match(/access to FilteredContext#if_ready is not allowed/i, error.message)
  end
  
  it 'supports rescuing from Surrounded defined error when trigger method not allowed' do
    begin
      context.if_ready
    rescue ::Surrounded::Context::AccessError => error
      assert "rescued!"
    end
    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

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-0.8.2 test/context_access_test.rb