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
[RSpec/Rails] Help setting up feature test with authentication #441
Comments
WHOOPS. I need to log in, not just create a user: before(:all) do
post '/sign-up', credentials: user_params
post '/sign-in', credentials: user_params
@token = JSON.parse(response.body)['user']['token']
@user_id = JSON.parse(response.body)['user']['id']
@user = User.first
def list_params
{
title: 'Example List',
user: @user
}
end
def headers
{
'HTTP_AUTHORIZATION' => "Token token=#{@token}"
}
end
List.create!(list_params)
end I feel like I'm getting closer, but I'm still getting an error: 1) Lists API while authenticated GET /lists lists all lists
Failure/Error: expect(response).to be_success
expected `#<ActionDispatch::TestResponse:0x007ff3597ab940 @mon_owner=nil,
@mon_count=0, @mon_mutex=#<Thread::Mutex:0x007ff3597ab8f0>,
@stream=#<ActionDispatch::Response::Buffer:0x007ff3597ab8a0
@response=#<ActionDispatch::TestResponse:0x007ff3597ab940 ...>,
@buf=["HTTP Token: Access denied.\n"], @closed=false>, @header={"X-Frame-
Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-
Options"=>"nosniff", "WWW-Authenticate"=>"Token realm=\"Application\"", "Content-
Type"=>"text/html; charset=utf-8", "Vary"=>"Origin", "Cache-Control"=>"no-cache", "X-
Request-Id"=>"e31aa2d7-dc6c-4c63-88de-e126417e95a9", "X-Runtime"=>"0.019383",
"Content-Length"=>"27"}, @status=401, @sending_file=false, @blank=false, @cv=#
<MonitorMixin::ConditionVariable:0x007ff3597ab878 @monitor=#
<ActionDispatch::TestResponse:0x007ff3597ab940 ...>, @cond=#
<Thread::ConditionVariable:0x007ff3597ab850>>, @committed=false, @sending=false,
@sent=false, @content_type=#<Mime::Type:0x007ff359967ae0 @synonyms=
["application/xhtml+xml"], @symbol=:html, @string="text/html",
@hash=1837517924790437652>, @charset="utf-8", @cache_control={:no_cache=>true},
@etag=nil>.success?` to return true, got false |
Looks like it's the wrong token.
I'm a bit perplexed. I'm still thinking on this. |
Can you confirm for me that |
If that's not the issue, would you try |
Running |
What about if you look at |
rspec error: Failure/Error: User.find_by(token: @token).id
NoMethodError:
undefined method `id' for nil:NilClass |
FWIW happy to table this for now and schedule a one-on-one later. I'd like to get this working, but it's not essential in order for my project to function as it should tomorrow. |
That indicates there's no user with that token. What is |
Output from running the test: user.id 14
@user_id 14
@user.id 14
user.token 2132533e989408ce403534278eb274d0
current @user.token 2132533e989408ce403534278eb274d0
current @token BAhJIiUyMTMyNTMzZTk4OTQwOGNlNDAzNTM0Mjc4ZWIyNzRkMAY6BkVG--20d3ea0440a32f4586169666c0505c2460de84e8 Also reposting the entirety of my spec, in case there's something I'm missing elsewhere. I know that some of this is redundant; I've been looking at a couple of different examples (the user spec that's part of the template; this rails-api-bdd-iteration repo that Lauren referenced in #225 ). require 'rails_helper'
RSpec.describe 'Lists API' do
def list_params
{
title: 'New Example List',
user: user
}
end
def lists
List.all
end
def list
List.first
end
def user_params
{
email: 'alice@example.com',
password: 'foobarbaz',
password_confirmation: 'foobarbaz'
}
end
def user
User.first
end
before(:all) do
User.create!(user_params)
List.create!(list_params)
end
after(:all) do
List.destroy_all
User.destroy_all
end
context 'while authenticated' do
def headers
{
'HTTP_AUTHORIZATION' => "Token token=#{@token}"
}
end
before(:each) do
# post '/sign-up', credentials: user_params
post '/sign-in', credentials: user_params
@token = JSON.parse(response.body)['user']['token']
@user_id = JSON.parse(response.body)['user']['id']
@user = User.first
puts "user.id #{user.id}"
puts "@user_id #{@user_id}"
puts "@user.id #{@user.id}"
puts "current user.token #{user.token}"
puts "current @user.token #{@user.token}"
puts "current @token #{@token}"
def user_by_token
User.find_by(token: @token).id
end
puts "user_by_token #{user_by_token}"
# List.create!(list_params)
end
describe 'GET /lists' do
it 'lists all lists' do
get '/lists', headers
expect(response).to be_success
lists_response = JSON.parse(response.body)
expect(lists_response.length).to eq(lists.count)
expect(lists_response.first['title']).to eq(list[:title])
end
end
end
end |
This is going to sound weird. Replace the |
|
I think this will solve it. I'm about to go to bed so I just wanted to congratulate you on excellent debugging and also to thank you for reposting the spec. I hope my congratulations aren't premature. |
Commented out this line: @token = JSON.parse(response.body)['user']['token'] And replaced with: @token = @user.token Still getting the same error. Tried Test output from the "puts" statements in both cases looks like this: user.id 17
@user_id 17
@user.id 17
current user.token 5d82f1b3f6d3ff294cb74f2d3e257965
current @user.token 5d82f1b3f6d3ff294cb74f2d3e257965
current @token 5d82f1b3f6d3ff294cb74f2d3e257965
user_by_token 17 |
Sorry @rebekahheacock. That's not what I intended. My fault for trying to answer via mobile and be lazy. Go back to your previous code and try this instead: def headers
{
- 'HTTP_AUTHORIZATION' => "Token token=#{@token}"
+ 'HTTP_AUTHORIZATION' => "Token token=#{@user.token}"
}
end |
Just tried this out—still no dice. |
Closing even though there was no solution. :-( |
@jrhorn424 and I talked about this and finally figured out that I wasn't using the |
Preliminaries:
I'm trying to write a feature test for
GET /lists
, which—when a user is logged in—should return all of the lists they own. Users have a one-to-many relationship with lists.Functional curl script for this request is here:
curl --include --request GET http://localhost:3000/lists \ --header "Authorization: Token token=$TOKEN"
lists#index method is defined here
I'm struggling with the right way to set up my
specs/requests/lists_spec.rb
file so that I can create a user, create a list that belongs to that user (and, I'm guessing, a list that belongs to another user?), and test that my GET request returns that list (and only that list).Here's my current test:
And here's the error I'm getting when I run
bundle exec rspec spec/requests/lists_spec.rb
Any chance I could get a few minutes to talk through an initial approach to this? In case it makes things easier, I installed the
shoulda-matchers
gem and have successfully used it to test my List model.The text was updated successfully, but these errors were encountered: