summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/incident_management/timeline_event_tag/create_spec.rb
blob: 57a32f7e4bcd257fe9735e3c5ef890c56e78d8b7 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::IncidentManagement::TimelineEventTag::Create do
  let_it_be(:current_user) { create(:user) }
  let_it_be_with_reload(:project) { create(:project) }

  let(:args) { { name: 'Test tag 1' } }

  before do
    project.add_maintainer(current_user)
  end

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

  describe '#resolve' do
    subject(:resolve) { mutation_for(project, current_user).resolve(project_path: project.full_path, **args) }

    context 'when user has permission to create timeline event tag' do
      it 'adds the tag to the project' do
        expect { resolve }.to change(IncidentManagement::TimelineEventTag, :count).by(1)
        expect(project.incident_management_timeline_event_tags.by_names(['Test tag 1']).pluck_names)
          .to match_array(['Test tag 1'])
      end
    end

    context 'when TimelineEventTags::CreateService responds with an error' do
      let(:args) { {} }

      it 'returns errors' do
        expect(resolve).to eq(timeline_event_tag: nil, errors: ["Name can't be blank and Name is invalid"])
      end
    end

    context 'when user has no permissions to create tags on a project' do
      before do
        project.add_developer(current_user)
      end

      it 'raises an error' do
        expect { resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end
  end

  private

  def mutation_for(project, user)
    described_class.new(object: project, context: { current_user: user }, field: nil)
  end
end