module ExternalServices::RSpec::Helpers::ClassMethods

Public Instance Methods

describe_external_service_api(object:, api_name:, **kwargs, &blk) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

# File lib/rspec/helpers.rb, line 21
def describe_external_service_api(object:, api_name:, **kwargs, &blk)
  action_class = kwargs[:action_class] || "ExternalServices::ApiActions::#{api_name.to_s.camelize}".constantize
  methods = %i[create update destroy]
  methods -= [kwargs[:except]].flatten if kwargs[:except]
  methods &= [kwargs[:only]].flatten if kwargs[:only]

  describe action_class.to_s do
    before :all do
      Disabler.enable_external_services
      Disabler.disable_external_services except: [api_name]
    end

    after :all do
      Disabler.enable_external_services
    end

    before do
      @api_object = case object
                    when Symbol
                      send(object)
                    else
                      instance_exec(&object)
                    end
      @action_class = action_class
      @id_key = kwargs[:id_key].try(:to_s) || "#{api_name.to_s.underscore}_id"
      @methods = kwargs[:methods] || { create: :post, update: :put, destroy: :delete }

      perform_unprocessed_actions
    end

    if :create.in? methods
      it 'creates action on create' do
        expect_api_action_on_create
      end
    end

    if :update.in? methods
      it 'creates action on update' do
        @api_object.send("#{@id_key}=", SecureRandom.hex)
        @api_object.save

        expect_api_action_on_update(@api_object)
      end
    end

    if :destroy.in? methods
      it 'creates action on delete' do
        @api_object.send("#{@id_key}=", SecureRandom.hex)
        @api_object.save

        @api_object.reload.descendants.each(&:delete) if @api_object.respond_to?(:descendants)
        @api_object.destroy
        expect_api_action_on_destroy(@api_object)
      end
    end

    if kwargs[:descendants_identifier_check]
      it 'create actions for all descendants on identifier change' do
        @api_object.update! identifier: "#{@api_object.identifier}1"

        @api_object.descendants.each do |c|
          expect_api_action_on_update(c)
        end
      end
    end

    instance_exec(&blk) if block_given?
  end

  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
end
disable_external_services(except = []) click to toggle source
# File lib/rspec/helpers.rb, line 9
def disable_external_services(except = [])
  before :all do
    ExternalServices::RSpec::Disabler.disable_external_services except: except
  end

  after :all do
    ExternalServices::RSpec::Disabler.enable_external_services
  end
end