Commit be25bbc4 authored by Rémy Coutable's avatar Rémy Coutable

Fix ProjectWiki#http_url_to_repo signature

New Gitlab::UrlSanitizer.http_credentials_for_user method responsible
for generating a credentials hash from a user.
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent cd3e4101
...@@ -880,13 +880,9 @@ class Project < ActiveRecord::Base ...@@ -880,13 +880,9 @@ class Project < ActiveRecord::Base
end end
def http_url_to_repo(user = nil) def http_url_to_repo(user = nil)
url = web_url credentials = Gitlab::UrlSanitizer.http_credentials_for_user(user)
if user Gitlab::UrlSanitizer.new("#{web_url}.git", credentials: credentials).full_url
url.sub!(%r{\Ahttps?://}) { |protocol| "#{protocol}#{user.username}@" }
end
"#{url}.git"
end end
# Check if current branch name is marked as protected in the system # Check if current branch name is marked as protected in the system
......
...@@ -42,8 +42,11 @@ class ProjectWiki ...@@ -42,8 +42,11 @@ class ProjectWiki
url_to_repo url_to_repo
end end
def http_url_to_repo def http_url_to_repo(user = nil)
[Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('') url = "#{Gitlab.config.gitlab.url}/#{path_with_namespace}.git"
credentials = Gitlab::UrlSanitizer.http_credentials_for_user(user)
Gitlab::UrlSanitizer.new(url, credentials: credentials).full_url
end end
def wiki_base_path def wiki_base_path
......
...@@ -18,6 +18,12 @@ module Gitlab ...@@ -18,6 +18,12 @@ module Gitlab
false false
end end
def self.http_credentials_for_user(user)
return {} unless user.respond_to?(:username)
{ user: user.username }
end
def initialize(url, credentials: nil) def initialize(url, credentials: nil)
@url = Addressable::URI.parse(url.strip) @url = Addressable::URI.parse(url.strip)
@credentials = credentials @credentials = credentials
......
require 'spec_helper'
describe 'Projects > Wiki > User views Git access wiki page', :feature do
let(:user) { create(:user) }
let(:project) { create(:project, :public) }
let(:wiki_page) do
WikiPages::CreateService.new(
project,
user,
title: 'home',
content: '[some link](other-page)'
).execute
end
before do
login_as(user)
end
scenario 'Visit Wiki Page Current Commit' do
visit namespace_project_wiki_path(project.namespace, project, wiki_page)
click_link 'Clone repository'
expect(page).to have_text("Clone repository #{project.wiki.path_with_namespace}")
expect(page).to have_text(project.wiki.http_url_to_repo(user))
end
end
...@@ -5,6 +5,7 @@ describe Gitlab::UrlSanitizer, lib: true do ...@@ -5,6 +5,7 @@ describe Gitlab::UrlSanitizer, lib: true do
let(:url_sanitizer) do let(:url_sanitizer) do
described_class.new("https://github.com/me/project.git", credentials: credentials) described_class.new("https://github.com/me/project.git", credentials: credentials)
end end
let(:user) { double(:user, username: 'john.doe') }
describe '.sanitize' do describe '.sanitize' do
def sanitize_url(url) def sanitize_url(url)
...@@ -53,12 +54,33 @@ describe Gitlab::UrlSanitizer, lib: true do ...@@ -53,12 +54,33 @@ describe Gitlab::UrlSanitizer, lib: true do
end end
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
describe '.http_credentials_for_user' do
it { expect(described_class.http_credentials_for_user(user)).to eq({ user: 'john.doe' }) }
it { expect(described_class.http_credentials_for_user('foo')).to eq({}) }
end
describe '#sanitized_url' do describe '#sanitized_url' do
it { expect(url_sanitizer.sanitized_url).to eq("https://github.com/me/project.git") } it { expect(url_sanitizer.sanitized_url).to eq("https://github.com/me/project.git") }
end end
describe '#credentials' do describe '#credentials' do
it { expect(url_sanitizer.credentials).to eq(credentials) } it { expect(url_sanitizer.credentials).to eq(credentials) }
context 'when user is given to #initialize' do
let(:url_sanitizer) do
described_class.new("https://github.com/me/project.git", credentials: described_class.http_credentials_for_user(user))
end
it { expect(url_sanitizer.credentials).to eq({ user: 'john.doe' }) }
end
end end
describe '#full_url' do describe '#full_url' do
...@@ -69,13 +91,13 @@ describe Gitlab::UrlSanitizer, lib: true do ...@@ -69,13 +91,13 @@ describe Gitlab::UrlSanitizer, lib: true do
expect(sanitizer.full_url).to eq('user@server:project.git') expect(sanitizer.full_url).to eq('user@server:project.git')
end end
end
describe '.valid?' do context 'when user is given to #initialize' do
it 'validates url strings' do let(:url_sanitizer) do
expect(described_class.valid?(nil)).to be(false) described_class.new("https://github.com/me/project.git", credentials: described_class.http_credentials_for_user(user))
expect(described_class.valid?('valid@project:url.git')).to be(true) end
expect(described_class.valid?('123://invalid:url')).to be(false)
it { expect(url_sanitizer.full_url).to eq("https://john.doe@github.com/me/project.git") }
end end
end end
end end
...@@ -1900,10 +1900,8 @@ describe Project, models: true do ...@@ -1900,10 +1900,8 @@ describe Project, models: true do
context 'when no user is given' do context 'when no user is given' do
it 'returns the url to the repo without a username' do it 'returns the url to the repo without a username' do
url = project.http_url_to_repo expect(project.http_url_to_repo).to eq("#{project.web_url}.git")
expect(project.http_url_to_repo).not_to include('@')
expect(url).to eq(project.http_url_to_repo)
expect(url).not_to include('@')
end end
end end
...@@ -1911,7 +1909,7 @@ describe Project, models: true do ...@@ -1911,7 +1909,7 @@ describe Project, models: true do
it 'returns the url to the repo with the username' do it 'returns the url to the repo with the username' do
user = build_stubbed(:user) user = build_stubbed(:user)
expect(project.http_url_to_repo(user)).to match(%r{https?:\/\/#{user.username}@}) expect(project.http_url_to_repo(user)).to start_with("http://#{user.username}@")
end end
end end
end end
......
...@@ -35,10 +35,23 @@ describe ProjectWiki, models: true do ...@@ -35,10 +35,23 @@ describe ProjectWiki, models: true do
end end
describe "#http_url_to_repo" do describe "#http_url_to_repo" do
it "provides the full http url to the repo" do let(:project) { create :empty_project }
gitlab_url = Gitlab.config.gitlab.url
repo_http_url = "#{gitlab_url}/#{subject.path_with_namespace}.git" context 'when no user is given' do
expect(subject.http_url_to_repo).to eq(repo_http_url) it 'returns the url to the repo without a username' do
expected_url = "#{Gitlab.config.gitlab.url}/#{subject.path_with_namespace}.git"
expect(project_wiki.http_url_to_repo).to eq(expected_url)
expect(project_wiki.http_url_to_repo).not_to include('@')
end
end
context 'when user is given' do
it 'returns the url to the repo with the username' do
user = build_stubbed(:user)
expect(project_wiki.http_url_to_repo(user)).to start_with("http://#{user.username}@")
end
end end
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