Extract User::Confirmation concern (#35582)
This commit is contained in:
114
spec/support/examples/models/concerns/user/confirmation.rb
Normal file
114
spec/support/examples/models/concerns/user/confirmation.rb
Normal file
@@ -0,0 +1,114 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.shared_examples 'User::Confirmation' do
|
||||
describe 'Scopes' do
|
||||
let!(:unconfirmed_user) { Fabricate :user, confirmed_at: nil }
|
||||
let!(:confirmed_user) { Fabricate :user, confirmed_at: Time.now.utc }
|
||||
|
||||
describe '.confirmed' do
|
||||
it 'returns users who are confirmed' do
|
||||
expect(described_class.confirmed)
|
||||
.to contain_exactly(confirmed_user)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.unconfirmed' do
|
||||
it 'returns users who are not confirmed' do
|
||||
expect(described_class.unconfirmed)
|
||||
.to contain_exactly(unconfirmed_user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#confirmed?' do
|
||||
subject { Fabricate.build(:user, confirmed_at:) }
|
||||
|
||||
context 'when confirmed_at is set' do
|
||||
let(:confirmed_at) { Time.now.utc }
|
||||
|
||||
it { is_expected.to be_confirmed }
|
||||
end
|
||||
|
||||
context 'when confirmed_at is not set' do
|
||||
let(:confirmed_at) { nil }
|
||||
|
||||
it { is_expected.to_not be_confirmed }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#unconfirmed?' do
|
||||
subject { Fabricate.build(:user, confirmed_at:) }
|
||||
|
||||
context 'when confirmed_at is set' do
|
||||
let(:confirmed_at) { Time.now.utc }
|
||||
|
||||
it { is_expected.to_not be_unconfirmed }
|
||||
end
|
||||
|
||||
context 'when confirmed_at is not set' do
|
||||
let(:confirmed_at) { nil }
|
||||
|
||||
it { is_expected.to be_unconfirmed }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#confirm' do
|
||||
subject { user.confirm }
|
||||
|
||||
let(:new_email) { 'new-email@host.example' }
|
||||
|
||||
before { allow(TriggerWebhookWorker).to receive(:perform_async) }
|
||||
|
||||
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 does not require explicit approval' do
|
||||
before { Setting.registrations_mode = 'open' }
|
||||
|
||||
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 registrations mode is approved' do
|
||||
before { Setting.registrations_mode = 'approved' }
|
||||
|
||||
context 'when the user is already approved' do
|
||||
before { user.approve! }
|
||||
|
||||
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 is not approved' do
|
||||
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
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user