captcha_spec.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. allow(Auth::ConfirmationsController).to receive(:new).and_return(stubbed_controller)
  8. end
  9. context 'when the user signed up through an app' do
  10. let(:client_app) { Fabricate(:application) }
  11. it 'logs in' do
  12. visit "/auth/confirmation?confirmation_token=#{user.confirmation_token}&redirect_to_app=true"
  13. # It presents the user with a captcha form
  14. expect(page).to have_title(I18n.t('auth.captcha_confirmation.title'))
  15. # It does not confirm the user just yet
  16. expect(user.reload.confirmed?).to be false
  17. # It redirects to app and confirms user
  18. click_button I18n.t('challenge.confirm')
  19. expect(user.reload.confirmed?).to be true
  20. expect(page).to have_current_path(/\A#{client_app.confirmation_redirect_uri}/, url: true)
  21. # Browsers will generally reload the original page upon redirection
  22. # to external handlers, so test this as well
  23. visit "/auth/confirmation?confirmation_token=#{user.confirmation_token}&redirect_to_app=true"
  24. # It presents a page with a link to the app callback
  25. expect(page).to have_content(I18n.t('auth.confirmations.registration_complete', domain: 'cb6e6126.ngrok.io'))
  26. expect(page).to have_link(I18n.t('auth.confirmations.clicking_this_link'), href: client_app.confirmation_redirect_uri)
  27. end
  28. end
  29. private
  30. def stubbed_controller
  31. Auth::ConfirmationsController.new.tap do |controller|
  32. allow(controller).to receive_messages(captcha_enabled?: true, check_captcha!: true, render_captcha: nil)
  33. end
  34. end
  35. end