2019-09-30 08:06:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-10 15:37:48 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe TreeHelper do
|
2017-11-10 15:39:00 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:repository) { project.repository }
|
2018-10-17 20:03:15 -04:00
|
|
|
let(:sha) { 'c1c67abbaf91f624347bb3ae96eabe3a1b742478' }
|
2017-11-10 15:39:00 -05:00
|
|
|
|
2020-10-27 08:08:33 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2018-11-26 17:07:55 -05:00
|
|
|
def create_file(filename)
|
|
|
|
project.repository.create_file(
|
|
|
|
project.creator,
|
|
|
|
filename,
|
|
|
|
'test this',
|
|
|
|
message: "Automatically created file #{filename}",
|
|
|
|
branch_name: 'master'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-01-10 15:37:48 -05:00
|
|
|
describe 'flatten_tree' do
|
2017-09-06 16:47:25 -04:00
|
|
|
let(:tree) { repository.tree(sha, 'files') }
|
|
|
|
let(:root_path) { 'files' }
|
|
|
|
let(:tree_item) { tree.entries.find { |entry| entry.path == path } }
|
2015-01-10 15:37:48 -05:00
|
|
|
|
2017-09-06 16:47:25 -04:00
|
|
|
subject { flatten_tree(root_path, tree_item) }
|
2015-01-10 15:37:48 -05:00
|
|
|
|
|
|
|
context "on a directory containing more than one file/directory" do
|
2017-09-06 16:47:25 -04:00
|
|
|
let(:path) { 'files/html' }
|
2015-01-10 15:37:48 -05:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it "returns the directory name" do
|
2017-09-06 16:47:25 -04:00
|
|
|
expect(subject).to match('html')
|
2015-01-10 15:37:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "on a directory containing only one directory" do
|
2017-09-06 16:47:25 -04:00
|
|
|
let(:path) { 'files/flat' }
|
2015-01-10 15:37:48 -05:00
|
|
|
|
2016-07-25 14:16:19 -04:00
|
|
|
it "returns the flattened path" do
|
2017-09-06 16:47:25 -04:00
|
|
|
expect(subject).to match('flat/path/correct')
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a nested root path" do
|
|
|
|
let(:root_path) { 'files/flat' }
|
|
|
|
|
|
|
|
it "returns the flattened path with the root path suffix removed" do
|
|
|
|
expect(subject).to match('path/correct')
|
|
|
|
end
|
2015-01-10 15:37:48 -05:00
|
|
|
end
|
|
|
|
end
|
2018-04-04 09:56:41 -04:00
|
|
|
|
|
|
|
context 'when the root path contains a plus character' do
|
|
|
|
let(:root_path) { 'gtk/C++' }
|
|
|
|
let(:tree_item) { double(flat_path: 'gtk/C++/glade') }
|
|
|
|
|
|
|
|
it 'returns the flattened path' do
|
|
|
|
expect(subject).to eq('glade')
|
|
|
|
end
|
|
|
|
end
|
2015-01-10 15:37:48 -05:00
|
|
|
end
|
2018-02-28 03:06:18 -05:00
|
|
|
|
|
|
|
describe '#commit_in_single_accessible_branch' do
|
|
|
|
it 'escapes HTML from the branch name' do
|
|
|
|
helper.instance_variable_set(:@branch_name, "<script>alert('escape me!');</script>")
|
|
|
|
escaped_branch_name = '<script>alert('escape me!');</script>'
|
|
|
|
|
|
|
|
expect(helper.commit_in_single_accessible_branch).to include(escaped_branch_name)
|
|
|
|
end
|
|
|
|
end
|
2020-07-15 17:09:26 -04:00
|
|
|
|
|
|
|
describe '#vue_file_list_data' do
|
|
|
|
it 'returns a list of attributes related to the project' do
|
|
|
|
expect(helper.vue_file_list_data(project, sha)).to include(
|
|
|
|
project_path: project.full_path,
|
|
|
|
project_short_path: project.path,
|
2020-09-16 14:09:47 -04:00
|
|
|
ref: sha,
|
|
|
|
escaped_ref: sha,
|
2020-07-15 17:09:26 -04:00
|
|
|
full_name: project.name_with_namespace
|
|
|
|
)
|
|
|
|
end
|
2020-09-16 14:09:47 -04:00
|
|
|
end
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
describe '#web_ide_button_data' do
|
|
|
|
let(:blob) { project.repository.blob_at('refs/heads/master', @path) }
|
|
|
|
|
2020-09-16 14:09:47 -04:00
|
|
|
before do
|
2020-10-15 02:09:14 -04:00
|
|
|
@path = ''
|
|
|
|
@project = project
|
|
|
|
@ref = sha
|
|
|
|
|
2020-09-16 14:09:47 -04:00
|
|
|
allow(helper).to receive(:current_user).and_return(nil)
|
|
|
|
allow(helper).to receive(:can_collaborate_with_project?).and_return(true)
|
|
|
|
allow(helper).to receive(:can?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
subject { helper.web_ide_button_data(blob: blob) }
|
2020-09-16 14:09:47 -04:00
|
|
|
|
|
|
|
it 'returns a list of attributes related to the project' do
|
|
|
|
expect(subject).to include(
|
2020-10-15 02:09:14 -04:00
|
|
|
project_path: project.full_path,
|
|
|
|
ref: sha,
|
|
|
|
|
|
|
|
is_fork: false,
|
2020-09-16 14:09:47 -04:00
|
|
|
needs_to_fork: false,
|
2020-10-15 02:09:14 -04:00
|
|
|
gitpod_enabled: false,
|
|
|
|
is_blob: false,
|
|
|
|
|
|
|
|
show_edit_button: false,
|
2020-09-16 14:09:47 -04:00
|
|
|
show_web_ide_button: true,
|
|
|
|
show_gitpod_button: false,
|
2020-10-15 02:09:14 -04:00
|
|
|
|
|
|
|
edit_url: '',
|
|
|
|
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}",
|
|
|
|
gitpod_url: ''
|
2020-09-16 14:09:47 -04:00
|
|
|
)
|
|
|
|
end
|
2020-07-15 17:09:26 -04:00
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
context 'a blob is passed' do
|
|
|
|
before do
|
|
|
|
@path = 'README.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns edit url and webide url for the blob' do
|
|
|
|
expect(subject).to include(
|
|
|
|
show_edit_button: true,
|
|
|
|
edit_url: "/#{project.full_path}/-/edit/#{sha}/#{@path}",
|
|
|
|
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}/-/#{@path}"
|
|
|
|
)
|
|
|
|
end
|
2020-12-09 07:09:42 -05:00
|
|
|
|
|
|
|
it 'does not load blob from repository again' do
|
|
|
|
blob
|
|
|
|
|
|
|
|
expect(repository).not_to receive(:blob_at)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nil blob is passed' do
|
|
|
|
let(:blob) { nil }
|
|
|
|
|
|
|
|
it 'does not load blob from repository' do
|
|
|
|
expect(repository).not_to receive(:blob_at)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-10-15 02:09:14 -04:00
|
|
|
end
|
|
|
|
|
2020-07-15 17:09:26 -04:00
|
|
|
context 'user does not have write access but a personal fork exists' do
|
|
|
|
include ProjectForksHelper
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
let(:forked_project) { create(:project, :repository, namespace: user.namespace) }
|
2020-07-15 17:09:26 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
fork_project(project, nil, target_project: forked_project)
|
|
|
|
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
it 'includes forked project path as project_path' do
|
|
|
|
expect(subject).to include(
|
|
|
|
project_path: forked_project.full_path,
|
|
|
|
is_fork: true,
|
|
|
|
needs_to_fork: false,
|
|
|
|
show_edit_button: false,
|
|
|
|
web_ide_url: "/-/ide/project/#{forked_project.full_path}/edit/#{sha}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a blob is passed' do
|
|
|
|
before do
|
|
|
|
@path = 'README.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns edit url and web ide for the blob in the fork' do
|
|
|
|
expect(subject).to include(
|
|
|
|
is_blob: true,
|
|
|
|
show_edit_button: true,
|
|
|
|
# edit urls are automatically redirected to the fork
|
|
|
|
edit_url: "/#{project.full_path}/-/edit/#{sha}/#{@path}",
|
|
|
|
web_ide_url: "/-/ide/project/#{forked_project.full_path}/edit/#{sha}/-/#{@path}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for archived project' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:can_collaborate_with_project?).and_return(false)
|
|
|
|
allow(helper).to receive(:can?).and_return(false)
|
|
|
|
|
|
|
|
project.update!(archived: true)
|
|
|
|
|
|
|
|
@path = 'README.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not show any buttons' do
|
2020-09-16 14:09:47 -04:00
|
|
|
expect(subject).to include(
|
2020-10-15 02:09:14 -04:00
|
|
|
is_blob: true,
|
|
|
|
show_edit_button: false,
|
|
|
|
show_web_ide_button: false,
|
|
|
|
show_gitpod_button: false
|
2020-07-15 17:09:26 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user has write access' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
2020-10-15 02:09:14 -04:00
|
|
|
it 'includes original project path as project_path' do
|
2020-09-16 14:09:47 -04:00
|
|
|
expect(subject).to include(
|
2020-10-15 02:09:14 -04:00
|
|
|
project_path: project.full_path,
|
|
|
|
|
|
|
|
is_fork: false,
|
|
|
|
needs_to_fork: false,
|
|
|
|
|
|
|
|
show_edit_button: false,
|
|
|
|
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}"
|
2020-09-16 14:09:47 -04:00
|
|
|
)
|
|
|
|
end
|
2020-10-15 02:09:14 -04:00
|
|
|
|
|
|
|
context 'a blob is passed' do
|
|
|
|
before do
|
|
|
|
@path = 'README.md'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns edit url and web ide for the blob in the fork' do
|
|
|
|
expect(subject).to include(
|
|
|
|
is_blob: true,
|
|
|
|
show_edit_button: true,
|
|
|
|
edit_url: "/#{project.full_path}/-/edit/#{sha}/#{@path}",
|
|
|
|
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}/-/#{@path}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-09-16 14:09:47 -04:00
|
|
|
end
|
|
|
|
|
2021-01-12 10:10:37 -05:00
|
|
|
context 'gitpod settings is enabled' do
|
2020-09-16 14:09:47 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::CurrentSettings)
|
|
|
|
.to receive(:gitpod_enabled)
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has show_gitpod_button: true' do
|
|
|
|
expect(subject).to include(
|
|
|
|
show_gitpod_button: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has gitpod_enabled: true when user has enabled gitpod' do
|
|
|
|
user.gitpod_enabled = true
|
|
|
|
|
|
|
|
expect(subject).to include(
|
|
|
|
gitpod_enabled: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has gitpod_enabled: false when user has not enabled gitpod' do
|
|
|
|
user.gitpod_enabled = false
|
|
|
|
|
|
|
|
expect(subject).to include(
|
|
|
|
gitpod_enabled: false
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has show_gitpod_button: false when web ide button is not shown' do
|
|
|
|
allow(helper).to receive(:can_collaborate_with_project?).and_return(false)
|
|
|
|
allow(helper).to receive(:can?).and_return(false)
|
|
|
|
|
|
|
|
expect(subject).to include(
|
|
|
|
show_web_ide_button: false,
|
|
|
|
show_gitpod_button: false
|
2020-07-15 17:09:26 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-27 08:08:33 -04:00
|
|
|
|
|
|
|
describe '.patch_branch_name' do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { helper.patch_branch_name('master') }
|
|
|
|
|
|
|
|
it 'returns a patch branch name' do
|
|
|
|
freeze_time do
|
|
|
|
epoch = Time.now.strftime('%s%L').last(5)
|
|
|
|
|
|
|
|
expect(subject).to eq "#{user.username}-master-patch-#{epoch}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a current_user' do
|
|
|
|
let(:user) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-01-10 15:37:48 -05:00
|
|
|
end
|