Skip to content

Commit 1843502

Browse files
committed
Merge branch 'sh-fix-snippet-visibility-api' into 'master'
Fix snippets API not working with visibility level Closes #66050 See merge request gitlab-org/gitlab-ce!32286
2 parents f7e3693 + 680f437 commit 1843502

File tree

9 files changed

+108
-25
lines changed

9 files changed

+108
-25
lines changed

app/services/base_service.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def deny_visibility_level(model, denied_visibility_level = nil)
4444
model.errors.add(:visibility_level, "#{level_name} has been restricted by your GitLab administrator")
4545
end
4646

47+
def visibility_level
48+
params[:visibility].is_a?(String) ? Gitlab::VisibilityLevel.level_value(params[:visibility]) : params[:visibility_level]
49+
end
50+
4751
private
4852

4953
def error(message, http_status = nil)

app/services/create_snippet_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def execute
1212
PersonalSnippet.new(params)
1313
end
1414

15-
unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
15+
unless Gitlab::VisibilityLevel.allowed_for?(current_user, snippet.visibility_level)
1616
deny_visibility_level(snippet)
1717
return snippet
1818
end

app/services/groups/create_service.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,5 @@ def can_use_visibility_level?
6868

6969
true
7070
end
71-
72-
def visibility_level
73-
params[:visibility].present? ? Gitlab::VisibilityLevel.level_value(params[:visibility]) : params[:visibility_level]
74-
end
7571
end
7672
end

app/services/update_snippet_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(project, user, snippet, params)
1212

1313
def execute
1414
# check that user is allowed to set specified visibility_level
15-
new_visibility = params[:visibility_level]
15+
new_visibility = visibility_level
1616

1717
if new_visibility && new_visibility.to_i != snippet.visibility_level
1818
unless Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Fix snippets API not working with visibility level
3+
merge_request: 32286
4+
author:
5+
type: fixed

spec/requests/api/project_snippets_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@
9696
}
9797
end
9898

99+
context 'with a regular user' do
100+
let(:user) { create(:user) }
101+
102+
before do
103+
project.add_developer(user)
104+
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::PRIVATE])
105+
params['visibility'] = 'internal'
106+
end
107+
108+
it 'creates a new snippet' do
109+
post api("/projects/#{project.id}/snippets/", user), params: params
110+
111+
expect(response).to have_gitlab_http_status(201)
112+
snippet = ProjectSnippet.find(json_response['id'])
113+
expect(snippet.content).to eq(params[:code])
114+
expect(snippet.description).to eq(params[:description])
115+
expect(snippet.title).to eq(params[:title])
116+
expect(snippet.file_name).to eq(params[:file_name])
117+
expect(snippet.visibility_level).to eq(Snippet::INTERNAL)
118+
end
119+
end
120+
99121
it 'creates a new snippet' do
100122
post api("/projects/#{project.id}/snippets/", admin), params: params
101123

@@ -190,12 +212,13 @@ def create_snippet(project, snippet_params = {})
190212
new_content = 'New content'
191213
new_description = 'New description'
192214

193-
put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin), params: { code: new_content, description: new_description }
215+
put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin), params: { code: new_content, description: new_description, visibility: 'private' }
194216

195217
expect(response).to have_gitlab_http_status(200)
196218
snippet.reload
197219
expect(snippet.content).to eq(new_content)
198220
expect(snippet.description).to eq(new_description)
221+
expect(snippet.visibility).to eq('private')
199222
end
200223

201224
it 'updates snippet with content parameter' do

spec/requests/api/snippets_spec.rb

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,32 @@
193193
}
194194
end
195195

196-
it 'creates a new snippet' do
197-
expect do
198-
post api("/snippets/", user), params: params
199-
end.to change { PersonalSnippet.count }.by(1)
196+
shared_examples 'snippet creation' do
197+
it 'creates a new snippet' do
198+
expect do
199+
post api("/snippets/", user), params: params
200+
end.to change { PersonalSnippet.count }.by(1)
201+
202+
expect(response).to have_gitlab_http_status(201)
203+
expect(json_response['title']).to eq(params[:title])
204+
expect(json_response['description']).to eq(params[:description])
205+
expect(json_response['file_name']).to eq(params[:file_name])
206+
expect(json_response['visibility']).to eq(params[:visibility])
207+
end
208+
end
209+
210+
context 'with restricted visibility settings' do
211+
before do
212+
stub_application_setting(restricted_visibility_levels:
213+
[Gitlab::VisibilityLevel::INTERNAL,
214+
Gitlab::VisibilityLevel::PRIVATE])
215+
end
200216

