Commit 21649cabdd53edd201a2b138107c2ffe01376ca3
Exists in
spb-stable
and in
2 other branches
Merge branch 'fix/drag_and_drop_image_tests' into 'master'
Fix drag and drop image tests
Showing
6 changed files
with
122 additions
and
18 deletions
Show diff stats
app/controllers/projects_controller.rb
| @@ -163,13 +163,14 @@ class ProjectsController < ApplicationController | @@ -163,13 +163,14 @@ class ProjectsController < ApplicationController | ||
| 163 | end | 163 | end |
| 164 | 164 | ||
| 165 | def upload_image | 165 | def upload_image |
| 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) } | 166 | + link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute |
| 167 | + | ||
| 171 | respond_to do |format| | 168 | respond_to do |format| |
| 172 | - format.json { render json: { link: link } } | 169 | + if link_to_image |
| 170 | + format.json { render json: { link: link_to_image } } | ||
| 171 | + else | ||
| 172 | + format.json { render json: "Invalid file.", status: :unprocessable_entity } | ||
| 173 | + end | ||
| 173 | end | 174 | end |
| 174 | end | 175 | end |
| 175 | 176 |
| @@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
| 1 | +module Projects | ||
| 2 | + class ImageService < BaseService | ||
| 3 | + include Rails.application.routes.url_helpers | ||
| 4 | + def initialize(repository, params, root_url) | ||
| 5 | + @repository, @params, @root_url = repository, params.dup, root_url | ||
| 6 | + end | ||
| 7 | + | ||
| 8 | + def execute | ||
| 9 | + uploader = FileUploader.new('uploads', upload_path, accepted_images) | ||
| 10 | + image = @params['markdown_img'] | ||
| 11 | + | ||
| 12 | + if image && correct_mime_type?(image) | ||
| 13 | + alt = image.original_filename | ||
| 14 | + uploader.store!(image) | ||
| 15 | + link = { | ||
| 16 | + 'alt' => File.basename(alt, '.*'), | ||
| 17 | + 'url' => File.join(@root_url, uploader.url) | ||
| 18 | + } | ||
| 19 | + else | ||
| 20 | + link = nil | ||
| 21 | + end | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + protected | ||
| 25 | + | ||
| 26 | + def upload_path | ||
| 27 | + base_dir = FileUploader.generate_dir | ||
| 28 | + File.join(@repository.path_with_namespace, base_dir) | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + def accepted_images | ||
| 32 | + %w(png jpg jpeg gif) | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + def correct_mime_type?(image) | ||
| 36 | + accepted_images.map{ |format| image.content_type.include? format }.any? | ||
| 37 | + end | ||
| 38 | + end | ||
| 39 | +end |
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/commits_controller_spec.rb
| @@ -6,7 +6,7 @@ describe Projects::CommitsController do | @@ -6,7 +6,7 @@ describe Projects::CommitsController do | ||
| 6 | 6 | ||
| 7 | before do | 7 | before do |
| 8 | sign_in(user) | 8 | sign_in(user) |
| 9 | - project.creator = user | 9 | + project.team << [user, :master] |
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | describe "GET show" do | 12 | describe "GET show" do |
spec/controllers/projects_controller_spec.rb
| @@ -3,42 +3,41 @@ require('spec_helper') | @@ -3,42 +3,41 @@ require('spec_helper') | ||
| 3 | describe ProjectsController do | 3 | describe ProjectsController do |
| 4 | let(:project) { create(:project) } | 4 | let(:project) { create(:project) } |
| 5 | let(:user) { create(:user) } | 5 | let(:user) { create(:user) } |
| 6 | - let(:png) { fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png') } | ||
| 7 | let(:jpg) { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') } | 6 | let(:jpg) { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') } |
| 8 | - let(:gif) { fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif') } | ||
| 9 | let(:txt) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') } | 7 | let(:txt) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') } |
| 10 | 8 | ||
| 11 | describe "POST #upload_image" do | 9 | describe "POST #upload_image" do |
| 12 | before do | 10 | before do |
| 13 | sign_in(user) | 11 | sign_in(user) |
| 12 | + project.team << [user, :developer] | ||
| 14 | end | 13 | end |
| 15 | 14 | ||
| 16 | context "without params['markdown_img']" do | 15 | context "without params['markdown_img']" do |
| 17 | it "returns an error" do | 16 | it "returns an error" do |
| 18 | - post :upload_image, id: project.to_param | ||
| 19 | - expect(response.status).to eq(404) | 17 | + post :upload_image, id: project.to_param, format: :json |
| 18 | + expect(response.status).to eq(422) | ||
| 20 | end | 19 | end |
| 21 | end | 20 | end |
| 22 | 21 | ||
| 23 | context "with invalid file" do | 22 | context "with invalid file" do |
| 24 | before do | 23 | before do |
| 25 | - post :upload_image, id: project.to_param, markdown_img: @img | 24 | + post :upload_image, id: project.to_param, markdown_img: txt, format: :json |
| 26 | end | 25 | end |
| 27 | 26 | ||
| 28 | it "returns an error" do | 27 | it "returns an error" do |
| 29 | - expect(response.status).to eq(404) | 28 | + expect(response.status).to eq(422) |
| 30 | end | 29 | end |
| 31 | end | 30 | end |
| 32 | 31 | ||
| 33 | context "with valid file" do | 32 | context "with valid file" do |
| 34 | before do | 33 | before do |
| 35 | - post :upload_image, id: project.to_param, markdown_img: @img | 34 | + post :upload_image, id: project.to_param, markdown_img: jpg, format: :json |
| 36 | end | 35 | end |
| 37 | 36 | ||
| 38 | it "returns a content with original filename and new link." do | 37 | 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 | 38 | + expect(response.body).to match "\"alt\":\"rails_sample\"" |
| 39 | + expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}" | ||
| 41 | end | 40 | end |
| 42 | end | 41 | end |
| 43 | end | 42 | end |
| 44 | -end | ||
| 45 | \ No newline at end of file | 43 | \ No newline at end of file |
| 44 | +end |
| @@ -0,0 +1,65 @@ | @@ -0,0 +1,65 @@ | ||
| 1 | +require 'spec_helper' | ||
| 2 | + | ||
| 3 | +describe Projects::ImageService do | ||
| 4 | + before(:each) { enable_observers } | ||
| 5 | + after(:each) { disable_observers } | ||
| 6 | + | ||
| 7 | + describe 'Image service' do | ||
| 8 | + before do | ||
| 9 | + @user = create :user | ||
| 10 | + @project = create :project, creator_id: @user.id, namespace: @user.namespace | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + context 'for valid gif file' do | ||
| 14 | + before do | ||
| 15 | + gif = fixture_file_upload(Rails.root + 'spec/fixtures/banana_sample.gif', 'image/gif') | ||
| 16 | + @link_to_image = upload_image(@project.repository, { 'markdown_img' => gif }, "http://test.example/") | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + it { expect(@link_to_image).to have_key("alt") } | ||
| 20 | + it { expect(@link_to_image).to have_key("url") } | ||
| 21 | + it { expect(@link_to_image).to have_value("banana_sample") } | ||
| 22 | + it { expect(@link_to_image["url"]).to match("http://test.example/uploads/#{@project.path_with_namespace}") } | ||
| 23 | + it { expect(@link_to_image["url"]).to match("banana_sample.gif") } | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + context 'for valid png file' do | ||
| 27 | + before do | ||
| 28 | + png = fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png') | ||
| 29 | + @link_to_image = upload_image(@project.repository, { 'markdown_img' => png }, "http://test.example/") | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + it { expect(@link_to_image).to have_key("alt") } | ||
| 33 | + it { expect(@link_to_image).to have_key("url") } | ||
| 34 | + it { expect(@link_to_image).to have_value("dk") } | ||
| 35 | + it { expect(@link_to_image["url"]).to match("http://test.example/uploads/#{@project.path_with_namespace}") } | ||
| 36 | + it { expect(@link_to_image["url"]).to match("dk.png") } | ||
| 37 | + end | ||
| 38 | + | ||
| 39 | + context 'for valid jpg file' do | ||
| 40 | + before do | ||
| 41 | + jpg = fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') | ||
| 42 | + @link_to_image = upload_image(@project.repository, { 'markdown_img' => jpg }, "http://test.example/") | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + it { expect(@link_to_image).to have_key("alt") } | ||
| 46 | + it { expect(@link_to_image).to have_key("url") } | ||
| 47 | + it { expect(@link_to_image).to have_value("rails_sample") } | ||
| 48 | + it { expect(@link_to_image["url"]).to match("http://test.example/uploads/#{@project.path_with_namespace}") } | ||
| 49 | + it { expect(@link_to_image["url"]).to match("rails_sample.jpg") } | ||
| 50 | + end | ||
| 51 | + | ||
| 52 | + context 'for txt file' do | ||
| 53 | + before do | ||
| 54 | + txt = fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') | ||
| 55 | + @link_to_image = upload_image(@project.repository, { 'markdown_img' => txt }, "http://test.example/") | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + it { expect(@link_to_image).to be_nil } | ||
| 59 | + end | ||
| 60 | + end | ||
| 61 | + | ||
| 62 | + def upload_image(repository, params, root_url) | ||
| 63 | + Projects::ImageService.new(repository, params, root_url).execute | ||
| 64 | + end | ||
| 65 | +end |