2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Upload do
|
2013-06-16 04:39:48 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
let(:upload) { build(:upload) }
|
|
|
|
let(:thumbnail) { build(:optimized_image, upload: upload) }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
let(:user_id) { 1 }
|
|
|
|
let(:url) { "http://domain.com" }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:image_filename) { "logo.png" }
|
2014-11-03 13:54:10 -05:00
|
|
|
let(:image) { file_from_fixtures(image_filename) }
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:image_filesize) { File.size(image) }
|
2016-09-02 02:50:13 -04:00
|
|
|
let(:image_sha1) { Upload.generate_digest(image) }
|
2013-07-13 17:42:19 -04:00
|
|
|
|
2014-11-03 13:54:10 -05:00
|
|
|
let(:image_svg_filename) { "image.svg" }
|
|
|
|
let(:image_svg) { file_from_fixtures(image_svg_filename) }
|
|
|
|
let(:image_svg_filesize) { File.size(image_svg) }
|
|
|
|
|
2017-01-11 17:37:12 -05:00
|
|
|
let(:huge_image_filename) { "huge.jpg" }
|
|
|
|
let(:huge_image) { file_from_fixtures(huge_image_filename) }
|
|
|
|
let(:huge_image_filesize) { File.size(huge_image) }
|
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
let(:attachment_path) { __FILE__ }
|
|
|
|
let(:attachment) { File.new(attachment_path) }
|
|
|
|
let(:attachment_filename) { File.basename(attachment_path) }
|
|
|
|
let(:attachment_filesize) { File.size(attachment_path) }
|
2013-07-23 18:54:18 -04:00
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".create_thumbnail!" do
|
|
|
|
|
|
|
|
it "does not create a thumbnail when disabled" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.create_thumbnails = false
|
2013-07-31 17:26:34 -04:00
|
|
|
OptimizedImage.expects(:create_for).never
|
2013-09-27 04:55:50 -04:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a thumbnail" do
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
thumbnail = Fabricate(:optimized_image, upload: upload)
|
|
|
|
SiteSetting.expects(:create_thumbnails?).returns(true)
|
|
|
|
OptimizedImage.expects(:create_for).returns(thumbnail)
|
2013-09-27 04:55:50 -04:00
|
|
|
upload.create_thumbnail!(100, 100)
|
2013-07-13 17:42:19 -04:00
|
|
|
upload.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(upload.optimized_images.count).to eq(1)
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-04 11:50:08 -04:00
|
|
|
it "extracts file extension" do
|
|
|
|
created_upload = UploadCreator.new(image, image_filename).create_for(user_id)
|
|
|
|
expect(created_upload.extension).to eq("png")
|
|
|
|
end
|
|
|
|
|
2017-12-13 15:51:09 -05:00
|
|
|
it "should create an invalid upload when the filename is blank" do
|
|
|
|
SiteSetting.authorized_extensions = "*"
|
|
|
|
|
|
|
|
created_upload = UploadCreator.new(image, nil).create_for(user_id)
|
|
|
|
expect(created_upload.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2013-07-13 17:42:19 -04:00
|
|
|
context ".get_from_url" do
|
2016-10-18 01:39:16 -04:00
|
|
|
let(:url) { "/uploads/default/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
|
2016-10-18 03:58:45 -04:00
|
|
|
let(:upload) { Fabricate(:upload, url: url) }
|
2013-07-13 17:42:19 -04:00
|
|
|
|
2013-07-21 18:37:23 -04:00
|
|
|
it "works when the file has been uploaded" do
|
2016-10-18 03:58:45 -04:00
|
|
|
expect(Upload.get_from_url(upload.url)).to eq(upload)
|
2013-07-21 18:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works when using a cdn" do
|
2016-10-18 01:39:16 -04:00
|
|
|
begin
|
|
|
|
original_asset_host = Rails.configuration.action_controller.asset_host
|
|
|
|
Rails.configuration.action_controller.asset_host = 'http://my.cdn.com'
|
2016-10-18 03:58:45 -04:00
|
|
|
|
|
|
|
expect(Upload.get_from_url(
|
|
|
|
URI.join("http://my.cdn.com", upload.url).to_s
|
|
|
|
)).to eq(upload)
|
2016-10-18 01:39:16 -04:00
|
|
|
ensure
|
|
|
|
Rails.configuration.action_controller.asset_host = original_asset_host
|
|
|
|
end
|
2013-07-21 18:37:23 -04:00
|
|
|
end
|
2016-10-18 03:58:45 -04:00
|
|
|
|
|
|
|
it "should return the right upload when using the full URL" do
|
|
|
|
expect(Upload.get_from_url(
|
|
|
|
URI.join("http://discourse.some.com:3000/", upload.url).to_s
|
|
|
|
)).to eq(upload)
|
|
|
|
end
|
|
|
|
|
2016-10-20 06:34:42 -04:00
|
|
|
it "doesn't blow up with an invalid URI" do
|
|
|
|
expect { Upload.get_from_url("http://ip:port/index.html") }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
2016-10-18 03:58:45 -04:00
|
|
|
describe "s3 store" do
|
|
|
|
let(:path) { "/original/3X/1/0/10f73034616a796dfd70177dc54b6def44c4ba6f.png" }
|
|
|
|
let(:url) { "//#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_upload_bucket = "s3-upload-bucket"
|
|
|
|
SiteSetting.s3_access_key_id = "some key"
|
|
|
|
SiteSetting.s3_secret_access_key = "some secret key"
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
SiteSetting.enable_s3_uploads = false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the right upload when using a CDN for s3" do
|
|
|
|
upload
|
|
|
|
s3_cdn_url = 'https://mycdn.slowly.net'
|
|
|
|
SiteSetting.s3_cdn_url = s3_cdn_url
|
|
|
|
|
|
|
|
expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload)
|
|
|
|
end
|
|
|
|
end
|
2013-07-13 17:42:19 -04:00
|
|
|
end
|
|
|
|
|
2016-09-02 02:50:13 -04:00
|
|
|
describe '.generate_digest' do
|
|
|
|
it "should return the right digest" do
|
|
|
|
expect(Upload.generate_digest(image.path)).to eq('bc975735dfc6409c1c2aa5ebf2239949bcbdbd65')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-22 11:46:15 -04:00
|
|
|
describe '.short_url' do
|
|
|
|
it "should generate a correct short url" do
|
|
|
|
upload = Upload.new(sha1: 'bda2c513e1da04f7b4e99230851ea2aafeb8cc4e', extension: 'png')
|
|
|
|
expect(upload.short_url).to eq('upload://r3AYqESanERjladb4vBB7VsMBm6.png')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.sha1_from_short_url' do
|
|
|
|
it "should be able to look up sha1" do
|
|
|
|
sha1 = 'bda2c513e1da04f7b4e99230851ea2aafeb8cc4e'
|
|
|
|
|
|
|
|
expect(Upload.sha1_from_short_url('upload://r3AYqESanERjladb4vBB7VsMBm6.png')).to eq(sha1)
|
|
|
|
expect(Upload.sha1_from_short_url('upload://r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
|
|
|
|
expect(Upload.sha1_from_short_url('r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
|
|
|
|
end
|
2017-08-23 11:08:18 -04:00
|
|
|
|
|
|
|
it "should be able to look up sha1 even with leading zeros" do
|
|
|
|
sha1 = '0000c513e1da04f7b4e99230851ea2aafeb8cc4e'
|
|
|
|
expect(Upload.sha1_from_short_url('upload://1Eg9p8rrCURq4T3a6iJUk0ri6.png')).to eq(sha1)
|
|
|
|
end
|
2017-08-22 11:46:15 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|