2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2015-05-26 13:41:50 +08:00
|
|
|
|
|
|
|
describe UserAvatarsController do
|
|
|
|
|
2016-07-28 06:59:58 +08:00
|
|
|
context 'show_proxy_letter' do
|
|
|
|
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 21:21:25 +08:00
|
|
|
get "/letter_avatar_proxy/v2/letter/a/aaaaaa/20.png"
|
2016-07-28 06:59:58 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an avatar if we are allowing the proxy' do
|
2018-06-05 21:21:25 +08:00
|
|
|
get "/letter_avatar_proxy/v2/letter/a/aaaaaa/360.png"
|
2016-07-28 06:59:58 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-26 13:41:50 +08:00
|
|
|
context 'show' do
|
2018-08-16 14:32:36 +08:00
|
|
|
|
|
|
|
context 'invalid' do
|
|
|
|
after do
|
|
|
|
FileUtils.rm(Discourse.store.path_for(upload))
|
|
|
|
end
|
|
|
|
# travis is not good here, no image magick
|
|
|
|
if !ENV["TRAVIS"]
|
|
|
|
let :upload do
|
|
|
|
File.open("#{Rails.root}/spec/fixtures/images/cropped.png") do |f|
|
|
|
|
UploadCreator.new(
|
|
|
|
f,
|
|
|
|
"test.png"
|
|
|
|
).create_for(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'automatically corrects bad avatar extensions' do
|
|
|
|
orig = Discourse.store.path_for(upload)
|
|
|
|
|
|
|
|
upload.update_columns(
|
|
|
|
original_filename: 'bob.jpg',
|
|
|
|
extension: 'jpg',
|
|
|
|
url: upload.url + '.jpg'
|
|
|
|
)
|
|
|
|
|
|
|
|
# at this point file is messed up
|
|
|
|
FileUtils.mv(orig, Discourse.store.path_for(upload))
|
|
|
|
|
|
|
|
SiteSetting.avatar_sizes = "50"
|
|
|
|
|
|
|
|
get "/user_avatar/default/#{user.username}/50/#{upload.id}.png"
|
|
|
|
|
|
|
|
expect(OptimizedImage.where(upload_id: upload.id).count).to eq(1)
|
|
|
|
expect(response.status).to eq(200)
|
2018-08-16 16:48:02 +08:00
|
|
|
|
|
|
|
upload.reload
|
|
|
|
expect(upload.extension).to eq('png')
|
2018-08-16 14:32:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-27 10:02:57 +08:00
|
|
|
it 'handles non local content correctly' do
|
2015-05-26 13:41:50 +08:00
|
|
|
SiteSetting.avatar_sizes = "100|49"
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_access_key_id = "XXX"
|
|
|
|
SiteSetting.s3_secret_access_key = "XXX"
|
|
|
|
SiteSetting.s3_upload_bucket = "test"
|
2015-05-27 10:02:57 +08:00
|
|
|
SiteSetting.s3_cdn_url = "http://cdn.com"
|
2015-05-26 13:41:50 +08:00
|
|
|
|
2017-04-15 12:11:02 +08:00
|
|
|
stub_request(:get, "http://cdn.com/something/else").to_return(body: 'image')
|
2015-12-16 10:48:49 +08:00
|
|
|
|
2015-05-26 13:41:50 +08:00
|
|
|
GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom")
|
|
|
|
|
2018-08-27 09:22:46 +08:00
|
|
|
upload = Fabricate(:upload, url: "//test.s3.dualstack.us-east-1.amazonaws.com/something")
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-08-24 07:36:11 +08:00
|
|
|
optimized_image = Fabricate(:optimized_image,
|
2017-08-31 12:06:56 +08:00
|
|
|
sha1: SecureRandom.hex << "A" * 8,
|
|
|
|
upload: upload,
|
|
|
|
width: 98,
|
|
|
|
height: 98,
|
2018-08-27 09:22:46 +08:00
|
|
|
url: "//test.s3.dualstack.us-east-1.amazonaws.com/something/else"
|
2017-08-31 12:06:56 +08:00
|
|
|
)
|
2015-05-27 10:02:57 +08:00
|
|
|
|
2015-05-26 13:41:50 +08:00
|
|
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
|
|
|
|
2018-06-05 21:21:25 +08:00
|
|
|
get "/user_avatar/default/#{user.username}/97/#{upload.id}.png"
|
2015-05-26 13:41:50 +08:00
|
|
|
|
2015-05-27 23:50:49 +08:00
|
|
|
# 98 is closest which is 49 * 2 for retina
|
2015-05-30 00:51:17 +08:00
|
|
|
expect(response).to redirect_to("http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}_#{OptimizedImage::VERSION}.png")
|
2015-05-27 10:02:57 +08:00
|
|
|
|
2018-06-05 21:21:25 +08:00
|
|
|
get "/user_avatar/default/#{user.username}/98/#{upload.id}.png"
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-12-16 10:48:49 +08:00
|
|
|
expect(response.body).to eq("image")
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(response.headers["Cache-Control"]).to eq('max-age=31556952, public, immutable')
|
2018-08-24 07:36:11 +08:00
|
|
|
expect(response.headers["Last-Modified"]).to eq(optimized_image.upload.created_at.httpdate)
|
|
|
|
end
|
|
|
|
|
|
|
|
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 15:07:31 +08:00
|
|
|
|
|
|
|
# this image should be really old so when it is fixed various algorithms pick it up
|
|
|
|
expect(response.headers["Last-Modified"]).to eq(Time.new('1990-01-01').httpdate)
|
2015-05-26 13:41:50 +08: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 21:21:25 +08:00
|
|
|
get "/user_avatar/default/#{user.username}/51/#{upload.id}.png"
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-05-26 13:41:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|