mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
@@ -41,7 +41,6 @@ describe CookedPostProcessor do
|
||||
before do
|
||||
@topic = Fabricate(:topic)
|
||||
@post = Fabricate.build(:post_with_image_url, topic: @topic, user: @topic.user)
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
@cpp = CookedPostProcessor.new(@post, image_sizes: {'http://www.forumwarz.com/images/header/logo.png' => {'width' => 111, 'height' => 222}})
|
||||
@cpp.expects(:get_size).returns([111,222])
|
||||
end
|
||||
@@ -64,8 +63,6 @@ describe CookedPostProcessor do
|
||||
|
||||
before do
|
||||
FastImage.stubs(:size).returns([123, 456])
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
CookedPostProcessor.any_instance.expects(:image_dimensions).returns([123, 456])
|
||||
creator = PostCreator.new(user, raw: Fabricate.build(:post_with_images).raw, topic_id: topic.id)
|
||||
@post = creator.create
|
||||
end
|
||||
@@ -89,7 +86,6 @@ describe CookedPostProcessor do
|
||||
let(:processor) { CookedPostProcessor.new(post) }
|
||||
|
||||
before do
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
processor.post_process_images
|
||||
end
|
||||
|
||||
@@ -151,4 +147,17 @@ describe CookedPostProcessor do
|
||||
end
|
||||
end
|
||||
|
||||
context 'get_image_uri' do
|
||||
|
||||
it "returns nil unless the scheme is either http or https" do
|
||||
cpp.get_image_uri("http://domain.com").should == URI.parse("http://domain.com")
|
||||
cpp.get_image_uri("https://domain.com").should == URI.parse("https://domain.com")
|
||||
cpp.get_image_uri("ftp://domain.com").should == nil
|
||||
cpp.get_image_uri("ftps://domain.com").should == nil
|
||||
cpp.get_image_uri("//domain.com").should == nil
|
||||
cpp.get_image_uri("/tmp/image.png").should == nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -21,8 +21,8 @@ describe LocalStore do
|
||||
File.stubs(:open)
|
||||
# The Time needs to be frozen as it is used to generate a clean & unique name
|
||||
Time.stubs(:now).returns(Time.utc(2013, 2, 17, 12, 0, 0, 0))
|
||||
#
|
||||
LocalStore.store_file(file, image_info, 1).should == '/uploads/default/1/253dc8edf9d4ada1.png'
|
||||
#
|
||||
LocalStore.store_file(file, "", image_info, 1).should == '/uploads/default/1/253dc8edf9d4ada1.png'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -16,7 +16,7 @@ describe S3 do
|
||||
|
||||
let(:image_info) { FastImage.new(file) }
|
||||
|
||||
before(:each) do
|
||||
before(:each) do
|
||||
SiteSetting.stubs(:s3_upload_bucket).returns("s3_upload_bucket")
|
||||
SiteSetting.stubs(:s3_access_key_id).returns("s3_access_key_id")
|
||||
SiteSetting.stubs(:s3_secret_access_key).returns("s3_secret_access_key")
|
||||
@@ -24,7 +24,7 @@ describe S3 do
|
||||
end
|
||||
|
||||
it 'returns the url of the S3 upload if successful' do
|
||||
S3.store_file(file, image_info, 1).should == '//s3_upload_bucket.s3.amazonaws.com/1e8b1353813a7d091231f9a27f03566f123463fc1.png'
|
||||
S3.store_file(file, "SHA", image_info, 1).should == '//s3_upload_bucket.s3.amazonaws.com/1SHA.png'
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
|
||||
@@ -14,64 +14,54 @@ describe UploadsController do
|
||||
|
||||
context '.create' do
|
||||
|
||||
context 'missing params' do
|
||||
it 'raises an error without the topic_id param' do
|
||||
-> { xhr :post, :create }.should raise_error(ActionController::ParameterMissing)
|
||||
let(:logo) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'logo.png',
|
||||
type: 'image/png',
|
||||
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo.png")
|
||||
})
|
||||
end
|
||||
|
||||
let(:logo_dev) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'logo-dev.png',
|
||||
type: 'image/png',
|
||||
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo-dev.png")
|
||||
})
|
||||
end
|
||||
|
||||
let(:text_file) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'LICENSE.txt',
|
||||
type: 'text/plain',
|
||||
tempfile: File.new("#{Rails.root}/LICENSE.txt")
|
||||
})
|
||||
end
|
||||
|
||||
let(:files) { [ logo_dev, logo ] }
|
||||
|
||||
context 'with a file' do
|
||||
it 'is succesful' do
|
||||
xhr :post, :create, file: logo
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it 'supports only images' do
|
||||
xhr :post, :create, file: text_file
|
||||
response.status.should eq 415
|
||||
end
|
||||
end
|
||||
|
||||
context 'correct params' do
|
||||
context 'with some files' do
|
||||
|
||||
let(:logo) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'logo.png',
|
||||
type: 'image/png',
|
||||
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo.png")
|
||||
})
|
||||
it 'is succesful' do
|
||||
xhr :post, :create, files: files
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
let(:logo_dev) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'logo-dev.png',
|
||||
type: 'image/png',
|
||||
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo-dev.png")
|
||||
})
|
||||
end
|
||||
|
||||
let(:text_file) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
filename: 'LICENSE.txt',
|
||||
type: 'text/plain',
|
||||
tempfile: File.new("#{Rails.root}/LICENSE.txt")
|
||||
})
|
||||
end
|
||||
|
||||
let(:files) { [ logo_dev, logo ] }
|
||||
|
||||
context 'with a file' do
|
||||
it 'is succesful' do
|
||||
xhr :post, :create, topic_id: 1234, file: logo
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it 'supports only images' do
|
||||
xhr :post, :create, topic_id: 1234, file: text_file
|
||||
response.status.should eq 415
|
||||
end
|
||||
end
|
||||
|
||||
context 'with some files' do
|
||||
|
||||
it 'is succesful' do
|
||||
xhr :post, :create, topic_id: 1234, files: files
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it 'takes the first file' do
|
||||
xhr :post, :create, topic_id: 1234, files: files
|
||||
response.body.should match /logo-dev.png/
|
||||
end
|
||||
|
||||
it 'takes the first file' do
|
||||
xhr :post, :create, files: files
|
||||
response.body.should match /logo-dev.png/
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
8
spec/fabricators/upload_fabricator.rb
Normal file
8
spec/fabricators/upload_fabricator.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
Fabricator(:upload) do
|
||||
user
|
||||
original_filename "uploaded.jpg"
|
||||
filesize 1234
|
||||
width 100
|
||||
height 200
|
||||
url "/uploads/default/123456789.jpg"
|
||||
end
|
||||
31
spec/models/optimized_image_spec.rb
Normal file
31
spec/models/optimized_image_spec.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OptimizedImage do
|
||||
|
||||
it { should belong_to :upload }
|
||||
|
||||
let(:upload) { build(:upload) }
|
||||
let(:oi) { OptimizedImage.create_for(upload, 100, 100) }
|
||||
|
||||
describe ".create_for" do
|
||||
|
||||
before(:each) do
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(true)
|
||||
FastImage.any_instance.stubs(:size).returns([244, 66])
|
||||
# make sure we don't hit the filesystem
|
||||
FileUtils.stubs(:mkdir_p)
|
||||
File.stubs(:open)
|
||||
end
|
||||
|
||||
it "works" do
|
||||
Tempfile.any_instance.expects(:close).once
|
||||
Tempfile.any_instance.expects(:unlink).once
|
||||
oi.sha1.should == "da39a3ee5e6b4b0d3255bfef95601890afd80709"
|
||||
oi.extension.should == ".jpg"
|
||||
oi.width.should == 244
|
||||
oi.height.should == 66
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -2,11 +2,6 @@ require 'spec_helper'
|
||||
require_dependency 'post_destroyer'
|
||||
|
||||
describe PostAction do
|
||||
|
||||
before do
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
end
|
||||
|
||||
it { should belong_to :user }
|
||||
it { should belong_to :post }
|
||||
it { should belong_to :post_action_type }
|
||||
|
||||
@@ -5,7 +5,6 @@ describe PostAlertObserver do
|
||||
|
||||
before do
|
||||
ActiveRecord::Base.observers.enable :post_alert_observer
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
end
|
||||
|
||||
let!(:evil_trout) { Fabricate(:evil_trout) }
|
||||
|
||||
@@ -2,11 +2,6 @@ require 'spec_helper'
|
||||
require_dependency 'post_destroyer'
|
||||
|
||||
describe Post do
|
||||
|
||||
before do
|
||||
ImageSorcery.any_instance.stubs(:convert).returns(false)
|
||||
end
|
||||
|
||||
# Help us build a post with a raw body
|
||||
def post_with_body(body, user=nil)
|
||||
args = post_args.merge(raw: body)
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
require 'spec_helper'
|
||||
require 'digest/sha1'
|
||||
|
||||
describe Upload do
|
||||
|
||||
it { should belong_to :user }
|
||||
it { should belong_to :topic }
|
||||
|
||||
it { should have_many :post_uploads }
|
||||
it { should have_many :posts }
|
||||
|
||||
it { should have_many :optimized_images }
|
||||
|
||||
it { should validate_presence_of :original_filename }
|
||||
it { should validate_presence_of :filesize }
|
||||
|
||||
context '.create_for' do
|
||||
|
||||
let(:user_id) { 1 }
|
||||
let(:topic_id) { 42 }
|
||||
|
||||
let(:logo) do
|
||||
ActionDispatch::Http::UploadedFile.new({
|
||||
@@ -24,16 +25,16 @@ describe Upload do
|
||||
})
|
||||
end
|
||||
|
||||
let(:upload) { Upload.create_for(user_id, logo, topic_id) }
|
||||
let(:upload) { Upload.create_for(user_id, logo) }
|
||||
|
||||
let(:url) { "http://domain.com" }
|
||||
|
||||
shared_examples_for "upload" do
|
||||
it "is valid" do
|
||||
upload.user_id.should == user_id
|
||||
upload.topic_id.should == topic_id
|
||||
upload.original_filename.should == logo.original_filename
|
||||
upload.filesize.should == File.size(logo.tempfile)
|
||||
upload.sha.should == Digest::SHA1.file(logo.tempfile).hexdigest
|
||||
upload.width.should == 244
|
||||
upload.height.should == 66
|
||||
upload.url.should == url
|
||||
@@ -57,4 +58,23 @@ describe Upload do
|
||||
|
||||
end
|
||||
|
||||
context 'has_been_uploaded?' do
|
||||
|
||||
it "identifies internal or relatives urls" do
|
||||
Discourse.expects(:base_url_no_prefix).returns("http://discuss.site.com")
|
||||
Upload.has_been_uploaded?("http://discuss.site.com/upload/1234/42/ABCD.jpg").should == true
|
||||
Upload.has_been_uploaded?("/upload/42/ABCD.jpg").should == true
|
||||
end
|
||||
|
||||
it "identifies internal urls when using a CDN" do
|
||||
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
|
||||
Upload.has_been_uploaded?("http://my.cdn.com/upload/1234/42/ABCD.jpg").should == true
|
||||
end
|
||||
|
||||
it "identifies external urls" do
|
||||
Upload.has_been_uploaded?("http://domain.com/upload/1234/42/ABCD.jpg").should == false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user