Module: Spree::Api::TestingSupport::Helpers

Defined in:
lib/spree/api/testing_support/helpers.rb

Instance Method Summary collapse

Instance Method Details

#assert_not_found!Object



16
17
18
19
# File 'lib/spree/api/testing_support/helpers.rb', line 16

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

#assert_unauthorized!Object



21
22
23
24
# File 'lib/spree/api/testing_support/helpers.rb', line 21

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

#current_api_userObject

This method can be overridden (with a let block) inside a context For instance, if you wanted to have an admin user instead.



32
33
34
# File 'lib/spree/api/testing_support/helpers.rb', line 32

def current_api_user
  @current_api_user ||= stub_model(Spree::LegacyUser, email: "[email protected]", spree_roles: [])
end

#image(filename) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spree/api/testing_support/helpers.rb', line 36

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

#json_responseObject



7
8
9
10
11
12
13
14
# File 'lib/spree/api/testing_support/helpers.rb', line 7

def json_response
  case body = JSON.parse(response.body)
  when Hash
    body.with_indifferent_access
  when Array
    body
  end
end

#stub_authentication!Object



26
27
28
# File 'lib/spree/api/testing_support/helpers.rb', line 26

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

#upload_image(filename) ⇒ Object



49
50
51
# File 'lib/spree/api/testing_support/helpers.rb', line 49

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