Skip to content

Instantly share code, notes, and snippets.

@sirramongabriel
Created January 19, 2020 20:03
Show Gist options
  • Save sirramongabriel/67ba2db89729b9e1859c278e78a5dd38 to your computer and use it in GitHub Desktop.
Save sirramongabriel/67ba2db89729b9e1859c278e78a5dd38 to your computer and use it in GitHub Desktop.
Apologies, I haven't cleaned this up after my pair a few min ago.
require "rails_helper"
require "spec_helper"
describe "API authentication" , :type => :request do
let!(:user) { Fabricate(:user) }
context "when faliure" do
it "has missing or invalid credentials" do
post "/api/v1/sessions"#, :format => :json
status = '401'
error = 'invalid_credentials'
expect(response.body).to include(status)
expect(response.body).to include(error)
end
end
context "when success" do
# it "contains valid credintials" do
#
# post "/api/v1/sessions"#, { email: 'test@example.com', password: '123456', password_confirmation: '123456' }
# binding.pry
# end
context "the following headers are present" do
it "Accepts JSON data" do
post "/api/v1/sessions"#, {email: 'test@example.com', password: '123456'}
json_header = response.headers
expect(json_header['Content-Type']).to eq('application/json; charset=utf-8')
end
it "has application/x-www-form-urlencoded content-type" do
post "/api/v1/sessions"
expect(request.env['CONTENT_TYPE']).to eql("application/x-www-form-urlencoded")
end
it "returns status code 200" do
# request.headers.env['HTTP_USER_EMAIL'], request.headers.env['HTTP_USER_PASSWORD']
# request.headers.env['HTTP_USER_EMAIL'] = user.email
# request.headers.env['HTTP_USER_PASSWORD'] = user.password
headers = {
env: {
"HTTP_ENV": {
"HTTP_USER_EMAIL" => user.email, # This is what Rails 4 accepts
"HTTP_USER_PASSWORD" => user.password # This is what Rails 3 accepts
}
}
}
post "/api/v1/sessions", params: {}, headers: headers
# binding.pry
expect(response).to have_http_status(200)
# expect(response.body).to eq(200)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment