File: open_issues_count_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (84 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Groups::OpenIssuesCountService, :use_clean_rails_memory_store_caching, feature_category: :groups_and_projects do
  let_it_be(:group) { create(:group, :public) }
  let_it_be(:project) { create(:project, :public, namespace: group) }
  let_it_be(:user) { create(:user) }
  let_it_be(:issue) { create(:issue, :opened, project: project) }
  let_it_be(:confidential) { create(:issue, :opened, confidential: true, project: project) }
  let_it_be(:closed) { create(:issue, :closed, project: project) }

  subject { described_class.new(group, user) }

  describe '#relation_for_count' do
    before do
      allow(IssuesFinder).to receive(:new).and_call_original
    end

    it 'uses the IssuesFinder to scope issues' do
      expect(IssuesFinder)
        .to receive(:new)
        .with(
          user,
          group_id: group.id, state: 'opened', non_archived: true, include_subgroups: true, confidential: false
        )

      subject.count
    end
  end

  describe '#count' do
    context 'when user is nil' do
      it 'does not include confidential issues in the issue count' do
        expect(described_class.new(group).count).to eq(1)
      end
    end

    context 'when user is provided' do
      context 'when user can read confidential issues' do
        before do
          group.add_reporter(user)
        end

        it 'returns the right count with confidential issues' do
          expect(subject.count).to eq(2)
        end
      end

      context 'when user cannot read confidential issues' do
        before do
          group.add_guest(user)
        end

        it 'does not include confidential issues' do
          expect(subject.count).to eq(1)
        end
      end

      it_behaves_like 'a counter caching service with threshold'
    end

    context 'when fast_timeout is enabled' do
      subject { described_class.new(group, fast_timeout: true) }

      it 'executes the query with a fast timeout' do
        expect(ApplicationRecord).to receive(:with_fast_read_statement_timeout).and_call_original

        expect(subject.count).to eq(1)
      end
    end
  end

  describe '#clear_all_cache_keys' do
    it 'calls `Rails.cache.delete` with the correct keys' do
      expect(Rails.cache).to receive(:delete)
        .with(['groups', 'open_issues_count_service', 1, group.id, described_class::PUBLIC_COUNT_KEY])
      expect(Rails.cache).to receive(:delete)
        .with(['groups', 'open_issues_count_service', 1, group.id, described_class::TOTAL_COUNT_KEY])

      subject.clear_all_cache_keys
    end
  end
end