1
0
mirror of https://github.com/mastodon/mastodon synced 2024-11-27 06:18:17 +09:00

Add coverage for User validations (#33028)

This commit is contained in:
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

View File

@ -33,26 +33,20 @@ RSpec.describe User do
end
end
describe 'validations' do
describe 'Associations' do
it { is_expected.to belong_to(:account).required }
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')
user.save(validate: false)
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)