<commit_before>require 'spec_helper'

describe SessionsController do
  include SessionsHelper
  let!(:user) { User.create!(first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, username: Faker::Internet.user_name, email: Faker::Internet.email, password: Faker::Internet.password) }

  describe "GET #new" do
    it "assigns @user as a new instance of User" do
      get :new
      expect(assigns(:user)).to be_a(User)
    end
  end

  describe "POST #create" do
    context "when valid params are passed" do
      it "assigns @user to the appropriate user" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(assigns(:user).username).to eq(user.username)
      end

      it "sets the session[:user_id] to the logged in user's id" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(session[:user_id]).to eq(user.id)
      end

      it "redirects to the logged in user's profile page" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(response).to redirect_to(user)
      end

      it "changes the logged_in? from false to true" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(logged_in?).to be(true)
      end

      it "ensures that the current_user has the correct e-mail address" do
        post :create, user: { first_name: user.first_name, last_name: user.last_name, username: user.username, email: user.email, password: <PASSWORD>}
        expect(current_user.email).to eq(user.email)
      end
    end

    context "when invalid params are passed" do
      it "assigns 'The username or password is incorrect.' as a flash[:notice]" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(flash[:notice]).to eq("The username or password is incorrect.")
      end

      it "re-renders the login page if the username or password are invalid" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(response).to render_template(:new)
      end

      it "does not assign a user to the current_user" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(current_user).to be(nil)
      end

      it "returns false for the logged_in? method" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(logged_in?).to be(false)
      end
    end
  end


  describe "GET #logout" do
    it "removes the session user_id" do
      get :logout
      expect(session[:user_id]).to be(nil)
    end

    it "redirects to the root route" do
      get :logout
      expect(response).to redirect_to("/")
    end
  end
end<commit_msg>Fix test for logout<commit_after>require 'spec_helper'

describe SessionsController do
  include SessionsHelper
  let!(:user) { User.create!(first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, username: Faker::Internet.user_name, email: Faker::Internet.email, password: <PASSWORD>) }

  describe "GET #new" do
    it "assigns @user as a new instance of User" do
      get :new
      expect(assigns(:user)).to be_a(User)
    end
  end

  describe "POST #create" do
    context "when valid params are passed" do
      it "assigns @user to the appropriate user" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(assigns(:user).username).to eq(user.username)
      end

      it "sets the session[:user_id] to the logged in user's id" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(session[:user_id]).to eq(user.id)
      end

      it "redirects to the logged in user's profile page" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(response).to redirect_to(user)
      end

      it "changes the logged_in? from false to true" do
        post :create, user: { username: user.username, password: <PASSWORD> }
        expect(logged_in?).to be(true)
      end

      it "ensures that the current_user has the correct e-mail address" do
        post :create, user: { first_name: user.first_name, last_name: user.last_name, username: user.username, email: user.email, password: <PASSWORD>}
        expect(current_user.email).to eq(user.email)
      end
    end

    context "when invalid params are passed" do
      it "assigns 'The username or password is incorrect.' as a flash[:notice]" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(flash[:notice]).to eq("The username or password is incorrect.")
      end

      it "re-renders the login page if the username or password are invalid" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(response).to render_template(:new)
      end

      it "does not assign a user to the current_user" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(current_user).to be(nil)
      end

      it "returns false for the logged_in? method" do
        post :create, user: { username: user.username, password: "<PASSWORD>" }
        expect(logged_in?).to be(false)
      end
    end
  end


  describe "GET #logout" do
    it "removes the session user_id" do
      post :create, user: { username: user.username, password: <PASSWORD> }
      get :logout
      expect(session[:user_id]).to be(nil)
    end

    it "redirects to the root route" do
      get :logout
      expect(response).to redirect_to("/")
    end
  end
end
{'fraction_non_alphanumeric': 0.10839532412327312, 'fraction_numerical': 0.0, 'mean_word_length': 2.9964614295824488, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4730099', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1657, 'n_words': 571}