Module: ExternalServices::RSpec::Helpers::ClassMethods

Defined in:
lib/rspec/helpers.rb

Instance Method Summary collapse

Instance Method Details

#describe_external_service_api(object:, api_name:, **kwargs, &blk) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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_attributes! 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 = []) ⇒ Object



9
10
11
12
13
14
15
16
17
# 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