Commit 8bec6b0bcb100b30a43fcd9c6649d1bee113b6a7
1 parent
2a8aa742
Exists in
spb-stable
and in
2 other branches
Make existing tests test something, return correct errors.
Showing
3 changed files
with
24 additions
and
14 deletions
Show diff stats
app/controllers/projects_controller.rb
| ... | ... | @@ -164,12 +164,21 @@ class ProjectsController < ApplicationController |
| 164 | 164 | |
| 165 | 165 | def upload_image |
| 166 | 166 | uploader = FileUploader.new('uploads', upload_path, accepted_images) |
| 167 | - alt = params['markdown_img'].original_filename | |
| 168 | - uploader.store!(params['markdown_img']) | |
| 169 | - link = { 'alt' => File.basename(alt, '.*'), | |
| 170 | - 'url' => File.join(root_url, uploader.url) } | |
| 167 | + image = params['markdown_img'] | |
| 168 | + | |
| 169 | + if image && accepted_images.map{ |format| image.content_type.include? format }.any? | |
| 170 | + alt = image.original_filename | |
| 171 | + uploader.store!(image) | |
| 172 | + link = { 'alt' => File.basename(alt, '.*'), | |
| 173 | + 'url' => File.join(root_url, uploader.url) } | |
| 174 | + end | |
| 175 | + | |
| 171 | 176 | respond_to do |format| |
| 172 | - format.json { render json: { link: link } } | |
| 177 | + if link | |
| 178 | + format.json { render json: { link: link } } | |
| 179 | + else | |
| 180 | + format.json { render json: "Invalid file.", status: :unprocessable_entity } | |
| 181 | + end | |
| 173 | 182 | end |
| 174 | 183 | end |
| 175 | 184 | ... | ... |
app/uploaders/file_uploader.rb
spec/controllers/projects_controller_spec.rb
| ... | ... | @@ -11,34 +11,35 @@ describe ProjectsController do |
| 11 | 11 | describe "POST #upload_image" do |
| 12 | 12 | before do |
| 13 | 13 | sign_in(user) |
| 14 | + project.team << [user, :developer] | |
| 14 | 15 | end |
| 15 | 16 | |
| 16 | 17 | context "without params['markdown_img']" do |
| 17 | 18 | it "returns an error" do |
| 18 | - post :upload_image, id: project.to_param | |
| 19 | - expect(response.status).to eq(404) | |
| 19 | + post :upload_image, id: project.to_param, format: :json | |
| 20 | + expect(response.status).to eq(422) | |
| 20 | 21 | end |
| 21 | 22 | end |
| 22 | 23 | |
| 23 | 24 | context "with invalid file" do |
| 24 | 25 | before do |
| 25 | - post :upload_image, id: project.to_param, markdown_img: @img | |
| 26 | + post :upload_image, id: project.to_param, markdown_img: txt, format: :json | |
| 26 | 27 | end |
| 27 | 28 | |
| 28 | 29 | it "returns an error" do |
| 29 | - expect(response.status).to eq(404) | |
| 30 | + expect(response.status).to eq(422) | |
| 30 | 31 | end |
| 31 | 32 | end |
| 32 | 33 | |
| 33 | 34 | context "with valid file" do |
| 34 | 35 | before do |
| 35 | - post :upload_image, id: project.to_param, markdown_img: @img | |
| 36 | + post :upload_image, id: project.to_param, markdown_img: jpg, format: :json | |
| 36 | 37 | end |
| 37 | 38 | |
| 38 | 39 | it "returns a content with original filename and new link." do |
| 39 | - link = { alt: 'rails_sample', link: '' }.to_json | |
| 40 | - expect(response.body).to have_content link | |
| 40 | + expect(response.body).to match "\"alt\":\"rails_sample\"" | |
| 41 | + expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}" | |
| 41 | 42 | end |
| 42 | 43 | end |
| 43 | 44 | end |
| 44 | -end | |
| 45 | 45 | \ No newline at end of file |
| 46 | +end | ... | ... |