2
0

Extract User::Confirmation concern (#35582)

This commit is contained in:
Matt Jankowski
2025-07-30 06:33:57 -04:00
committed by GitHub
parent 15b72591d4
commit 6dc55a2f4e
4 changed files with 138 additions and 97 deletions

View File

@@ -10,6 +10,7 @@ RSpec.describe User do
let(:account) { Fabricate(:account, username: 'alice') }
it_behaves_like 'two_factor_backupable'
it_behaves_like 'User::Confirmation'
describe 'otp_secret' do
it 'encrypts the saved value' do
@@ -65,14 +66,6 @@ RSpec.describe User do
end
end
describe 'confirmed' do
it 'returns an array of users who are confirmed' do
Fabricate(:user, confirmed_at: nil)
confirmed_user = Fabricate(:user, confirmed_at: Time.zone.now)
expect(described_class.confirmed).to contain_exactly(confirmed_user)
end
end
describe 'signed_in_recently' do
it 'returns a relation of users who have signed in during the recent period' do
recent_sign_in_user = Fabricate(:user, current_sign_in_at: within_duration_window_days.ago)
@@ -228,79 +221,6 @@ RSpec.describe User do
end
end
describe '#confirmed?' do
it 'returns true when a confirmed_at is set' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc)
expect(user.confirmed?).to be true
end
it 'returns false if a confirmed_at is nil' do
user = Fabricate.build(:user, confirmed_at: nil)
expect(user.confirmed?).to be false
end
end
describe '#confirm' do
subject { user.confirm }
let(:new_email) { 'new-email@example.com' }
before do
allow(TriggerWebhookWorker).to receive(:perform_async)
end
context 'when the user is already confirmed' do
let!(:user) { Fabricate(:user, confirmed_at: Time.now.utc, approved: true, unconfirmed_email: new_email) }
it 'sets email to unconfirmed_email and does not trigger web hook' do
expect { subject }.to change { user.reload.email }.to(new_email)
expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
end
end
context 'when the user is a new user' do
let(:user) { Fabricate(:user, confirmed_at: nil, unconfirmed_email: new_email) }
context 'when the user is already approved' do
before do
Setting.registrations_mode = 'approved'
user.approve!
end
it 'sets email to unconfirmed_email and triggers `account.approved` web hook' do
expect { subject }.to change { user.reload.email }.to(new_email)
expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
end
end
context 'when the user does not require explicit approval' do
before do
Setting.registrations_mode = 'open'
end
it 'sets email to unconfirmed_email and triggers `account.approved` web hook' do
expect { subject }.to change { user.reload.email }.to(new_email)
expect(TriggerWebhookWorker).to have_received(:perform_async).with('account.approved', 'Account', user.account_id).once
end
end
context 'when the user requires explicit approval but is not approved' do
before do
Setting.registrations_mode = 'approved'
end
it 'sets email to unconfirmed_email and does not trigger web hook' do
expect { subject }.to change { user.reload.email }.to(new_email)
expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
end
end
end
end
describe '#approve!' do
subject { user.approve! }