Guest User

Untitled

a guest
Feb 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. it 'should authenticate for admin' do
  2. basic_auth('user', 'secret')
  3. visit '/admin'
  4. response.status.should eql 200
  5. response.status.should_not eql 401
  6. end
  7.  
  8. if page.driver.respond_to?(:basic_auth)
  9. page.driver.basic_auth(name, password)
  10. elsif page.driver.respond_to?(:basic_authorize)
  11. page.driver.basic_authorize(name, password)
  12. elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
  13. page.driver.browser.basic_authorize(name, password)
  14. else
  15. raise "I don't know how to log in!"
  16. end
  17.  
  18. page.driver.browser.authorize 'login', 'password'
  19.  
  20. def basic_auth(user, password)
  21. encoded_login = ["#{user}:#{password}"].pack("m*")
  22. page.driver.header 'Authorization', "Basic #{encoded_login}"
  23. end
  24.  
  25. Given /^I am logged in as "([^"]*)" with "([^"]*)"$/ do |username, password|
  26. authorize username, password
  27. end
  28.  
  29. Given I am logged in as "admin" with "password"
  30.  
  31. RSpec.shared_context "When authenticated" do
  32. before do
  33. username = 'yourusername'
  34. password = 'yourpassword'
  35. visit "http://#{username}:#{password}@#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}/"
  36. end
  37. end
  38.  
  39. feature "Your feature", js: true do
  40. include_context "When authenticated"
  41.  
  42. # Your test code here...
  43. end
  44.  
  45. Given /^I am logged in$/ do
  46. if page.driver.respond_to?(:basic_authorize)
  47. page.driver.basic_authorize('admin', 'password')
  48. else
  49. # 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
  50. page.driver.visit('/')
  51. page.driver.visit("http://admin:password@#{page.driver.current_url.gsub(/^http:///, '')}")
  52. end
  53. end
  54.  
  55. RSpec.shared_context 'When authenticated' do
  56. background do
  57. authenticate
  58. end
  59.  
  60. def authenticate
  61. if page.driver.browser.respond_to?(:authorize)
  62. # When headless
  63. page.driver.browser.authorize(username, password)
  64. else
  65. # When javascript test
  66. visit "http://#{username}:#{password}@#{host}:#{port}/"
  67. end
  68. end
  69.  
  70. def username
  71. # Your value here. Replace with string or config location
  72. Rails.application.secrets.http_auth_username
  73. end
  74.  
  75. def password
  76. # Your value here. Replace with string or config location
  77. Rails.application.secrets.http_auth_password
  78. end
  79.  
  80. def host
  81. Capybara.current_session.server.host
  82. end
  83.  
  84. def port
  85. Capybara.current_session.server.port
  86. end
  87. end
  88.  
  89. feature 'User does something' do
  90. include_context 'When authenticated'
  91.  
  92. # test examples
  93. end
  94.  
  95. tab = Selenium::WebDriver::Keys::KEYS[:tab]
  96. SESSION.accept_alert(with: username + tab + password)
Add Comment
Please, Sign In to add comment