DEV: Test against real `Upload#url` format.

This commit is contained in:
Guo Xiang Tan 2018-09-14 13:42:59 +08:00
parent 419b14e58b
commit c3f6b4d966
3 changed files with 39 additions and 19 deletions

View File

@ -30,7 +30,7 @@ describe Stylesheet::Importer do
background = Fabricate(:upload_s3)
category = Fabricate(:category, uploaded_background: background)
expect(compile_css("category_backgrounds")).to include("body.category-#{category.full_slug}{background-image:url(https://s3.cdn/uploads")
expect(compile_css("category_backgrounds")).to include("body.category-#{category.full_slug}{background-image:url(https://s3.cdn/original")
end
end

View File

@ -5,18 +5,27 @@ Fabricator(:upload) do
filesize 1234
width 100
height 200
url { sequence(:url) { |n| "/uploads/default/#{n}/1234567890123456.png" } }
url do |attrs|
sequence(:url) do |n|
Discourse.store.get_path_for(
"original", n + 1, attrs[:sha1], ".#{attrs[:extension]}"
)
end
end
extension "png"
end
Fabricator(:upload_s3, from: :upload) do
url { sequence(:url) { |n| "#{Discourse.store.absolute_base_url}/uploads/default/#{n}/1234567890123456.png" } }
url do |attrs|
sequence(:url) do |n|
File.join(
Discourse.store.absolute_base_url,
Discourse.store.get_path_for(
"original", n + 1, attrs[:sha1], ".#{attrs[:extension]}"
)
)
end
end
Fabricator(:attachment, from: :upload) do
id 42
user
original_filename "archive.zip"
filesize 1234
url "/uploads/default/42/66b3ed1503efc936.zip"
end

View File

@ -75,23 +75,34 @@ describe Upload do
context ".get_from_url" do
let(:sha1) { "10f73034616a796dfd70177dc54b6def44c4ba6f" }
let(:url) { "/uploads/default/original/3X/1/0/#{sha1}.png" }
let(:upload) { Fabricate(:upload, url: url, sha1: sha1) }
let(:upload) { Fabricate(:upload, sha1: sha1) }
it "works when the file has been uploaded" do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
describe 'for an extensionless url' do
let(:url) { "/uploads/default/original/1X/#{sha1}" }
before do
upload.update!(url: upload.url.sub('.png', ''))
upload.reload
end
it 'should return the right upload' do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
end
describe 'for a url without a tree' do
let(:url) { "/uploads/default/original/1X/#{sha1}.png" }
describe 'for a url a tree' do
before do
upload.update!(url:
Discourse.store.get_path_for(
"original",
16001,
upload.sha1,
".#{upload.extension}"
)
)
end
it 'should return the right upload' do
expect(Upload.get_from_url(upload.url)).to eq(upload)
@ -124,8 +135,8 @@ describe Upload do
end
describe "s3 store" do
let(:path) { "/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
let(:url) { "#{SiteSetting.Upload.absolute_base_url}#{path}" }
let(:upload) { Fabricate(:upload_s3) }
let(:path) { upload.url.sub(SiteSetting.Upload.s3_base_url, '') }
before do
SiteSetting.enable_s3_uploads = true
@ -136,7 +147,7 @@ describe Upload do
it "should return the right upload when using base url (not CDN) for s3" do
upload
expect(Upload.get_from_url(url)).to eq(upload)
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
describe 'when using a cdn' do