Skip to content

Commit

Permalink
#217307 Simple spec for authenticate with facebook.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassus committed May 23, 2012
1 parent 277fcf7 commit 143a71f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
9 changes: 4 additions & 5 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ def build_resource(*args)
super

if session[:omniauth]
omniauth = session[:omniauth]
omniauth = ActiveSupport::HashWithIndifferentAccess.new(session[:omniauth])

@user.email = omniauth['info']['nickname'] if @user.email.blank?
@user.accounts.build do |a|
a.provider = omniauth['provider']
a.uid = omniauth['uid']
a.token = omniauth['credentials']['token']
a.provider = omniauth[:provider]
a.uid = omniauth[:uid]
a.token = omniauth[:credentials][:token]
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/user_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def index
def create
logger.info short_auth_hash.to_yaml

provider = auth_hash['provider']
uid = auth_hash['uid']
provider = auth_hash[:provider]
uid = auth_hash[:uid]

account = UserAccount.find_by_provider_and_uid(provider, uid)
if account.present?
Expand All @@ -19,8 +19,8 @@ def create
else
if current_user.present?
# Connect logged user to the given account
token = auth_hash['credentials']['token']
current_user.connect_to(provider, uid: uid, token: token, auth_response: short_auth_hash)
token = auth_hash[:credentials][:token]
current_user.connect_to(provider, :uid => uid, :token => token, :auth_response => short_auth_hash)

flash[:success] = 'Authentication successful.'
redirect_to user_accounts_path
Expand All @@ -43,11 +43,11 @@ def destroy
private

def auth_hash
request.env['omniauth.auth']
ActiveSupport::HashWithIndifferentAccess.new(request.env['omniauth.auth'])
end

def short_auth_hash
auth_hash.except('extra')
auth_hash.except(:extra)
end

end
4 changes: 2 additions & 2 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h2><%= t('users.sign_in') %></h2>

<div class="row auth-buttons">
<%= link_to t('action.login_via_facebook'), '/auth/facebook', class: 'btn info' %>
<%= link_to t('action.login_via_twitter'), '/auth/twitter', class: 'btn info' %>
<%= link_to t('actions.login_via_facebook'), '/auth/facebook', class: 'btn info' %>
<%= link_to t('actions.login_via_twitter'), '/auth/twitter', class: 'btn info' %>
</div>

<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => { :class => 'form-horizontal' }) do |f| %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
are_you_sure: 'Are you sure?'
actions:
back: 'Back'
login_via_facebook: 'Login via Facebook'
links:
home: 'Home'
posts: 'Blog'
Expand Down
52 changes: 52 additions & 0 deletions spec/requests/user_facebook_sign_up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'

feature 'Sign up via Facebook' do

background do
visit root_path
click_link 'Sign in'

OmniAuth.config.mock_auth[:facebook] = {
:provider => 'facebook',
:uid => '123545',
:credentials => {
:token => 'facebook token'
}
}
end

scenario 'click login via facebook' do
click_link 'Login via Facebook'
page.should have_content('Sign up')

within 'form#new_user' do
fill_in 'Email', :with => 'facebook@user.com'
fill_in 'Password', :with => 'password'
fill_in 'Password confirmation', :with => 'password'
click_button 'Sign up'
end

page.should have_content('Welcome! You have signed up successfully.')
page.should have_content('facebook@user.com')
current_path.should == root_path

user = User.find_by_email('facebook@user.com')
user.should_not be_nil
user.accounts.should have(1).item

facebook_account = user.accounts.last
facebook_account.provider.should == 'facebook'
facebook_account.uid.should == '123545'
facebook_account.token.should == 'facebook token'

click_link 'Sign out'

visit root_path
click_link 'Sign in'
click_link 'Login via Facebook'

page.should have_content('Signed in successfully.')
page.should have_content('facebook@user.com')
end

end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ def format(result)
require 'rspec/autorun'
require 'database_cleaner'
require 'capybara/email/rspec'
require 'vcr'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

I18n.default_locale = I18n.locale = :en

VCR.configure do |c|
c.cassette_library_dir = File.expand_path('spec/vcr_cassettes', Rails.root)
c.hook_into :webmock
end

OmniAuth.config.test_mode = true

RSpec.configure do |config|
config.extend VCR::RSpec::Macros
config.include FactoryGirl::Syntax::Methods
config.include Macros::UserLogin, :type => :request
config.include Macros::Mailer, :type => :request
Expand Down

0 comments on commit 143a71f

Please sign in to comment.