Module: ApiMaker::ExpectToBeAbleToHelper

Defined in:
lib/api_maker/expect_to_able_to_helper.rb

Instance Method Summary collapse

Instance Method Details

#expect_not_to_be_able_to(ability, model, permissions) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/api_maker/expect_to_able_to_helper.rb', line 17

def expect_not_to_be_able_to(ability, model, permissions)
  require "cancan/matchers"

  permissions.each do |permission|
    # Test access through 'can?'
    expect(ability).not_to be_able_to permission, model

    # Test access through 'accessible_by'
    if model.is_a?(ActiveRecord::Base) && model.persisted?
      readable_models = model.class.where(id: model).accessible_by(ability, permission)
      expect(readable_models).to be_empty
    end
  end
end

#expect_to_be_able_to(ability, model, permissions) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/api_maker/expect_to_able_to_helper.rb', line 2

def expect_to_be_able_to(ability, model, permissions)
  require "cancan/matchers"

  permissions.each do |permission|
    # Test access through 'can?'
    expect(ability).to be_able_to permission, model

    # Test access through 'accessible_by'
    if model.is_a?(ActiveRecord::Base) && model.persisted?
      readable_models = model.class.where(id: model).accessible_by(ability, permission)
      expect(readable_models).to eq [model]
    end
  end
end