Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.4 current_user is nil #69

Open
nrosella opened this issue Apr 8, 2016 · 9 comments
Open

5.4 current_user is nil #69

nrosella opened this issue Apr 8, 2016 · 9 comments

Comments

@nrosella
Copy link

nrosella commented Apr 8, 2016

I've been following the tutorial fine so far but I'm getting stuck at 5.4

In my user_controllers_spec.rb tests I'm able to set the request headers to the user's auth token that's provided in the factory.

However, by the time I get into the users_controller.rb I find that

User.find_by(auth_token: request.headers['Authorization']) == nil

and hence I don't have a current_user in my update action.

You can find my latest code here should it help: https://github.com/nrosella/market_place_api

Any help would be great!

Thanks

@kurenn
Copy link
Owner

kurenn commented Apr 8, 2016

You need a before callback for the authenticate method on the ApplicationController

@nrosella
Copy link
Author

nrosella commented Apr 8, 2016

I just added before_action :authenticate_user! and now it's breaking lots of tests.

Lots of them with this error:

1) Api::V1::SessionsController POST #create when the credentials are correct returns the user record corresponding to the given credentials
 Failure/Error: @json_response ||= JSON.parse(response.body, symbolize_names: true)

 JSON::ParserError:
   757: unexpected token at 'You need to sign in or sign up before continuing.'`

@kurenn
Copy link
Owner

kurenn commented Apr 8, 2016

Add it just for the update action:

before_action :authenticate_user!, only: [:update]

@nrosella
Copy link
Author

nrosella commented Apr 8, 2016

Ok back to just the 5 errors now:

Failures:

1) Api::V1::UsersController PUT/PATCH #update when is successfully updated renders the json representation for the updated user
 Failure/Error: @json_response ||= JSON.parse(response.body, symbolize_names: true)

 JSON::ParserError:
   A JSON text must at least contain two octets!
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `initialize'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `new'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # ./spec/support/request_helpers.rb:4:in `json_response'
 # ./spec/controllers/api/v1/users_controller_spec.rb:69:in `block (4 levels) in <top (required)>'

2) Api::V1::UsersController PUT/PATCH #update when is successfully updated should respond with 200
 Failure/Error: it { should respond_with 200 }

 NoMethodError:
   undefined method `response_code' for nil:NilClass
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/action_controller/respond_with_matcher.rb:126:in `response_code'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/action_controller/respond_with_matcher.rb:117:in `correct_status_code?'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/action_controller/respond_with_matcher.rb:99:in `matches?'
 # ./spec/controllers/api/v1/users_controller_spec.rb:73:in `block (4 levels) in <top (required)>'

3) Api::V1::UsersController PUT/PATCH #update when is not created renders an errors json
 Failure/Error: @json_response ||= JSON.parse(response.body, symbolize_names: true)

 JSON::ParserError:
   757: unexpected token at 'You need to sign in or sign up before continuing.'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # ./spec/support/request_helpers.rb:4:in `json_response'
 # ./spec/controllers/api/v1/users_controller_spec.rb:84:in `block (4 levels) in <top (required)>'

4) Api::V1::UsersController PUT/PATCH #update when is not created renders the json errors on whye the user could not be created
 Failure/Error: @json_response ||= JSON.parse(response.body, symbolize_names: true)

 JSON::ParserError:
   757: unexpected token at 'You need to sign in or sign up before continuing.'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # /Users/Nino/.rvm/gems/ruby-2.2.1/gems/json-1.8.3/lib/json/common.rb:155:in `parse'
 # ./spec/support/request_helpers.rb:4:in `json_response'
 # ./spec/controllers/api/v1/users_controller_spec.rb:89:in `block (4 levels) in <top (required)>'

5) Api::V1::UsersController PUT/PATCH #update when is not created should respond with 422
 Failure/Error: it { should respond_with 422 }
   Expected response to be a 422, but was 401
 # ./spec/controllers/api/v1/users_controller_spec.rb:93:in `block (4 levels) in <top (required)>'

@kurenn
Copy link
Owner

kurenn commented Apr 8, 2016

It seems that the controller is not rendering the json object

@nrosella
Copy link
Author

nrosella commented Apr 9, 2016

Yeah. Been doing a little digging and response.body in users_controller_spec.rb is equal to ""

I found some entries on Stack Overflow that said to include render_views in the test file, but that hasn't done anything for me. Completely stumped on this one...

@kurenn
Copy link
Owner

kurenn commented Apr 11, 2016

How are you rendering the controller json response?

@Orinameh
Copy link

My test fails and I get the errors..

  1. Api::V1::UsersController PUT/PATCH #update when is not created should respond with 422
    Failure/Error: it { should respond_with 422 }
    Expected response to be a 422, but was 401

    ./spec/controllers/api/v1/users_controller_spec.rb:101:in `block (4 levels) in <top (required)>'

  2. Api::V1::UsersController PUT/PATCH #update when is not created renders the json errors on when the user could not be created
    Failure/Error: expect(user_response[:errors][:email]).to include "is invalid"

 TypeError:
   no implicit conversion of Symbol into Integer
 # ./spec/controllers/api/v1/users_controller_spec.rb:98:in `[]'
 # ./spec/controllers/api/v1/users_controller_spec.rb:98:in `block (4 levels) in <top (required)>'
  1. Api::V1::UsersController PUT/PATCH #update when is successfully updated should respond with 200
    Failure/Error: it { should respond_with 200 }
    Expected response to be a 200, but was 401

    ./spec/controllers/api/v1/users_controller_spec.rb:81:in `block (4 levels) in <top (required)>'

  2. Api::V1::UsersController PUT/PATCH #update when is successfully updated renders the json representation for the updated user
    Failure/Error: expect(user_response[:email]).to eql "[email protected]"

   expected: "[email protected]"
        got: nil

   (compared using eql?)
 # ./spec/controllers/api/v1/users_controller_spec.rb:78:in `block (4 levels) in <top (required)>'

Finished in 0.22121 seconds (files took 1.66 seconds to load)
13 examples, 4 failures

Failed examples:

rspec ./spec/controllers/api/v1/users_controller_spec.rb:101 # Api::V1::UsersController PUT/PATCH #update when is not created should respond with 422
rspec ./spec/controllers/api/v1/users_controller_spec.rb:96 # Api::V1::UsersController PUT/PATCH #update when is not created renders the json errors on when the user could not be created
rspec ./spec/controllers/api/v1/users_controller_spec.rb:81 # Api::V1::UsersController PUT/PATCH #update when is successfully updated should respond with 200
rspec ./spec/controllers/api/v1/users_controller_spec.rb:76 # Api::V1::UsersController PUT/PATCH #update when is successfully updated renders the json representation for the updated user

But once I remove before_action :authenticate_with_token!, only: [:update, :destroy] from the Users Controller, the test passes. What could be the cause?

@kurenn
Copy link
Owner

kurenn commented Dec 24, 2016

Can you share the controller's code along with the tests and routes please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants