debian-mirror-gitlab/spec/lib/gitlab/avatar_cache_spec.rb

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

114 lines
2.7 KiB
Ruby
Raw Normal View History

2021-04-17 20:07:23 +05:30
# frozen_string_literal: true
require "spec_helper"
RSpec.describe Gitlab::AvatarCache, :clean_gitlab_redis_cache do
def with(&blk)
Gitlab::Redis::Cache.with(&blk) # rubocop:disable CodeReuse/ActiveRecord
end
def read(key, subkey)
with do |redis|
redis.hget(key, subkey)
end
end
let(:thing) { double("thing", avatar_path: avatar_path) }
let(:avatar_path) { "/avatars/my_fancy_avatar.png" }
let(:key) { described_class.send(:email_key, "foo@bar.com") }
let(:perform_fetch) do
described_class.by_email("foo@bar.com", 20, 2, true) do
thing.avatar_path
end
end
describe "#by_email" do
it "writes a new value into the cache" do
expect(read(key, "20:2:true")).to eq(nil)
perform_fetch
expect(read(key, "20:2:true")).to eq(avatar_path)
end
it "finds the cached value and doesn't execute the block" do
expect(thing).to receive(:avatar_path).once
described_class.by_email("foo@bar.com", 20, 2, true) do
thing.avatar_path
end
described_class.by_email("foo@bar.com", 20, 2, true) do
thing.avatar_path
end
end
it "finds the cached value in the request store and doesn't execute the block" do
expect(thing).to receive(:avatar_path).once
Gitlab::WithRequestStore.with_request_store do
described_class.by_email("foo@bar.com", 20, 2, true) do
thing.avatar_path
end
described_class.by_email("foo@bar.com", 20, 2, true) do
thing.avatar_path
end
expect(Gitlab::SafeRequestStore.read([key, "20:2:true"])).to eq(avatar_path)
end
end
end
describe "#delete_by_email" do
2023-07-09 08:55:56 +05:30
shared_examples 'delete emails' do
subject { described_class.delete_by_email(*emails) }
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
before do
perform_fetch
end
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
context "no emails, somehow" do
let(:emails) { [] }
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
it { is_expected.to eq(0) }
end
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
context "single email" do
let(:emails) { "foo@bar.com" }
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
it "removes the email" do
expect(read(key, "20:2:true")).to eq(avatar_path)
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(subject).to eq(1)
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(read(key, "20:2:true")).to eq(nil)
end
2021-04-17 20:07:23 +05:30
end
2023-07-09 08:55:56 +05:30
context "multiple emails" do
let(:emails) { ["foo@bar.com", "missing@baz.com"] }
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
it "removes the emails it finds" do
expect(read(key, "20:2:true")).to eq(avatar_path)
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(subject).to eq(1)
2021-04-17 20:07:23 +05:30
2023-07-09 08:55:56 +05:30
expect(read(key, "20:2:true")).to eq(nil)
end
end
end
context 'when feature flag disabled' do
before do
stub_feature_flags(use_pipeline_over_multikey: false)
2021-04-17 20:07:23 +05:30
end
2023-07-09 08:55:56 +05:30
it_behaves_like 'delete emails'
2021-04-17 20:07:23 +05:30
end
2023-07-09 08:55:56 +05:30
it_behaves_like 'delete emails'
2021-04-17 20:07:23 +05:30
end
end