Merge pull request #1865 from basecamp/public-avatar-caching

Serve own avatar from its own endpoint
This commit is contained in:
Kevin McConnell
2025-12-15 12:20:28 +00:00
committed by GitHub
10 changed files with 91 additions and 41 deletions
@@ -0,0 +1,33 @@
require "test_helper"
class My::AvatarsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :david
end
test "show own initials" do
get my_avatar_path
assert_match "image/svg+xml", @response.content_type
assert_match "private", @response.headers["Cache-Control"]
assert_match "must-revalidate", @response.headers["Cache-Control"]
end
test "show own image redirects to the blob url" do
users(:david).avatar.attach(io: File.open(file_fixture("moon.jpg")), filename: "moon.jpg", content_type: "image/jpeg")
assert users(:david).avatar.attached?
get my_avatar_path
assert_redirected_to rails_blob_url(users(:david).avatar.variant(:thumb), disposition: "inline")
assert_match "private", @response.headers["Cache-Control"]
assert_match "must-revalidate", @response.headers["Cache-Control"]
end
test "requires authentication" do
sign_out
get my_avatar_path
assert_response :redirect
end
end
@@ -12,17 +12,10 @@ class Users::AvatarsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to ActionController::Base.helpers.image_path("system_user.png")
end
test "show own initials without caching" do
test "show initials with public caching" do
get user_avatar_path(users(:david))
assert_match "image/svg+xml", @response.content_type
assert @response.cache_control[:private]
assert_equal "0", @response.cache_control[:max_age]
end
test "show other initials with caching" do
get user_avatar_path(users(:kevin))
assert_match "image/svg+xml", @response.content_type
assert @response.cache_control[:private]
assert @response.cache_control[:public]
assert_equal 30.minutes.to_s, @response.cache_control[:max_age]
end