Skip to content

Instantly share code, notes, and snippets.

@yu123
Forked from jittuu/gist:792715
Created December 23, 2011 05:08
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 yu123/1513209 to your computer and use it in GitHub Desktop.
Save yu123/1513209 to your computer and use it in GitHub Desktop.
Test Omniauth(1.0) Facebook Callback Controllers in Devise(1.5) with rspec
require 'spec_helper'
describe Users::OauthCallbacksController, "handle facebook authentication callback" do
describe "#annonymous user" do
context "when facebook email doesn't exist in the system" do
before(:each) do
stub_env_for_omniauth
get :facebook
@user = User.where(:email => "ghost@nobody.com").first
end
it { @user.should_not be_nil }
it "should create authentication with facebook id" do
authentication = @user.authentications.where(:provider => "facebook", :uid => "1234").first
authentication.should_not be_nil
end
it { should be_user_signed_in }
it { response.should redirect_to tasks_path }
end
context "when facebook email already exist in the system" do
before(:each) do
stub_env_for_omniauth
User.create!(:email => "ghost@nobody.com", :password => "my_secret")
get :facebook
end
it { flash[:notice].should == "Your email ghost@nobody.com is already exist in the system. You need to sign in first."}
it { response.should redirect_to new_user_session_path }
end
end
describe "#logged in user" do
context "when user don't have facebook authentication" do
before(:each) do
stub_env_for_omniauth
user = User.create!(:email => "user@example.com", :password => "my_secret")
sign_in user
get :facebook
end
it "should add facebook authentication to current user" do
user = User.where(:email => "user@example.com").first
user.should_not be_nil
fb_authentication = user.authentications.where(:provider => "facebook").first
fb_authentication.should_not be_nil
fb_authentication.uid.should == "1234"
end
it { should be_user_signed_in }
it { response.should redirect_to authentications_path }
it { flash[:notice].should == "Facebook is connected with your account."}
end
context "when user already connect with facebook" do
before(:each) do
stub_env_for_omniauth
user = User.create!(:email => "ghost@nobody.com", :password => "my_secret")
user.authentications.create!(:provider => "facebook", :uid => "1234")
sign_in user
get :facebook
end
it "should not add new facebook authentication" do
user = User.where(:email => "ghost@nobody.com").first
user.should_not be_nil
fb_authentications = user.authentications.where(:provider => "facebook")
fb_authentications.count.should == 1
end
it { should be_user_signed_in }
it { flash[:notice].should == "Signed in successfully." }
it { response.should redirect_to tasks_path }
end
end
end
def stub_env_for_omniauth
# This a Devise specific thing for functional tests. See https://github.com/plataformatec/devise/issues/closed#issue/608
request.env["devise.mapping"] = Devise.mappings[:user]
pre = { "omniauth.auth" => { "provider" => "facebook", "uid" => "1234", "credentials" => {"token" => "abcdefg"}, "extra"=>{"raw_info" => {"id" => "1234567", "email" => "ghost@nobody.com", "name" => "Mark", "gender" => "male" }}}}
# add require 'omniauth' in your spec_helper.rb
env = OmniAuth::AuthHash.new(pre)
@controller.stub!(:env).and_return(env)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment