Sha256: db13dfc5f271a589aae6634cc93692a361a26e203fca3c8979f5bc3e1a838d80

Contents?: true

Size: 1.55 KB

Versions: 46

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Spree
  module Api
    module TestingSupport
      module Helpers
        def json_response
          case body = JSON.parse(response.body)
          when Hash
            body.with_indifferent_access
          when Array
            body
          end
        end

        def assert_not_found!
          expect(json_response).to eq({ "error" => "The resource you were looking for could not be found." })
          expect(response.status).to eq 404
        end

        def assert_unauthorized!
          expect(json_response).to eq({ "error" => "You are not authorized to perform that action." })
          expect(response.status).to eq 401
        end

        def stub_authentication!
          allow(Spree.user_class).to receive(:find_by).with(hash_including(:spree_api_key)) { current_api_user }
        end

        # This method can be overridden (with a let block) inside a context
        # For instance, if you wanted to have an admin user instead.
        def current_api_user
          @current_api_user ||= stub_model(Spree::LegacyUser, email: "solidus@example.com", spree_roles: [])
        end

        def image(filename)
          File.open(
            File.join(
              Spree::Core::Engine.root,
              "lib",
              "spree",
              "testing_support",
              "fixtures",
              filename
            )
          )
        end

        def upload_image(filename)
          Rack::Test::UploadedFile.new(File.open(image(filename).path), 'image/jpeg')
        end
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
solidus_api-4.4.1 lib/spree/api/testing_support/helpers.rb
solidus_api-4.4.0 lib/spree/api/testing_support/helpers.rb
solidus_api-4.3.4 lib/spree/api/testing_support/helpers.rb
solidus_api-4.2.4 lib/spree/api/testing_support/helpers.rb
solidus_api-4.1.5 lib/spree/api/testing_support/helpers.rb
solidus_api-4.3.3 lib/spree/api/testing_support/helpers.rb
solidus_api-4.3.2 lib/spree/api/testing_support/helpers.rb
solidus_api-4.1.4 lib/spree/api/testing_support/helpers.rb
solidus_api-4.3.1 lib/spree/api/testing_support/helpers.rb
solidus_api-4.3.0 lib/spree/api/testing_support/helpers.rb
solidus_api-4.2.3 lib/spree/api/testing_support/helpers.rb
solidus_api-4.1.3 lib/spree/api/testing_support/helpers.rb
solidus_api-4.0.4 lib/spree/api/testing_support/helpers.rb
solidus_api-3.4.6 lib/spree/api/testing_support/helpers.rb
solidus_api-4.0.3 lib/spree/api/testing_support/helpers.rb
solidus_api-4.1.2 lib/spree/api/testing_support/helpers.rb
solidus_api-4.2.2 lib/spree/api/testing_support/helpers.rb
solidus_api-3.4.5 lib/spree/api/testing_support/helpers.rb
solidus_api-4.2.1 lib/spree/api/testing_support/helpers.rb
solidus_api-4.2.0 lib/spree/api/testing_support/helpers.rb