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
|
# frozen_string_literal: true
require 'spec_helper'
describe SnippetsFinder do
include ExternalAuthorizationServiceHelpers
include Gitlab::Allowable
describe '#initialize' do
it 'raises ArgumentError when a project and author are given' do
user = build(:user)
project = build(:project)
expect { described_class.new(user, author: user, project: project) }
.to raise_error(ArgumentError)
end
end
describe '#execute' do
let_it_be(:user) { create(:user) }
let_it_be(:admin) { create(:admin) }
let_it_be(:group) { create(:group, :public) }
let_it_be(:project) { create(:project, :public, group: group) }
let_it_be(:private_personal_snippet) { create(:personal_snippet, :private, author: user) }
let_it_be(:internal_personal_snippet) { create(:personal_snippet, :internal, author: user) }
let_it_be(:public_personal_snippet) { create(:personal_snippet, :public, author: user) }
let_it_be(:private_project_snippet) { create(:project_snippet, :private, project: project) }
let_it_be(:internal_project_snippet) { create(:project_snippet, :internal, project: project) }
let_it_be(:public_project_snippet) { create(:project_snippet, :public, project: project) }
context 'filter by scope' do
it "returns all snippets for 'all' scope" do
snippets = described_class.new(user, scope: :all).execute
expect(snippets).to contain_exactly(
private_personal_snippet, internal_personal_snippet, public_personal_snippet,
internal_project_snippet, public_project_snippet
)
end
it "returns all snippets for 'are_private' scope" do
snippets = described_class.new(user, scope: :are_private).execute
expect(snippets).to contain_exactly(private_personal_snippet)
end
it "returns all snippets for 'are_internal' scope" do
snippets = described_class.new(user, scope: :are_internal).execute
expect(snippets).to contain_exactly(internal_personal_snippet, internal_project_snippet)
end
it "returns all snippets for 'are_public' scope" do
snippets = described_class.new(user, scope: :are_public).execute
expect(snippets).to contain_exactly(public_personal_snippet, public_project_snippet)
end
end
context 'filter by author' do
it 'returns all public and internal snippets' do
snippets = described_class.new(create(:user), author: user).execute
expect(snippets).to contain_exactly(internal_personal_snippet, public_personal_snippet)
end
it 'returns internal snippets' do
snippets = described_class.new(user, author: user, scope: :are_internal).execute
expect(snippets).to contain_exactly(internal_personal_snippet)
end
it 'returns private snippets' do
snippets = described_class.new(user, author: user, scope: :are_private).execute
expect(snippets).to contain_exactly(private_personal_snippet)
end
it 'returns public snippets' do
snippets = described_class.new(user, author: user, scope: :are_public).execute
expect(snippets).to contain_exactly(public_personal_snippet)
end
it 'returns all snippets' do
snippets = described_class.new(user, author: user).execute
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
end
it 'returns only public snippets if unauthenticated user' do
snippets = described_class.new(nil, author: user).execute
expect(snippets).to contain_exactly(public_personal_snippet)
end
it 'returns all snippets for an admin' do
snippets = described_class.new(admin, author: user).execute
expect(snippets).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
end
end
context 'project snippets' do
it 'returns public personal and project snippets for unauthorized user' do
snippets = described_class.new(nil, project: project).execute
expect(snippets).to contain_exactly(public_project_snippet)
end
it 'returns public and internal snippets for non project members' do
snippets = described_class.new(user, project: project).execute
expect(snippets).to contain_exactly(internal_project_snippet, public_project_snippet)
end
it 'returns public snippets for non project members' do
snippets = described_class.new(user, project: project, scope: :are_public).execute
expect(snippets).to contain_exactly(public_project_snippet)
end
it 'returns internal snippets for non project members' do
snippets = described_class.new(user, project: project, scope: :are_internal).execute
expect(snippets).to contain_exactly(internal_project_snippet)
end
it 'does not return private snippets for non project members' do
snippets = described_class.new(user, project: project, scope: :are_private).execute
expect(snippets).to be_empty
end
it 'returns all snippets for project members' do
project.add_developer(user)
snippets = described_class.new(user, project: project).execute
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
end
it 'returns private snippets for project members' do
project.add_developer(user)
snippets = described_class.new(user, project: project, scope: :are_private).execute
expect(snippets).to contain_exactly(private_project_snippet)
end
it 'returns all snippets for an admin' do
snippets = described_class.new(admin, project: project).execute
expect(snippets).to contain_exactly(private_project_snippet, internal_project_snippet, public_project_snippet)
end
context 'filter by author' do
let!(:other_user) { create(:user) }
let!(:other_private_project_snippet) { create(:project_snippet, :private, project: project, author: other_user) }
let!(:other_internal_project_snippet) { create(:project_snippet, :internal, project: project, author: other_user) }
let!(:other_public_project_snippet) { create(:project_snippet, :public, project: project, author: other_user) }
it 'returns all snippets for project members' do
project.add_developer(user)
snippets = described_class.new(user, author: other_user).execute
expect(snippets)
.to contain_exactly(
other_private_project_snippet,
other_internal_project_snippet,
other_public_project_snippet
)
end
end
end
context 'explore snippets' do
it 'returns only public personal snippets for unauthenticated users' do
snippets = described_class.new(nil, explore: true).execute
expect(snippets).to contain_exactly(public_personal_snippet)
end
it 'also returns internal personal snippets for authenticated users' do
snippets = described_class.new(user, explore: true).execute
expect(snippets).to contain_exactly(
internal_personal_snippet, public_personal_snippet
)
end
it 'returns all personal snippets for admins' do
snippets = described_class.new(admin, explore: true).execute
expect(snippets).to contain_exactly(
private_personal_snippet, internal_personal_snippet, public_personal_snippet
)
end
end
context 'when the user cannot read cross project' do
before do
allow(Ability).to receive(:allowed?).and_call_original
allow(Ability).to receive(:allowed?).with(user, :read_cross_project) { false }
end
it 'returns only personal snippets when the user cannot read cross project' do
expect(described_class.new(user).execute).to contain_exactly(private_personal_snippet, internal_personal_snippet, public_personal_snippet)
end
end
end
it_behaves_like 'snippet visibility'
context 'external authorization' do
let(:user) { create(:user) }
let(:project) { create(:project) }
let!(:snippet) { create(:project_snippet, :public, project: project) }
before do
project.add_maintainer(user)
end
it_behaves_like 'a finder with external authorization service' do
let!(:subject) { create(:project_snippet, project: project) }
let(:project_params) { { project: project } }
end
it 'includes the result if the external service allows access' do
external_service_allow_access(user, project)
results = described_class.new(user, project: project).execute
expect(results).to contain_exactly(snippet)
end
it 'does not include any results if the external service denies access' do
external_service_deny_access(user, project)
results = described_class.new(user, project: project).execute
expect(results).to be_empty
end
end
end
|