2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-06-15 05:52:40 -04:00
|
|
|
require 'digest/sha1'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Upload do
|
|
|
|
|
|
|
|
it { should belong_to :user }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-06-13 17:44:24 -04:00
|
|
|
it { should have_many :post_uploads }
|
|
|
|
it { should have_many :posts }
|
2013-06-12 19:43:50 -04:00
|
|
|
|
2013-06-16 04:39:48 -04:00
|
|
|
it { should have_many :optimized_images }
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it { should validate_presence_of :original_filename }
|
|
|
|
it { should validate_presence_of :filesize }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
|
|
|
context '.create_for' do
|
|
|
|
|
|
|
|
let(:user_id) { 1 }
|
|
|
|
|
|
|
|
let(:logo) do
|
|
|
|
ActionDispatch::Http::UploadedFile.new({
|
|
|
|
filename: 'logo.png',
|
|
|
|
content_type: 'image/png',
|
|
|
|
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo.png")
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2013-06-15 03:54:49 -04:00
|
|
|
let(:upload) { Upload.create_for(user_id, logo) }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-06-04 18:34:53 -04:00
|
|
|
let(:url) { "http://domain.com" }
|
2013-04-07 11:52:46 -04:00
|
|
|
|
2013-05-30 21:13:37 -04:00
|
|
|
shared_examples_for "upload" do
|
|
|
|
it "is valid" do
|
2013-06-04 18:34:53 -04:00
|
|
|
upload.user_id.should == user_id
|
2013-05-30 21:13:37 -04:00
|
|
|
upload.original_filename.should == logo.original_filename
|
2013-06-04 18:34:53 -04:00
|
|
|
upload.filesize.should == File.size(logo.tempfile)
|
2013-06-17 16:16:14 -04:00
|
|
|
upload.sha1.should == Digest::SHA1.file(logo.tempfile).hexdigest
|
2013-05-30 21:13:37 -04:00
|
|
|
upload.width.should == 244
|
|
|
|
upload.height.should == 66
|
2013-06-04 18:34:53 -04:00
|
|
|
upload.url.should == url
|
2013-05-30 21:13:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-04 18:34:53 -04:00
|
|
|
context "s3" do
|
2013-06-12 19:43:50 -04:00
|
|
|
before(:each) do
|
2013-06-04 18:34:53 -04:00
|
|
|
SiteSetting.stubs(:enable_s3_uploads?).returns(true)
|
|
|
|
S3.stubs(:store_file).returns(url)
|
2013-05-30 21:13:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like "upload"
|
|
|
|
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
2013-06-04 18:34:53 -04:00
|
|
|
context "locally" do
|
|
|
|
before(:each) { LocalStore.stubs(:store_file).returns(url) }
|
2013-05-30 21:13:37 -04:00
|
|
|
it_behaves_like "upload"
|
2013-04-07 11:52:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-16 04:21:01 -04:00
|
|
|
context 'has_been_uploaded?' do
|
|
|
|
|
|
|
|
it "identifies internal or relatives urls" do
|
|
|
|
Discourse.expects(:base_url_no_prefix).returns("http://discuss.site.com")
|
2013-06-19 15:51:41 -04:00
|
|
|
Upload.has_been_uploaded?("http://discuss.site.com/upload/1234/42/0123456789ABCDEF.jpg").should == true
|
|
|
|
Upload.has_been_uploaded?("/upload/42/0123456789ABCDEF.jpg").should == true
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "identifies internal urls when using a CDN" do
|
|
|
|
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
|
2013-06-19 15:51:41 -04:00
|
|
|
Upload.has_been_uploaded?("http://my.cdn.com/upload/1234/42/0123456789ABCDEF.jpg").should == true
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
2013-06-22 07:38:42 -04:00
|
|
|
it "identifies S3 uploads" do
|
|
|
|
SiteSetting.stubs(:enable_s3_uploads).returns(true)
|
|
|
|
SiteSetting.stubs(:s3_upload_bucket).returns("bucket")
|
|
|
|
Upload.has_been_uploaded?("//bucket.s3.amazonaws.com/1337.png").should == true
|
|
|
|
end
|
|
|
|
|
2013-06-16 04:21:01 -04:00
|
|
|
it "identifies external urls" do
|
2013-06-19 15:51:41 -04:00
|
|
|
Upload.has_been_uploaded?("http://domain.com/upload/1234/42/0123456789ABCDEF.jpg").should == false
|
2013-06-22 07:38:42 -04:00
|
|
|
Upload.has_been_uploaded?("//bucket.s3.amazonaws.com/1337.png").should == false
|
2013-06-16 04:21:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|