debian-mirror-gitlab/spec/services/projects/open_issues_count_service_spec.rb

107 lines
3.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Projects::OpenIssuesCountService, :use_clean_rails_memory_store_caching do
2019-12-04 20:38:33 +05:30
let(:project) { create(:project) }
2021-11-11 11:23:49 +05:30
let(:user) { create(:user) }
let(:banned_user) { create(:user, :banned) }
2020-01-01 13:55:28 +05:30
2021-11-11 11:23:49 +05:30
subject { described_class.new(project, user) }
2019-12-04 20:38:33 +05:30
it_behaves_like 'a counter caching service'
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
before do
create(:issue, :opened, project: project)
create(:issue, :opened, confidential: true, project: project)
create(:issue, :opened, author: banned_user, project: project)
create(:issue, :closed, project: project)
described_class.new(project).refresh_cache
end
2019-12-04 20:38:33 +05:30
describe '#count' do
2021-11-11 11:23:49 +05:30
shared_examples 'counts public issues, does not count hidden or confidential' do
it 'counts only public issues' do
expect(subject.count).to eq(1)
end
2018-11-08 19:23:39 +05:30
2021-11-11 11:23:49 +05:30
it 'uses PUBLIC_COUNT_WITHOUT_HIDDEN_KEY cache key' do
expect(subject.cache_key).to include('project_open_public_issues_without_hidden_count')
2018-11-08 19:23:39 +05:30
end
end
2021-11-11 11:23:49 +05:30
context 'when user is nil' do
let(:user) { nil }
it_behaves_like 'counts public issues, does not count hidden or confidential'
end
2018-11-08 19:23:39 +05:30
2021-11-11 11:23:49 +05:30
context 'when user is provided' do
2018-11-08 19:23:39 +05:30
context 'when user can read confidential issues' do
before do
project.add_reporter(user)
end
2021-11-11 11:23:49 +05:30
it 'includes confidential issues and does not include hidden issues in count' do
expect(subject.count).to eq(2)
2018-11-08 19:23:39 +05:30
end
2021-11-11 11:23:49 +05:30
it 'uses TOTAL_COUNT_WITHOUT_HIDDEN_KEY cache key' do
expect(subject.cache_key).to include('project_open_issues_without_hidden_count')
2018-11-08 19:23:39 +05:30
end
end
2021-11-11 11:23:49 +05:30
context 'when user cannot read confidential or hidden issues' do
2018-11-08 19:23:39 +05:30
before do
project.add_guest(user)
end
2021-11-11 11:23:49 +05:30
it_behaves_like 'counts public issues, does not count hidden or confidential'
end
context 'when user is an admin' do
let_it_be(:user) { create(:user, :admin) }
context 'when admin mode is enabled', :enable_admin_mode do
it 'includes confidential and hidden issues in count' do
expect(subject.count).to eq(3)
end
2018-11-08 19:23:39 +05:30
2021-11-11 11:23:49 +05:30
it 'uses TOTAL_COUNT_KEY cache key' do
expect(subject.cache_key).to include('project_open_issues_including_hidden_count')
end
2018-11-08 19:23:39 +05:30
end
2021-11-11 11:23:49 +05:30
context 'when admin mode is disabled' do
it_behaves_like 'counts public issues, does not count hidden or confidential'
2018-11-08 19:23:39 +05:30
end
end
2018-03-17 18:26:18 +05:30
end
2021-11-11 11:23:49 +05:30
end
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
describe '#refresh_cache', :aggregate_failures do
context 'when cache is empty' do
it 'refreshes cache keys correctly' do
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::PUBLIC_COUNT_WITHOUT_HIDDEN_KEY))).to eq(1)
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::TOTAL_COUNT_WITHOUT_HIDDEN_KEY))).to eq(2)
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::TOTAL_COUNT_KEY))).to eq(3)
2018-11-08 19:23:39 +05:30
end
2021-11-11 11:23:49 +05:30
end
2018-11-08 19:23:39 +05:30
2021-11-11 11:23:49 +05:30
context 'when cache is outdated' do
it 'refreshes cache keys correctly' do
create(:issue, :opened, project: project)
create(:issue, :opened, confidential: true, project: project)
create(:issue, :opened, author: banned_user, project: project)
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
described_class.new(project).refresh_cache
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::PUBLIC_COUNT_WITHOUT_HIDDEN_KEY))).to eq(2)
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::TOTAL_COUNT_WITHOUT_HIDDEN_KEY))).to eq(4)
expect(Rails.cache.read(described_class.new(project).cache_key(described_class::TOTAL_COUNT_KEY))).to eq(6)
2018-11-08 19:23:39 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
end