project_from_ref returns nil when reference doesn't exist.

This commit is contained in:
Douwe Maan 2015-04-17 12:34:51 +02:00 committed by Robert Speicher
parent b595249462
commit b5802d144b
2 changed files with 14 additions and 11 deletions

View file

@ -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])

View file

@ -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