Skip to content

Instantly share code, notes, and snippets.

@zishe
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zishe/9735713 to your computer and use it in GitHub Desktop.
Save zishe/9735713 to your computer and use it in GitHub Desktop.
ApplicationController spec (rspec syntax)
class ApplicationController < ActionController::Base
protected
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def signed_in?
!!current_user
end
helper_method :current_user, :signed_in?
def current_user=(user)
@current_user = user
session[:user_id] = user.try(:id).try(:to_s)
end
def authenticate_user!
render nothing: true, status: :unauthorized unless current_user
end
end
describe ApplicationController do
describe "current_user" do
context "when current_user already assigned" do
let(:user) { create(:user) }
before { subject.send(:current_user=, user) }
it "returns it without calling db" do
expect(User).to_not receive(:find)
expect(subject.send(:current_user)).to eq user
end
end
context "when current_user is nil" do
context "and user_id stores in session" do
let!(:user) { create(:user) }
before { controller.session[:user_id] = user.id.to_s }
it "finds and returns it from db" do
expect(User).to receive(:find).and_return(user)
expect(subject.send(:current_user)).to eq user
end
end
context "and user_id doesn't exists in session" do
before { controller.session[:user_id] = nil }
it "returns nil" do
expect(User).to_not receive(:find)
expect(subject.send(:current_user)).to eq nil
end
end
end
end
describe "signed_in?" do
context "when user logged in" do
before { subject.send(:current_user=, create(:user)) }
it "returns true" do
expect(subject.send(:signed_in?)).to eq true
end
end
context "when user not logged in" do
before { subject.send(:current_user=, nil) }
it "returns false" do
expect(subject.send(:signed_in?)).to eq false
end
end
end
describe "current_user=" do
let(:user) { create(:user) }
before { subject.send(:current_user=, user) }
context "when assigns user" do
it "assign current_user to user" do
expect(subject.send(:current_user)).to eq user
end
it "saves user_id in session" do
expect(session[:user_id]).to eq(user.id.to_s)
end
end
context "when assigns nil" do
before { subject.send(:current_user=, nil) }
it "reset current_user" do
expect(subject.send(:current_user)).to eq nil
end
it "remove user_id from session" do
expect(session[:user_id]).to eq nil
end
end
end
describe "authenticate_user!" do
context "when user logged in" do
before { subject.send(:current_user=, create(:user)) }
it "do nothing" do
expect(subject.send(:authenticate_user!)).to eq nil
end
end
context "when user not logged in" do
controller(ApplicationController) do
before_action :authenticate_user!
def custom
render text: 'response'
end
end
before do
subject.send(:current_user=, nil)
routes.draw { get 'custom' => 'anonymous#custom' }
get 'custom'
end
it { should respond_with :unauthorized }
it "returns nothing" do
expect(response.body).to be_blank
end
end
end
end
ApplicationController
current_user
when current_user already assigned
returns it without calling db
when current_user is nil
and user_id stores in session
finds and returns it from db
and user_id doesn't exists in session
returns nil
signed_in?
when user logged in
returns true
when user not logged in
returns false
current_user=
when assigns user
assign current_user to user
saves user_id in session
when assigns nil
reset current_user
remove user_id from session
authenticate_user!
when user logged in
do nothing
when user not logged in
should respond with 401
returns nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment