summaryrefslogtreecommitdiff
path: root/spec/models/user_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/user_spec.rb')
-rw-r--r--spec/models/user_spec.rb125
1 files changed, 62 insertions, 63 deletions
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index dd4b174a38f..fa2e4b63648 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper'
-describe User do
+RSpec.describe User do
include ProjectForksHelper
include TermsHelper
include ExclusiveLeaseHelpers
@@ -58,6 +58,10 @@ describe User do
it { is_expected.to delegate_method(:job_title).to(:user_detail).allow_nil }
it { is_expected.to delegate_method(:job_title=).to(:user_detail).with_arguments(:args).allow_nil }
+
+ it { is_expected.to delegate_method(:bio).to(:user_detail).allow_nil }
+ it { is_expected.to delegate_method(:bio=).to(:user_detail).with_arguments(:args).allow_nil }
+ it { is_expected.to delegate_method(:bio_html).to(:user_detail).allow_nil }
end
describe 'associations' do
@@ -91,64 +95,28 @@ describe User do
it { is_expected.to have_many(:metrics_users_starred_dashboards).inverse_of(:user) }
it { is_expected.to have_many(:reviews).inverse_of(:author) }
- describe "#bio" do
- it 'syncs bio with `user_details.bio` on create' do
- user = create(:user, bio: 'my bio')
-
- expect(user.bio).to eq(user.user_detail.bio)
+ describe "#user_detail" do
+ it 'does not persist `user_detail` by default' do
+ expect(create(:user).user_detail).not_to be_persisted
end
- context 'when `migrate_bio_to_user_details` feature flag is off' do
- before do
- stub_feature_flags(migrate_bio_to_user_details: false)
- end
-
- it 'does not sync bio with `user_details.bio`' do
- user = create(:user, bio: 'my bio')
+ it 'creates `user_detail` when `bio` is given' do
+ user = create(:user, bio: 'my bio')
- expect(user.bio).to eq('my bio')
- expect(user.user_detail.bio).to eq('')
- end
+ expect(user.user_detail).to be_persisted
+ expect(user.user_detail.bio).to eq('my bio')
end
- it 'syncs bio with `user_details.bio` on update' do
- user = create(:user)
-
- user.update!(bio: 'my bio')
+ it 'delegates `bio` to `user_detail`' do
+ user = create(:user, bio: 'my bio')
expect(user.bio).to eq(user.user_detail.bio)
end
- context 'when `user_details` association already exists' do
- let(:user) { create(:user) }
-
- before do
- create(:user_detail, user: user)
- end
-
- it 'syncs bio with `user_details.bio`' do
- user.update!(bio: 'my bio')
-
- expect(user.bio).to eq(user.user_detail.bio)
- end
-
- it 'falls back to "" when nil is given' do
- user.update!(bio: nil)
-
- expect(user.bio).to eq(nil)
- expect(user.user_detail.bio).to eq('')
- end
-
- # very unlikely scenario
- it 'truncates long bio when syncing to user_details' do
- invalid_bio = 'a' * 256
- truncated_bio = 'a' * 255
-
- user.bio = invalid_bio
- user.save(validate: false)
+ it 'creates `user_detail` when `bio` is first updated' do
+ user = create(:user)
- expect(user.user_detail.bio).to eq(truncated_bio)
- end
+ expect { user.update(bio: 'my bio') }.to change { user.user_detail.persisted? }.from(false).to(true)
end
end
@@ -214,7 +182,7 @@ describe User do
describe 'validations' do
describe 'password' do
- let!(:user) { create(:user) }
+ let!(:user) { build_stubbed(:user) }
before do
allow(Devise).to receive(:password_length).and_return(8..128)
@@ -337,8 +305,6 @@ describe User do
it { is_expected.not_to allow_value(-1).for(:projects_limit) }
it { is_expected.not_to allow_value(Gitlab::Database::MAX_INT_VALUE + 1).for(:projects_limit) }
- it { is_expected.to validate_length_of(:bio).is_at_most(255) }
-
it_behaves_like 'an object with email-formated attributes', :email do
subject { build(:user) }
end
@@ -3745,6 +3711,12 @@ describe User do
expect(user.namespace).not_to be_nil
end
+
+ it 'creates the namespace setting' do
+ user.save!
+
+ expect(user.namespace.namespace_settings).to be_persisted
+ end
end
context 'for an existing user' do
@@ -4634,7 +4606,8 @@ describe User do
[
{ state: 'blocked' },
{ user_type: :ghost },
- { user_type: :alert_bot }
+ { user_type: :alert_bot },
+ { user_type: :support_bot }
]
end
@@ -4688,6 +4661,7 @@ describe User do
where(:user_type, :expected_result) do
'human' | true
'alert_bot' | false
+ 'support_bot' | false
end
with_them do
@@ -4756,19 +4730,44 @@ describe User do
end
end
- describe '#migration_bot' do
- it 'creates the user if it does not exist' do
- expect do
- described_class.migration_bot
- end.to change { User.where(user_type: :migration_bot).count }.by(1)
+ context 'bot users' do
+ shared_examples 'bot users' do |bot_type|
+ it 'creates the user if it does not exist' do
+ expect do
+ described_class.public_send(bot_type)
+ end.to change { User.where(user_type: bot_type).count }.by(1)
+ end
+
+ it 'creates a route for the namespace of the created user' do
+ bot_user = described_class.public_send(bot_type)
+
+ expect(bot_user.namespace.route).to be_present
+ end
+
+ it 'does not create a new user if it already exists' do
+ described_class.public_send(bot_type)
+
+ expect do
+ described_class.public_send(bot_type)
+ end.not_to change { User.count }
+ end
end
- it 'does not create a new user if it already exists' do
- described_class.migration_bot
+ shared_examples 'bot user avatars' do |bot_type, avatar_filename|
+ it 'sets the custom avatar for the created bot' do
+ bot_user = described_class.public_send(bot_type)
- expect do
- described_class.migration_bot
- end.not_to change { User.count }
+ expect(bot_user.avatar.url).to be_present
+ expect(bot_user.avatar.filename).to eq(avatar_filename)
+ end
end
+
+ it_behaves_like 'bot users', :alert_bot
+ it_behaves_like 'bot users', :support_bot
+ it_behaves_like 'bot users', :migration_bot
+ it_behaves_like 'bot users', :ghost
+
+ it_behaves_like 'bot user avatars', :alert_bot, 'alert-bot.png'
+ it_behaves_like 'bot user avatars', :support_bot, 'support-bot.png'
end
end