captcha_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe 'email confirmation flow when captcha is enabled' do
  4. let(:user) { Fabricate(:user, confirmed_at: nil, confirmation_token: 'foobar', created_by_application: client_app) }
  5. let(:client_app) { nil }
  6. before do
  7. # rubocop:disable RSpec/AnyInstance -- easiest way to deal with that that I know of
  8. allow_any_instance_of(Auth::ConfirmationsController).to receive(:captcha_enabled?).and_return(true)
  9. allow_any_instance_of(Auth::ConfirmationsController).to receive(:check_captcha!).and_return(true)
  10. allow_any_instance_of(Auth::ConfirmationsController).to receive(:render_captcha).and_return(nil)
  11. # rubocop:enable RSpec/AnyInstance
  12. end
  13. context 'when the user signed up through an app' do
  14. let(:client_app) { Fabricate(:application) }
  15. it 'logs in' do
  16. visit "/auth/confirmation?confirmation_token=#{user.confirmation_token}&redirect_to_app=true"
  17. # It presents the user with a captcha form
  18. expect(page).to have_title(I18n.t('auth.captcha_confirmation.title'))
  19. # It does not confirm the user just yet
  20. expect(user.reload.confirmed?).to be false
  21. # It redirects to app and confirms user
  22. click_button I18n.t('challenge.confirm')
  23. expect(user.reload.confirmed?).to be true
  24. expect(page).to have_current_path(/\A#{client_app.confirmation_redirect_uri}/, url: true)
  25. # Browsers will generally reload the original page upon redirection
  26. # to external handlers, so test this as well
  27. visit "/auth/confirmation?confirmation_token=#{user.confirmation_token}&redirect_to_app=true"
  28. # It presents a page with a link to the app callback
  29. expect(page).to have_content(I18n.t('auth.confirmations.registration_complete', domain: 'cb6e6126.ngrok.io'))
  30. expect(page).to have_link(I18n.t('auth.confirmations.clicking_this_link'), href: client_app.confirmation_redirect_uri)
  31. end
  32. end
  33. end