Ruby on Rails Pundit current_user is zero in the integration test

I use pundit and devise . I have a delete link, which appears only if you are an administrator. I have an integration test that I would like to verify that the delete link is displayed only for administrators.

 test 'comment delete link shows when it should' do log_in_as @admin get movie_path(@movie) assert_select 'a[href=?]', movie_comment_path(comments(:one), @movie.id) end 

My test_helper.rb looks like this:

 ... class ActiveSupport::TestCase ... def log_in_as(user, options = {}) password = options[:password] || 'password' if integration_test? post user_session_path, 'user[email]' => user.email, 'user[password]' => user.password else Devise::TestHelpers.sign_in user end end private # Returns true inside an integration test. def integration_test? defined?(post_via_redirect) end end 

response.body looks right, but there really is no deletion link. There is one when I start the development server and visit the page myself. I narrowed it down to current_user , which uses pundit in politics, with a value of nil . This is my comment_policy.rb :

 class CommentPolicy attr_reader :current_user, :comment def initialize(current_user, model) @current_user = current_user @comment = model end def create? if @current_user @current_user.member? or @current_user.content_creator? or @current_user.moderator? or @current_user.admin? end end def destroy? if @current_user @current_user == @comment.user or @current_user.moderator? or @current_user.admin? end end end 

As a final note, I heard that Rails 5 chose integration tests instead of controller tests, since we know them from Rails 4 for the default test types that will be created for our controllers. If this is the case, then using Rails 5 devise it would be useful to use a lot more if the sign_in / sign_out who work in the controller tests also work with integration tests, But do I still have a question about pundit , not knowing what current_user ? I assume that all this works fine in controller tests, because current_user bound to controllers? Any and all easy shedding on this topic is very appreciated, but I would really like to find out how to get integration tests to work with this installation, because I have about a billion that I want to write right now.

+6
source share
1 answer

Not that this is important, but whether to use current_user in a policy or just use a user in a policy. By that, I mean that according to the elabs / pundit README on Github, I would simply use @user and user everywhere instead of current_user . Read README if I embarrassed you.

Also, nil for current_user usually occurs when you don't have a valid CSRF token for your request. When you do this on a website manually by going to localhost:3000 or w / e, you first get in the login path before making a message in the login path with your credentials. In your integration test, I don't seem to see where you are executing this get to get the CSRF for your session.

Hope this helps !!!

+1
source

All Articles