2016-08-30 2 views
1

Ich verwende Auth0 für die Authentifizierung in meiner Rails App. Ich muss einige Feature-Tests für die Anmeldung und Anmeldung schreiben. Ich kann anscheinend nichts Konkretes finden, wie man das mit Rspec und Capybara macht.Test Auth0 Anmeldung mit RSpec in Rails

Versucht, etwas nach den in diesem gist erläuterten Linien zu tun, aber es funktioniert immer noch nicht. Wenn jemand Erfahrung mit rspec Feature-Tests mit Auth0 hatte, würde ich mich freuen, wenn Sie mich in die richtige Richtung führen würden.

Danke!

Meine Konfiguration

# in spec/support/omniauth_macros.rb 
module OmniauthMacros 
    def mock_auth_hash 
    # The mock_auth configuration allows you to set per-provider (or default) 
    # authentication hashes to return during integration testing. 
    OmniAuth.config.mock_auth[:auth0] = { 
     'provider' => 'auth0', 
     'uid' => '123545', 
     'user_info' => { 
     'name' => 'mockuser', 
     'image' => 'mock_user_thumbnail_url' 
     }, 
     'credentials' => { 
     'token' => 'mock_token', 
     'secret' => 'mock_secret' 
     } 
    } 
    end 
end 

# in spec/requests/spec_helper.rb 
RSpec.configure do |config| 
    # ... 
    # include our macro 
    config.include(OmniauthMacros) 
end 

OmniAuth.config.test_mode = true 

Da ist in meinem spec Ich habe

scenario 'Should successfully login user' do 
     visit login_path 

     mock_auth_hash 
     click_link "Sign in" 

     expect(page).to have_content('Signed in successfully') 
     expect(page).to have_link("Logout", href: logout_path) 
    end 
+0

Verwenden Sie die gleichen Schritte wie im Text, aber verwenden Sie 'OmniAuth.config.mock_auth [: auth0]'. Wie geschrieben steht diese Frage aus einer Reihe von Gründen außerhalb des Themas - Sie fragen nach einem Tutorial oder einer Off-Site-Referenz, und diese Frage ist sehr offen und kann nicht autoritativ beantwortet werden. – max

Antwort

0

Dies ist, wie ich es

# in spec/support/omniauth_macros.rb 
module OmniauthMacros 
    def mock_valid_auth_hash(user) 
    # The mock_auth configuration allows you to set per-provider (or default) 
    # authentication hashes to return during integration testing. 
    opts = { 
     "provider": user.provider, 
     "uid": user.uid, 
     "info": { 
      "email": user.email, 
      "first_name": user.first_name, 
      "last_name": user.last_name, 
     }, 
     "credentials": { 
      "token": "XKLjnkKJj7hkHKJkk", 
      "expires": true, 
      "id_token": "eyJ0eXAiOiJK1VveHkwaTFBNXdTek41dXAiL.Wz8bwniRJLQ4Fqx_omnGDCX1vrhHjzw", 
      "token_type": "Bearer" 
     } 
    } 
    OmniAuth.config.mock_auth[:auth0] = OmniAuth::AuthHash.new(opts) 
    end 

    def mock_invalid_auth_hash 
    OmniAuth.config.mock_auth[:auth0] = :invalid_credentials 
    end 
end 

# in spec/support/features/session_helpers.rb 
module Features 
    module SessionHelpers 

    def log_in(user, invalid=false, strategy = :auth0) 
     invalid ? mock_invalid_auth_hash : mock_valid_auth_hash(user) 
     Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[strategy.to_sym] 
     visit "/auth/#{strategy.to_s}/callback?code=vihipkGaumc5IVgs" 
    end 
    end 
end 

# in spec/support/rails_helper.rb 
RSpec.configure do |config| 
    # ... 
    # include our macro 
    config.include(OmniauthMacros) 

    # include Session helpers 
    config.include Features::SessionHelpers, type: :feature 
end 

gelöst Und schließlich Login sieht aus wie

meiner Feature-Test zu simulieren
# user signin feature test 

require 'rails_helper' 

feature 'Login with Omniauth' do 
    given(:user) { create(:user) } 

    context "With valid credentials" do 
    scenario 'Should successfully login user' do 
     log_in(user) 

     expect(page).to have_content("successfully signed in") 
     expect(page).to have_link("Logout", href: logout_path) 
    end 
    end 

    context "With invalid credentials" do 
    scenario "Should not log in user" do 
     log_in(user, true) 
     expect(page).to have_content('invalid_credentials') 
    end 
    end 
end 

Hoffe, das hilft!