Sha256: 37305bc18829b4af632c840e1df1f816e3691d431a3cac979dcd0f75bae94e61

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

require "spec_helper"

class PagesController < ApplicationController
  include Clearance::Controller
  before_action :require_login, only: :private

  # A page requiring user authentication
  def private
    head :ok
  end

  # A page that does not require user authentication
  def public
    head :ok
  end
end

describe "Authentication cookies in the response" do
  before do
    draw_test_routes
    create_user_and_sign_in
  end

  after do
    Rails.application.reload_routes!
  end

  it "are not present if the request does not authenticate" do
    get public_path

    expect(headers["Set-Cookie"]).to be_nil
  end

  it "are present if the request does authenticate" do
    get private_path

    expect(headers["Set-Cookie"]).to match(/remember_token=/)
  end

  def draw_test_routes
    Rails.application.routes.draw do
      get "/private" => "pages#private", as: :private
      get "/public" => "pages#public", as: :public
      resource :session, controller: "clearance/sessions", only: [:create]
    end
  end

  def create_user_and_sign_in
    user = create(:user, password: "password")

    post session_path, params: {
      session: { email: user.email, password: "password" },
    }
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
clearance-2.9.2 spec/requests/authentication_cookie_spec.rb
clearance-2.9.1 spec/requests/authentication_cookie_spec.rb
clearance-2.9.0 spec/requests/authentication_cookie_spec.rb
clearance-2.8.0 spec/requests/authentication_cookie_spec.rb
clearance-2.7.2 spec/requests/authentication_cookie_spec.rb
clearance-2.7.0 spec/requests/authentication_cookie_spec.rb
clearance-2.6.2 spec/requests/authentication_cookie_spec.rb
clearance-2.6.1 spec/requests/authentication_cookie_spec.rb
clearance-2.6.0 spec/requests/authentication_cookie_spec.rb
clearance-2.5.0 spec/requests/authentication_cookie_spec.rb
clearance-2.4.0 spec/requests/authentication_cookie_spec.rb
clearance-2.3.1 spec/requests/authentication_cookie_spec.rb
clearance-2.3.0 spec/requests/authentication_cookie_spec.rb
clearance-2.2.1 spec/requests/authentication_cookie_spec.rb
clearance-2.2.0 spec/requests/authentication_cookie_spec.rb
clearance-2.1.0 spec/requests/authentication_cookie_spec.rb