Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-12-09 09:07:18 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-12-09 09:07:18 +0300
commiteba9cac2e8181aaf1a08e1730f440155c765741e (patch)
tree0a1a4bd55cf4c59c95c368a7ed2386c2b023c84a /spec
parent8e7172c40e205a27c204de448ac4fe0551ffa1af (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r--spec/graphql/mutations/issues/link_alerts_spec.rb84
-rw-r--r--spec/requests/api/graphql/mutations/issues/link_alerts_spec.rb65
-rw-r--r--spec/services/incident_management/link_alerts/create_service_spec.rb98
-rw-r--r--spec/support/helpers/countries_controller_test_helper.rb9
4 files changed, 247 insertions, 9 deletions
diff --git a/spec/graphql/mutations/issues/link_alerts_spec.rb b/spec/graphql/mutations/issues/link_alerts_spec.rb
new file mode 100644
index 00000000000..a6ce5cdd7ab
--- /dev/null
+++ b/spec/graphql/mutations/issues/link_alerts_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Mutations::Issues::LinkAlerts, feature_category: :incident_management do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:guest) { create(:user) }
+ let_it_be(:developer) { create(:user) }
+ let_it_be(:issue) { create(:incident, project: project) }
+ let_it_be(:alert1) { create(:alert_management_alert, project: project) }
+ let_it_be(:alert2) { create(:alert_management_alert, project: project) }
+
+ let(:mutation) { described_class.new(object: nil, context: { current_user: user }, field: nil) }
+
+ specify { expect(described_class).to require_graphql_authorizations(:update_issue, :admin_issue) }
+
+ before_all do
+ project.add_guest(guest)
+ project.add_developer(developer)
+ end
+
+ describe '#resolve' do
+ let(:alert_references) { [alert1.to_reference, alert2.details_url, 'invalid-reference'] }
+
+ subject(:resolve) do
+ mutation.resolve(
+ project_path: issue.project.full_path,
+ iid: issue.iid,
+ alert_references: alert_references
+ )
+ end
+
+ context 'when the user is a guest' do
+ let(:user) { guest }
+
+ it 'raises an error' do
+ expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+
+ context 'when a user is also an author' do
+ let!(:issue) { create(:incident, project: project, author: user) }
+
+ it 'raises an error' do
+ expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+
+ context 'when a user is also an assignee' do
+ let!(:issue) { create(:incident, project: project, assignee_ids: [user.id]) }
+
+ it 'raises an error' do
+ expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
+ end
+ end
+ end
+
+ context 'when the user is a developer' do
+ let(:user) { developer }
+
+ context 'when issue type is an incident' do
+ it 'calls LinkAlerts::CreateService with correct arguments' do
+ expect(::IncidentManagement::LinkAlerts::CreateService)
+ .to receive(:new)
+ .with(issue, user, alert_references)
+ .and_call_original
+
+ resolve
+ end
+
+ it 'returns no errors' do
+ expect(resolve[:errors]).to be_empty
+ end
+ end
+
+ context 'when issue type is not an incident' do
+ let!(:issue) { create(:issue, project: project) }
+
+ it 'does not update alert_management_alerts' do
+ expect { resolve }.not_to change { issue.alert_management_alerts }
+ end
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/graphql/mutations/issues/link_alerts_spec.rb b/spec/requests/api/graphql/mutations/issues/link_alerts_spec.rb
new file mode 100644
index 00000000000..85e21952f47
--- /dev/null
+++ b/spec/requests/api/graphql/mutations/issues/link_alerts_spec.rb
@@ -0,0 +1,65 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe 'Link alerts to an incident', feature_category: :incident_management do
+ include GraphqlHelpers
+
+ let_it_be(:user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+ let_it_be(:linked_alert) { create(:alert_management_alert, project: project) }
+ let_it_be(:alert1) { create(:alert_management_alert, project: project) }
+ let_it_be(:alert2) { create(:alert_management_alert, project: project) }
+ let_it_be(:incident) { create(:incident, project: project, alert_management_alerts: [linked_alert]) }
+
+ let(:mutation) do
+ variables = {
+ project_path: project.full_path,
+ iid: incident.iid.to_s,
+ alert_references: [alert1.to_reference, alert2.details_url]
+ }
+
+ graphql_mutation(:issue_link_alerts, variables,
+ <<-QL.strip_heredoc
+ clientMutationId
+ errors
+ issue {
+ iid
+ alertManagementAlerts {
+ nodes {
+ iid
+ }
+ }
+ }
+ QL
+ )
+ end
+
+ def mutation_response
+ graphql_mutation_response(:issue_link_alerts)
+ end
+
+ context 'when the user is not allowed to update the incident' do
+ it 'returns an error' do
+ error = Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR
+ post_graphql_mutation(mutation, current_user: user)
+
+ expect(response).to have_gitlab_http_status(:success)
+ expect(graphql_errors).to include(a_hash_including('message' => error))
+ end
+ end
+
+ context 'when the user is allowed to update the incident' do
+ before do
+ project.add_developer(user)
+ end
+
+ it 'links alerts to the incident' do
+ post_graphql_mutation(mutation, current_user: user)
+
+ expect(response).to have_gitlab_http_status(:success)
+ expected_response = [linked_alert, alert1, alert2].map { |a| { 'iid' => a.iid.to_s } }
+ expect(mutation_response.dig('issue', 'alertManagementAlerts', 'nodes')).to match_array(expected_response)
+ end
+ end
+end
diff --git a/spec/services/incident_management/link_alerts/create_service_spec.rb b/spec/services/incident_management/link_alerts/create_service_spec.rb
new file mode 100644
index 00000000000..51cdb0d18a6
--- /dev/null
+++ b/spec/services/incident_management/link_alerts/create_service_spec.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe IncidentManagement::LinkAlerts::CreateService, feature_category: :incident_management do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:another_project) { create(:project) }
+ let_it_be(:linked_alert) { create(:alert_management_alert, project: project) }
+ let_it_be(:alert1) { create(:alert_management_alert, project: project) }
+ let_it_be(:alert2) { create(:alert_management_alert, project: project) }
+ let_it_be(:external_alert) { create(:alert_management_alert, project: another_project) }
+ let_it_be(:incident) { create(:incident, project: project, alert_management_alerts: [linked_alert]) }
+ let_it_be(:guest) { create(:user) }
+ let_it_be(:developer) { create(:user) }
+ let_it_be(:another_developer) { create(:user) }
+
+ before_all do
+ project.add_guest(guest)
+ project.add_developer(developer)
+ project.add_developer(another_developer)
+
+ another_project.add_guest(guest)
+ another_project.add_developer(developer)
+ end
+
+ describe '#execute' do
+ subject(:execute) { described_class.new(incident, current_user, alert_references).execute }
+
+ let(:alert_references) { [alert1.to_reference, alert2.details_url] }
+
+ context 'when current user is a guest' do
+ let(:current_user) { guest }
+
+ it 'responds with error', :aggregate_failures do
+ response = execute
+
+ expect(response).to be_error
+ expect(response.message).to eq('You have insufficient permissions to manage alerts for this project')
+ end
+
+ it 'does not link alerts to the incident' do
+ expect { execute }.not_to change { incident.reload.alert_management_alerts.to_a }
+ end
+ end
+
+ context 'when current user is a developer' do
+ let(:current_user) { developer }
+
+ it 'responds with success', :aggregate_failures do
+ response = execute
+
+ expect(response).to be_success
+ expect(response.payload[:incident]).to eq(incident)
+ end
+
+ it 'links alerts to the incident' do
+ expect { execute }
+ .to change { incident.reload.alert_management_alerts.to_a }
+ .from([linked_alert])
+ .to([linked_alert, alert1, alert2])
+ end
+
+ context 'when linking an already linked alert' do
+ let(:alert_references) { [linked_alert.details_url] }
+
+ it 'does not change incident alerts list' do
+ expect { execute }.not_to change { incident.reload.alert_management_alerts.to_a }
+ end
+ end
+
+ context 'when linking an alert from another project' do
+ let(:alert_references) { [external_alert.details_url] }
+
+ it 'links an external alert to the incident' do
+ expect { execute }
+ .to change { incident.reload.alert_management_alerts.to_a }
+ .from([linked_alert])
+ .to([linked_alert, external_alert])
+ end
+ end
+ end
+
+ context 'when current user does not have permission to read alerts on external project' do
+ let(:current_user) { another_developer }
+
+ context 'when linking alerts from current and external projects' do
+ let(:alert_references) { [alert1.details_url, external_alert.details_url] }
+
+ it 'links only alerts the current user can read' do
+ expect { execute }
+ .to change { incident.reload.alert_management_alerts.to_a }
+ .from([linked_alert])
+ .to([linked_alert, alert1])
+ end
+ end
+ end
+ end
+end
diff --git a/spec/support/helpers/countries_controller_test_helper.rb b/spec/support/helpers/countries_controller_test_helper.rb
deleted file mode 100644
index 5d36a29bba7..00000000000
--- a/spec/support/helpers/countries_controller_test_helper.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-# frozen_string_literal: true
-
-module CountriesControllerTestHelper
- def world_deny_list
- ::World::DENYLIST + ::World::JH_MARKET
- end
-end
-
-CountriesControllerTestHelper.prepend_mod