Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- it 'should authenticate for admin' do
- basic_auth('user', 'secret')
- visit '/admin'
- response.status.should eql 200
- response.status.should_not eql 401
- end
- 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
- page.driver.browser.authorize 'login', 'password'
- def basic_auth(user, password)
- encoded_login = ["#{user}:#{password}"].pack("m*")
- page.driver.header 'Authorization', "Basic #{encoded_login}"
- end
- Given /^I am logged in as "([^"]*)" with "([^"]*)"$/ do |username, password|
- authorize username, password
- end
- Given I am logged in as "admin" with "password"
- 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
- 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
- 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
- tab = Selenium::WebDriver::Keys::KEYS[:tab]
- SESSION.accept_alert(with: username + tab + password)
Add Comment
Please, Sign In to add comment