summaryrefslogtreecommitdiff
path: root/spec/services/issues/reopen_service_spec.rb
blob: 6013826f9b1efd22b9f8c2851ebff4f28030c809 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Issues::ReopenService do
  let(:project) { create(:project) }
  let(:issue) { create(:issue, :closed, project: project) }

  describe '#execute' do
    context 'when user is not authorized to reopen issue' do
      it 'does not reopen the issue' do
        guest = create(:user)
        project.add_guest(guest)

        described_class.new(project: project, current_user: guest).execute(issue)

        expect(issue).to be_closed
      end

      context 'when skip_authorization is true' do
        it 'does close the issue even if user is not authorized' do
          non_authorized_user = create(:user)

          service = described_class.new(project: project, current_user: non_authorized_user)

          expect do
            service.execute(issue, skip_authorization: true)
          end.to change { issue.reload.state }.from('closed').to('opened')
        end
      end
    end

    context 'when user is authorized to reopen issue' do
      let(:user) { create(:user) }

      subject(:execute) { described_class.new(project: project, current_user: user).execute(issue) }

      before do
        project.add_maintainer(user)
      end

      it 'invalidates counter cache for assignees' do
        issue.assignees << user
        expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)

        execute
      end

      it 'refreshes the number of opened issues' do
        expect do
          execute

          BatchLoader::Executor.clear_current
        end.to change { project.open_issues_count }.from(0).to(1)
      end

      it 'deletes milestone issue counters cache' do
        issue.update!(milestone: create(:milestone, project: project))

        expect_next_instance_of(Milestones::ClosedIssuesCountService, issue.milestone) do |service|
          expect(service).to receive(:delete_cache).and_call_original
        end

        execute
      end

      it 'does not create timeline event' do
        expect { execute }.not_to change { issue.incident_management_timeline_events.count }
      end

      context 'issue is incident type' do
        let(:issue) { create(:incident, :closed, project: project) }
        let(:current_user) { user }

        it_behaves_like 'an incident management tracked event', :incident_management_incident_reopened

        it 'creates a timeline event' do
          expect(IncidentManagement::TimelineEvents::CreateService)
            .to receive(:reopen_incident)
            .with(issue, current_user)
            .and_call_original

          expect { execute }.to change { issue.incident_management_timeline_events.count }.by(1)
        end
      end

      context 'when issue is not confidential' do
        let(:expected_payload) do
          include(
            event_type: 'issue',
            object_kind: 'issue',
            changes: {
              closed_at: { current: nil, previous: kind_of(Time) },
              state_id: { current: 1, previous: 2 },
              updated_at: { current: kind_of(Time), previous: kind_of(Time) }
            },
            object_attributes: include(
              state: 'opened',
              action: 'reopen'
            )
          )
        end

        it 'executes issue hooks' do
          expect(project).to receive(:execute_hooks).with(expected_payload, :issue_hooks)
          expect(project).to receive(:execute_integrations).with(expected_payload, :issue_hooks)

          execute
        end
      end

      context 'when issue is confidential' do
        let(:issue) { create(:issue, :confidential, :closed, project: project) }

        it 'executes confidential issue hooks' do
          expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :confidential_issue_hooks)
          expect(project).to receive(:execute_integrations).with(an_instance_of(Hash), :confidential_issue_hooks)

          execute
        end
      end
    end
  end
end