Convert admin/confirmations spec controller->request/system (#37738)

This commit is contained in:
Matt Jankowski
2026-02-05 05:04:10 -05:00
committed by GitHub
parent 8a65965ded
commit e82eb2b037
3 changed files with 110 additions and 64 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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