debian-mirror-gitlab/spec/lib/gitlab/pages/cache_control_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.6 KiB
Ruby
Raw Permalink Normal View History

2022-08-13 15:12:31 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe Gitlab::Pages::CacheControl, feature_category: :pages do
2023-03-17 16:20:25 +05:30
RSpec.shared_examples 'cache_control' do |type|
it { expect(subject.cache_key).to match(/pages_domain_for_#{type}_1_*/) }
2022-08-13 15:12:31 +05:30
2023-03-17 16:20:25 +05:30
describe '#clear_cache', :use_clean_rails_redis_caching do
before do
Rails.cache.write("pages_domain_for_#{type}_1", ['settings-hash'])
Rails.cache.write("pages_domain_for_#{type}_1_settings-hash", 'payload')
end
2022-08-13 15:12:31 +05:30
it 'clears the cache' do
2023-04-23 21:23:45 +05:30
cached_keys = [
"pages_domain_for_#{type}_1_settings-hash",
"pages_domain_for_#{type}_1"
]
expect(::Gitlab::AppLogger)
.to receive(:info)
.with(
message: 'clear pages cache',
pages_keys: cached_keys,
pages_type: type,
pages_id: 1
)
2022-08-13 15:12:31 +05:30
expect(Rails.cache)
2023-03-04 22:38:38 +05:30
.to receive(:delete_multi)
2023-04-23 21:23:45 +05:30
.with(cached_keys)
2022-08-13 15:12:31 +05:30
subject.clear_cache
end
end
end
2023-03-17 16:20:25 +05:30
describe '.for_namespace' do
subject(:cache_control) { described_class.for_namespace(1) }
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'cache_control', :namespace
2023-03-17 16:20:25 +05:30
end
2022-08-13 15:12:31 +05:30
2023-03-17 16:20:25 +05:30
describe '.for_domain' do
subject(:cache_control) { described_class.for_domain(1) }
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
it_behaves_like 'cache_control', :domain
2022-08-13 15:12:31 +05:30
end
2022-11-25 23:54:43 +05:30
describe '#cache_key' do
it 'does not change the pages config' do
2023-03-17 16:20:25 +05:30
expect { described_class.new(type: :domain, id: 1).cache_key }
2022-11-25 23:54:43 +05:30
.not_to change(Gitlab.config, :pages)
end
it 'is based on pages settings' do
access_control = Gitlab.config.pages.access_control
2023-03-17 16:20:25 +05:30
cache_key = described_class.new(type: :domain, id: 1).cache_key
2022-11-25 23:54:43 +05:30
stub_config(pages: { access_control: !access_control })
2023-03-17 16:20:25 +05:30
expect(described_class.new(type: :domain, id: 1).cache_key).not_to eq(cache_key)
2022-11-25 23:54:43 +05:30
end
it 'is based on the force_pages_access_control settings' do
force_pages_access_control = ::Gitlab::CurrentSettings.force_pages_access_control
2023-03-17 16:20:25 +05:30
cache_key = described_class.new(type: :domain, id: 1).cache_key
2022-11-25 23:54:43 +05:30
::Gitlab::CurrentSettings.force_pages_access_control = !force_pages_access_control
2023-03-17 16:20:25 +05:30
expect(described_class.new(type: :domain, id: 1).cache_key).not_to eq(cache_key)
2022-11-25 23:54:43 +05:30
end
2023-03-04 22:38:38 +05:30
it 'caches the application settings hash' do
expect(Rails.cache)
.to receive(:write)
2023-03-17 16:20:25 +05:30
.with('pages_domain_for_domain_1', kind_of(Set))
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
described_class.new(type: :domain, id: 1).cache_key
2023-03-04 22:38:38 +05:30
end
2022-11-25 23:54:43 +05:30
end
it 'fails with invalid type' do
expect { described_class.new(type: :unknown, id: nil) }
2023-03-17 16:20:25 +05:30
.to raise_error(ArgumentError, 'type must be :namespace or :domain')
2022-11-25 23:54:43 +05:30
end
2022-08-13 15:12:31 +05:30
end