Fix for creating a project through API when import_url is nil

The API was returning 500 when `nil` is passed for the `import_url`.

In fact, it was `Gitlab::UrlSanitizer.valid?` which was throwing a
`NoMethodError` when `nil` value was passed.
This commit is contained in:
Toon Claes 2017-03-09 16:39:09 +01:00
parent 7a7f487775
commit c92808ed32
4 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
title: Fix for creating a project through API when import_url is nil
merge_request: 9841
author:

View File

@ -9,6 +9,8 @@ module Gitlab
end
def self.valid?(url)
return false unless url
Addressable::URI.parse(url.strip)
true

View File

@ -70,4 +70,12 @@ describe Gitlab::UrlSanitizer, lib: true do
expect(sanitizer.full_url).to eq('user@server:project.git')
end
end
describe '.valid?' do
it 'validates url strings' do
expect(described_class.valid?(nil)).to be(false)
expect(described_class.valid?('valid@project:url.git')).to be(true)
expect(described_class.valid?('123://invalid:url')).to be(false)
end
end
end

View File

@ -424,6 +424,14 @@ describe API::Projects, api: true do
expect(json_response['only_allow_merge_if_all_discussions_are_resolved']).to be_truthy
end
it 'ignores import_url when it is nil' do
project = attributes_for(:project, { import_url: nil })
post api('/projects', user), project
expect(response).to have_http_status(201)
end
context 'when a visibility level is restricted' do
let(:project_param) { attributes_for(:project, visibility: 'public') }