diff options
author | Marin Jankovski <marin@gitlab.com> | 2014-05-26 14:17:46 +0200 |
---|---|---|
committer | Marin Jankovski <marin@gitlab.com> | 2014-05-26 14:17:46 +0200 |
commit | 8bec6b0bcb100b30a43fcd9c6649d1bee113b6a7 (patch) | |
tree | 6aef03a4d7da1220721a3d5d7550ac8d2212c227 | |
parent | 2a8aa7429b6f489acd07c99e2e33a09c21c2ba01 (diff) | |
download | gitlab-ce-8bec6b0bcb100b30a43fcd9c6649d1bee113b6a7.tar.gz |
Make existing tests test something, return correct errors.
-rw-r--r-- | app/controllers/projects_controller.rb | 19 | ||||
-rw-r--r-- | app/uploaders/file_uploader.rb | 2 | ||||
-rw-r--r-- | spec/controllers/projects_controller_spec.rb | 17 |
3 files changed, 24 insertions, 14 deletions
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index c15205fb68f..3144ece977c 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -164,12 +164,21 @@ class ProjectsController < ApplicationController def upload_image uploader = FileUploader.new('uploads', upload_path, accepted_images) - alt = params['markdown_img'].original_filename - uploader.store!(params['markdown_img']) - link = { 'alt' => File.basename(alt, '.*'), - 'url' => File.join(root_url, uploader.url) } + image = params['markdown_img'] + + if image && accepted_images.map{ |format| image.content_type.include? format }.any? + alt = image.original_filename + uploader.store!(image) + link = { 'alt' => File.basename(alt, '.*'), + 'url' => File.join(root_url, uploader.url) } + end + respond_to do |format| - format.json { render json: { link: link } } + if link + format.json { render json: { link: link } } + else + format.json { render json: "Invalid file.", status: :unprocessable_entity } + end end end diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb index cbc9271ac14..0fa987c93f6 100644 --- a/app/uploaders/file_uploader.rb +++ b/app/uploaders/file_uploader.rb @@ -25,7 +25,7 @@ class FileUploader < CarrierWave::Uploader::Base end def store!(file) - file.original_filename = self.class.generate_filename(file) + @filename = self.class.generate_filename(file) super end diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 07ca8d25026..1d465d4996e 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -11,34 +11,35 @@ describe ProjectsController do describe "POST #upload_image" do before do sign_in(user) + project.team << [user, :developer] end context "without params['markdown_img']" do it "returns an error" do - post :upload_image, id: project.to_param - expect(response.status).to eq(404) + post :upload_image, id: project.to_param, format: :json + expect(response.status).to eq(422) end end context "with invalid file" do before do - post :upload_image, id: project.to_param, markdown_img: @img + post :upload_image, id: project.to_param, markdown_img: txt, format: :json end it "returns an error" do - expect(response.status).to eq(404) + expect(response.status).to eq(422) end end context "with valid file" do before do - post :upload_image, id: project.to_param, markdown_img: @img + post :upload_image, id: project.to_param, markdown_img: jpg, format: :json end it "returns a content with original filename and new link." do - link = { alt: 'rails_sample', link: '' }.to_json - expect(response.body).to have_content link + expect(response.body).to match "\"alt\":\"rails_sample\"" + expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}" end end end -end
\ No newline at end of file +end |