* Add article's record key to Fastly Surrogate-Key to separate caches After #4744 we successfully setup an auto purging system based on Fastly's surrogate key based cache and purging API. Unfortunately this worked only in the `:show` action as comments are their own tree-based resource. The `:index` though, is related to a single article, so without the article's record key in the `Surrogate-Key` header the first time we hit the API the result would become the value stored in Fastly's API, for all articles. * Fix commentable mock for job spec * Add comment.commentable.purge to the worker as well * Add more thorough tests for commentable
69 lines
1.9 KiB
Ruby
69 lines
1.9 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe Comments::BustCacheWorker, type: :worker do
|
|
include_examples "#enqueues_on_correct_queue", "high_priority", 1
|
|
|
|
describe "#perform" do
|
|
let(:worker) { subject }
|
|
|
|
before do
|
|
allow(EdgeCache::Commentable::Bust).to receive(:call)
|
|
end
|
|
|
|
context "with comment" do
|
|
let(:comment) { double }
|
|
let(:comment_id) { 1 }
|
|
let(:commentable) { double }
|
|
|
|
before do
|
|
allow(comment).to receive(:commentable).and_return(commentable)
|
|
allow(Comment).to receive(:find_by).with(id: comment_id).and_return(comment)
|
|
allow(comment).to receive(:purge)
|
|
allow(commentable).to receive(:purge)
|
|
end
|
|
|
|
it "calls the service" do
|
|
worker.perform(comment_id)
|
|
|
|
expect(EdgeCache::Commentable::Bust).to have_received(:call).with(comment.commentable).once
|
|
end
|
|
|
|
it "does not call purge on comment when commentable is not available" do
|
|
allow(comment).to receive(:commentable).and_return(nil)
|
|
|
|
worker.perform(comment_id)
|
|
|
|
expect(comment).not_to have_received(:purge)
|
|
expect(commentable).not_to have_received(:purge)
|
|
end
|
|
|
|
it "does not call purge on commentable when commentable is not available" do
|
|
allow(comment).to receive(:commentable).and_return(nil)
|
|
|
|
worker.perform(comment_id)
|
|
|
|
expect(commentable).not_to have_received(:purge)
|
|
end
|
|
|
|
it "does not call the service when commentable is not available" do
|
|
allow(comment).to receive(:commentable).and_return(nil)
|
|
|
|
worker.perform(comment_id)
|
|
|
|
expect(EdgeCache::Commentable::Bust).not_to have_received(:call)
|
|
end
|
|
end
|
|
|
|
context "without comment" do
|
|
it "does not break" do
|
|
expect { worker.perform(nil) }.not_to raise_error
|
|
end
|
|
|
|
it "doesn't call the service" do
|
|
worker.perform(nil)
|
|
|
|
expect(EdgeCache::Commentable::Bust).not_to have_received(:call)
|
|
end
|
|
end
|
|
end
|
|
end
|