summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/slash_commands/issue_new_spec.rb
blob: 29a941f3691c3fb32d22c8ebb5e6efad54946bc8 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::SlashCommands::IssueNew do
  describe '#execute' do
    let(:project) { create(:project) }
    let(:user) { create(:user) }
    let(:chat_name) { double(:chat_name, user: user) }
    let(:regex_match) { described_class.match("issue create bird is the word") }

    before do
      project.add_maintainer(user)
    end

    subject do
      described_class.new(project, chat_name).execute(regex_match)
    end

    context 'without description' do
      it 'creates the issue' do
        expect { subject }.to change { project.issues.count }.by(1)

        expect(subject[:response_type]).to be(:in_channel)
      end
    end

    context 'with description' do
      let(:description) { "Surfin bird" }
      let(:regex_match) { described_class.match("issue create bird is the word\n#{description}") }

      it 'creates the issue with description' do
        subject

        expect(Issue.last.description).to eq(description)
      end
    end

    context "with more newlines between the title and the description" do
      let(:description) { "Surfin bird" }
      let(:regex_match) { described_class.match("issue create bird is the word\n\n#{description}\n") }

      it 'creates the issue' do
        expect { subject }.to change { project.issues.count }.by(1)
      end
    end

    context 'issue cannot be created' do
      let!(:issue) { create(:issue, project: project, title: 'bird is the word') }
      let(:regex_match) { described_class.match("issue create #{'a' * 512}}") }

      it 'displays the errors' do
        expect(subject[:response_type]).to be(:ephemeral)
        expect(subject[:text]).to match("- Title is too long")
      end

      context 'when create issue service return an unrecoverable error' do
        let(:regex_match) { described_class.match("issue create title}") }

        before do
          allow_next_instance_of(Issues::CreateService) do |create_service|
            allow(create_service).to receive(:execute).and_return(ServiceResponse.error(message: 'unauthorized'))
          end
        end

        it 'displays the errors' do
          expect(subject[:response_type]).to be(:ephemeral)
          expect(subject[:text]).to eq('unauthorized')
        end
      end
    end
  end

  describe '.match' do
    it 'matches the title without description' do
      match = described_class.match("issue create my title")

      expect(match[:title]).to eq('my title')
      expect(match[:description]).to eq("")
    end

    it 'matches the title with description' do
      match = described_class.match("issue create my title\n\ndescription")

      expect(match[:title]).to eq('my title')
      expect(match[:description]).to eq('description')
    end

    it 'matches the alias new' do
      match = described_class.match("issue new my title")

      expect(match).not_to be_nil
      expect(match[:title]).to eq('my title')
    end
  end
end