summaryrefslogtreecommitdiff
path: root/spec/services/issues/reopen_service_spec.rb
blob: f04029e64aa5ce1a062b16d5f3afafaee54a8ca1 (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
# frozen_string_literal: true

require 'spec_helper'

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
      before do
        guest = create(:user)
        project.add_guest(guest)

        perform_enqueued_jobs do
          described_class.new(project, guest).execute(issue)
        end
      end

      it 'does not reopen the issue' do
        expect(issue).to be_closed
      end
    end

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

      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)

        described_class.new(project, user).execute(issue)
      end

      it 'refreshes the number of opened issues' do
        service = described_class.new(project, user)

        expect { service.execute(issue) }
          .to change { project.open_issues_count }.from(0).to(1)
      end

      context 'when issue is not confidential' do
        it 'executes issue hooks' do
          expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :issue_hooks)
          expect(project).to receive(:execute_services).with(an_instance_of(Hash), :issue_hooks)

          described_class.new(project, user).execute(issue)
        end
      end

      context 'when issue is confidential' do
        it 'executes confidential issue hooks' do
          issue = create(:issue, :confidential, :closed, project: project)

          expect(project).to receive(:execute_hooks).with(an_instance_of(Hash), :confidential_issue_hooks)
          expect(project).to receive(:execute_services).with(an_instance_of(Hash), :confidential_issue_hooks)

          described_class.new(project, user).execute(issue)
        end
      end
    end
  end
end