summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/metrics/dashboard/annotations/delete_spec.rb
blob: c104138b7255e896727f92978bc6f3f39568d74a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Metrics::Dashboard::Annotations::Delete, feature_category: :metrics do
  include GraphqlHelpers

  let_it_be(:current_user) { create(:user) }
  let_it_be(:project) { create(:project, :private, :repository) }
  let_it_be(:environment) { create(:environment, project: project) }
  let_it_be(:annotation) { create(:metrics_dashboard_annotation, environment: environment) }

  let(:variables) { { id: GitlabSchema.id_from_object(annotation).to_s } }
  let(:mutation)  { graphql_mutation(:delete_annotation, variables) }

  def mutation_response
    graphql_mutation_response(:delete_annotation)
  end

  specify { expect(described_class).to require_graphql_authorizations(:admin_metrics_dashboard_annotation) }

  context 'when the user has permission to delete the annotation' do
    before do
      project.add_developer(current_user)
    end

    context 'with valid params' do
      it 'deletes the annotation' do
        expect do
          post_graphql_mutation(mutation, current_user: current_user)
        end.to change { Metrics::Dashboard::Annotation.count }.by(-1)
      end
    end

    context 'with invalid params' do
      let(:variables) { { id: GitlabSchema.id_from_object(project).to_s } }

      it_behaves_like 'a mutation that returns top-level errors' do
        let(:match_errors) { contain_exactly(include('invalid value for id')) }
      end
    end

    context 'when the delete fails' do
      let(:service_response) { { message: 'Annotation has not been deleted', status: :error, last_step: :delete } }

      before do
        allow_next_instance_of(Metrics::Dashboard::Annotations::DeleteService) do |delete_service|
          allow(delete_service).to receive(:execute).and_return(service_response)
        end
      end
      it 'returns the error' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(mutation_response['errors']).to eq([service_response[:message]])
      end
    end
  end

  context 'when the user does not have permission to delete the annotation' do
    before do
      project.add_reporter(current_user)
    end

    it_behaves_like 'a mutation that returns top-level errors', errors: [Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR]

    it 'does not delete the annotation' do
      expect do
        post_graphql_mutation(mutation, current_user: current_user)
      end.not_to change { Metrics::Dashboard::Annotation.count }
    end
  end
end