2013-04-03 01:17:17 +02:00
require 'spec_helper'
describe UploadsController do
2013-09-06 19:18:42 +02:00
context '.create' do
2013-04-03 01:17:17 +02:00
2013-09-06 19:18:42 +02:00
it 'requires you to be logged in' do
- > { xhr :post , :create } . should raise_error ( Discourse :: NotLoggedIn )
2013-04-03 01:17:17 +02:00
end
2013-09-06 19:18:42 +02:00
context 'logged in' do
before { @user = log_in :user }
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
let ( :logo ) do
ActionDispatch :: Http :: UploadedFile . new ( {
filename : 'logo.png' ,
tempfile : File . new ( " #{ Rails . root } /spec/fixtures/images/logo.png " )
} )
2013-04-03 01:17:17 +02:00
end
2013-06-15 09:54:49 +02:00
let ( :logo_dev ) do
ActionDispatch :: Http :: UploadedFile . new ( {
filename : 'logo-dev.png' ,
tempfile : File . new ( " #{ Rails . root } /spec/fixtures/images/logo-dev.png " )
} )
end
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
let ( :text_file ) do
ActionDispatch :: Http :: UploadedFile . new ( {
2014-04-29 19:12:35 +02:00
filename : 'LICENSE.TXT' ,
2013-06-15 09:54:49 +02:00
tempfile : File . new ( " #{ Rails . root } /LICENSE.txt " )
} )
end
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
let ( :files ) { [ logo_dev , logo ] }
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
context 'with a file' do
2013-07-10 22:59:07 +02:00
context 'when authorized' do
2014-04-29 19:12:35 +02:00
before { SiteSetting . stubs ( :authorized_extensions ) . returns ( " .PNG|.txt " ) }
2013-07-10 22:59:07 +02:00
2013-07-24 00:54:18 +02:00
it 'is successful with an image' do
xhr :post , :create , file : logo
response . status . should eq 200
end
it 'is successful with an attachment' do
2013-07-10 22:59:07 +02:00
xhr :post , :create , file : text_file
response . status . should eq 200
end
2013-07-24 00:54:18 +02:00
context 'with a big file' do
before { SiteSetting . stubs ( :max_attachment_size_kb ) . returns ( 1 ) }
it 'rejects the upload' do
xhr :post , :create , file : text_file
2014-04-14 22:55:57 +02:00
response . status . should eq 422
2013-07-24 00:54:18 +02:00
end
end
2013-07-10 22:59:07 +02:00
end
context 'when not authorized' do
before { SiteSetting . stubs ( :authorized_extensions ) . returns ( " .png " ) }
it 'rejects the upload' do
xhr :post , :create , file : text_file
2014-04-14 22:55:57 +02:00
response . status . should eq 422
2013-07-10 22:59:07 +02:00
end
2013-04-03 01:17:17 +02:00
end
2013-07-10 22:59:07 +02:00
2014-04-29 19:12:35 +02:00
context 'when everything is authorized' do
before { SiteSetting . stubs ( :authorized_extensions ) . returns ( " * " ) }
it 'is successful with an image' do
xhr :post , :create , file : logo
response . status . should eq 200
end
it 'is successful with an attachment' do
xhr :post , :create , file : text_file
response . status . should eq 200
end
end
2013-06-15 09:54:49 +02:00
end
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
context 'with some files' do
2013-04-03 01:17:17 +02:00
2013-07-10 22:59:07 +02:00
it 'is successful' do
2013-06-15 09:54:49 +02:00
xhr :post , :create , files : files
response . should be_success
end
2013-04-03 01:17:17 +02:00
2013-06-15 09:54:49 +02:00
it 'takes the first file' do
xhr :post , :create , files : files
response . body . should match / logo-dev.png /
2013-04-03 01:17:17 +02:00
end
end
end
end
2013-09-06 19:18:42 +02:00
context '.show' do
it " returns 404 when using external storage " do
store = stub ( internal? : false )
Discourse . stubs ( :store ) . returns ( store )
2014-05-06 14:41:59 +01:00
Upload . expects ( :find_by ) . never
2013-09-06 19:18:42 +02:00
get :show , site : " default " , id : 1 , sha : " 1234567890abcdef " , extension : " pdf "
response . response_code . should == 404
end
it " returns 404 when the upload doens't exist " do
2014-05-06 14:41:59 +01:00
Upload . expects ( :find_by ) . with ( id : 2 , url : " /uploads/default/2/1234567890abcdef.pdf " ) . returns ( nil )
2013-09-06 19:18:42 +02:00
get :show , site : " default " , id : 2 , sha : " 1234567890abcdef " , extension : " pdf "
response . response_code . should == 404
end
it 'uses send_file' do
2014-04-14 22:55:57 +02:00
upload = build ( :upload )
2014-05-06 14:41:59 +01:00
Upload . expects ( :find_by ) . with ( id : 42 , url : " /uploads/default/42/66b3ed1503efc936.zip " ) . returns ( upload )
2014-04-14 22:55:57 +02:00
2013-09-06 19:18:42 +02:00
controller . stubs ( :render )
controller . expects ( :send_file )
2014-04-14 22:55:57 +02:00
2013-09-06 19:18:42 +02:00
get :show , site : " default " , id : 42 , sha : " 66b3ed1503efc936 " , extension : " zip "
end
end
2013-04-03 01:17:17 +02:00
end