Commit 903aab5f authored by James Lopez's avatar James Lopez

refactor wiki and added spec

parent c8321792
module EE
module Gitlab
module GeoGitAccess
GEO_SERVER_DOCS_URL = 'https://docs.gitlab.com/ee/gitlab-geo/using_a_geo_server.html'.freeze
private
def push_to_read_only_message
message = super
if ::Gitlab::Geo.primary_node
primary_url = ActionController::Base.helpers.link_to('primary node', ::Gitlab::Geo.primary_node.url)
message += " Please use the Primary node URL: #{primary_url.html_safe}. Documentation: #{GEO_SERVER_DOCS_URL}"
end
message
end
end
end
end
module Gitlab
class GitAccessWiki < GitAccess
prepend EE::Gitlab::GeoGitAccess
ERROR_MESSAGES = {
read_only: "You can't push code to a read-only GitLab instance.",
write_to_wiki: "You are not allowed to write to this project's wiki."
......@@ -19,10 +21,14 @@ module Gitlab
end
if Gitlab::Database.read_only?
raise UnauthorizedError, ERROR_MESSAGES[:read_only]
raise UnauthorizedError, push_to_read_only_message
end
true
end
def push_to_read_only_message
ERROR_MESSAGES[:read_only]
end
end
end
require 'spec_helper'
describe Gitlab::GitAccessWiki do
let(:access) { described_class.new(user, project, 'web', authentication_abilities: authentication_abilities, redirected_path: redirected_path) }
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:changes) { ['6f6d7e7ed 570e7b2ab refs/heads/master'] }
let(:redirected_path) { nil }
let(:authentication_abilities) do
[
:read_project,
:download_code,
:push_code
]
end
subject { access.check('git-receive-pack', changes) }
context "when in a read-only GitLab instance" do
before do
create(:protected_branch, name: 'feature', project: project)
allow(Gitlab::Database).to receive(:read_only?) { true }
end
it 'denies push access' do
project.add_master(user)
expect { subject }.to raise_unauthorized("You can't push code to a read-only GitLab instance.")
end
it 'denies push access with primary present' do
error_message = "You can't push code to a read-only GitLab instance. Please use the Primary node URL:"\
" <a href=\"https://localhost:3000/gitlab/\">primary node</a>. Documentation: https://docs.gitlab.com/ee/gitlab-geo/using_a_geo_server.html"
primary_node = create(:geo_node, :primary, url: 'https://localhost:3000/gitlab')
allow(Gitlab::Geo).to receive(:primary).and_return(primary_node)
project.add_master(user)
expect { subject }.to raise_unauthorized(error_message)
end
end
private
def raise_unauthorized(message)
raise_error(Gitlab::GitAccess::UnauthorizedError, message)
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