Sha256: 8fcae4f05951cb9e4b1d00e9f0a534a0f00590d930a152a8b3b3e8b5ee31e2e4

Contents?: true

Size: 923 Bytes

Versions: 1

Compression:

Stored size: 923 Bytes

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
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 `if_ready' is not allowed/i, error.message)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
surrounded-0.7.0 test/context_access_test.rb