authenticator_app_setup_examples.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'authenticator app setup' do
  3. let(:password_check) { true }
  4. it 'sets up authenticator app method with recovery codes' do
  5. Setting.set('two_factor_authentication_recovery_codes', true)
  6. setup_authenticator_app_method(user: agent, password_check: password_check, expect_recovery_codes: true)
  7. end
  8. it 'sets up authenticator app method without recovery codes' do
  9. Setting.set('two_factor_authentication_recovery_codes', false)
  10. setup_authenticator_app_method(user: agent, password_check: password_check, expect_recovery_codes: false)
  11. end
  12. end
  13. def setup_authenticator_app_method(user:, password_check:, expect_recovery_codes: false)
  14. if password_check
  15. in_modal do
  16. expect(page).to have_text('Set up two-factor authentication: Confirm Password')
  17. fill_in 'Password', with: password_check
  18. click_on 'Next'
  19. end
  20. end
  21. in_modal do
  22. expect(page).to have_text('Set up two-factor authentication: Authenticator App')
  23. click '.qr-code-canvas'
  24. secret = find('.secret').text
  25. security_code = ROTP::TOTP.new(secret).now
  26. fill_in 'Security Code', with: security_code
  27. click_on 'Set Up'
  28. end
  29. if expect_recovery_codes
  30. in_modal do
  31. stored_codes_amount = user.two_factor_preferences.recovery_codes.configuration[:codes].count
  32. displayed_codes_amount = find('.two-factor-auth code').text.tr("\n", ' ').split.count
  33. expect(page).to have_text('Set up two-factor authentication: Save Codes')
  34. expect(stored_codes_amount).to eq(displayed_codes_amount)
  35. click_on "OK, I've saved my recovery codes"
  36. end
  37. end
  38. expect(page).to have_no_css('.modal')
  39. expect(user.reload.two_factor_configured?).to be(true)
  40. end