201-
expect(response).to have_gitlab_http_status(201)
202-
expect(json_response['title']).to eq(params[:title])
203-
expect(json_response['description']).to eq(params[:description])
204-
expect(json_response['file_name']).to eq(params[:file_name])
205-
expect(json_response['visibility']).to eq(params[:visibility])
217+
it_behaves_like 'snippet creation'
206218
end
207219

220+
it_behaves_like 'snippet creation'
221+
208222
it 'returns 400 for missing parameters' do
209223
params.delete(:title)
210224

@@ -253,18 +267,33 @@ def create_snippet(snippet_params = {})
253267
create(:personal_snippet, author: user, visibility_level: visibility_level)
254268
end
255269

256-
it 'updates snippet' do
257-
new_content = 'New content'
258-
new_description = 'New description'
270+
shared_examples 'snippet updates' do
271+
it 'updates a snippet' do
272+
new_content = 'New content'
273+
new_description = 'New description'
259274

260-
put api("/snippets/#{snippet.id}", user), params: { content: new_content, description: new_description }
275+
put api("/snippets/#{snippet.id}", user), params: { content: new_content, description: new_description, visibility: 'internal' }
261276

262-
expect(response).to have_gitlab_http_status(200)
263-
snippet.reload
264-
expect(snippet.content).to eq(new_content)
265-
expect(snippet.description).to eq(new_description)
277+
expect(response).to have_gitlab_http_status(200)
278+
snippet.reload
279+
expect(snippet.content).to eq(new_content)
280+
expect(snippet.description).to eq(new_description)
281+
expect(snippet.visibility).to eq('internal')
282+
end
266283
end
267284

285+
context 'with restricted visibility settings' do
286+
before do
287+
stub_application_setting(restricted_visibility_levels:
288+
[Gitlab::VisibilityLevel::PUBLIC,
289+
Gitlab::VisibilityLevel::PRIVATE])
290+
end
291+
292+
it_behaves_like 'snippet updates'
293+
end
294+
295+
it_behaves_like 'snippet updates'
296+
268297
it 'returns 404 for invalid snippet id' do
269298
put api("/snippets/1234", user), params: { title: 'foo' }
270299

spec/services/create_snippet_service_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@
3434
expect(snippet.errors.any?).to be_falsey
3535
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
3636
end
37+
38+
describe "when visibility level is passed as a string" do
39+
before do
40+
@opts[:visibility] = 'internal'
41+
@opts.delete(:visibility_level)
42+
end
43+
44+
it "assigns the correct visibility level" do
45+
snippet = create_snippet(nil, @user, @opts)
46+
expect(snippet.errors.any?).to be_falsey
47+
expect(snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
48+
end
49+
end
3750
end
3851

3952
describe 'usage counter' do

spec/services/update_snippet_service_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,25 @@
3232
expect(@snippet.visibility_level).to eq(old_visibility)
3333
end
3434

35-
it 'admins should be able to update to pubic visibility' do
35+
it 'admins should be able to update to public visibility' do
3636
old_visibility = @snippet.visibility_level
3737
update_snippet(@project, @admin, @snippet, @opts)
3838
expect(@snippet.visibility_level).not_to eq(old_visibility)
3939
expect(@snippet.visibility_level).to eq(Gitlab::VisibilityLevel::PUBLIC)
4040
end
41+
42+
describe "when visibility level is passed as a string" do
43+
before do
44+
@opts[:visibility] = 'internal'
45+
@opts.delete(:visibility_level)
46+
end
47+
48+
it "assigns the correct visibility level" do
49+
update_snippet(@project, @user, @snippet, @opts)
50+
expect(@snippet.errors.any?).to be_falsey
51+
expect(@snippet.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
52+
end
53+
end
4154
end
4255

4356
describe 'usage counter' do

0 commit comments

Comments
 (0)