From e82eb2b0376d0d9a35f1aea4a005a67091ca1f0a Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 5 Feb 2026 05:04:10 -0500 Subject: [PATCH] Convert `admin/confirmations` spec controller->request/system (#37738) --- .../admin/confirmations_controller_spec.rb | 64 ------------------- spec/requests/admin/confirmations_spec.rb | 56 ++++++++++++++++ spec/system/admin/confirmations_spec.rb | 54 ++++++++++++++++ 3 files changed, 110 insertions(+), 64 deletions(-) delete mode 100644 spec/controllers/admin/confirmations_controller_spec.rb create mode 100644 spec/requests/admin/confirmations_spec.rb create mode 100644 spec/system/admin/confirmations_spec.rb diff --git a/spec/controllers/admin/confirmations_controller_spec.rb b/spec/controllers/admin/confirmations_controller_spec.rb deleted file mode 100644 index 22035d15e6..0000000000 --- a/spec/controllers/admin/confirmations_controller_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Admin::ConfirmationsController do - render_views - - before do - sign_in Fabricate(:admin_user), scope: :user - end - - describe 'POST #create' do - it 'confirms the user' do - user = Fabricate(:user, confirmed_at: nil) - post :create, params: { account_id: user.account.id } - - expect(response).to redirect_to(admin_accounts_path) - expect(user.reload).to be_confirmed - end - - it 'raises an error when there is no account' do - post :create, params: { account_id: 'fake' } - - expect(response).to have_http_status(404) - end - - it 'raises an error when there is no user' do - account = Fabricate(:account, user: nil) - post :create, params: { account_id: account.id } - - expect(response).to have_http_status(404) - end - end - - describe 'POST #resend' do - subject { post :resend, params: { account_id: user.account.id } } - - let!(:user) { Fabricate(:user, confirmed_at: confirmed_at) } - - before do - allow(UserMailer).to receive(:confirmation_instructions) { instance_double(ActionMailer::MessageDelivery, deliver_later: nil) } - end - - context 'when email is not confirmed' do - let(:confirmed_at) { nil } - - it 'resends confirmation mail' do - expect(subject).to redirect_to admin_accounts_path - expect(flash[:notice]).to eq I18n.t('admin.accounts.resend_confirmation.success') - expect(UserMailer).to have_received(:confirmation_instructions).once - end - end - - context 'when email is confirmed' do - let(:confirmed_at) { Time.zone.now } - - it 'does not resend confirmation mail' do - expect(subject).to redirect_to admin_accounts_path - expect(flash[:error]).to eq I18n.t('admin.accounts.resend_confirmation.already_confirmed') - expect(UserMailer).to_not have_received(:confirmation_instructions) - end - end - end -end diff --git a/spec/requests/admin/confirmations_spec.rb b/spec/requests/admin/confirmations_spec.rb new file mode 100644 index 0000000000..5f9dbb203f --- /dev/null +++ b/spec/requests/admin/confirmations_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Admin Confirmations' do + before { sign_in Fabricate(:admin_user) } + + describe 'POST /admin/accounts/:account_id/confirmation' do + context 'when account does not exist' do + let(:account_id) { 'fake' } + + it 'raises an error' do + post admin_account_confirmation_path(account_id:) + + expect(response) + .to have_http_status(404) + end + end + + context 'when account does not have a user' do + let(:account) { Fabricate :account, user: nil } + + it 'raises an error' do + post admin_account_confirmation_path(account_id: account.id) + + expect(response) + .to have_http_status(404) + end + end + end + + describe 'POST /admin/accounts/:account_id/confirmation/resend' do + subject { post resend_admin_account_confirmation_path(account_id: user.account.id) } + + let(:user) { Fabricate(:user, confirmed_at: confirmed_at) } + + context 'when email is confirmed' do + let(:confirmed_at) { Time.zone.now } + + it 'does not resend confirmation mail' do + emails = capture_emails { subject } + + expect(emails) + .to be_empty + + expect(response) + .to redirect_to admin_accounts_path + + follow_redirect! + + expect(response.body) + .to include(I18n.t('admin.accounts.resend_confirmation.already_confirmed')) + end + end + end +end diff --git a/spec/system/admin/confirmations_spec.rb b/spec/system/admin/confirmations_spec.rb new file mode 100644 index 0000000000..70821af701 --- /dev/null +++ b/spec/system/admin/confirmations_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Admin Confirmations' do + before { sign_in Fabricate(:admin_user) } + + describe 'Confirming a user' do + let!(:user) { Fabricate :user, confirmed_at: nil } + + it 'changes user to confirmed and returns to accounts page' do + # Go to accounts listing page + visit admin_accounts_path + expect(page) + .to have_title(I18n.t('admin.accounts.title')) + + # Go to account page + click_on user.account.username + + # Click to confirm + expect { click_on I18n.t('admin.accounts.confirm') } + .to change(Admin::ActionLog.where(action: 'confirm'), :count).by(1) + expect(page) + .to have_title(I18n.t('admin.accounts.title')) + expect(user.reload) + .to be_confirmed + end + end + + describe 'Resending a confirmation email', :inline_jobs do + let!(:user) { Fabricate(:user, confirmed_at: confirmed_at) } + + context 'when email is not confirmed' do + let(:confirmed_at) { nil } + + it 'resends the confirmation mail' do + visit admin_account_path(id: user.account.id) + + emails = capture_emails { resend_confirmation } + expect(page) + .to have_title(I18n.t('admin.accounts.title')) + .and have_content(I18n.t('admin.accounts.resend_confirmation.success')) + + expect(emails.first) + .to be_present + .and deliver_to(user.email) + end + end + + def resend_confirmation + click_on I18n.t('admin.accounts.resend_confirmation.send') + end + end +end