2016-05-19 02:16:36 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Gitlab::UrlSanitizer do
|
2016-05-19 02:16:36 +00:00
|
|
|
let(:credentials) { { user: 'blah', password: 'password' } }
|
|
|
|
let(:url_sanitizer) do
|
|
|
|
described_class.new("https://github.com/me/project.git", credentials: credentials)
|
|
|
|
end
|
2017-03-20 09:56:43 +00:00
|
|
|
let(:user) { double(:user, username: 'john.doe') }
|
2016-05-19 02:16:36 +00:00
|
|
|
|
|
|
|
describe '.sanitize' do
|
|
|
|
def sanitize_url(url)
|
|
|
|
# We want to try with multi-line content because is how error messages are formatted
|
|
|
|
described_class.sanitize(%Q{
|
|
|
|
remote: Not Found
|
|
|
|
fatal: repository '#{url}' not found
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'mask the credentials from HTTP URLs' do
|
|
|
|
filtered_content = sanitize_url('http://user:pass@test.com/root/repoC.git/')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("http://*****:*****@test.com/root/repoC.git/")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'mask the credentials from HTTPS URLs' do
|
|
|
|
filtered_content = sanitize_url('https://user:pass@test.com/root/repoA.git/')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("https://*****:*****@test.com/root/repoA.git/")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'mask credentials from SSH URLs' do
|
|
|
|
filtered_content = sanitize_url('ssh://user@host.test/path/to/repo.git')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("ssh://*****@host.test/path/to/repo.git")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not modify Git URLs' do
|
|
|
|
# git protocol does not support authentication
|
|
|
|
filtered_content = sanitize_url('git://host.test/path/to/repo.git')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("git://host.test/path/to/repo.git")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not modify scp-like URLs' do
|
|
|
|
filtered_content = sanitize_url('user@server:project.git')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("user@server:project.git")
|
|
|
|
end
|
2016-07-11 07:01:09 +00:00
|
|
|
|
|
|
|
it 'returns an empty string for invalid URLs' do
|
|
|
|
filtered_content = sanitize_url('ssh://')
|
|
|
|
|
|
|
|
expect(filtered_content).to include("repository '' not found")
|
|
|
|
end
|
2016-05-19 02:16:36 +00:00
|
|
|
end
|
|
|
|
|
2017-03-20 09:56:43 +00:00
|
|
|
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
|
|
|
|
|
2016-05-19 02:16:36 +00:00
|
|
|
describe '#sanitized_url' do
|
|
|
|
it { expect(url_sanitizer.sanitized_url).to eq("https://github.com/me/project.git") }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#credentials' do
|
|
|
|
it { expect(url_sanitizer.credentials).to eq(credentials) }
|
2017-03-20 09:56:43 +00:00
|
|
|
|
|
|
|
context 'when user is given to #initialize' do
|
|
|
|
let(:url_sanitizer) do
|
2017-05-30 12:18:58 +00:00
|
|
|
described_class.new("https://github.com/me/project.git", credentials: { user: user.username })
|
2017-03-20 09:56:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(url_sanitizer.credentials).to eq({ user: 'john.doe' }) }
|
|
|
|
end
|
2016-05-19 02:16:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#full_url' do
|
|
|
|
it { expect(url_sanitizer.full_url).to eq("https://blah:password@github.com/me/project.git") }
|
|
|
|
|
|
|
|
it 'supports scp-like URLs' do
|
|
|
|
sanitizer = described_class.new('user@server:project.git')
|
|
|
|
|
|
|
|
expect(sanitizer.full_url).to eq('user@server:project.git')
|
|
|
|
end
|
2017-03-09 15:39:09 +00:00
|
|
|
|
2017-03-20 09:56:43 +00:00
|
|
|
context 'when user is given to #initialize' do
|
|
|
|
let(:url_sanitizer) do
|
2017-05-30 12:18:58 +00:00
|
|
|
described_class.new("https://github.com/me/project.git", credentials: { user: user.username })
|
2017-03-20 09:56:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(url_sanitizer.full_url).to eq("https://john.doe@github.com/me/project.git") }
|
2017-03-09 15:39:09 +00:00
|
|
|
end
|
|
|
|
end
|
2016-05-19 02:16:36 +00:00
|
|
|
end
|