Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
Added more omniauth_callbacks_controller specs
Browse files Browse the repository at this point in the history
  • Loading branch information
LBRapid committed Jan 24, 2012
1 parent 3395730 commit d5fbc33
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
9 changes: 5 additions & 4 deletions app/controllers/spree/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ def #{provider}
authentication = Spree::UserAuthentication.find_by_provider_and_uid(auth_hash['provider'], auth_hash['uid'])
if authentication
if !authentication.nil?
flash[:notice] = "Signed in successfully"
sign_in_and_redirect :user, authentication.user
elsif current_user
current_user.user_authentications.create!(:provider => auth_hash['provider'], :uid => auth_hash['uid'])
flash[:notice] = "Authentication successful."
redirect_to account_path
redirect_back_or_default(account_url)
else
user = Spree::User.new
user.apply_omniauth(auth_hash)
if user.save
flash[:notice] = "Signed in successfully."
sign_in_and_redirect :user, user
else
session[:omniauth] = auth_hash.except('extra')
redirect_to spree.new_user_registration_url
session[:omniauth] = auth_hash
redirect_to new_user_registration_url
end
end
if current_order
user = current_user if current_user
current_order.associate_user!(user)
session[:guest_token] = nil
end
Expand Down
83 changes: 81 additions & 2 deletions spec/controllers/spree/omniauth_callbacks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe Spree::OmniauthCallbacksController do
let(:user) { Factory(:user) }
let(:omni_params) { mock("omni", :[] => nil) }

let(:omni_params) { mock("omni", :[] => nil).as_null_object }
let(:order) { mock_model(Spree::Order, :associate_user => nil) }

before(:each) do
request.env["omniauth.auth"] = omni_params
Expand Down Expand Up @@ -31,6 +31,21 @@
end
end

shared_examples_for "associate_order" do
before { controller.stub :current_order => order }

it "should associate the order with the user" do
order.should_receive(:associate_user!).with(user)
controller.twitter
end
end

shared_examples_for "authenticate_as_user" do
it "should authenticate as that user" do
controller.should_receive(:sign_in_and_redirect).with(user, :event => :authentication)
end
end

context "#callback" do
context "when user is authenticated" do
before { controller.stub :current_user => user }
Expand Down Expand Up @@ -59,6 +74,70 @@

context "when no existing user_authentication" do
before { Spree::UserAuthentication.stub :find_by_provider_and_uid => nil }

it "should create a new user_authentication" do
user.user_authentications.should_receive(:create!)
controller.twitter
end

it "should set the flash notice" do
controller.twitter
flash[:notice].should_not be_blank
end

it "should redirect properly" do
controller.should_receive(:redirect_back_or_default)
controller.twitter
end

it_should_behave_like "associate_order"
end
end

context "when user is not authenticated" do
before { controller.stub :current_user => nil }

it_should_behave_like "denied_permissions"

context "when existing user_authentication" do
let(:user_authentication) { mock("user_authentication", :user => user) }
before { Spree::UserAuthentication.stub :find_by_provider_and_uid => user_authentication }

it "should not need to create the user_authentication" do
user.user_authentications.should_not_receive(:create!)
controller.twitter
end

it "should not create a new user account" do
Spree::User.should_not_receive :new
controller.twitter
end

it "should authenticate as that user" do
controller.should_receive(:sign_in_and_redirect).with(:user, user)
controller.twitter
end
end

context "when no existing user_authentication" do
let(:user) { Spree::User.new }
before do
Spree::UserAuthentication.stub :find_by_provider_and_uid => nil
Spree::User.stub(:new).and_return(user)
#controller.auth_hash.should_receive :except
end

it "should create a new user" do
pending "Need to resolve routes issue"
Spree::User.should_receive(:new)
controller.twitter
end

it "should apply_omniauth params to the new user" do
pending "Need to resolve routes issue"
#user.should_receive(:apply_omniauth).with(controller.auth_hash)
#controller.twitter
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

config.include Spree::Core::Engine.routes.url_helpers, :type => :controller
end

0 comments on commit d5fbc33

Please sign in to comment.