class RSpec::Resources::DSL::Actions::IncludeIfNeeded

Attributes

action[R]

Public Class Methods

new(context, action) click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 8
def initialize(context, action)
  @ctx = context
  @action = action
end

Public Instance Methods

auth_examples() click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 15
def auth_examples
  with_meta_for :it_needs_authentication do |incl_meta|
    context 'when authentication is missing' do
      let(incl_meta[:headers]) { {} }

      it "denies access with status code #{incl_meta[:error_status]}" do
        expect(response).to have_http_status(incl_meta[:error_status])
      end
    end
  end
end
restricted_examples() click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 27
def restricted_examples
  with_meta_for :it_has_restricted_access do |incl_meta|
    case action
    when :index then restricted_index_examples incl_meta
    when :create then restricted_create_examples incl_meta
    else
      context 'when trying to access a restricted resource' do
        subject { send incl_meta[:for_resource] }

        it "denies access with status code #{incl_meta[:error_status]}" do
          expect(response).to have_http_status(incl_meta[:error_status])
        end
      end
    end
  end
end

Private Instance Methods

context(*args, &block) click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 74
def context(*args, &block)
  @ctx.context(*args, &block)
end
metadata() click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 78
def metadata
  @ctx.metadata
end
restricted_create_examples(incl_meta) click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 64
def restricted_create_examples(incl_meta)
  return unless Util.nested_resource? metadata

  context 'when using a restricted parent resource' do
    let(:instantiation_resource) { send incl_meta[:for_resource] }

    it { returns_status_code 404 }
  end
end
restricted_index_examples(incl_meta) click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 46
def restricted_index_examples(incl_meta)
  context 'when only restricted resources are in database' do
    subject { [send(incl_meta[:for_resource])] }

    if Util.nested_resource? metadata
      # we can not access the parent resource
      # thus we get a not found error
      it { returns_status_code 404 }
    else
      it 'returns no records' do
        expect(base_doc).to be_empty
      end

      it { returns_status_code 200 }
    end
  end
end
with_meta_for(key) { |incl_meta| ... } click to toggle source
# File lib/rspec/resources/dsl/actions/include_if_needed.rb, line 82
def with_meta_for(key)
  incl_meta = metadata[key]
  return if incl_meta.nil? || !incl_meta[:only].include?(action)

  yield incl_meta
end