Skip to content

Commit

Permalink
add specs of session controller
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Jul 13, 2013
1 parent 874ef64 commit edbe41a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class RootController < ApplicationController
# GET /
def index
if @current_user
if params[:tag].nil?
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SessionsController < ApplicationController
# GET /auth/github/callback
def create
auth = request.env["omniauth.auth"]
user = User.find_by_uid(auth[:uid])
Expand All @@ -11,6 +12,7 @@ def create
redirect_to root_path, notice: "Sign in was successful."
end

# GET /logout
def destroy
session[:user] = nil
redirect_to root_path
Expand Down
52 changes: 48 additions & 4 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,64 @@ def valid_session
{user: user.id}
end

describe "GET create" do
context "with registered user" do
before do
User.any_instance.should_receive(:update_with_omniauth).and_return(true)
request.env["omniauth.auth"] = {uid: user.uid}
get :create
end

it "return http redirect" do
expect(response).to be_redirect
end

it "should redirect to root_path" do
expect(response).to redirect_to(root_path)
end

it "session should be created" do
expect(session[:user]).to eq(user.id)
end
end

context "with not registered user" do
before do
User.should_receive(:find_by_uid).and_return(nil)
User.should_receive(:create_with_omniauth).and_return(user)
request.env["omniauth.auth"] = {uid: "1"}
get :create
end

it "return http redirect" do
expect(response).to be_redirect
end

it "should redirect to root_path" do
expect(response).to redirect_to(root_path)
end

it "session should be created" do
expect(session[:user]).to eq(user.id)
end
end
end

describe "GET destroy" do
it "returns http redirect" do
before do
session[:user] = user.id
get :destroy, {}, valid_session
end

it "returns http redirect" do
expect(response).to be_redirect
end

it "should redirect to root_path" do
get :destroy, {}, valid_session
expect(response).to redirect_to(root_path)
end

it "should clear session" do
session[:user] = user.id
get :destroy, {}, valid_session
expect(session[:user]).to be_nil
end
end
Expand Down

0 comments on commit edbe41a

Please sign in to comment.