confirmations_controller_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Auth::ConfirmationsController do
  4. render_views
  5. describe 'GET #new' do
  6. it 'returns http success' do
  7. request.env['devise.mapping'] = Devise.mappings[:user]
  8. get :new
  9. expect(response).to have_http_status(200)
  10. end
  11. end
  12. describe 'GET #show' do
  13. context 'when user is unconfirmed' do
  14. let!(:user) { Fabricate(:user, confirmation_token: 'foobar', confirmed_at: nil) }
  15. before do
  16. allow(BootstrapTimelineWorker).to receive(:perform_async)
  17. request.env['devise.mapping'] = Devise.mappings[:user]
  18. get :show, params: { confirmation_token: 'foobar' }
  19. end
  20. it 'redirects to login and queues worker' do
  21. expect(response)
  22. .to redirect_to(new_user_session_path)
  23. expect(BootstrapTimelineWorker)
  24. .to have_received(:perform_async).with(user.account_id)
  25. end
  26. end
  27. context 'when user is unconfirmed and unapproved' do
  28. let!(:user) { Fabricate(:user, confirmation_token: 'foobar', confirmed_at: nil, approved: false) }
  29. before do
  30. allow(BootstrapTimelineWorker).to receive(:perform_async)
  31. request.env['devise.mapping'] = Devise.mappings[:user]
  32. get :show, params: { confirmation_token: 'foobar' }
  33. end
  34. it 'redirects to login and confirms user' do
  35. expect(response).to redirect_to(new_user_session_path)
  36. expect(user.reload.confirmed_at).to_not be_nil
  37. end
  38. end
  39. context 'when user is already confirmed' do
  40. let!(:user) { Fabricate(:user) }
  41. before do
  42. allow(BootstrapTimelineWorker).to receive(:perform_async)
  43. request.env['devise.mapping'] = Devise.mappings[:user]
  44. sign_in(user, scope: :user)
  45. get :show, params: { confirmation_token: 'foobar' }
  46. end
  47. it 'redirects to root path' do
  48. expect(response).to redirect_to(root_path)
  49. end
  50. end
  51. context 'when user is already confirmed but unapproved' do
  52. let!(:user) { Fabricate(:user, approved: false) }
  53. before do
  54. allow(BootstrapTimelineWorker).to receive(:perform_async)
  55. request.env['devise.mapping'] = Devise.mappings[:user]
  56. user.approved = false
  57. user.save!
  58. sign_in(user, scope: :user)
  59. get :show, params: { confirmation_token: 'foobar' }
  60. end
  61. it 'redirects to settings' do
  62. expect(response).to redirect_to(edit_user_registration_path)
  63. end
  64. end
  65. context 'when user is updating email' do
  66. let!(:user) { Fabricate(:user, confirmation_token: 'foobar', unconfirmed_email: 'new-email@example.com') }
  67. before do
  68. allow(BootstrapTimelineWorker).to receive(:perform_async)
  69. request.env['devise.mapping'] = Devise.mappings[:user]
  70. get :show, params: { confirmation_token: 'foobar' }
  71. end
  72. it 'redirects to login, confirms email, does not queue worker' do
  73. expect(response)
  74. .to redirect_to(new_user_session_path)
  75. expect(user.reload.unconfirmed_email)
  76. .to be_nil
  77. expect(BootstrapTimelineWorker)
  78. .to_not have_received(:perform_async)
  79. end
  80. end
  81. end
  82. end