2016-08-30 69 views
1

我在我的rails應用程序中使用Auth0進行身份驗證。我需要爲登錄和註冊編寫一些功能測試。我似乎無法找到具體的如何與rspec和水豚做到這一點。在Rails中使用RSpec測試Auth0登錄

嘗試按照此gist中所述的方法進行操作,但仍無效。如果有人在使用Auth0進行rspec功能測試方面有經驗,我會很感激你是否會指引我朝着正確的方向發展。

謝謝!

我的配置

# 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 

然後在我的天賦我有

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

使用與要點相同的步驟,但使用'OmniAuth.config.mock_auth [:auth0]'。正如所寫的那樣,這個問題因爲多種原因而成爲主題 - 您正在尋求教程或非現場參考,而且這個問題非常開放,無法以權威的方式回答。 – max

回答

0

這是我如何解決它

# 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 

最後我的功能測試,以模擬登錄的樣子

# 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 

希望這有助於!