mirror of
https://github.com/discourse/discourse.git
synced 2025-04-15 17:32:56 +08:00
add UploadsController specs
This commit is contained in:
parent
e8d66beea5
commit
2b120ef886
@ -1,5 +1,8 @@
|
|||||||
class UploadsController < ApplicationController
|
class UploadsController < ApplicationController
|
||||||
|
before_filter :ensure_logged_in
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
requires_parameter(:topic_id)
|
||||||
file = params[:file] || params[:files].first
|
file = params[:file] || params[:files].first
|
||||||
upload = Upload.create_for(current_user, file, params[:topic_id])
|
upload = Upload.create_for(current_user, file, params[:topic_id])
|
||||||
render_serialized(upload, UploadSerializer, root: false)
|
render_serialized(upload, UploadSerializer, root: false)
|
||||||
|
70
spec/controllers/uploads_controller_spec.rb
Normal file
70
spec/controllers/uploads_controller_spec.rb
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe UploadsController do
|
||||||
|
|
||||||
|
it 'requires you to be logged in' do
|
||||||
|
-> { xhr :post, :create }.should raise_error(Discourse::NotLoggedIn)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'logged in' do
|
||||||
|
|
||||||
|
before do
|
||||||
|
@user = log_in :user
|
||||||
|
end
|
||||||
|
|
||||||
|
context '.create' do
|
||||||
|
|
||||||
|
context 'missing params' do
|
||||||
|
it 'raises an error without the topic_id param' do
|
||||||
|
-> { xhr :post, :create }.should raise_error(Discourse::InvalidParameters)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'correct params' do
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
let(:logo_dev) do
|
||||||
|
ActionDispatch::Http::UploadedFile.new({
|
||||||
|
filename: 'logo-dev.png',
|
||||||
|
content_type: 'image/png',
|
||||||
|
tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo-dev.png")
|
||||||
|
})
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
BIN
spec/fixtures/images/logo-dev.png
vendored
Normal file
BIN
spec/fixtures/images/logo-dev.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
BIN
spec/fixtures/images/logo.png
vendored
Normal file
BIN
spec/fixtures/images/logo.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
Loading…
x
Reference in New Issue
Block a user