diff options
Diffstat (limited to 'spec/services/users')
-rw-r--r-- | spec/services/users/activity_service_spec.rb | 47 | ||||
-rw-r--r-- | spec/services/users/authorized_build_service_spec.rb | 16 | ||||
-rw-r--r-- | spec/services/users/build_service_spec.rb | 139 | ||||
-rw-r--r-- | spec/services/users/update_assigned_open_issue_count_service_spec.rb | 49 |
4 files changed, 68 insertions, 183 deletions
diff --git a/spec/services/users/activity_service_spec.rb b/spec/services/users/activity_service_spec.rb index 4bbf6a2bcb8..cfafa9eff45 100644 --- a/spec/services/users/activity_service_spec.rb +++ b/spec/services/users/activity_service_spec.rb @@ -84,4 +84,51 @@ RSpec.describe Users::ActivityService do end end end + + context 'with DB Load Balancing', :request_store, :redis, :clean_gitlab_redis_shared_state do + include_context 'clear DB Load Balancing configuration' + + let(:user) { create(:user, last_activity_on: last_activity_on) } + + context 'when last activity is in the past' do + let(:user) { create(:user, last_activity_on: Date.today - 1.week) } + + context 'database load balancing is configured' do + before do + # Do not pollute AR for other tests, but rather simulate effect of configure_proxy. + allow(ActiveRecord::Base.singleton_class).to receive(:prepend) + ::Gitlab::Database::LoadBalancing.configure_proxy + allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy) + end + + let(:service) do + service = described_class.new(user) + + ::Gitlab::Database::LoadBalancing::Session.clear_session + + service + end + + it 'does not stick to primary' do + expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_performed_write + + service.execute + + expect(user.last_activity_on).to eq(Date.today) + expect(::Gitlab::Database::LoadBalancing::Session.current).to be_performed_write + expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_using_primary + end + end + + context 'database load balancing is not configured' do + let(:service) { described_class.new(user) } + + it 'updates user without error' do + service.execute + + expect(user.last_activity_on).to eq(Date.today) + end + end + end + end end diff --git a/spec/services/users/authorized_build_service_spec.rb b/spec/services/users/authorized_build_service_spec.rb new file mode 100644 index 00000000000..57a122cbf35 --- /dev/null +++ b/spec/services/users/authorized_build_service_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::AuthorizedBuildService do + describe '#execute' do + let_it_be(:current_user) { create(:user) } + + let(:params) { build_stubbed(:user).slice(:first_name, :last_name, :username, :email, :password) } + + subject(:user) { described_class.new(current_user, params).execute } + + it_behaves_like 'common user build items' + it_behaves_like 'current user not admin build items' + end +end diff --git a/spec/services/users/build_service_spec.rb b/spec/services/users/build_service_spec.rb index e8786c677d1..98fe6d9b5ba 100644 --- a/spec/services/users/build_service_spec.rb +++ b/spec/services/users/build_service_spec.rb @@ -11,148 +11,19 @@ RSpec.describe Users::BuildService do let(:params) { build_stubbed(:user).slice(:first_name, :last_name, :username, :email, :password) } let(:service) { described_class.new(current_user, params) } - shared_examples_for 'common build items' do - it { is_expected.to be_valid } - - it 'sets the created_by_id' do - expect(user.created_by_id).to eq(current_user&.id) - end - - it 'calls UpdateCanonicalEmailService' do - expect(Users::UpdateCanonicalEmailService).to receive(:new).and_call_original - - user - end - - context 'when user_type is provided' do - context 'when project_bot' do - before do - params.merge!({ user_type: :project_bot }) - end - - it { expect(user.project_bot?).to be true } - end - - context 'when not a project_bot' do - before do - params.merge!({ user_type: :alert_bot }) - end - - it { expect(user).to be_human } - end - end - end - - shared_examples_for 'current user not admin' do - context 'with "user_default_external" application setting' do - where(:user_default_external, :external, :email, :user_default_internal_regex, :result) do - true | nil | 'fl@example.com' | nil | true - true | true | 'fl@example.com' | nil | true - true | false | 'fl@example.com' | nil | true # admin difference - - true | nil | 'fl@example.com' | '' | true - true | true | 'fl@example.com' | '' | true - true | false | 'fl@example.com' | '' | true # admin difference - - true | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - true | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - true | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - - true | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true - true | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true - true | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | true # admin difference - - false | nil | 'fl@example.com' | nil | false - false | true | 'fl@example.com' | nil | false # admin difference - false | false | 'fl@example.com' | nil | false - - false | nil | 'fl@example.com' | '' | false - false | true | 'fl@example.com' | '' | false # admin difference - false | false | 'fl@example.com' | '' | false - - false | nil | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - false | true | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - false | false | 'fl@example.com' | '^(?:(?!\.ext@).)*$\r?' | false - - false | nil | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false - false | true | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false # admin difference - false | false | 'tester.ext@domain.com' | '^(?:(?!\.ext@).)*$\r?' | false - end - - with_them do - before do - stub_application_setting(user_default_external: user_default_external) - stub_application_setting(user_default_internal_regex: user_default_internal_regex) - - params.merge!({ external: external, email: email }.compact) - end - - it 'sets the value of Gitlab::CurrentSettings.user_default_external' do - expect(user.external).to eq(result) - end - end - end - - context 'when "send_user_confirmation_email" application setting is true' do - before do - stub_application_setting(send_user_confirmation_email: true, signup_enabled?: true) - end - - it 'does not confirm the user' do - expect(user).not_to be_confirmed - end - end - - context 'when "send_user_confirmation_email" application setting is false' do - before do - stub_application_setting(send_user_confirmation_email: false, signup_enabled?: true) - end - - it 'confirms the user' do - expect(user).to be_confirmed - end - end - - context 'with allowed params' do - let(:params) do - { - email: 1, - name: 1, - password: 1, - password_automatically_set: 1, - username: 1, - user_type: 'project_bot' - } - end - - it 'sets all allowed attributes' do - expect(User).to receive(:new).with(hash_including(params)).and_call_original - - user - end - end - end - context 'with nil current_user' do subject(:user) { service.execute } - it_behaves_like 'common build items' - it_behaves_like 'current user not admin' + it_behaves_like 'common user build items' + it_behaves_like 'current user not admin build items' end context 'with non admin current_user' do let_it_be(:current_user) { create(:user) } - let(:service) { described_class.new(current_user, params) } - - subject(:user) { service.execute(skip_authorization: true) } - - it 'raises AccessDeniedError exception when authorization is not skipped' do - expect { service.execute }.to raise_error Gitlab::Access::AccessDeniedError + it 'raises AccessDeniedError exception' do + expect { described_class.new(current_user, params).execute }.to raise_error Gitlab::Access::AccessDeniedError end - - it_behaves_like 'common build items' - it_behaves_like 'current user not admin' end context 'with an admin current_user' do @@ -163,7 +34,7 @@ RSpec.describe Users::BuildService do subject(:user) { service.execute } - it_behaves_like 'common build items' + it_behaves_like 'common user build items' context 'with allowed params' do let(:params) do diff --git a/spec/services/users/update_assigned_open_issue_count_service_spec.rb b/spec/services/users/update_assigned_open_issue_count_service_spec.rb deleted file mode 100644 index 55fc60a7893..00000000000 --- a/spec/services/users/update_assigned_open_issue_count_service_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Users::UpdateAssignedOpenIssueCountService do - let_it_be(:user) { create(:user) } - - describe '#initialize' do - context 'incorrect arguments provided' do - it 'raises an error if there are no target user' do - expect { described_class.new(target_user: nil) }.to raise_error(ArgumentError, /Please provide a target user/) - expect { described_class.new(target_user: "nonsense") }.to raise_error(ArgumentError, /Please provide a target user/) - end - end - - context 'when correct arguments provided' do - it 'is successful' do - expect { described_class.new(target_user: user) }.not_to raise_error - end - end - end - - describe "#execute", :clean_gitlab_redis_cache do - let(:fake_update_service) { double } - let(:fake_issue_count_service) { double } - let(:provided_value) { nil } - - subject { described_class.new(target_user: user).execute } - - context 'successful' do - it 'returns a success response' do - expect(subject).to be_success - end - - it 'writes the cache with the new value' do - expect(Rails.cache).to receive(:write).with(['users', user.id, 'assigned_open_issues_count'], 0, expires_in: User::COUNT_CACHE_VALIDITY_PERIOD) - - subject - end - - it 'calls the issues finder to get the latest value' do - expect(IssuesFinder).to receive(:new).with(user, assignee_id: user.id, state: 'opened', non_archived: true).and_return(fake_issue_count_service) - expect(fake_issue_count_service).to receive(:execute) - - subject - end - end - end -end |