Created
February 10, 2011 20:40
-
-
Save sevos/821291 to your computer and use it in GitHub Desktop.
Spec file for authentications controller used in OmniAuth integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create_table "authentications", :force => true do |t| | |
# t.integer "user_id" | |
# t.string "provider" | |
# t.string "uid" | |
# t.datetime "created_at" | |
# t.datetime "updated_at" | |
# end | |
class Authentication < ActiveRecord::Base | |
belongs_to :user | |
validates :uid, :provider, :presence => true | |
attr_accessor :raw | |
def email | |
self.raw["user_info"]["email"] | |
rescue | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
development: | |
facebook: | |
- '55a66eefce926c2eb1412507bc04f2787' | |
- '2f64ae3613398a553ec2460da9f75a2b' | |
- scope: 'email' | |
twitter: | |
- 'j7WltZaDVaNcGB8n28Da' | |
- 'Rs0zOf1yUaybGsAMW77Ahc2x11KaZWfX2q7ohU' | |
test: | |
facebook: | |
- 'test' | |
- 'test' | |
production: | |
facebook: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Authentication do | |
describe :email do | |
context "for facebook" do | |
before { @auth = Authentication.new(:provider => 'facebook') } | |
it "from raw" do | |
@auth.raw = {'user_info' => {'email' => '[email protected]'}} | |
@auth.email.should == '[email protected]' | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthenticationsController < ApplicationController | |
before_filter :authenticate_user!, :only => :destroy | |
def create | |
omniauth = request.env['omniauth.auth'] | |
authentication = Authentication.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) | |
if current_user | |
if authentication && authentication.try(:user) != current_user | |
flash[:error] = I18n.t("This %{provider} account is already connected to another account in our service", :provider => authentication.provider) | |
elsif authentication.nil? | |
current_user.authentications.create!(:provider => omniauth["provider"], :uid => omniauth["uid"]) | |
end | |
redirect_to edit_user_registration_path(current_user) | |
else # user logged out | |
if authentication # sign in user | |
sign_in_and_redirect :user, authentication.user | |
else # create new user | |
user = User.new.tap {|user| user.apply_authentication(omniauth) } | |
if user.save | |
sign_in_and_redirect :user, user | |
else | |
session["omniauth"] = omniauth | |
redirect_to new_user_registration_path | |
end | |
end | |
end | |
end | |
def destroy | |
@authentication = current_user.authentications.find(params[:id]) | |
@authentication.destroy | |
redirect_to :back | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe AuthenticationsController do | |
before { @user = Factory(:user) } | |
describe "POST / from facebook" do | |
before do | |
@omniauth = { | |
'uid' => "12345", | |
'provider' => "facebook" | |
} | |
request.env["omniauth.auth"] = @omniauth | |
end | |
context "user logged in" do | |
before do | |
sign_in @user | |
end | |
context "having no authentications" do | |
it "should create authentication " do | |
post :create | |
@user.reload.should have(1).authentication | |
end | |
it "should redirect to user's profile" do | |
post :create | |
response.should redirect_to(edit_user_registration_path(@user)) | |
end | |
end | |
context "having facebook authentication" do | |
before { @user.authentications.create!(:provider => "facebook", :uid => "12345")} | |
it "should not create authentication " do | |
post :create | |
@user.reload.should have(1).authentication | |
end | |
it "should redirect to user's profile" do | |
post :create | |
response.should redirect_to(edit_user_registration_path(@user)) | |
end | |
end | |
context "facebook authentication connected to another account" do | |
before do | |
@another_user = Factory(:user) | |
@another_user.authentications.create!(:provider => "facebook", :uid => "12345") | |
end | |
it "should disallow to connect accounts" do | |
post :create | |
@user.reload.should have(0).authentications | |
flash[:error].should == "This facebook account is already connected to another account in our service" | |
response.should redirect_to(edit_user_registration_path(@user)) | |
end | |
end | |
end | |
context "user logged out" do | |
context "user has attached authentication", "and logging in" do | |
before { @user.authentications.create!(:provider => "facebook", :uid => "12345") } | |
it "should sign in user" do | |
post :create | |
controller.send(:current_user).should == @user | |
end | |
it "should redirect" do | |
post :create | |
response.should be_redirect | |
end | |
end | |
end | |
context "no matching user" do | |
context "no extra credentials given" do | |
before do | |
@user = User.new | |
@user.stub!(:save => false) | |
User.stub!(:new => @user) | |
end | |
it "should apply authentication" do | |
@user.should_receive(:apply_authentication).with(request.env["omniauth.auth"]) | |
post :create | |
end | |
it "should save authentication to session" do | |
post :create | |
session[:omniauth].should == @omniauth | |
end | |
it "should redirect to new registration path" do | |
post :create | |
response.should redirect_to(new_user_registration_path) | |
end | |
end | |
context "facebook credentials given" do | |
before { request.env["omniauth.auth"]["user_info"] = {"email" => "[email protected]"} } | |
it "should create user" do | |
-> { post :create }.should change(User, :count).by(1) | |
end | |
it "should sign in created user" do | |
post :create | |
controller.send(:current_user).should_not be_nil | |
end | |
it "should redirect" do | |
post :create | |
response.should be_redirect | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file_name = File.join(File.dirname(__FILE__), "..", "authentication_services.yml") | |
OMNIAUTH_KEYS = YAML.load(ERB.new(File.new(file_name).read).result)[Rails.env].freeze | |
Rails.application.config.middleware.use OmniAuth::Builder do | |
OMNIAUTH_KEYS.each do |prov, config| | |
provider prov, *config | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment