Skip to content

Instantly share code, notes, and snippets.

@Ross-Hunter
Last active January 8, 2018 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ross-Hunter/a14ff663d79b42dfec5d2a7c3b94c4b0 to your computer and use it in GitHub Desktop.
Save Ross-Hunter/a14ff663d79b42dfec5d2a7c3b94c4b0 to your computer and use it in GitHub Desktop.
Request Spec Helpers
module RequestSpecHelpers
def authenticate user
params = { email: user.email, password: 'test_password' }
post api_v2_auth_path, params: params
# You will need to modify this line based on your auth scheme
# This code works with token based auth
@auth_token = json_body.dig :meta, :'auth-token'
end
def authed_get endpoint, opts = {}
get endpoint, params: opts, headers: auth_header
end
def authed_post endpoint, opts = {}
post endpoint, params: jsonapify(opts), headers: auth_header
end
def authed_patch endpoint, opts = {}
patch endpoint, params: jsonapify(opts), headers: auth_header
end
def authed_delete endpoint, opts = {}
delete endpoint, params: opts, headers: auth_header
end
private
def auth_header
{ 'Authorization' => @auth_token,
'CONTENT_TYPE' => 'application/vnd.api+json' }
end
private
def jsonapify opts
data = opts.slice(:type, :id, :attributes, :relationships)
data = data.deep_transform_keys { |key| key.to_s.dasherize.to_sym }
{ data: data }.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment