login_helpers.rb 638 Bytes
Newer Older
1 2 3 4 5
module LoginHelpers
  # Internal: Create and log in as a user of the specified role
  #
  # role - User role (e.g., :admin, :user)
  def login_as(role)
6 7 8 9
    ActiveRecord::Base.observers.enable(:user_observer) do
      @user = create(role)
    end

10 11 12 13 14 15 16 17
    login_with(@user)
  end

  # Internal: Login as the specified user
  #
  # user - User instance to login with
  def login_with(user)
    visit new_user_session_path
18
    fill_in "user_login", with: user.email
19
    fill_in "user_password", with: "12345678"
20
    click_button "Sign in"
21
    Thread.current[:current_user] = user
22 23 24 25 26 27
  end

  def logout
    click_link "Logout" rescue nil
  end
end