summaryrefslogtreecommitdiff
path: root/spec/models/state_note_spec.rb
blob: 0afdf6bbcb912f27db0d42fb46e8ae0fd209e248 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe StateNote do
  describe '.from_event' do
    let_it_be(:author) { create(:user) }
    let_it_be(:project) { create(:project, :repository) }
    let_it_be(:noteable) { create(:issue, author: author, project: project) }

    ResourceStateEvent.states.each do |state, _value|
      context "with event state #{state}" do
        let(:event) { create(:resource_state_event, issue: noteable, state: state, created_at: '2020-02-05') }

        subject { described_class.from_event(event, resource: noteable, resource_parent: project) }

        it_behaves_like 'a synthetic note', state == 'reopened' ? 'opened' : state

        it 'contains the expected values' do
          expect(subject.author).to eq(author)
          expect(subject.created_at).to eq(event.created_at)
          expect(subject.updated_at).to eq(event.created_at)
          expect(subject.note).to eq(state)
        end
      end
    end

    context 'with a mentionable source' do
      subject { described_class.from_event(event, resource: noteable, resource_parent: project) }

      context 'with a commit' do
        let(:commit) { create(:commit, project: project) }
        let(:event) { create(:resource_state_event, issue: noteable, state: :closed, created_at: '2020-02-05', source_commit: commit.id) }

        it 'contains the expected values' do
          expect(subject.author).to eq(author)
          expect(subject.created_at).to eq(event.created_at)
          expect(subject.updated_at).to eq(event.created_at)
          expect(subject.note).to eq("closed via commit #{commit.id}")
        end
      end

      context 'with a merge request' do
        let(:merge_request) { create(:merge_request, source_project: project) }
        let(:event) { create(:resource_state_event, issue: noteable, state: :closed, created_at: '2020-02-05', source_merge_request: merge_request) }

        it 'contains the expected values' do
          expect(subject.author).to eq(author)
          expect(subject.created_at).to eq(event.created_at)
          expect(subject.updated_at).to eq(event.created_at)
          expect(subject.note).to eq("closed via merge request !#{merge_request.iid}")
        end
      end

      context 'when closed by error tracking' do
        let(:event) { create(:resource_state_event, issue: noteable, state: :closed, created_at: '2020-02-05', close_after_error_tracking_resolve: true) }

        it 'contains the expected values' do
          expect(subject.author).to eq(author)
          expect(subject.created_at).to eq(event.created_at)
          expect(subject.updated_at).to eq(event.created_at)
          expect(subject.note).to eq('resolved the corresponding error and closed the issue')
        end
      end

      context 'when closed by promotheus alert' do
        let(:event) { create(:resource_state_event, issue: noteable, state: :closed, created_at: '2020-02-05', close_auto_resolve_prometheus_alert: true) }

        it 'contains the expected values' do
          expect(subject.author).to eq(author)
          expect(subject.created_at).to eq(event.created_at)
          expect(subject.updated_at).to eq(event.created_at)
          expect(subject.note).to eq('automatically closed this incident because the alert resolved')
        end
      end
    end
  end
end