title: Devise + Rspec: not signed in user passing through authenticate_user
body: <p>In my tests, despite not getting user signed in , tests get through <code>before_filter: authenticate_user!</code> to parts of the API it should not have access to.</p>
  
  <p>Adding some print out to my setup:</p>
  
  <pre><code>class ClubsController &lt; ApplicationController
    before_filter :authenticate_user!, :derp, only: [:create]
    before_filter :authenticate_user!,:authorize_user, :derp, only: [:update, :destroy, :admins, :add_admin, :delete_admin]
  
    def derp
      puts "derp: #{user_signed_in?}"
    end
  
    def authorize_user
      user = current_user
      club = Club.find(params[:id])
      head :unauathorized unless ClubAdmin.exists?(club_id: club.id, user_id: user.id)
    end
  </code></pre>
  
  <p>Shows that during tests all <code>derp</code>'s are true, but </p>
  
  <pre><code>def create
      puts "create: #{user_signed_in?}"
      @club = Club.new(params.require(:club).permit(:name, :city, :description))
      @club.creator = current_user
      @club.save
  </code></pre>
  
  <p>prints one <code>create: false</code>.</p>
  
  <p>This issue is probably cause of most fails in my tests.</p>
  
  <p>Tests code:</p>
  
  <pre><code>require 'spec_helper'
  
  describe ClubsController do
    render_views
  
    def prepare_user(authenticated = false, authorized = false, club_id = nil)
      @request.env["devise.mapping"] = Devise.mappings[:user]
  
      if authenticated
        user = FactoryGirl.create(:user)
        sign_in user
        ClubAdmin.create(club_id: club_id, user_id: user.id) if authorized
      end
    end
  
    def prepare_create
      xhr :post, :create, format: :json, club: {
        name: "CreatedClub",
        city: "CreativeCity",
        description: "Sooo creative"
      }
    end
  
    describe "create" do
      context "when the user is not authenticated" do
        before do
          prepare_user
          prepare_create
        end
        puts "cre un #{User.all} "
        it{expect(response.status).to eq(401)}
      end
  
      context "when the user is authenticated" do
        before do
          prepare_user(true, false)
          prepare_create
        end
  
        puts "cre au #{User.all} "
        it {expect(response.status).to eq(201)}
        it {expect(Club.last.name).to eq("CreatedClub")}
        it {expect(Club.last.city).to eq("CreativeCity")}
        it {expect(Club.last.description).to eq("Sooo creative")}
      end
    end
  </code></pre>
  
  <p>To gather it all up: what should I do to prevent signed_in state from propagating between tests or how can make <code>authenticate_user!</code> filter out tests with not signed in user?</p>
  
  <p>The full project in current state can be found <a href="https://github.com/morynicz/Ippon/tree/9046376abccf9be30d303febfe287a8619110ea6" rel="nofollow">here</a> </p>
  
  <p>========================== EDIT =======================</p>
  
  <p>@Max's solution solved almost all the issues besides <code>create</code> method getting accessed when user is not authenticated. This time 'derp log' is returning false</p>
  
  <p>Current state of code <a href="https://github.com/morynicz/Ippon/tree/8f1d9aac46e03f8b244a1066fdd5a8d7fbf4b826" rel="nofollow">here</a></p>