confirmations_controller_spec.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::ConfirmationsController do
  4. render_views
  5. before do
  6. sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
  7. end
  8. describe 'POST #create' do
  9. it 'confirms the user' do
  10. user = Fabricate(:user, confirmed_at: false)
  11. post :create, params: { account_id: user.account.id }
  12. expect(response).to redirect_to(admin_accounts_path)
  13. expect(user.reload).to be_confirmed
  14. end
  15. it 'raises an error when there is no account' do
  16. post :create, params: { account_id: 'fake' }
  17. expect(response).to have_http_status(404)
  18. end
  19. it 'raises an error when there is no user' do
  20. account = Fabricate(:account, user: nil)
  21. post :create, params: { account_id: account.id }
  22. expect(response).to have_http_status(404)
  23. end
  24. end
  25. describe 'POST #resend' do
  26. subject { post :resend, params: { account_id: user.account.id } }
  27. let!(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
  28. before do
  29. allow(UserMailer).to receive(:confirmation_instructions) { instance_double(ActionMailer::MessageDelivery, deliver_later: nil) }
  30. end
  31. context 'when email is not confirmed' do
  32. let(:confirmed_at) { nil }
  33. it 'resends confirmation mail' do
  34. expect(subject).to redirect_to admin_accounts_path
  35. expect(flash[:notice]).to eq I18n.t('admin.accounts.resend_confirmation.success')
  36. expect(UserMailer).to have_received(:confirmation_instructions).once
  37. end
  38. end
  39. context 'when email is confirmed' do
  40. let(:confirmed_at) { Time.zone.now }
  41. it 'does not resend confirmation mail' do
  42. expect(subject).to redirect_to admin_accounts_path
  43. expect(flash[:error]).to eq I18n.t('admin.accounts.resend_confirmation.already_confirmed')
  44. expect(UserMailer).to_not have_received(:confirmation_instructions)
  45. end
  46. end
  47. end
  48. end