module ShipwireHacks
  def signature_hash(params)
    { 'X-Shipwire-Signature' => signature(params.to_json) }
  end

  private

  def signature(data)
    Base64.encode64(
      OpenSSL::HMAC.digest(
        OpenSSL::Digest.new('sha256'),
        Spree::SolidusShipwireConfig.secret,
        data
      )
    ).strip
  end
end

RSpec.configure do |config|
  config.include ShipwireHacks, type: :controller
end

module ShipwireHacks
  module StubbingHelpers
    def stub_signature!
      before do
        allow(controller).to receive(:validate_key).and_return true
      end
    end
  end

  module SignatureHelpers
    def signature_hash(params)
      { 'X-Shipwire-Signature' => signature(params.to_json) }
    end

    private

    def signature(data)
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest.new('sha256'),
          Spree::SolidusShipwireConfig.secret,
          data
        )
      ).strip
    end
  end
end

RSpec.configure do |config|
  config.extend  ShipwireHacks::StubbingHelpers,  type: :controller
  config.include ShipwireHacks::SignatureHelpers, type: :controller
end

Add helper to stub signature
Add helper to stub signature

In test the signature can be stubbed using `stub_signature!`
