Add coverage for `User` validations (#33028)

pull/2910/head
Matt Jankowski 2024-11-25 03:18:10 -05:00 committed by GitHub
parent fd90f04f0e
commit ab9c62e8c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 11 deletions

View File

@ -33,14 +33,12 @@ RSpec.describe User do
end
end
describe 'validations' do
describe 'Associations' do
it { is_expected.to belong_to(:account).required }
end
it 'is invalid without a valid email' do
user = Fabricate.build(:user, email: 'john@')
user.valid?
expect(user).to model_have_error_on_field(:email)
end
describe 'Validations' do
it { is_expected.to_not allow_value('john@').for(:email) }
it 'is valid with an invalid e-mail that has already been saved' do
user = Fabricate.build(:user, email: 'invalid-email')
@ -48,11 +46,7 @@ RSpec.describe User do
expect(user.valid?).to be true
end
it 'is valid with a localhost e-mail address' do
user = Fabricate.build(:user, email: 'admin@localhost')
user.valid?
expect(user.valid?).to be true
end
it { is_expected.to allow_value('admin@localhost').for(:email) }
end
describe 'Normalizations' do
@ -183,6 +177,39 @@ RSpec.describe User do
end
end
describe '#update_sign_in!' do
context 'with an existing user' do
let!(:user) { Fabricate :user, last_sign_in_at: 10.days.ago, current_sign_in_at: 1.hour.ago, sign_in_count: 123 }
context 'with new sign in false' do
it 'updates timestamps but not counts' do
expect { user.update_sign_in!(new_sign_in: false) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and not_change(user, :sign_in_count)
end
end
context 'with new sign in true' do
it 'updates timestamps and counts' do
expect { user.update_sign_in!(new_sign_in: true) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and change(user, :sign_in_count).by(1)
end
end
end
context 'with a new user' do
let(:user) { Fabricate.build :user }
it 'does not persist the user' do
expect { user.update_sign_in! }
.to_not change(user, :persisted?).from(false)
end
end
end
describe '#confirmed?' do
it 'returns true when a confirmed_at is set' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc)