update back-end specs

This commit is contained in:
Régis Hanol 2013-07-10 22:59:07 +02:00
parent 6beb12e778
commit b94d26d798
7 changed files with 73 additions and 13 deletions

View file

@ -9,7 +9,8 @@ describe CookedPostProcessor do
let(:cpp) { CookedPostProcessor.new(post) } let(:cpp) { CookedPostProcessor.new(post) }
let(:post_process) { sequence("post_process") } 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_images).in_sequence(post_process)
cpp.expects(:post_process_oneboxes).in_sequence(post_process) cpp.expects(:post_process_oneboxes).in_sequence(post_process)
cpp.post_process cpp.post_process
@ -17,6 +18,35 @@ describe CookedPostProcessor do
end 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 =~ /<span class=\"size\">\(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 "post_process_images" do
context "with images in quotes and oneboxes" do context "with images in quotes and oneboxes" do
@ -48,7 +78,7 @@ describe CookedPostProcessor do
Upload.expects(:get_from_url).returns(upload).twice Upload.expects(:get_from_url).returns(upload).twice
cpp.post_process_images cpp.post_process_images
# ensures absolute urls on uploaded images # ensures absolute urls on uploaded images
cpp.html.should =~ /#{Discourse.base_url_no_prefix}/ cpp.html.should =~ /#{LocalStore.base_url}/
# dirty # dirty
cpp.should be_dirty cpp.should be_dirty
# keeps the reverse index up to date # keeps the reverse index up to date
@ -58,7 +88,7 @@ describe CookedPostProcessor do
end end
context "width sized images" do context "with sized images" do
let(:post) { build(:post_with_image_url) } let(:post) { build(:post_with_image_url) }
let(:cpp) { CookedPostProcessor.new(post, image_sizes: {'http://foo.bar/image.png' => {'width' => 111, 'height' => 222}}) } let(:cpp) { CookedPostProcessor.new(post, image_sizes: {'http://foo.bar/image.png' => {'width' => 111, 'height' => 222}}) }

View file

@ -22,7 +22,7 @@ describe LocalStore do
# The Time needs to be frozen as it is used to generate a clean & unique name # 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)) 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
end end

View file

@ -24,7 +24,7 @@ describe S3 do
end end
it 'returns the url of the S3 upload if successful' do 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 end
after(:each) do after(:each) do

View file

@ -41,20 +41,39 @@ describe UploadsController do
let(:files) { [ logo_dev, logo ] } let(:files) { [ logo_dev, logo ] }
context 'with a file' do context 'with a file' do
it 'is succesful' do
it 'is successful' do
xhr :post, :create, file: logo xhr :post, :create, file: logo
response.should be_success response.should be_success
end end
it 'supports only images' do context 'when authorized' do
xhr :post, :create, file: text_file
response.status.should eq 415 before { SiteSetting.stubs(:authorized_extensions).returns(".txt") }
it 'is successful' do
xhr :post, :create, file: text_file
response.status.should eq 200
end
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 end
context 'with some files' do context 'with some files' do
it 'is succesful' do it 'is successful' do
xhr :post, :create, files: files xhr :post, :create, files: files
response.should be_success response.should be_success
end end

View file

@ -50,6 +50,10 @@ Fabricator(:post_with_uploaded_images, from: :post) do
' '
end end
Fabricator(:post_with_an_attachment, from: :post) do
cooked '<a class="attachment" href="/uploads/default/186/66b3ed1503efc936.zip">archive.zip</a>'
end
Fabricator(:post_with_unsized_images, from: :post) do Fabricator(:post_with_unsized_images, from: :post) do
cooked ' cooked '
<img src="http://foo.bar/image.png"> <img src="http://foo.bar/image.png">

View file

@ -6,3 +6,10 @@ Fabricator(:upload) do
height 200 height 200
url "/uploads/default/1/1234567890123456.jpg" url "/uploads/default/1/1234567890123456.jpg"
end end
Fabricator(:attachment, from: :upload) do
user
original_filename "archive.zip"
filesize 1234
url "/uploads/default/186/66b3ed1503efc936.zip"
end

View file

@ -62,13 +62,13 @@ describe Upload do
it "identifies internal or relatives urls" do it "identifies internal or relatives urls" do
Discourse.expects(:base_url_no_prefix).returns("http://discuss.site.com") 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 Upload.has_been_uploaded?("/upload/42/0123456789ABCDEF.jpg").should == true
end end
it "identifies internal urls when using a CDN" do it "identifies internal urls when using a CDN" do
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice 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 end
it "identifies S3 uploads" do it "identifies S3 uploads" do
@ -78,7 +78,7 @@ describe Upload do
end end
it "identifies external urls" do 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 Upload.has_been_uploaded?("//bucket.s3.amazonaws.com/1337.png").should == false
end end