CapybaraのHTTP基本認証
-
29-09-2019 - |
質問
Rails 3アプリケーションのRSPECテストをいくつか書いており、から切り替えようとしています ウェブラット に カピバラ. 。これまでのところ非常に良いですが、アプリケーションはHTTP Basic Authを使用して管理者ユーザーを承認します。Capybaraでそれをどのようにテストできるか考えてみませんか?
これが私の現在のウェブラットのステップです:
it 'should authenticate for admin' do
basic_auth('user', 'secret')
visit '/admin'
response.status.should eql 200
response.status.should_not eql 401
end
カピバラでこれを行うにはどうすればよいですか?ありがとう!
解決
私はそれを使って動作させました page.driver.basic_authorize(name, password)
代わりは
アップデート:
現時点では、Capybaraのアップグレードの後、この回避策を使用しています。
if page.driver.respond_to?(:basic_auth)
page.driver.basic_auth(name, password)
elsif page.driver.respond_to?(:basic_authorize)
page.driver.basic_authorize(name, password)
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
page.driver.browser.basic_authorize(name, password)
else
raise "I don't know how to log in!"
end
他のヒント
デフォルトのCapybaraドライバーであるラックテストには basic_authorize
メソッド(エイリアス付き authorize
)基本的なhttp auth、および digest_authorize
Digest HTTP AUTHの場合、ここでそれらを見つけることができます: https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb
だからあなたはすることができます:
page.driver.browser.authorize 'login', 'password'
または、基本的なHTTP認証のための簡単なヘルパーを書くことができます。
def basic_auth(user, password)
encoded_login = ["#{user}:#{password}"].pack("m*")
page.driver.header 'Authorization', "Basic #{encoded_login}"
end
のどれも page.driver.*
ソリューションは私のために働きました。私はセレンではなくポルターガイストを使用しているので、それはそれと関係があるかもしれません。これがうまくいったことです:
RSpec.shared_context "When authenticated" do
before do
username = 'yourusername'
password = 'yourpassword'
visit "http://#{username}:#{password}@#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}/"
end
end
次に、あなたの仕様で:
feature "Your feature", js: true do
include_context "When authenticated"
# Your test code here...
end
これは、最近のバージョンのキュウリレールで変化しました(私は1.0.2を使用しています)。
キュウリレールはデフォルトでラック/テストドライバーを使用するため、それを変更していない場合は、次の指示が機能します。
feature/step_definitions/authorize.rbを作成する
Given /^I am logged in as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
authorize username, password
end
これを使用することができます。
Given I am logged in as "admin" with "password"
私はこの恐ろしいハックをしなければなりませんでした。
Given /^I am logged in$/ do
if page.driver.respond_to?(:basic_authorize)
page.driver.basic_authorize('admin', 'password')
else
# FIXME for this to work you need to add pref("network.http.phishy-userpass-length", 255); to /Applications/Firefox.app/Contents/MacOS/defaults/pref/firefox.js
page.driver.visit('/')
page.driver.visit("http://admin:password@#{page.driver.current_url.gsub(/^http\:\/\//, '')}")
end
end
男、これらのソリューションのどれも私のために働いていませんでした。
Pistosのソリューションが近くに来て、機能仕様のために働いていました js: true
しかし、頭のないときに失敗しました。
これ以下のソリューションは私のために機能します 両方とも ヘッドレスと js: true
仕様。
spec/support/when_authenticated.rb
RSpec.shared_context 'When authenticated' do
background do
authenticate
end
def authenticate
if page.driver.browser.respond_to?(:authorize)
# When headless
page.driver.browser.authorize(username, password)
else
# When javascript test
visit "http://#{username}:#{password}@#{host}:#{port}/"
end
end
def username
# Your value here. Replace with string or config location
Rails.application.secrets.http_auth_username
end
def password
# Your value here. Replace with string or config location
Rails.application.secrets.http_auth_password
end
def host
Capybara.current_session.server.host
end
def port
Capybara.current_session.server.port
end
end
次に、あなたの仕様で:
feature 'User does something' do
include_context 'When authenticated'
# test examples
end