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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe EventsHelper do
include Gitlab::Routing
describe '#event_commit_title' do
let(:message) { 'foo & bar ' + 'A' * 70 + '\n' + 'B' * 80 }
subject { helper.event_commit_title(message) }
it 'returns the first line, truncated to 70 chars' do
is_expected.to eq(message[0..66] + "...")
end
it 'is not html-safe' do
is_expected.not_to be_a(ActiveSupport::SafeBuffer)
end
it 'handles empty strings' do
expect(helper.event_commit_title("")).to eq("")
end
it 'handles nil values' do
expect(helper.event_commit_title(nil)).to eq('')
end
it 'does not escape HTML entities' do
expect(helper.event_commit_title('foo & bar')).to eq('foo & bar')
end
end
describe '#event_feed_url' do
let(:event) { create(:event).present }
let(:project) { create(:project, :public, :repository) }
context 'issue' do
before do
event.target = create(:issue)
end
it 'returns the project issue url' do
expect(helper.event_feed_url(event)).to eq(project_issue_url(event.project, event.target))
end
it 'contains the project issue IID link' do
expect(helper.event_feed_title(event)).to include("##{event.target.iid}")
end
end
context 'merge request' do
before do
event.target = create(:merge_request)
end
it 'returns the project merge request url' do
expect(helper.event_feed_url(event)).to eq(project_merge_request_url(event.project, event.target))
end
it 'contains the project merge request IID link' do
expect(helper.event_feed_title(event)).to include("!#{event.target.iid}")
end
end
it 'returns project commit url' do
event.target = create(:note_on_commit, project: project)
expect(helper.event_feed_url(event)).to eq(project_commit_url(event.project, event.note_target))
end
it 'returns event note target url' do
event.target = create(:note)
expect(helper.event_feed_url(event)).to eq(event_note_target_url(event))
end
it 'returns project url' do
event.project = project
event.action = 1
expect(helper.event_feed_url(event)).to eq(project_url(event.project))
end
it 'returns push event feed url' do
event = create(:push_event)
create(:push_event_payload, event: event, action: :pushed)
expect(helper.event_feed_url(event)).to eq(push_event_feed_url(event))
end
it 'returns nil for push event with multiple refs' do
event = create(:push_event)
create(:push_event_payload, event: event, ref_count: 2, ref: nil, ref_type: :tag, commit_count: 0, action: :pushed)
expect(helper.event_feed_url(event)).to eq(nil)
end
end
describe '#event_preposition' do
context 'for wiki page events' do
let(:event) { create(:wiki_page_event) }
it 'returns a suitable phrase' do
expect(helper.event_preposition(event)).to eq('in the wiki for')
end
end
context 'for push action events' do
let(:event) { create(:push_event) }
it 'returns a suitable phrase' do
expect(helper.event_preposition(event)).to eq('at')
end
end
context 'for commented actions' do
let(:event) { create(:event, :commented) }
it 'returns a suitable phrase' do
expect(helper.event_preposition(event)).to eq('at')
end
end
context 'for any event with a target' do
let(:event) { create(:event, target: create(:issue)) }
it 'returns a suitable phrase' do
expect(helper.event_preposition(event)).to eq('at')
end
end
context 'for milestone events' do
let(:event) { create(:event, target: create(:milestone)) }
it 'returns a suitable phrase' do
expect(helper.event_preposition(event)).to eq('in')
end
end
context 'for non-matching events' do
let(:event) { create(:event, :created) }
it 'returns no preposition' do
expect(helper.event_preposition(event)).to be_nil
end
end
end
describe 'event_wiki_page_target_url' do
let(:project) { create(:project) }
let(:wiki_page) { create(:wiki_page, wiki: create(:project_wiki, project: project)) }
let(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
it 'links to the wiki page' do
url = helper.project_wiki_url(project, wiki_page.slug)
expect(helper.event_wiki_page_target_url(event)).to eq(url)
end
context 'there is no canonical slug' do
let(:event) { create(:wiki_page_event, project: project) }
before do
event.target.slugs.update_all(canonical: false)
event.target.clear_memoization(:canonical_slug)
end
it 'links to the home page' do
url = helper.project_wiki_url(project, Wiki::HOMEPAGE)
expect(helper.event_wiki_page_target_url(event)).to eq(url)
end
end
end
describe '#event_wiki_title_html' do
let(:event) { create(:wiki_page_event) }
it 'produces a suitable title chunk' do
url = helper.event_wiki_page_target_url(event)
title = event.target_title
html = [
"<span class=\"event-target-type gl-mr-2\">wiki page</span>",
"<a title=\"#{title}\" class=\"has-tooltip event-target-link gl-mr-2\" href=\"#{url}\">",
title,
"</a>"
].join
expect(helper.event_wiki_title_html(event)).to eq(html)
end
end
describe '#event_note_target_url' do
let(:project) { create(:project, :public, :repository) }
let(:event) { create(:event, project: project) }
let(:project_base_url) { namespace_project_url(namespace_id: project.namespace, id: project) }
subject { helper.event_note_target_url(event) }
it 'returns a commit note url' do
event.target = create(:note_on_commit, note: '+1 from me')
expect(subject).to eq("#{project_base_url}/-/commit/#{event.target.commit_id}#note_#{event.target.id}")
end
it 'returns a project snippet note url' do
event.target = create(:note_on_project_snippet, note: 'keep going')
expect(subject).to eq("#{project_snippet_url(event.note_target.project, event.note_target)}#note_#{event.target.id}")
end
it 'returns a personal snippet note url' do
event.target = create(:note_on_personal_snippet, note: 'keep going')
expect(subject).to eq("#{snippet_url(event.note_target)}#note_#{event.target.id}")
end
it 'returns a project issue url' do
event.target = create(:note_on_issue, note: 'nice work')
expect(subject).to eq("#{project_base_url}/-/issues/#{event.note_target.iid}#note_#{event.target.id}")
end
it 'returns a merge request url' do
event.target = create(:note_on_merge_request, note: 'LGTM!')
expect(subject).to eq("#{project_base_url}/-/merge_requests/#{event.note_target.iid}#note_#{event.target.id}")
end
context 'for design note events' do
let(:event) { create(:event, :for_design, project: project) }
it 'returns an appropriate URL' do
iid = event.note_target.issue.iid
filename = event.note_target.filename
note_id = event.target.id
expect(subject).to eq("#{project_base_url}/-/issues/#{iid}/designs/#{filename}#note_#{note_id}")
end
end
end
describe '#event_filter_visible' do
include DesignManagementTestHelpers
let_it_be(:project) { create(:project) }
let_it_be(:current_user) { create(:user) }
subject { helper.event_filter_visible(key) }
before do
enable_design_management
project.add_reporter(current_user)
allow(helper).to receive(:current_user).and_return(current_user)
end
def disable_read_design_activity(object)
allow(Ability).to receive(:allowed?)
.with(current_user, :read_design_activity, eq(object))
.and_return(false)
end
context 'for :designs' do
let(:key) { :designs }
context 'there is no relevant instance variable' do
it { is_expected.to be(true) }
end
context 'a project has been assigned' do
before do
assign(:project, project)
end
it { is_expected.to be(true) }
context 'the current user cannot read design activity' do
before do
disable_read_design_activity(project)
end
it { is_expected.to be(false) }
end
end
context 'projects have been assigned' do
before do
assign(:projects, Project.where(id: project.id))
end
it { is_expected.to be(true) }
context 'the collection is empty' do
before do
assign(:projects, Project.none)
end
it { is_expected.to be(false) }
end
context 'the current user cannot read design activity' do
before do
disable_read_design_activity(project)
end
it { is_expected.to be(false) }
end
end
context 'a group has been assigned' do
let_it_be(:group) { create(:group) }
before do
assign(:group, group)
end
context 'there are no projects in the group' do
it { is_expected.to be(false) }
end
context 'the group has at least one project' do
before do
create(:project_group_link, project: project, group: group)
end
it { is_expected.to be(true) }
context 'the current user cannot read design activity' do
before do
disable_read_design_activity(group)
end
it { is_expected.to be(false) }
end
end
end
end
end
end
|