diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 3fc9c0cfc..e216c8b6c 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -9,7 +9,8 @@ describe CookedPostProcessor do let(:cpp) { CookedPostProcessor.new(post) } let(:post_process) { sequence("post_process") } - it "works on images before oneboxes" do + it "post process in sequence" do + cpp.expects(:post_process_attachments).in_sequence(post_process) cpp.expects(:post_process_images).in_sequence(post_process) cpp.expects(:post_process_oneboxes).in_sequence(post_process) cpp.post_process @@ -17,6 +18,35 @@ describe CookedPostProcessor do end + context "post_process_attachments" do + + context "with attachment" do + + let(:upload) { Fabricate(:upload) } + let(:post) { Fabricate(:post_with_an_attachment) } + let(:cpp) { CookedPostProcessor.new(post) } + + # all in one test to speed things up + it "works" do + Upload.expects(:get_from_url).returns(upload) + cpp.post_process_attachments + # ensures absolute urls on attachment + cpp.html.should =~ /#{LocalStore.base_url}/ + # ensure name is present + cpp.html.should =~ /archive.zip/ + # ensure size is present + cpp.html.should =~ /\(1.21 KB\)<\/span>/ + # dirty + cpp.should be_dirty + # keeps the reverse index up to date + post.uploads.reload + post.uploads.count.should == 1 + end + + end + + end + context "post_process_images" do context "with images in quotes and oneboxes" do @@ -48,7 +78,7 @@ describe CookedPostProcessor do Upload.expects(:get_from_url).returns(upload).twice cpp.post_process_images # ensures absolute urls on uploaded images - cpp.html.should =~ /#{Discourse.base_url_no_prefix}/ + cpp.html.should =~ /#{LocalStore.base_url}/ # dirty cpp.should be_dirty # keeps the reverse index up to date @@ -58,7 +88,7 @@ describe CookedPostProcessor do end - context "width sized images" do + context "with sized images" do let(:post) { build(:post_with_image_url) } let(:cpp) { CookedPostProcessor.new(post, image_sizes: {'http://foo.bar/image.png' => {'width' => 111, 'height' => 222}}) } diff --git a/spec/components/local_store_spec.rb b/spec/components/local_store_spec.rb index 2b79ef676..3e87081e1 100644 --- a/spec/components/local_store_spec.rb +++ b/spec/components/local_store_spec.rb @@ -22,7 +22,7 @@ describe LocalStore do # 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, "", 1).should == '/uploads/default/1/253dc8edf9d4ada1.png' end end diff --git a/spec/components/s3_spec.rb b/spec/components/s3_spec.rb index f2b4d2682..d0de2d44a 100644 --- a/spec/components/s3_spec.rb +++ b/spec/components/s3_spec.rb @@ -24,7 +24,7 @@ describe S3 do end it 'returns the url of the S3 upload if successful' do - S3.store_file(file, "SHA", image_info, 1).should == '//s3_upload_bucket.s3.amazonaws.com/1SHA.png' + S3.store_file(file, "SHA", 1).should == '//s3_upload_bucket.s3.amazonaws.com/1SHA.png' end after(:each) do diff --git a/spec/controllers/uploads_controller_spec.rb b/spec/controllers/uploads_controller_spec.rb index f71342b18..686713cfc 100644 --- a/spec/controllers/uploads_controller_spec.rb +++ b/spec/controllers/uploads_controller_spec.rb @@ -41,20 +41,39 @@ describe UploadsController do let(:files) { [ logo_dev, logo ] } context 'with a file' do - it 'is succesful' do + + it 'is successful' 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 + context 'when authorized' do + + before { SiteSetting.stubs(:authorized_extensions).returns(".txt") } + + it 'is successful' do + xhr :post, :create, file: text_file + response.status.should eq 200 + end + end + + context 'when not authorized' do + + before { SiteSetting.stubs(:authorized_extensions).returns(".png") } + + it 'rejects the upload' do + xhr :post, :create, file: text_file + response.status.should eq 415 + end + + end + end context 'with some files' do - it 'is succesful' do + it 'is successful' do xhr :post, :create, files: files response.should be_success end diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index 9ef164e2e..5bb76f998 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -50,6 +50,10 @@ Fabricator(:post_with_uploaded_images, from: :post) do ' end +Fabricator(:post_with_an_attachment, from: :post) do + cooked 'archive.zip' +end + Fabricator(:post_with_unsized_images, from: :post) do cooked ' diff --git a/spec/fabricators/upload_fabricator.rb b/spec/fabricators/upload_fabricator.rb index 16fed8837..53a7911ea 100644 --- a/spec/fabricators/upload_fabricator.rb +++ b/spec/fabricators/upload_fabricator.rb @@ -6,3 +6,10 @@ Fabricator(:upload) do height 200 url "/uploads/default/1/1234567890123456.jpg" end + +Fabricator(:attachment, from: :upload) do + user + original_filename "archive.zip" + filesize 1234 + url "/uploads/default/186/66b3ed1503efc936.zip" +end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index dc300dbac..850fb8410 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -62,13 +62,13 @@ describe Upload 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/0123456789ABCDEF.jpg").should == true + Upload.has_been_uploaded?("http://discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg").should == true Upload.has_been_uploaded?("/upload/42/0123456789ABCDEF.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/0123456789ABCDEF.jpg").should == true + Upload.has_been_uploaded?("http://my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg").should == true end it "identifies S3 uploads" do @@ -78,7 +78,7 @@ describe Upload do end it "identifies external urls" do - Upload.has_been_uploaded?("http://domain.com/upload/1234/42/0123456789ABCDEF.jpg").should == false + Upload.has_been_uploaded?("http://domain.com/uploads/default/42/0123456789ABCDEF.jpg").should == false Upload.has_been_uploaded?("//bucket.s3.amazonaws.com/1337.png").should == false end