diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-06 18:09:27 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-06 18:09:27 +0300 |
commit | 638e2f1c5f55988135da63c7aa57bcecb9355a2b (patch) | |
tree | c25a1deeec9e02411f52a5eb831c42fa41778f9a /spec/services | |
parent | 4958d96e262f6b31b2850123e4949536555b2d29 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/ci/pipelines/add_job_service_spec.rb | 10 | ||||
-rw-r--r-- | spec/services/issues/create_service_spec.rb | 70 | ||||
-rw-r--r-- | spec/services/snippets/create_service_spec.rb | 7 | ||||
-rw-r--r-- | spec/services/snippets/update_service_spec.rb | 7 | ||||
-rw-r--r-- | spec/services/spam/spam_action_service_spec.rb | 8 | ||||
-rw-r--r-- | spec/services/tasks_to_be_done/base_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/services/user_agent_detail_service_spec.rb | 41 | ||||
-rw-r--r-- | spec/services/work_items/create_and_link_service_spec.rb | 7 | ||||
-rw-r--r-- | spec/services/work_items/create_from_task_service_spec.rb | 7 | ||||
-rw-r--r-- | spec/services/work_items/create_service_spec.rb | 22 | ||||
-rw-r--r-- | spec/services/work_items/update_service_spec.rb | 8 |
11 files changed, 102 insertions, 87 deletions
diff --git a/spec/services/ci/pipelines/add_job_service_spec.rb b/spec/services/ci/pipelines/add_job_service_spec.rb index 6380a6a5ec3..9fb1d6933c6 100644 --- a/spec/services/ci/pipelines/add_job_service_spec.rb +++ b/spec/services/ci/pipelines/add_job_service_spec.rb @@ -86,15 +86,5 @@ RSpec.describe Ci::Pipelines::AddJobService, feature_category: :continuous_integ expect(execute.payload[:job]).to eq(job) end end - - it 'locks pipelines and stages before persisting builds', :aggregate_failures do - expect(job).not_to be_persisted - - recorder = ActiveRecord::QueryRecorder.new(skip_cached: false) { execute } - entries = recorder.log.select { |query| query.match(/LOCK|INSERT INTO ".{0,2}ci_builds"/) } - - expect(entries.size).to eq(2) - expect(entries.first).to match(/LOCK "ci_pipelines", "ci_stages" IN ROW SHARE MODE;/) - end end end diff --git a/spec/services/issues/create_service_spec.rb b/spec/services/issues/create_service_spec.rb index 548d9455ebf..cbedf3263d8 100644 --- a/spec/services/issues/create_service_spec.rb +++ b/spec/services/issues/create_service_spec.rb @@ -10,8 +10,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do let_it_be(:user) { create(:user) } let(:opts) { { title: 'title' } } - let(:spam_params) { double } - let(:service) { described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params) } + let(:service) { described_class.new(container: project, current_user: user, params: opts) } it_behaves_like 'rate limited service' do let(:key) { :issues_create } @@ -27,10 +26,6 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do let(:result) { service.execute } let(:issue) { result[:issue] } - before do - stub_spam_services - end - context 'when params are invalid' do let(:opts) { { title: '' } } @@ -155,7 +150,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do end context 'when a build_service is provided' do - let(:result) { described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params, build_service: build_service).execute } + let(:result) { described_class.new(container: project, current_user: user, params: opts, build_service: build_service).execute } let(:issue_from_builder) { build(:work_item, project: project, title: 'Issue from builder') } let(:build_service) { double(:build_service, execute: issue_from_builder) } @@ -168,7 +163,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do end context 'when skip_system_notes is true' do - let(:issue) { described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute(skip_system_notes: true) } + let(:issue) { described_class.new(container: project, current_user: user, params: opts).execute(skip_system_notes: true) } it 'does not call Issuable::CommonSystemNotesService' do expect(Issuable::CommonSystemNotesService).not_to receive(:new) @@ -264,7 +259,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do let_it_be(:non_member) { create(:user) } it 'filters out params that cannot be set without the :set_issue_metadata permission' do - result = described_class.new(container: project, current_user: non_member, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: non_member, params: opts).execute issue = result[:issue] expect(result).to be_success @@ -278,7 +273,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do end it 'can create confidential issues' do - result = described_class.new(container: project, current_user: non_member, params: opts.merge(confidential: true), spam_params: spam_params).execute + result = described_class.new(container: project, current_user: non_member, params: opts.merge(confidential: true)).execute issue = result[:issue] expect(result).to be_success @@ -289,7 +284,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'moves the issue to the end, in an asynchronous worker' do expect(Issues::PlacementWorker).to receive(:perform_async).with(be_nil, Integer) - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute end context 'when label belongs to project group' do @@ -376,13 +371,13 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'invalidates open issues counter for assignees when issue is assigned' do project.add_maintainer(assignee) - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute expect(assignee.assigned_open_issues_count).to eq 1 end it 'records the assignee assignment event' do - result = described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: user, params: opts).execute issue = result.payload[:issue] expect(issue.assignment_events).to match([have_attributes(user_id: assignee.id, action: 'add')]) @@ -454,7 +449,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do expect(project.project_namespace).to receive(:execute_hooks).with(expected_payload, :issue_hooks) expect(project.project_namespace).to receive(:execute_integrations).with(expected_payload, :issue_hooks) - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute end context 'when issue is confidential' do @@ -477,7 +472,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do expect(project.project_namespace).to receive(:execute_hooks).with(expected_payload, :confidential_issue_hooks) expect(project.project_namespace).to receive(:execute_integrations).with(expected_payload, :confidential_issue_hooks) - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute end end end @@ -523,7 +518,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'removes assignee when user id is invalid' do opts = { title: 'Title', description: 'Description', assignee_ids: [-1] } - result = described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: user, params: opts).execute issue = result[:issue] expect(result).to be_success @@ -533,7 +528,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'removes assignee when user id is 0' do opts = { title: 'Title', description: 'Description', assignee_ids: [0] } - result = described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: user, params: opts).execute issue = result[:issue] expect(result).to be_success @@ -544,7 +539,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do project.add_maintainer(assignee) opts = { title: 'Title', description: 'Description', assignee_ids: [assignee.id] } - result = described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: user, params: opts).execute issue = result[:issue] expect(result).to be_success @@ -564,7 +559,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do project.update!(visibility_level: level) opts = { title: 'Title', description: 'Description', assignee_ids: [assignee.id] } - result = described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + result = described_class.new(container: project, current_user: user, params: opts).execute issue = result[:issue] expect(result).to be_success @@ -576,7 +571,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do end it_behaves_like 'issuable record that supports quick actions' do - let(:issuable) { described_class.new(container: project, current_user: user, params: params, spam_params: spam_params).execute[:issue] } + let(:issuable) { described_class.new(container: project, current_user: user, params: params).execute[:issue] } end context 'Quick actions' do @@ -703,14 +698,14 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do let(:opts) { { discussion_to_resolve: discussion.id, merge_request_to_resolve_discussions_of: merge_request.iid } } it 'resolves the discussion' do - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute discussion.first_note.reload expect(discussion.resolved?).to be(true) end it 'added a system note to the discussion' do - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute reloaded_discussion = MergeRequest.find(merge_request.id).discussions.first @@ -720,8 +715,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'sets default title and description values if not provided' do result = described_class.new( container: project, current_user: user, - params: opts, - spam_params: spam_params + params: opts ).execute issue = result[:issue] @@ -738,8 +732,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do params: opts.merge( description: 'Custom issue description', title: 'My new issue' - ), - spam_params: spam_params + ) ).execute issue = result[:issue] @@ -754,14 +747,14 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do let(:opts) { { merge_request_to_resolve_discussions_of: merge_request.iid } } it 'resolves the discussion' do - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute discussion.first_note.reload expect(discussion.resolved?).to be(true) end it 'added a system note to the discussion' do - described_class.new(container: project, current_user: user, params: opts, spam_params: spam_params).execute + described_class.new(container: project, current_user: user, params: opts).execute reloaded_discussion = MergeRequest.find(merge_request.id).discussions.first @@ -771,8 +764,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do it 'sets default title and description values if not provided' do result = described_class.new( container: project, current_user: user, - params: opts, - spam_params: spam_params + params: opts ).execute issue = result[:issue] @@ -789,8 +781,7 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do params: opts.merge( description: 'Custom issue description', title: 'My new issue' - ), - spam_params: spam_params + ) ).execute issue = result[:issue] @@ -836,8 +827,10 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do } end + let(:perform_spam_check) { true } + subject do - described_class.new(container: project, current_user: user, params: params, spam_params: spam_params) + described_class.new(container: project, current_user: user, params: params, perform_spam_check: perform_spam_check) end it 'executes SpamActionService' do @@ -845,7 +838,6 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do Spam::SpamActionService, { spammable: kind_of(Issue), - spam_params: spam_params, user: an_instance_of(User), action: :create } @@ -855,6 +847,16 @@ RSpec.describe Issues::CreateService, feature_category: :team_planning do subject.execute end + + context 'when `perform_spam_check` is set to `false`' do + let(:perform_spam_check) { false } + + it 'does not execute the SpamActionService' do + expect(Spam::SpamActionService).not_to receive(:new) + + subject.execute + end + end end end end diff --git a/spec/services/snippets/create_service_spec.rb b/spec/services/snippets/create_service_spec.rb index 725f1b165a2..59deffe9ccd 100644 --- a/spec/services/snippets/create_service_spec.rb +++ b/spec/services/snippets/create_service_spec.rb @@ -20,9 +20,8 @@ RSpec.describe Snippets::CreateService, feature_category: :source_code_managemen let(:extra_opts) { {} } let(:creator) { admin } - let(:spam_params) { double } - subject { described_class.new(project: project, current_user: creator, params: opts, spam_params: spam_params).execute } + subject { described_class.new(project: project, current_user: creator, params: opts).execute } let(:snippet) { subject.payload[:snippet] } @@ -303,10 +302,6 @@ RSpec.describe Snippets::CreateService, feature_category: :source_code_managemen end end - before do - stub_spam_services - end - context 'when ProjectSnippet' do let_it_be(:project) { create(:project) } diff --git a/spec/services/snippets/update_service_spec.rb b/spec/services/snippets/update_service_spec.rb index 99bb70a3077..b428897ce27 100644 --- a/spec/services/snippets/update_service_spec.rb +++ b/spec/services/snippets/update_service_spec.rb @@ -21,9 +21,8 @@ RSpec.describe Snippets::UpdateService, feature_category: :source_code_managemen let(:extra_opts) { {} } let(:options) { base_opts.merge(extra_opts) } let(:updater) { user } - let(:spam_params) { double } - let(:service) { Snippets::UpdateService.new(project: project, current_user: updater, params: options, spam_params: spam_params) } + let(:service) { Snippets::UpdateService.new(project: project, current_user: updater, params: options, perform_spam_check: true) } subject { service.execute(snippet) } @@ -724,10 +723,6 @@ RSpec.describe Snippets::UpdateService, feature_category: :source_code_managemen end end - before do - stub_spam_services - end - context 'when Project Snippet' do let_it_be(:project) { create(:project) } diff --git a/spec/services/spam/spam_action_service_spec.rb b/spec/services/spam/spam_action_service_spec.rb index e2cc2ea7ce3..7072f5879ea 100644 --- a/spec/services/spam/spam_action_service_spec.rb +++ b/spec/services/spam/spam_action_service_spec.rb @@ -30,11 +30,15 @@ RSpec.describe Spam::SpamActionService, feature_category: :instance_resiliency d before do issue.spam = false personal_snippet.spam = false + + allow_next_instance_of(described_class) do |service| + allow(service).to receive(:spam_params).and_return(spam_params) + end end describe 'constructor argument validation' do subject do - described_service = described_class.new(spammable: issue, spam_params: spam_params, user: user, action: :create) + described_service = described_class.new(spammable: issue, user: user, action: :create) described_service.execute end @@ -108,7 +112,7 @@ RSpec.describe Spam::SpamActionService, feature_category: :instance_resiliency d let_it_be(:existing_spam_log) { create(:spam_log, user: user, recaptcha_verified: false) } subject do - described_service = described_class.new(spammable: target, spam_params: spam_params, extra_features: + described_service = described_class.new(spammable: target, extra_features: extra_features, user: user, action: :create) described_service.execute end diff --git a/spec/services/tasks_to_be_done/base_service_spec.rb b/spec/services/tasks_to_be_done/base_service_spec.rb index 3ca9d140197..32b07cab095 100644 --- a/spec/services/tasks_to_be_done/base_service_spec.rb +++ b/spec/services/tasks_to_be_done/base_service_spec.rb @@ -35,7 +35,7 @@ RSpec.describe TasksToBeDone::BaseService, feature_category: :team_planning do expect(Issues::CreateService) .to receive(:new) - .with(container: project, current_user: current_user, params: params, spam_params: nil) + .with(container: project, current_user: current_user, params: params, perform_spam_check: false) .and_call_original expect { service.execute }.to change(Issue, :count).by(1) diff --git a/spec/services/user_agent_detail_service_spec.rb b/spec/services/user_agent_detail_service_spec.rb new file mode 100644 index 00000000000..3984ec33716 --- /dev/null +++ b/spec/services/user_agent_detail_service_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe UserAgentDetailService, feature_category: :instance_resiliency do + describe '#create', :request_store do + let_it_be(:spammable) { create(:issue) } + + using RSpec::Parameterized::TableSyntax + + where(:perform_spam_check, :spam_params_present, :user_agent, :ip_address, :creates_user_agent_detail) do + true | true | 'UA' | 'IP' | true + true | false | 'UA' | 'IP' | false + false | true | 'UA' | 'IP' | false + true | true | '' | 'IP' | false + true | true | nil | 'IP' | false + true | true | 'UA' | '' | false + true | true | 'UA' | nil | false + end + + with_them do + let(:spam_params) do + instance_double('Spam::SpamParams', user_agent: user_agent, ip_address: ip_address) if spam_params_present + end + + before do + allow(Gitlab::RequestContext.instance).to receive(:spam_params).and_return(spam_params) + end + + subject { described_class.new(spammable: spammable, perform_spam_check: perform_spam_check).create } # rubocop:disable Rails/SaveBang + + it 'creates a user agent detail when expected' do + if creates_user_agent_detail + expect { subject }.to change { UserAgentDetail.count }.by(1) + else + expect(subject).to be_a ServiceResponse + end + end + end + end +end diff --git a/spec/services/work_items/create_and_link_service_spec.rb b/spec/services/work_items/create_and_link_service_spec.rb index 00372d460e1..b83492274a3 100644 --- a/spec/services/work_items/create_and_link_service_spec.rb +++ b/spec/services/work_items/create_and_link_service_spec.rb @@ -9,7 +9,6 @@ RSpec.describe WorkItems::CreateAndLinkService, feature_category: :portfolio_man let_it_be(:related_work_item, refind: true) { create(:work_item, project: project) } let_it_be(:invalid_parent) { create(:work_item, :task, project: project) } - let(:spam_params) { double } let(:link_params) { {} } let(:params) do @@ -45,11 +44,7 @@ RSpec.describe WorkItems::CreateAndLinkService, feature_category: :portfolio_man end describe '#execute' do - subject(:service_result) { described_class.new(project: project, current_user: user, params: params, spam_params: spam_params, link_params: link_params).execute } - - before do - stub_spam_services - end + subject(:service_result) { described_class.new(project: project, current_user: user, params: params, link_params: link_params).execute } context 'when work item params are valid' do it { is_expected.to be_success } diff --git a/spec/services/work_items/create_from_task_service_spec.rb b/spec/services/work_items/create_from_task_service_spec.rb index b2f81f1dc54..2ab9209ab05 100644 --- a/spec/services/work_items/create_from_task_service_spec.rb +++ b/spec/services/work_items/create_from_task_service_spec.rb @@ -8,7 +8,6 @@ RSpec.describe WorkItems::CreateFromTaskService, feature_category: :team_plannin let_it_be(:list_work_item, refind: true) { create(:work_item, project: project, description: "- [ ] Item to be converted\n second line\n third line") } let(:work_item_to_update) { list_work_item } - let(:spam_params) { double } let(:link_params) { {} } let(:current_user) { developer } let(:params) do @@ -38,11 +37,7 @@ RSpec.describe WorkItems::CreateFromTaskService, feature_category: :team_plannin end describe '#execute' do - subject(:service_result) { described_class.new(work_item: work_item_to_update, current_user: current_user, work_item_params: params, spam_params: spam_params).execute } - - before do - stub_spam_services - end + subject(:service_result) { described_class.new(work_item: work_item_to_update, current_user: current_user, work_item_params: params).execute } context 'when work item params are valid' do it { is_expected.to be_success } diff --git a/spec/services/work_items/create_service_spec.rb b/spec/services/work_items/create_service_spec.rb index 46e598c3f11..ec775ba36f3 100644 --- a/spec/services/work_items/create_service_spec.rb +++ b/spec/services/work_items/create_service_spec.rb @@ -30,7 +30,7 @@ RSpec.describe WorkItems::CreateService, feature_category: :team_planning do let_it_be(:user_with_no_access) { create(:user) } let(:widget_params) { {} } - let(:spam_params) { double } + let(:perform_spam_check) { false } let(:current_user) { guest } let(:opts) do { @@ -60,17 +60,13 @@ RSpec.describe WorkItems::CreateService, feature_category: :team_planning do container: container, current_user: current_user, params: opts, - spam_params: spam_params, + perform_spam_check: perform_spam_check, widget_params: widget_params ) end subject(:service_result) { service.execute } - before do - stub_spam_services - end - context 'when user is not allowed to create a work item in the container' do let(:current_user) { user_with_no_access } @@ -151,12 +147,13 @@ RSpec.describe WorkItems::CreateService, feature_category: :team_planning do end context 'checking spam' do + let(:perform_spam_check) { true } + it 'executes SpamActionService' do expect_next_instance_of( Spam::SpamActionService, { spammable: kind_of(WorkItem), - spam_params: spam_params, user: an_instance_of(User), action: :create } @@ -166,6 +163,16 @@ RSpec.describe WorkItems::CreateService, feature_category: :team_planning do service_result end + + context 'when `perform_spam_check` is set to `false`' do + let(:perform_spam_check) { false } + + it 'does not execute the SpamActionService' do + expect(Spam::SpamActionService).not_to receive(:new) + + service_result + end + end end it_behaves_like 'work item widgetable service' do @@ -180,7 +187,6 @@ RSpec.describe WorkItems::CreateService, feature_category: :team_planning do container: container, current_user: current_user, params: opts, - spam_params: spam_params, widget_params: widget_params ) end diff --git a/spec/services/work_items/update_service_spec.rb b/spec/services/work_items/update_service_spec.rb index 2cf52ee853a..6a56e1a72e6 100644 --- a/spec/services/work_items/update_service_spec.rb +++ b/spec/services/work_items/update_service_spec.rb @@ -9,7 +9,6 @@ RSpec.describe WorkItems::UpdateService, feature_category: :team_planning do let_it_be(:parent) { create(:work_item, project: project) } let_it_be_with_reload(:work_item) { create(:work_item, project: project, assignees: [developer]) } - let(:spam_params) { double } let(:widget_params) { {} } let(:opts) { {} } let(:current_user) { developer } @@ -25,17 +24,12 @@ RSpec.describe WorkItems::UpdateService, feature_category: :team_planning do container: project, current_user: current_user, params: opts, - spam_params: spam_params, widget_params: widget_params ) end subject(:update_work_item) { service.execute(work_item) } - before do - stub_spam_services - end - shared_examples 'update service that triggers graphql dates updated subscription' do it 'triggers graphql subscription issueableDatesUpdated' do expect(GraphqlTriggers).to receive(:issuable_dates_updated).with(work_item).and_call_original @@ -176,7 +170,6 @@ RSpec.describe WorkItems::UpdateService, feature_category: :team_planning do container: project, current_user: current_user, params: opts, - spam_params: spam_params, widget_params: widget_params ) end @@ -392,7 +385,6 @@ RSpec.describe WorkItems::UpdateService, feature_category: :team_planning do container: project, current_user: current_user, params: update_params, - spam_params: spam_params, widget_params: widget_params ).execute(work_item) end |