Commit b5802d14 authored by Douwe Maan's avatar Douwe Maan Committed by Robert Speicher

project_from_ref returns nil when reference doesn't exist.

parent b5952494
......@@ -16,15 +16,12 @@ module Gitlab
#
# Returns a Project, or nil if the reference can't be accessed
def project_from_ref(ref)
if ref && other = Project.find_with_namespace(ref)
if user_can_reference_project?(other)
other
else
nil
end
else
context[:project]
end
return context[:project] unless ref
other = Project.find_with_namespace(ref)
return nil unless other && user_can_reference_project?(other)
other
end
def user_can_reference_project?(project, user = context[:current_user])
......
......@@ -13,9 +13,15 @@ module Gitlab::Markdown
include described_class
describe '#project_from_ref' do
context 'when referenced project does not exist' do
context 'when no project was referenced' do
it 'returns the project from context' do
expect(project_from_ref('invalid/reference')).to eq context[:project]
expect(project_from_ref(nil)).to eq context[:project]
end
end
context 'when referenced project does not exist' do
it 'returns nil' do
expect(project_from_ref('invalid/reference')).to be_nil
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