2012-06-09 15 views
5

Ecco il mio caratteristica:test Devise + Omniauth (Facebook) con cetrioli

Scenario: Professor is not signed up and tries to sign in with Facebook 
     Given I do not exist as a professor 
     When I sign in as a professor with Facebook 
     Then I should see a successful sign in message 
     When I return to the site 
     Then I should be signed in as a professor 

Ecco la definizione passo per When I sign in as a professor with Facebook

When /^I sign in as a professor with Facebook$/ do 
    set_omniauth 
    visit "/professors/auth/facebook" 
end 

E qui è la definizione di set_omniauth helper:

def set_omniauth(opts = {}) 
    default = {:provider => :facebook, 
      :uuid  => "1234", 
      :facebook => { 
          :email => "[email protected]", 
          } 
      } 

    credentials = default.merge(opts) 
    provider = credentials[:provider] 
    user_hash = credentials[provider] 

    OmniAuth.config.test_mode = true 

    OmniAuth.config.mock_auth[provider] = { 
    'uid' => credentials[:uuid], 
    "extra" => { 
    "user_hash" => { 
     "email" => user_hash[:email], 
     } 
    } 
    } 
end 

Quindi ... Quando visito /professors/auth/facebook, questa azione si chiama

def facebook 
    @professor = Professor.find_for_facebook_oauth(request.env["omniauth.auth"], current_professor) 
    if @professor.persisted? 
     flash[:notice] = "Welcome! You have signed up successfully." 
     sign_in_and_redirect @professor, :event => :authentication 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_professor_registration_url 
    end 
    end 

E, infine, il metodo find_for_facebook_oauth definizione è:

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token["extra"]["raw_info"] 
    if professor = self.find_by_email(data.email) 
     professor 
    else # Create a professor with a stub password. 
     self.create(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end 
    end 

Quando si esegue questa funzione, ho la seguente messaggio di errore:

undefined method `email' for {"email"=>"[email protected]"}:Hash (NoMethodError) 

Così, ho controllato ciò che Facebook in realtà ritorna:

#<Hashie::Mash email="[email protected]" ... 

ma questo è un oggetto diverso da un normale set di hash in:

OmniAuth.config.mock_auth[provider] = { 
    'uid' => credentials[:uuid], 
    "extra" => { 
    "user_hash" => { 
     "email" => user_hash[:email], 
     } 
    } 

Quindi, la mia domanda è: Come dovrei provare questo correttamente ? Ho seguito OmniAuth Integration Tetsing e hanno impostato un hash e non un hashie.

risposta

2

È necessario creare il Hash utilizzando una costruito nel metodo di OmniAuth per la creazione di un oggetto Hashie:

OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new({ 
'uid' => credentials[:uuid], 
"extra" => { 
"user_hash" => { 
    "email" => user_hash[:email], 
    } 
}) 
Problemi correlati