2012-09-22 16 views
7

Quando un utente passa a /auth/facebook, viene reindirizzato a FB, quindi torna al mio /auth/facebook/callback in caso di esito positivo.Come posso scrivere un Omniauth RSpec per il login?

Come posso scrivere un test RSpec che seguirà tutti questi reindirizzamenti per verificare che il mio utente sia stato autenticato?

risposta

6

Vorrei raccomandare un'alternativa, approccio più semplice. Cosa succede se hai testato il controller di callback direttamente per vedere come reagisce ai diversi valori passati ad esso nel omniauth.auth o se env ["omniauth.auth"] è mancante o errato. Seguire i reindirizzamenti equivarrebbe a testare il plugin omniauth, che non testerà il VOSTRO sistema.

Ad esempio, ecco quello che abbiamo nei nostri test (questo è solo alcuni esempi, ne abbiamo molti altri che verificano altre variazioni dell'hash omniauth e dello stato utente prima del tentativo di accesso, come lo stato di invito, utente conto di essere disabilitato per gli amministratori, etc.):

describe Users::OmniauthCallbacksController do 
    before :each do 
    # This a Devise specific thing for functional tests. See https://github.com/plataformatec/devise/issues/608 
    request.env["devise.mapping"] = Devise.mappings[:user] 
    end 
    describe ".create" do 

    it "should redirect back to sign_up page with an error when omniauth.auth is missing" do 
     @controller.stub!(:env).and_return({"some_other_key" => "some_other_value"}) 
     get :facebook 
     flash[:error].should be 
     flash[:error].should match /Unexpected response from Facebook\./ 
     response.should redirect_to new_user_registration_url 
    end 

    it "should redirect back to sign_up page with an error when provider is missing" do 
     stub_env_for_omniauth(nil) 
     get :facebook 
     flash[:error].should be 
     flash[:error].should match /Unexpected response from Facebook: Provider information is missing/ 
     response.should redirect_to new_user_registration_url 
    end 
    end 
end 

con stub_env_for_omniauth metodo definito come segue:

def stub_env_for_omniauth(provider = "facebook", uid = "1234567", email = "[email protected]", name = "John Doe") 
    env = { "omniauth.auth" => { "provider" => provider, "uid" => uid, "info" => { "email" => email, "name" => name } } } 
    @controller.stub!(:env).and_return(env) 
    env 
end 
+0

non dovrebbe il metodo stub_env_for_omniauth' ritorno OmniAuth :: AuthHash oggetto ', piuttosto che un hash ? – user3021270

Problemi correlati