security_keys_setup_examples.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'security keys setup', authenticated_as: :authenticate do
  3. let(:password_check) { true }
  4. let(:current_user) { agent }
  5. def authenticate
  6. Setting.set('two_factor_authentication_method_security_keys', true)
  7. current_user
  8. end
  9. before do
  10. skip('Mocking of Web Authentication API is currently supported only in Chrome.') if Capybara.current_driver != :zammad_chrome
  11. end
  12. it 'sets up security keys method' do
  13. setup_security_keys_method(user: current_user, password_check: password_check)
  14. end
  15. end
  16. def mock_security_key
  17. options = Selenium::WebDriver::VirtualAuthenticatorOptions.new(protocol: :u2f, transport: :usb, resident_key: false,
  18. user_consenting: true, user_verification: true,
  19. user_verified: true)
  20. page.driver.browser.add_virtual_authenticator(options)
  21. end
  22. def setup_security_keys_method(user:, password_check:)
  23. in_modal do
  24. if password_check
  25. expect(page).to have_text('Set up two-factor authentication: Confirm Password')
  26. fill_in 'Password', with: user.password_plain
  27. click_on 'Next'
  28. end
  29. expect(page).to have_text('Set up two-factor authentication: Security Keys')
  30. click_on 'Set Up'
  31. expect(page).to have_text('Set up two-factor authentication: Security Key')
  32. fill_in 'Name for this security key', with: Faker::Lorem.unique.word
  33. mock_security_key
  34. click_on 'Next'
  35. expect(page).to have_text('Set up two-factor authentication: Save Codes')
  36. click_on "OK, I've saved my recovery codes"
  37. end
  38. expect(page).to have_no_css('.modal')
  39. expect(user.reload.two_factor_configured?).to be(true)
  40. end