2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserAvatarsController do
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#show_proxy_letter" do
|
2016-07-27 18:59:58 -04:00
|
|
|
it "returns not found if external avatar is set somewhere else" do
|
|
|
|
SiteSetting.external_system_avatars_url = "https://somewhere.else.com/avatar.png"
|
2018-06-05 09:21:25 -04:00
|
|
|
get "/letter_avatar_proxy/v2/letter/a/aaaaaa/20.png"
|
2016-07-27 18:59:58 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an avatar if we are allowing the proxy" do
|
2020-08-27 07:05:36 -04:00
|
|
|
stub_request(:get, "https://avatars.discourse-cdn.com/v3/letter/a/aaaaaa/360.png").to_return(
|
|
|
|
body: "image",
|
|
|
|
)
|
2019-02-15 12:45:09 -05:00
|
|
|
get "/letter_avatar_proxy/v3/letter/a/aaaaaa/360.png"
|
2016-07-27 18:59:58 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#show" do
|
|
|
|
context "when invalid" do
|
2021-06-02 14:29:47 -04:00
|
|
|
after { FileUtils.rm(Discourse.store.path_for(upload)) }
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
let :upload do
|
|
|
|
File.open(file_from_fixtures("cropped.png")) do |f|
|
|
|
|
UploadCreator.new(f, "test.png").create_for(-1)
|
2018-08-16 02:32:36 -04:00
|
|
|
end
|
2021-06-02 14:29:47 -04:00
|
|
|
end
|
2020-06-16 11:41:15 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
let(:user) do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_avatar.update_columns(custom_upload_id: upload.id)
|
|
|
|
user.update_columns(uploaded_avatar_id: upload.id)
|
|
|
|
user
|
|
|
|
end
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
it "automatically corrects bad avatar extensions" do
|
|
|
|
orig = Discourse.store.path_for(upload)
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
upload.update_columns(
|
|
|
|
original_filename: "bob.jpg",
|
|
|
|
extension: "jpg",
|
|
|
|
url: upload.url + ".jpg",
|
|
|
|
)
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
# at this point file is messed up
|
|
|
|
FileUtils.mv(orig, Discourse.store.path_for(upload))
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
SiteSetting.avatar_sizes = "50"
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
get "/user_avatar/default/#{user.username}/50/#{upload.id}.png"
|
2018-08-16 02:32:36 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
expect(OptimizedImage.where(upload_id: upload.id).count).to eq(1)
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-16 04:48:02 -04:00
|
|
|
|
2021-06-02 14:29:47 -04:00
|
|
|
upload.reload
|
|
|
|
expect(upload.extension).to eq("png")
|
2018-08-16 02:32:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-26 22:02:57 -04:00
|
|
|
it "handles non local content correctly" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2023-05-31 20:00:01 -04:00
|
|
|
SiteSetting.avatar_sizes = "100|98|49"
|
2020-06-05 12:31:58 -04:00
|
|
|
SiteSetting.unicode_usernames = true
|
2020-09-14 07:32:25 -04:00
|
|
|
SiteSetting.s3_cdn_url = "http://cdn.com"
|
2015-05-26 01:41:50 -04:00
|
|
|
|
2020-09-14 07:32:25 -04:00
|
|
|
stub_request(:get, "#{SiteSetting.s3_cdn_url}/something/else").to_return(body: "image")
|
2020-06-05 12:31:58 -04:00
|
|
|
set_cdn_url("http://awesome.com/boom")
|
2015-05-26 01:41:50 -04:00
|
|
|
|
2020-09-14 07:32:25 -04:00
|
|
|
upload =
|
|
|
|
Fabricate(
|
|
|
|
:upload,
|
|
|
|
url: "//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-west-1.amazonaws.com/something",
|
|
|
|
)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-08-23 19:36:11 -04:00
|
|
|
optimized_image =
|
|
|
|
Fabricate(
|
|
|
|
:optimized_image,
|
2017-08-31 00:06:56 -04:00
|
|
|
sha1: SecureRandom.hex << "A" * 8,
|
|
|
|
upload: upload,
|
|
|
|
width: 98,
|
|
|
|
height: 98,
|
2020-09-14 07:32:25 -04:00
|
|
|
url:
|
|
|
|
"//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-west-1.amazonaws.com/something/else",
|
2019-01-03 02:34:10 -05:00
|
|
|
version: OptimizedImage::VERSION,
|
2017-08-31 00:06:56 -04:00
|
|
|
)
|
2015-05-26 22:02:57 -04:00
|
|
|
|
2015-05-26 01:41:50 -04:00
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
|
2018-06-05 09:21:25 -04:00
|
|
|
get "/user_avatar/default/#{user.username}/97/#{upload.id}.png"
|
2015-05-26 01:41:50 -04:00
|
|
|
|
2015-05-27 11:50:49 -04:00
|
|
|
# 98 is closest which is 49 * 2 for retina
|
2015-05-29 12:51:17 -04:00
|
|
|
expect(response).to redirect_to(
|
|
|
|
"http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}_#{OptimizedImage::VERSION}.png",
|
|
|
|
)
|
2015-05-26 22:02:57 -04:00
|
|
|
|
2018-06-05 09:21:25 -04:00
|
|
|
get "/user_avatar/default/#{user.username}/98/#{upload.id}.png"
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2015-12-15 21:48:49 -05:00
|
|
|
expect(response.body).to eq("image")
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(response.headers["Cache-Control"]).to eq("max-age=31556952, public, immutable")
|
2018-08-23 19:36:11 -04:00
|
|
|
expect(response.headers["Last-Modified"]).to eq(optimized_image.upload.created_at.httpdate)
|
2020-06-05 12:31:58 -04:00
|
|
|
|
|
|
|
user.update!(username: "Löwe")
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.encoded_username}/97/#{upload.id}.png"
|
|
|
|
expect(response).to redirect_to(
|
|
|
|
"http://awesome.com/boom/user_avatar/default/#{user.encoded_username(lower: true)}/98/#{upload.id}_#{OptimizedImage::VERSION}.png",
|
|
|
|
)
|
2018-08-23 19:36:11 -04:00
|
|
|
end
|
|
|
|
|
2022-12-02 05:07:25 -05:00
|
|
|
it "redirects to external store when enabled" do
|
|
|
|
global_setting :redirect_avatar_requests, true
|
|
|
|
setup_s3
|
2023-05-31 20:00:01 -04:00
|
|
|
SiteSetting.avatar_sizes = "100|98|49"
|
2022-12-02 05:07:25 -05:00
|
|
|
SiteSetting.s3_cdn_url = "https://s3-cdn.example.com"
|
|
|
|
set_cdn_url("https://app-cdn.example.com")
|
|
|
|
|
|
|
|
upload =
|
|
|
|
Fabricate(
|
|
|
|
:upload,
|
|
|
|
url: "//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-west-1.amazonaws.com/upload/path",
|
|
|
|
)
|
|
|
|
|
|
|
|
optimized_image =
|
|
|
|
Fabricate(
|
|
|
|
:optimized_image,
|
|
|
|
sha1: SecureRandom.hex << "A" * 8,
|
|
|
|
upload: upload,
|
|
|
|
width: 98,
|
|
|
|
height: 98,
|
|
|
|
url:
|
|
|
|
"//#{SiteSetting.s3_upload_bucket}.s3.dualstack.us-west-1.amazonaws.com/optimized/path",
|
|
|
|
version: OptimizedImage::VERSION,
|
|
|
|
)
|
|
|
|
|
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.username}/98/#{upload.id}.png"
|
|
|
|
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
expect(response.location).to eq("https://s3-cdn.example.com/optimized/path")
|
2023-02-15 04:13:19 -05:00
|
|
|
expect(response.headers["Cache-Control"]).to eq(
|
|
|
|
"max-age=3600, public, immutable, stale-while-revalidate=86400",
|
|
|
|
)
|
2022-12-02 05:07:25 -05:00
|
|
|
end
|
|
|
|
|
2019-01-08 03:51:33 -05:00
|
|
|
it "serves new version for old urls" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
SiteSetting.avatar_sizes = "45"
|
|
|
|
|
|
|
|
image = file_from_fixtures("cropped.png")
|
|
|
|
upload = UploadCreator.new(image, "image.png").create_for(user.id)
|
|
|
|
|
|
|
|
user.update_columns(uploaded_avatar_id: upload.id)
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.username}/45/#{upload.id}_1.png"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
image = response.body
|
|
|
|
optimized = upload.get_optimized_image(45, 45, {})
|
|
|
|
|
|
|
|
expect(optimized.filesize).to eq(body.length)
|
|
|
|
|
|
|
|
# clean up images
|
|
|
|
upload.destroy
|
|
|
|
end
|
|
|
|
|
2018-08-23 19:36:11 -04:00
|
|
|
it "serves a correct last modified for render blank" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
get "/user_avatar/default/xxx/51/777.png"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-31 03:07:31 -04:00
|
|
|
|
|
|
|
# this image should be really old so when it is fixed various algorithms pick it up
|
2022-12-13 14:03:53 -05:00
|
|
|
expect(response.headers["Last-Modified"]).to eq(Time.new(1990, 01, 01).httpdate)
|
2015-05-26 01:41:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "serves image even if size missing and its in local mode" do
|
|
|
|
SiteSetting.avatar_sizes = "50"
|
|
|
|
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
|
2018-06-05 09:21:25 -04:00
|
|
|
get "/user_avatar/default/#{user.username}/51/#{upload.id}.png"
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2015-05-26 01:41:50 -04:00
|
|
|
end
|
2020-06-05 12:31:58 -04:00
|
|
|
|
|
|
|
it "serves the correct image when the upload id changed" do
|
|
|
|
SiteSetting.avatar_sizes = "50"
|
|
|
|
SiteSetting.unicode_usernames = true
|
|
|
|
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
another_upload = Fabricate(:upload)
|
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.username}/50/#{another_upload.id}.png"
|
|
|
|
expect(response).to redirect_to(
|
|
|
|
"http://test.localhost/user_avatar/default/#{user.username_lower}/50/#{upload.id}_#{OptimizedImage::VERSION}.png",
|
|
|
|
)
|
|
|
|
|
|
|
|
user.update!(username: "Löwe")
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.encoded_username}/50/#{another_upload.id}.png"
|
|
|
|
expect(response).to redirect_to(
|
|
|
|
"http://test.localhost/user_avatar/default/#{user.encoded_username(lower: true)}/50/#{upload.id}_#{OptimizedImage::VERSION}.png",
|
|
|
|
)
|
|
|
|
end
|
2015-05-26 01:41:50 -04:00
|
|
|
end
|
|
|
|
end
|