Skip to content
代码片段 群组 项目
提交 8bec6b0b 编辑于 作者: Marin Jankovski's avatar Marin Jankovski
浏览文件

Make existing tests test something, return correct errors.

上级 2a8aa742
No related branches found
No related tags found
无相关合并请求
...@@ -164,12 +164,21 @@ def unarchive ...@@ -164,12 +164,21 @@ def unarchive
def upload_image def upload_image
uploader = FileUploader.new('uploads', upload_path, accepted_images) uploader = FileUploader.new('uploads', upload_path, accepted_images)
alt = params['markdown_img'].original_filename image = params['markdown_img']
uploader.store!(params['markdown_img'])
link = { 'alt' => File.basename(alt, '.*'), if image && accepted_images.map{ |format| image.content_type.include? format }.any?
'url' => File.join(root_url, uploader.url) } alt = image.original_filename
uploader.store!(image)
link = { 'alt' => File.basename(alt, '.*'),
'url' => File.join(root_url, uploader.url) }
end
respond_to do |format| 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
end end
......
...@@ -25,7 +25,7 @@ def extension_white_list ...@@ -25,7 +25,7 @@ def extension_white_list
end end
def store!(file) def store!(file)
file.original_filename = self.class.generate_filename(file) @filename = self.class.generate_filename(file)
super super
end end
......
...@@ -11,34 +11,35 @@ ...@@ -11,34 +11,35 @@
describe "POST #upload_image" do describe "POST #upload_image" do
before do before do
sign_in(user) sign_in(user)
project.team << [user, :developer]
end end
context "without params['markdown_img']" do context "without params['markdown_img']" do
it "returns an error" do it "returns an error" do
post :upload_image, id: project.to_param post :upload_image, id: project.to_param, format: :json
expect(response.status).to eq(404) expect(response.status).to eq(422)
end end
end end
context "with invalid file" do context "with invalid file" do
before 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 end
it "returns an error" do it "returns an error" do
expect(response.status).to eq(404) expect(response.status).to eq(422)
end end
end end
context "with valid file" do context "with valid file" do
before 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 end
it "returns a content with original filename and new link." do it "returns a content with original filename and new link." do
link = { alt: 'rails_sample', link: '' }.to_json expect(response.body).to match "\"alt\":\"rails_sample\""
expect(response.body).to have_content link expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}"
end end
end end
end end
end end
\ No newline at end of file
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册