Last active
January 8, 2018 22:28
-
-
Save Ross-Hunter/a14ff663d79b42dfec5d2a7c3b94c4b0 to your computer and use it in GitHub Desktop.
Request Spec Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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