Commit a87025cd authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '207867-wiki-services' into 'master'

Allow CreateAttachmentService to work with group

See merge request gitlab-org/gitlab!31195
parents 6959f7c4 6ac8983f
...@@ -5,12 +5,15 @@ module Wikis ...@@ -5,12 +5,15 @@ module Wikis
ATTACHMENT_PATH = 'uploads' ATTACHMENT_PATH = 'uploads'
MAX_FILENAME_LENGTH = 255 MAX_FILENAME_LENGTH = 255
delegate :wiki, to: :project attr_reader :container
delegate :wiki, to: :container
delegate :repository, to: :wiki delegate :repository, to: :wiki
def initialize(*args) def initialize(container:, current_user: nil, params: {})
super super(nil, current_user, params)
@container = container
@file_name = clean_file_name(params[:file_name]) @file_name = clean_file_name(params[:file_name])
@file_path = File.join(ATTACHMENT_PATH, SecureRandom.hex, @file_name) if @file_name @file_path = File.join(ATTACHMENT_PATH, SecureRandom.hex, @file_name) if @file_name
@commit_message ||= "Upload attachment #{@file_name}" @commit_message ||= "Upload attachment #{@file_name}"
...@@ -51,7 +54,7 @@ module Wikis ...@@ -51,7 +54,7 @@ module Wikis
end end
def validate_permissions! def validate_permissions!
unless can?(current_user, :create_wiki, project) unless can?(current_user, :create_wiki, container)
raise_error('You are not allowed to push to the wiki') raise_error('You are not allowed to push to the wiki')
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
describe Wikis::CreateAttachmentService do
it_behaves_like 'Wikis::CreateAttachmentService#execute', :group
end
...@@ -123,9 +123,11 @@ module API ...@@ -123,9 +123,11 @@ module API
post ":id/wikis/attachments" do post ":id/wikis/attachments" do
authorize! :create_wiki, user_project authorize! :create_wiki, user_project
result = ::Wikis::CreateAttachmentService.new(user_project, result = ::Wikis::CreateAttachmentService.new(
current_user, container: user_project,
commit_params(declared_params(include_missing: false))).execute current_user: current_user,
params: commit_params(declared_params(include_missing: false))
).execute
if result[:status] == :success if result[:status] == :success
status(201) status(201)
......
...@@ -320,7 +320,7 @@ describe Git::WikiPushService, services: true do ...@@ -320,7 +320,7 @@ describe Git::WikiPushService, services: true do
file_content: 'some stuff', file_content: 'some stuff',
branch_name: 'master' branch_name: 'master'
} }
::Wikis::CreateAttachmentService.new(project, project.owner, params).execute ::Wikis::CreateAttachmentService.new(container: project, current_user: project.owner, params: params).execute
end end
def update_page(title) def update_page(title)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Wikis::CreateAttachmentService do describe Wikis::CreateAttachmentService do
let(:project) { create(:project, :wiki_repo) } let(:container) { create(:project, :wiki_repo) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:file_name) { 'filename.txt' } let(:file_name) { 'filename.txt' }
let(:file_path_regex) { %r{#{described_class::ATTACHMENT_PATH}/\h{32}/#{file_name}} } let(:file_path_regex) { %r{#{described_class::ATTACHMENT_PATH}/\h{32}/#{file_name}} }
...@@ -15,25 +15,21 @@ describe Wikis::CreateAttachmentService do ...@@ -15,25 +15,21 @@ describe Wikis::CreateAttachmentService do
end end
let(:opts) { file_opts } let(:opts) { file_opts }
subject(:service) { described_class.new(project, user, opts) } subject(:service) { described_class.new(container: container, current_user: user, params: opts) }
before do before do
project.add_developer(user) container.add_developer(user)
end end
describe 'initialization' do describe 'initialization' do
context 'author commit info' do context 'author commit info' do
it 'does not raise error if user is nil' do it 'does not raise error if user is nil' do
service = described_class.new(project, nil, opts) service = described_class.new(container: container, current_user: nil, params: opts)
expect(service.instance_variable_get(:@author_email)).to be_nil expect(service.instance_variable_get(:@author_email)).to be_nil
expect(service.instance_variable_get(:@author_name)).to be_nil expect(service.instance_variable_get(:@author_name)).to be_nil
end end
it 'fills file_path from the repository uploads folder' do
expect(service.instance_variable_get(:@file_path)).to match(file_path_regex)
end
context 'when no author info provided' do context 'when no author info provided' do
it 'fills author_email and author_name from current_user info' do it 'fills author_email and author_name from current_user info' do
expect(service.instance_variable_get(:@author_email)).to eq user.email expect(service.instance_variable_get(:@author_email)).to eq user.email
...@@ -73,7 +69,7 @@ describe Wikis::CreateAttachmentService do ...@@ -73,7 +69,7 @@ describe Wikis::CreateAttachmentService do
context 'branch name' do context 'branch name' do
context 'when no branch provided' do context 'when no branch provided' do
it 'sets the branch from the wiki default_branch' do it 'sets the branch from the wiki default_branch' do
expect(service.instance_variable_get(:@branch_name)).to eq project.wiki.default_branch expect(service.instance_variable_get(:@branch_name)).to eq container.wiki.default_branch
end end
end end
...@@ -151,7 +147,7 @@ describe Wikis::CreateAttachmentService do ...@@ -151,7 +147,7 @@ describe Wikis::CreateAttachmentService do
context 'when user' do context 'when user' do
shared_examples 'wiki attachment user validations' do shared_examples 'wiki attachment user validations' do
it 'returns error' do it 'returns error' do
result = described_class.new(project, user2, opts).execute result = described_class.new(container: container, current_user: user2, params: opts).execute
expect(result[:status]).to eq :error expect(result[:status]).to eq :error
expect(result[:message]).to eq 'You are not allowed to push to the wiki' expect(result[:message]).to eq 'You are not allowed to push to the wiki'
...@@ -172,54 +168,5 @@ describe Wikis::CreateAttachmentService do ...@@ -172,54 +168,5 @@ describe Wikis::CreateAttachmentService do
end end
end end
describe '#execute' do it_behaves_like 'Wikis::CreateAttachmentService#execute', :project
let(:wiki) { project.wiki }
subject(:service_execute) { service.execute[:result] }
context 'creates branch if it does not exists' do
let(:branch_name) { 'new_branch' }
let(:opts) { file_opts.merge(branch_name: branch_name) }
it do
expect(wiki.repository.branches).to be_empty
expect { service.execute }.to change { wiki.repository.branches.count }.by(1)
expect(wiki.repository.branches.first.name).to eq branch_name
end
end
it 'adds file to the repository' do
expect(wiki.repository.ls_files('HEAD')).to be_empty
service.execute
files = wiki.repository.ls_files('HEAD')
expect(files.count).to eq 1
expect(files.first).to match(file_path_regex)
end
context 'returns' do
before do
allow(SecureRandom).to receive(:hex).and_return('fixed_hex')
service_execute
end
it 'returns the file name' do
expect(service_execute[:file_name]).to eq file_name
end
it 'returns the path where file was stored' do
expect(service_execute[:file_path]).to eq 'uploads/fixed_hex/filename.txt'
end
it 'returns the branch where the file was pushed' do
expect(service_execute[:branch]).to eq wiki.default_branch
end
it 'returns the commit id' do
expect(service_execute[:commit]).not_to be_empty
end
end
end
end end
...@@ -14,7 +14,10 @@ module WikiHelpers ...@@ -14,7 +14,10 @@ module WikiHelpers
file_content: File.read(expand_fixture_path(file_name)) file_content: File.read(expand_fixture_path(file_name))
} }
::Wikis::CreateAttachmentService.new(project, user, opts) ::Wikis::CreateAttachmentService.new(
.execute[:result][:file_path] container: project,
current_user: user,
params: opts
).execute[:result][:file_path]
end end
end end
# frozen_string_literal: true
RSpec.shared_examples 'Wikis::CreateAttachmentService#execute' do |container_type|
let(:container) { create(container_type, :wiki_repo) }
let(:wiki) { container.wiki }
let(:user) { create(:user) }
let(:file_name) { 'filename.txt' }
let(:file_path_regex) { %r{#{described_class::ATTACHMENT_PATH}/\h{32}/#{file_name}} }
let(:file_opts) do
{
file_name: file_name,
file_content: 'Content of attachment'
}
end
let(:opts) { file_opts }
let(:service) { Wikis::CreateAttachmentService.new(container: container, current_user: user, params: opts) }
subject(:service_execute) { service.execute[:result] }
before do
container.add_developer(user)
end
context 'creates branch if it does not exists' do
let(:branch_name) { 'new_branch' }
let(:opts) { file_opts.merge(branch_name: branch_name) }
it do
expect(wiki.repository.branches).to be_empty
expect { service.execute }.to change { wiki.repository.branches.count }.by(1)
expect(wiki.repository.branches.first.name).to eq branch_name
end
end
it 'adds file to the repository' do
expect(wiki.repository.ls_files('HEAD')).to be_empty
service.execute
files = wiki.repository.ls_files('HEAD')
expect(files.count).to eq 1
expect(files.first).to match(file_path_regex)
end
context 'returns' do
before do
allow(SecureRandom).to receive(:hex).and_return('fixed_hex')
service_execute
end
it 'returns related information', :aggregate_failures do
expect(service_execute[:file_name]).to eq file_name
expect(service_execute[:file_path]).to eq 'uploads/fixed_hex/filename.txt'
expect(service_execute[:branch]).to eq wiki.default_branch
expect(service_execute[:commit]).not_to be_empty
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment