summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/services
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/shared_examples/services')
-rw-r--r--spec/support/shared_examples/services/container_registry_auth_service_shared_examples.rb8
-rw-r--r--spec/support/shared_examples/services/incident_shared_examples.rb2
-rw-r--r--spec/support/shared_examples/services/issuable_links/create_links_shared_examples.rb133
-rw-r--r--spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb42
-rw-r--r--spec/support/shared_examples/services/rate_limited_service_shared_examples.rb73
-rw-r--r--spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb12
6 files changed, 261 insertions, 9 deletions
diff --git a/spec/support/shared_examples/services/container_registry_auth_service_shared_examples.rb b/spec/support/shared_examples/services/container_registry_auth_service_shared_examples.rb
index c808b9a5318..a780952d51b 100644
--- a/spec/support/shared_examples/services/container_registry_auth_service_shared_examples.rb
+++ b/spec/support/shared_examples/services/container_registry_auth_service_shared_examples.rb
@@ -69,10 +69,6 @@ RSpec.shared_examples 'a browsable' do
end
RSpec.shared_examples 'an accessible' do
- before do
- stub_feature_flags(container_registry_migration_phase1: false)
- end
-
let(:access) do
[{ 'type' => 'repository',
'name' => project.full_path,
@@ -161,10 +157,6 @@ end
RSpec.shared_examples 'a container registry auth service' do
include_context 'container registry auth service context'
- before do
- stub_feature_flags(container_registry_migration_phase1: false)
- end
-
describe '.full_access_token' do
let_it_be(:project) { create(:project) }
diff --git a/spec/support/shared_examples/services/incident_shared_examples.rb b/spec/support/shared_examples/services/incident_shared_examples.rb
index cc26cf87322..b533b095aac 100644
--- a/spec/support/shared_examples/services/incident_shared_examples.rb
+++ b/spec/support/shared_examples/services/incident_shared_examples.rb
@@ -70,7 +70,7 @@ RSpec.shared_examples 'incident management label service' do
expect(execute).to be_success
expect(execute.payload).to eq(label: label)
expect(label.title).to eq(title)
- expect(label.color).to eq(color)
+ expect(label.color).to be_color(color)
expect(label.description).to eq(description)
end
end
diff --git a/spec/support/shared_examples/services/issuable_links/create_links_shared_examples.rb b/spec/support/shared_examples/services/issuable_links/create_links_shared_examples.rb
new file mode 100644
index 00000000000..6146aae6b9b
--- /dev/null
+++ b/spec/support/shared_examples/services/issuable_links/create_links_shared_examples.rb
@@ -0,0 +1,133 @@
+# frozen_string_literal: true
+
+shared_examples 'issuable link creation' do
+ describe '#execute' do
+ subject { described_class.new(issuable, user, params).execute }
+
+ context 'when the reference list is empty' do
+ let(:params) do
+ { issuable_references: [] }
+ end
+
+ it 'returns error' do
+ is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
+ end
+ end
+
+ context 'when Issuable not found' do
+ let(:params) do
+ { issuable_references: ["##{non_existing_record_iid}"] }
+ end
+
+ it 'returns error' do
+ is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
+ end
+
+ it 'no relationship is created' do
+ expect { subject }.not_to change(issuable_link_class, :count)
+ end
+ end
+
+ context 'when user has no permission to target issuable' do
+ let(:params) do
+ { issuable_references: [guest_issuable.to_reference(issuable_parent)] }
+ end
+
+ it 'returns error' do
+ is_expected.to eq(message: "No matching #{issuable_type} found. Make sure that you are adding a valid #{issuable_type} URL.", status: :error, http_status: 404)
+ end
+
+ it 'no relationship is created' do
+ expect { subject }.not_to change(issuable_link_class, :count)
+ end
+ end
+
+ context 'source and target are the same issuable' do
+ let(:params) do
+ { issuable_references: [issuable.to_reference] }
+ end
+
+ it 'does not create notes' do
+ expect(SystemNoteService).not_to receive(:relate_issuable)
+
+ subject
+ end
+
+ it 'no relationship is created' do
+ expect { subject }.not_to change(issuable_link_class, :count)
+ end
+ end
+
+ context 'when there is an issuable to relate' do
+ let(:params) do
+ { issuable_references: [issuable2.to_reference, issuable3.to_reference(issuable_parent)] }
+ end
+
+ it 'creates relationships' do
+ expect { subject }.to change(issuable_link_class, :count).by(2)
+
+ expect(issuable_link_class.find_by!(target: issuable2)).to have_attributes(source: issuable, link_type: 'relates_to')
+ expect(issuable_link_class.find_by!(target: issuable3)).to have_attributes(source: issuable, link_type: 'relates_to')
+ end
+
+ it 'returns success status' do
+ is_expected.to eq(status: :success)
+ end
+
+ it 'creates notes' do
+ # First two-way relation notes
+ expect(SystemNoteService).to receive(:relate_issuable)
+ .with(issuable, issuable2, user)
+ expect(SystemNoteService).to receive(:relate_issuable)
+ .with(issuable2, issuable, user)
+
+ # Second two-way relation notes
+ expect(SystemNoteService).to receive(:relate_issuable)
+ .with(issuable, issuable3, user)
+ expect(SystemNoteService).to receive(:relate_issuable)
+ .with(issuable3, issuable, user)
+
+ subject
+ end
+ end
+
+ context 'when reference of any already related issue is present' do
+ let(:params) do
+ {
+ issuable_references: [
+ issuable_a.to_reference,
+ issuable_b.to_reference
+ ],
+ link_type: IssueLink::TYPE_RELATES_TO
+ }
+ end
+
+ it 'creates notes only for new relations' do
+ expect(SystemNoteService).to receive(:relate_issuable).with(issuable, issuable_a, anything)
+ expect(SystemNoteService).to receive(:relate_issuable).with(issuable_a, issuable, anything)
+ expect(SystemNoteService).not_to receive(:relate_issuable).with(issuable, issuable_b, anything)
+ expect(SystemNoteService).not_to receive(:relate_issuable).with(issuable_b, issuable, anything)
+
+ subject
+ end
+ end
+
+ context 'when there are invalid references' do
+ let(:params) do
+ { issuable_references: [issuable.to_reference, issuable_a.to_reference] }
+ end
+
+ it 'creates links only for valid references' do
+ expect { subject }.to change { issuable_link_class.count }.by(1)
+ end
+
+ it 'returns error status' do
+ expect(subject).to eq(
+ status: :error,
+ http_status: 422,
+ message: "#{issuable.to_reference} cannot be added: cannot be related to itself"
+ )
+ end
+ end
+ end
+end
diff --git a/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb b/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb
new file mode 100644
index 00000000000..53d637a9094
--- /dev/null
+++ b/spec/support/shared_examples/services/issuable_links/destroyable_issuable_links_shared_examples.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+shared_examples 'a destroyable issuable link' do
+ context 'when successfully removes an issuable link' do
+ before do
+ issuable_link.source.resource_parent.add_reporter(user)
+ issuable_link.target.resource_parent.add_reporter(user)
+ end
+
+ it 'removes related issue' do
+ expect { subject }.to change(issuable_link.class, :count).by(-1)
+ end
+
+ it 'creates notes' do
+ # Two-way notes creation
+ expect(SystemNoteService).to receive(:unrelate_issuable)
+ .with(issuable_link.source, issuable_link.target, user)
+ expect(SystemNoteService).to receive(:unrelate_issuable)
+ .with(issuable_link.target, issuable_link.source, user)
+
+ subject
+ end
+
+ it 'returns success message' do
+ is_expected.to eq(message: 'Relation was removed', status: :success)
+ end
+ end
+
+ context 'when failing to remove an issuable link' do
+ it 'does not remove relation' do
+ expect { subject }.not_to change(issuable_link.class, :count).from(1)
+ end
+
+ it 'does not create notes' do
+ expect(SystemNoteService).not_to receive(:unrelate_issuable)
+ end
+
+ it 'returns error message' do
+ is_expected.to eq(message: "No #{issuable_link.class.model_name.human.titleize} found", status: :error, http_status: 404)
+ end
+ end
+end
diff --git a/spec/support/shared_examples/services/rate_limited_service_shared_examples.rb b/spec/support/shared_examples/services/rate_limited_service_shared_examples.rb
new file mode 100644
index 00000000000..b79f1a332a6
--- /dev/null
+++ b/spec/support/shared_examples/services/rate_limited_service_shared_examples.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+# shared examples for testing rate limited functionality of a service
+#
+# following resources are expected to be set (example):
+# it_behaves_like 'rate limited service' do
+# let(:key) { :issues_create }
+# let(:key_scope) { %i[project current_user external_author] }
+# let(:application_limit_key) { :issues_create_limit }
+# let(:service) { described_class.new(project: project, current_user: user, params: { title: 'title' }, spam_params: double) }
+# let(:created_model) { Issue }
+# end
+
+RSpec.shared_examples 'rate limited service' do
+ describe '.rate_limiter_scoped_and_keyed' do
+ it 'is set via the rate_limit call' do
+ expect(described_class.rate_limiter_scoped_and_keyed).to be_a(RateLimitedService::RateLimiterScopedAndKeyed)
+
+ expect(described_class.rate_limiter_scoped_and_keyed.key).to eq(key)
+ expect(described_class.rate_limiter_scoped_and_keyed.opts[:scope]).to eq(key_scope)
+ expect(described_class.rate_limiter_scoped_and_keyed.rate_limiter).to eq(Gitlab::ApplicationRateLimiter)
+ end
+ end
+
+ describe '#rate_limiter_bypassed' do
+ it 'is nil by default' do
+ expect(service.rate_limiter_bypassed).to be_nil
+ end
+ end
+
+ describe '#execute' do
+ before do
+ stub_spam_services
+ end
+
+ context 'when rate limiting is in effect', :freeze_time, :clean_gitlab_redis_rate_limiting do
+ let(:user) { create(:user) }
+
+ before do
+ stub_application_setting(application_limit_key => 1)
+ end
+
+ subject do
+ 2.times { service.execute }
+ end
+
+ context 'when too many requests are sent by one user' do
+ it 'raises an error' do
+ expect do
+ subject
+ end.to raise_error(RateLimitedService::RateLimitedError)
+ end
+
+ it 'creates 1 issue' do
+ expect do
+ subject
+ rescue RateLimitedService::RateLimitedError
+ end.to change { created_model.count }.by(1)
+ end
+ end
+
+ context 'when limit is higher than count of issues being created' do
+ before do
+ stub_application_setting(issues_create_limit: 2)
+ end
+
+ it 'creates 2 issues' do
+ expect { subject }.to change { created_model.count }.by(2)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb b/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb
index 538fd2bb513..105c4247ff7 100644
--- a/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb
+++ b/spec/support/shared_examples/services/security/ci_configuration/create_service_shared_examples.rb
@@ -76,6 +76,18 @@ RSpec.shared_examples_for 'services security ci configuration create service' do
end
end
+ context 'when the project has a non-default ci config file' do
+ before do
+ project.ci_config_path = 'non-default/.gitlab-ci.yml'
+ end
+
+ it 'does track the snowplow event' do
+ subject
+
+ expect_snowplow_event(**snowplow_event)
+ end
+ end
+
unless skip_w_params
context 'with parameters' do
let(:params) { non_empty_params }