diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /spec/models/user_spec.rb | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'spec/models/user_spec.rb')
-rw-r--r-- | spec/models/user_spec.rb | 221 |
1 files changed, 186 insertions, 35 deletions
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3abf2a651a0..cb34917f073 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -20,6 +20,10 @@ RSpec.describe User do it { is_expected.to include_module(AsyncDeviseEmail) } end + describe 'constants' do + it { expect(described_class::COUNT_CACHE_VALIDITY_PERIOD).to be_a(Integer) } + end + describe 'delegations' do it { is_expected.to delegate_method(:path).to(:namespace).with_prefix } @@ -79,6 +83,7 @@ RSpec.describe User do it { is_expected.to have_one(:user_detail) } it { is_expected.to have_one(:atlassian_identity) } it { is_expected.to have_one(:user_highest_role) } + it { is_expected.to have_one(:credit_card_validation) } it { is_expected.to have_many(:snippets).dependent(:destroy) } it { is_expected.to have_many(:members) } it { is_expected.to have_many(:project_members) } @@ -132,7 +137,7 @@ RSpec.describe User do it 'creates `user_detail` when `bio` is first updated' do user = create(:user) - expect { user.update(bio: 'my bio') }.to change { user.user_detail.persisted? }.from(false).to(true) + expect { user.update!(bio: 'my bio') }.to change { user.user_detail.persisted? }.from(false).to(true) end end @@ -655,9 +660,10 @@ RSpec.describe User do it 'does not accept not verified emails' do email = create(:email) user = email.user - user.update(notification_email: email.email) + user.notification_email = email.email expect(user).to be_invalid + expect(user.errors[:notification_email]).to include('is not an email you own') end end @@ -665,7 +671,7 @@ RSpec.describe User do it 'accepts verified emails' do email = create(:email, :confirmed, email: 'test@test.com') user = email.user - user.update(public_email: email.email) + user.notification_email = email.email expect(user).to be_valid end @@ -673,9 +679,10 @@ RSpec.describe User do it 'does not accept not verified emails' do email = create(:email) user = email.user - user.update(public_email: email.email) + user.public_email = email.email expect(user).to be_invalid + expect(user.errors[:public_email]).to include('is not an email you own') end end @@ -721,6 +728,7 @@ RSpec.describe User do let_it_be(:blocked_user) { create(:user, :blocked) } let_it_be(:ldap_blocked_user) { create(:omniauth_user, :ldap_blocked) } let_it_be(:blocked_pending_approval_user) { create(:user, :blocked_pending_approval) } + let_it_be(:banned_user) { create(:user, :banned) } describe '.blocked' do subject { described_class.blocked } @@ -731,7 +739,7 @@ RSpec.describe User do ldap_blocked_user ) - expect(subject).not_to include(active_user, blocked_pending_approval_user) + expect(subject).not_to include(active_user, blocked_pending_approval_user, banned_user) end end @@ -742,6 +750,14 @@ RSpec.describe User do expect(subject).to contain_exactly(blocked_pending_approval_user) end end + + describe '.banned' do + subject { described_class.banned } + + it 'returns only banned users' do + expect(subject).to contain_exactly(banned_user) + end + end end describe ".with_two_factor" do @@ -1056,6 +1072,21 @@ RSpec.describe User do .to contain_exactly(user) end end + + describe '.for_todos' do + let_it_be(:user1) { create(:user) } + let_it_be(:user2) { create(:user) } + let_it_be(:issue) { create(:issue) } + + let_it_be(:todo1) { create(:todo, target: issue, author: user1, user: user1) } + let_it_be(:todo2) { create(:todo, target: issue, author: user1, user: user1) } + let_it_be(:todo3) { create(:todo, target: issue, author: user2, user: user2) } + + it 'returns users for the given todos' do + expect(described_class.for_todos(issue.todos)) + .to contain_exactly(user1, user2) + end + end end describe "Respond to" do @@ -1274,7 +1305,7 @@ RSpec.describe User do let(:secondary) { create(:email, :confirmed, email: 'secondary@example.com', user: user) } before do - user.emails.create(email_attrs) + user.emails.create!(email_attrs) user.tap { |u| u.update!(notification_email: email_attrs[:email]) }.reload end @@ -1366,6 +1397,26 @@ RSpec.describe User do end end + describe '#credit_card_validated_at' do + let_it_be(:user) { create(:user) } + + context 'when credit_card_validation does not exist' do + it 'returns nil' do + expect(user.credit_card_validated_at).to be nil + end + end + + context 'when credit_card_validation exists' do + it 'returns the credit card validated time' do + credit_card_validated_time = Time.current - 1.day + + create(:credit_card_validation, credit_card_validated_at: credit_card_validated_time, user: user) + + expect(user.credit_card_validated_at).to eq(credit_card_validated_time) + end + end + end + describe '#update_tracked_fields!', :clean_gitlab_redis_shared_state do let(:request) { OpenStruct.new(remote_ip: "127.0.0.1") } let(:user) { create(:user) } @@ -1428,7 +1479,7 @@ RSpec.describe User do let!(:accessible_deploy_keys_project) { create(:deploy_keys_project, project: project) } before do - public_deploy_keys_project.deploy_key.update(public: true) + public_deploy_keys_project.deploy_key.update!(public: true) project.add_developer(user) end @@ -1518,13 +1569,13 @@ RSpec.describe User do it 'receives callback when external changes' do expect(user).to receive(:ensure_user_rights_and_limits) - user.update(external: false) + user.update!(external: false) end it 'ensures correct rights and limits for user' do stub_config_setting(default_can_create_group: true) - expect { user.update(external: false) }.to change { user.can_create_group }.to(true) + expect { user.update!(external: false) }.to change { user.can_create_group }.to(true) .and change { user.projects_limit }.to(Gitlab::CurrentSettings.default_projects_limit) end end @@ -1535,11 +1586,11 @@ RSpec.describe User do it 'receives callback when external changes' do expect(user).to receive(:ensure_user_rights_and_limits) - user.update(external: true) + user.update!(external: true) end it 'ensures correct rights and limits for user' do - expect { user.update(external: true) }.to change { user.can_create_group }.to(false) + expect { user.update!(external: true) }.to change { user.can_create_group }.to(false) .and change { user.projects_limit }.to(0) end end @@ -1892,6 +1943,12 @@ RSpec.describe User do expect(described_class.filter_items('blocked')).to include user end + it 'filters by banned' do + expect(described_class).to receive(:banned).and_return([user]) + + expect(described_class.filter_items('banned')).to include user + end + it 'filters by blocked pending approval' do expect(described_class).to receive(:blocked_pending_approval).and_return([user]) @@ -2435,7 +2492,7 @@ RSpec.describe User do end context 'with a redirect route matching the given path' do - let!(:redirect_route) { user.namespace.redirect_routes.create(path: 'foo') } + let!(:redirect_route) { user.namespace.redirect_routes.create!(path: 'foo') } context 'without the follow_redirects option' do it 'returns nil' do @@ -2511,8 +2568,9 @@ RSpec.describe User do it 'is false if avatar is html page' do user.update_attribute(:avatar, 'uploads/avatar.html') + user.avatar_type - expect(user.avatar_type).to eq(['file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico, webp']) + expect(user.errors.added?(:avatar, "file format is not supported. Please try one of the following supported formats: png, jpg, jpeg, gif, bmp, tiff, ico, webp")).to be true end end @@ -2535,7 +2593,7 @@ RSpec.describe User do expect(Gitlab::AvatarCache).to receive(:delete_by_email).with(*user.verified_emails) - user.update(avatar: fixture_file_upload('spec/fixtures/dk.png')) + user.update!(avatar: fixture_file_upload('spec/fixtures/dk.png')) end end @@ -2849,12 +2907,12 @@ RSpec.describe User do expect(user.starred?(project1)).to be_truthy expect(user.starred?(project2)).to be_truthy - star1.destroy + star1.destroy! expect(user.starred?(project1)).to be_falsey expect(user.starred?(project2)).to be_truthy - star2.destroy + star2.destroy! expect(user.starred?(project1)).to be_falsey expect(user.starred?(project2)).to be_falsey @@ -3404,7 +3462,7 @@ RSpec.describe User do expect(user.authorized_projects).to include(project) - member.destroy + member.destroy! expect(user.authorized_projects).not_to include(project) end @@ -3429,7 +3487,7 @@ RSpec.describe User do expect(user2.authorized_projects).to include(project) - project.destroy + project.destroy! expect(user2.authorized_projects).not_to include(project) end @@ -3443,7 +3501,7 @@ RSpec.describe User do expect(user.authorized_projects).to include(project) - group.destroy + group.destroy! expect(user.authorized_projects).not_to include(project) end @@ -4200,16 +4258,45 @@ RSpec.describe User do end describe '#invalidate_issue_cache_counts' do - let(:user) { build_stubbed(:user) } + let_it_be(:user) { create(:user) } - it 'invalidates cache for issue counter' do - cache_mock = double + subject do + user.invalidate_issue_cache_counts + user.save! + end - expect(cache_mock).to receive(:delete).with(['users', user.id, 'assigned_open_issues_count']) + shared_examples 'invalidates the cached value' do + it 'invalidates cache for issue counter' do + expect(Rails.cache).to receive(:delete).with(['users', user.id, 'assigned_open_issues_count']) - allow(Rails).to receive(:cache).and_return(cache_mock) + subject + end + end - user.invalidate_issue_cache_counts + it_behaves_like 'invalidates the cached value' + + context 'if feature flag assigned_open_issues_cache is enabled' do + it 'calls the recalculate worker' do + expect(Users::UpdateOpenIssueCountWorker).to receive(:perform_async).with(user.id) + + subject + end + + it_behaves_like 'invalidates the cached value' + end + + context 'if feature flag assigned_open_issues_cache is disabled' do + before do + stub_feature_flags(assigned_open_issues_cache: false) + end + + it 'does not call the recalculate worker' do + expect(Users::UpdateOpenIssueCountWorker).not_to receive(:perform_async).with(user.id) + + subject + end + + it_behaves_like 'invalidates the cached value' end end @@ -4414,9 +4501,10 @@ RSpec.describe User do end it 'adds the namespace errors to the user' do - user.update(username: new_username) + user.username = new_username - expect(user.errors.full_messages.first).to eq('A user, alias, or group already exists with that username.') + expect(user).to be_invalid + expect(user.errors[:base]).to include('A user, alias, or group already exists with that username.') end end end @@ -5238,6 +5326,26 @@ RSpec.describe User do end end + describe 'user credit card validation' do + context 'when user is initialized' do + let(:user) { build(:user) } + + it { expect(user.credit_card_validation).not_to be_present } + end + + context 'when create user without credit card validation' do + let(:user) { create(:user) } + + it { expect(user.credit_card_validation).not_to be_present } + end + + context 'when user credit card validation exists' do + let(:user) { create(:user, :with_credit_card_validation) } + + it { expect(user.credit_card_validation).to be_persisted } + end + end + describe 'user detail' do context 'when user is initialized' do let(:user) { build(:user) } @@ -5307,21 +5415,21 @@ RSpec.describe User do with_them do context 'when state was changed' do - subject { user.update(attributes) } + subject { user.update!(attributes) } include_examples 'update highest role with exclusive lease' end end context 'when state was not changed' do - subject { user.update(email: 'newmail@example.com') } + subject { user.update!(email: 'newmail@example.com') } include_examples 'does not update the highest role' end end describe 'destroy user' do - subject { user.destroy } + subject { user.destroy! } include_examples 'does not update the highest role' end @@ -5343,7 +5451,7 @@ RSpec.describe User do context 'when user is a ghost user' do before do - user.update(user_type: :ghost) + user.update!(user_type: :ghost) end it { is_expected.to be false } @@ -5361,7 +5469,7 @@ RSpec.describe User do with_them do before do - user.update(user_type: user_type) + user.update!(user_type: user_type) end it { is_expected.to be expected_result } @@ -5384,7 +5492,7 @@ RSpec.describe User do context 'when user is an internal user' do before do - user.update(user_type: :ghost) + user.update!(user_type: :ghost) end it { is_expected.to be :forbidden } @@ -5418,7 +5526,7 @@ RSpec.describe User do context 'when user is an internal user' do before do - user.update(user_type: 'alert_bot') + user.update!(user_type: 'alert_bot') end it_behaves_like 'does not require password to be present' @@ -5426,7 +5534,7 @@ RSpec.describe User do context 'when user is a project bot user' do before do - user.update(user_type: 'project_bot') + user.update!(user_type: 'project_bot') end it_behaves_like 'does not require password to be present' @@ -5600,4 +5708,47 @@ RSpec.describe User do end end end + + describe '.dormant' do + it 'returns dormant users' do + freeze_time do + not_that_long_ago = (described_class::MINIMUM_INACTIVE_DAYS - 1).days.ago.to_date + too_long_ago = described_class::MINIMUM_INACTIVE_DAYS.days.ago.to_date + + create(:user, :deactivated, last_activity_on: too_long_ago) + + User::INTERNAL_USER_TYPES.map do |user_type| + create(:user, state: :active, user_type: user_type, last_activity_on: too_long_ago) + end + + create(:user, last_activity_on: not_that_long_ago) + + dormant_user = create(:user, last_activity_on: too_long_ago) + + expect(described_class.dormant).to contain_exactly(dormant_user) + end + end + end + + describe '.with_no_activity' do + it 'returns users with no activity' do + freeze_time do + not_that_long_ago = (described_class::MINIMUM_INACTIVE_DAYS - 1).days.ago.to_date + too_long_ago = described_class::MINIMUM_INACTIVE_DAYS.days.ago.to_date + + create(:user, :deactivated, last_activity_on: nil) + + User::INTERNAL_USER_TYPES.map do |user_type| + create(:user, state: :active, user_type: user_type, last_activity_on: nil) + end + + create(:user, last_activity_on: not_that_long_ago) + create(:user, last_activity_on: too_long_ago) + + user_with_no_activity = create(:user, last_activity_on: nil) + + expect(described_class.with_no_activity).to contain_exactly(user_with_no_activity) + end + end + end end |