Karan
Karan

Reputation: 15104

Trying to get Cucumber working with OmniAuth

I have been pulling my hair out because of this.

My cucumber step clicks on a login for facebook. I have mocked omniauth by following the following article:

http://pivotallabs.com/users/mgehard/blog/articles/1595-testing-omniauth-based-login-via-cucumber

My omniauth_callbacks_controller.rb has the following code:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def my_logger
    @@my_logger = Logger.new("#{Rails.root}/log/my.log")
  end


  def facebook
    @user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

I however, get the following error:

When I follow "facebook_login_button"            # features/step_definitions/basic.rb:14
      undefined method `extra' for #<Hash:0x007fda6d7cd950> (NoMethodError)
      ./app/models/user.rb:13:in `find_for_facebook_oauth'
      ./app/controllers/users/omniauth_callbacks_controller.rb:8:in `facebook'
      (eval):2:in `click_link'
      ./features/step_definitions/basic.rb:15:in `/^(?:|I )follow "([^"]*)"$/'
      features/homepage.feature:30:in `When I follow "facebook_login_button"'

Other articles I have read: Devise 1.5 + Omniauth 1.0 + Facebook: undefined method `extra` - problem: this is mocking out omniauth using rspec I think - not sure if it can be applied for cucumber

https://github.com/intridea/omniauth/issues/558 --post by benjamintanweihao works - but its hacking the code to work differently with tests - the git branches suggested dont work either

EDIT: my model/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable, :confirmable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  devise :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
           data = access_token.extra.raw_info
            if user = User.where(:email => data.email).first
                user
            else 
                User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
            end
  end
end

Upvotes: 0

Views: 678

Answers (1)

cantonic
cantonic

Reputation: 1033

This hapens due do this issue: https://github.com/intridea/omniauth/issues/558 It is not your fault, it is a small bug in omniauth. you can use methods like access_token.extra in production and development mode, but in order to make it work in test mode you should change it to access_token["extra"]

Upvotes: 1

Related Questions