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,12 +164,21 @@ class ProjectsController < ApplicationController | ||
164 | 164 | ||
165 | def upload_image | 165 | def upload_image |
166 | uploader = FileUploader.new('uploads', upload_path, accepted_images) | 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 | respond_to do |format| | 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 | end | 182 | end |
174 | end | 183 | end |
175 | 184 |
app/uploaders/file_uploader.rb
@@ -25,7 +25,7 @@ class FileUploader < CarrierWave::Uploader::Base | @@ -25,7 +25,7 @@ class FileUploader < CarrierWave::Uploader::Base | ||
25 | end | 25 | end |
26 | 26 | ||
27 | def store!(file) | 27 | def store!(file) |
28 | - file.original_filename = self.class.generate_filename(file) | 28 | + @filename = self.class.generate_filename(file) |
29 | super | 29 | super |
30 | end | 30 | end |
31 | 31 |
spec/controllers/projects_controller_spec.rb
@@ -11,34 +11,35 @@ describe ProjectsController do | @@ -11,34 +11,35 @@ describe ProjectsController do | ||
11 | describe "POST #upload_image" do | 11 | describe "POST #upload_image" do |
12 | before do | 12 | before do |
13 | sign_in(user) | 13 | sign_in(user) |
14 | + project.team << [user, :developer] | ||
14 | end | 15 | end |
15 | 16 | ||
16 | context "without params['markdown_img']" do | 17 | context "without params['markdown_img']" do |
17 | it "returns an error" do | 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 | end | 21 | end |
21 | end | 22 | end |
22 | 23 | ||
23 | context "with invalid file" do | 24 | context "with invalid file" do |
24 | before do | 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 | end | 27 | end |
27 | 28 | ||
28 | it "returns an error" do | 29 | it "returns an error" do |
29 | - expect(response.status).to eq(404) | 30 | + expect(response.status).to eq(422) |
30 | end | 31 | end |
31 | end | 32 | end |
32 | 33 | ||
33 | context "with valid file" do | 34 | context "with valid file" do |
34 | before do | 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 | end | 37 | end |
37 | 38 | ||
38 | it "returns a content with original filename and new link." do | 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 | end | 42 | end |
42 | end | 43 | end |
43 | end | 44 | end |
44 | -end | ||
45 | \ No newline at end of file | 45 | \ No newline at end of file |
46 | +end |