1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe TreeHelper do
let(:project) { create(:project, :repository) }
let(:repository) { project.repository }
let(:sha) { 'c1c67abbaf91f624347bb3ae96eabe3a1b742478' }
let_it_be(:user) { create(:user) }
def create_file(filename)
project.repository.create_file(
project.creator,
filename,
'test this',
message: "Automatically created file #{filename}",
branch_name: 'master'
)
end
describe 'flatten_tree' do
let(:tree) { repository.tree(sha, 'files') }
let(:root_path) { 'files' }
let(:tree_item) { tree.entries.find { |entry| entry.path == path } }
subject { flatten_tree(root_path, tree_item) }
context "on a directory containing more than one file/directory" do
let(:path) { 'files/html' }
it "returns the directory name" do
expect(subject).to match('html')
end
end
context "on a directory containing only one directory" do
let(:path) { 'files/flat' }
it "returns the flattened path" do
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
end
end
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
end
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
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,
ref: sha,
escaped_ref: sha,
full_name: project.name_with_namespace
)
end
end
describe '#web_ide_button_data' do
let(:blob) { project.repository.blob_at('refs/heads/master', @path) }
let_it_be(:user_preferences_gitpod_path) { '/-/profile/preferences#user_gitpod_enabled' }
let_it_be(:user_profile_enable_gitpod_path) { '/-/profile?user%5Bgitpod_enabled%5D=true' }
before do
@path = ''
@project = project
@ref = sha
allow(helper).to receive_messages(
current_user: nil,
can_collaborate_with_project?: true,
can?: true,
user_preferences_gitpod_path: user_preferences_gitpod_path,
user_profile_enable_gitpod_path: user_profile_enable_gitpod_path
)
end
subject { helper.web_ide_button_data(blob: blob) }
it 'returns a list of attributes related to the project' do
expect(subject).to include(
project_path: project.full_path,
ref: sha,
is_fork: false,
needs_to_fork: false,
gitpod_enabled: false,
is_blob: false,
show_edit_button: false,
show_web_ide_button: true,
show_gitpod_button: false,
edit_url: '',
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}",
gitpod_url: '',
user_preferences_gitpod_path: user_preferences_gitpod_path,
user_profile_enable_gitpod_path: user_profile_enable_gitpod_path
)
end
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
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
end
context 'user does not have write access but a personal fork exists' do
include ProjectForksHelper
let(:forked_project) { create(:project, :repository, namespace: user.namespace) }
before do
project.add_guest(user)
fork_project(project, nil, target_project: forked_project)
allow(helper).to receive(:current_user).and_return(user)
end
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
expect(subject).to include(
is_blob: true,
show_edit_button: false,
show_web_ide_button: false,
show_gitpod_button: false
)
end
end
context 'user has write access' do
before do
project.add_developer(user)
allow(helper).to receive(:current_user).and_return(user)
end
it 'includes original project path as project_path' do
expect(subject).to include(
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}"
)
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_url: "/#{project.full_path}/-/edit/#{sha}/#{@path}",
web_ide_url: "/-/ide/project/#{project.full_path}/edit/#{sha}/-/#{@path}"
)
end
end
end
context 'gitpod settings is enabled' do
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
)
end
end
end
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
end
|