Sha256: 8eff1010860ce527c4374d8e1eef306a2b5464ef68fdbfacbe68827d469c8e6a

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'webmock/rspec'

module Shark
  module RSpec
    module FakeConsentService
      class Request
        include Singleton

        def self.setup
          instance = self.instance
          instance.stub_requests
        end

        def stub_requests
          WebMock.stub_request(:post, %r{^#{host}/consents}).to_return do |request|
            log_info "[Shark][ConsentService] Faking POST request with body: #{request.body}"

            payload_data = JSON.parse(request.body)['data']
            object_data = ObjectCache.instance.add(payload_data)

            SharkSpec.fake_response(200, data: object_data)
          end

          WebMock.stub_request(:get, %r{^#{host}/consents/.+}).to_return do |request|
            log_info '[Shark][ConsentService] Faking GET request'

            id = request.uri.path.split('/')[2]

            object_data = ObjectCache.instance.objects.detect do |object|
              object['id'] == id
            end

            if object_data.present?
              SharkSpec.fake_response(200, data: object_data)
            else
              SharkSpec.fake_response(404, errors: [])
            end
          end
        end

        def host
          Shark.configuration.consent_service.site
        end

        def log_info(message)
          Shark.logger.info message
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bima-shark-sdk-2.3.1 lib/shark/rspec/fake_consent_service/request.rb
bima-shark-sdk-2.3.0 lib/shark/rspec/fake_consent_service/request.rb