Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
ac3438f5
Commit
ac3438f5
authored
May 18, 2020
by
Amparo Luna
Committed by
Heinrich Lee Yu
May 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve responses in the snippet create/update API endpoints
parent
b0dc27be
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
85 additions
and
16 deletions
+85
-16
changelogs/unreleased/al-214695-improve-response-in-snippets-api.yml
...unreleased/al-214695-improve-response-in-snippets-api.yml
+5
-0
lib/api/project_snippets.rb
lib/api/project_snippets.rb
+8
-8
lib/api/snippets.rb
lib/api/snippets.rb
+8
-8
spec/requests/api/project_snippets_spec.rb
spec/requests/api/project_snippets_spec.rb
+14
-0
spec/requests/api/snippets_spec.rb
spec/requests/api/snippets_spec.rb
+28
-0
spec/support/shared_examples/requests/snippet_shared_examples.rb
...pport/shared_examples/requests/snippet_shared_examples.rb
+22
-0
No files found.
changelogs/unreleased/al-214695-improve-response-in-snippets-api.yml
0 → 100644
View file @
ac3438f5
---
title
:
Improve responses in the snippet create/update API endpoints
merge_request
:
32282
author
:
type
:
fixed
lib/api/project_snippets.rb
View file @
ac3438f5
...
...
@@ -70,12 +70,12 @@ module API
service_response
=
::
Snippets
::
CreateService
.
new
(
user_project
,
current_user
,
snippet_params
).
execute
snippet
=
service_response
.
payload
[
:snippet
]
render_spam_error!
if
snippet
.
spam?
if
snippet
.
persisted?
if
service_response
.
success?
present
snippet
,
with:
Entities
::
ProjectSnippet
else
render_validation_error!
(
snippet
)
render_spam_error!
if
snippet
.
spam?
render_api_error!
({
error:
service_response
.
message
},
service_response
.
http_status
)
end
end
...
...
@@ -106,12 +106,12 @@ module API
service_response
=
::
Snippets
::
UpdateService
.
new
(
user_project
,
current_user
,
snippet_params
).
execute
(
snippet
)
snippet
=
service_response
.
payload
[
:snippet
]
render_spam_error!
if
snippet
.
spam?
if
snippet
.
valid?
if
service_response
.
success?
present
snippet
,
with:
Entities
::
ProjectSnippet
else
render_validation_error!
(
snippet
)
render_spam_error!
if
snippet
.
spam?
render_api_error!
({
error:
service_response
.
message
},
service_response
.
http_status
)
end
end
# rubocop: enable CodeReuse/ActiveRecord
...
...
lib/api/snippets.rb
View file @
ac3438f5
...
...
@@ -81,12 +81,12 @@ module API
service_response
=
::
Snippets
::
CreateService
.
new
(
nil
,
current_user
,
attrs
).
execute
snippet
=
service_response
.
payload
[
:snippet
]
render_spam_error!
if
snippet
.
spam?
if
snippet
.
persisted?
if
service_response
.
success?
present
snippet
,
with:
Entities
::
PersonalSnippet
else
render_validation_error!
(
snippet
)
render_spam_error!
if
snippet
.
spam?
render_api_error!
({
error:
service_response
.
message
},
service_response
.
http_status
)
end
end
...
...
@@ -115,12 +115,12 @@ module API
service_response
=
::
Snippets
::
UpdateService
.
new
(
nil
,
current_user
,
attrs
).
execute
(
snippet
)
snippet
=
service_response
.
payload
[
:snippet
]
render_spam_error!
if
snippet
.
spam?
if
snippet
.
persisted?
if
service_response
.
success?
present
snippet
,
with:
Entities
::
PersonalSnippet
else
render_validation_error!
(
snippet
)
render_spam_error!
if
snippet
.
spam?
render_api_error!
({
error:
service_response
.
message
},
service_response
.
http_status
)
end
end
...
...
spec/requests/api/project_snippets_spec.rb
View file @
ac3438f5
...
...
@@ -224,6 +224,20 @@ describe API::ProjectSnippets do
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
context
'when save fails because the repository could not be created'
do
before
do
allow_next_instance_of
(
Snippets
::
CreateService
)
do
|
instance
|
allow
(
instance
).
to
receive
(
:create_repository
).
and_raise
(
Snippets
::
CreateService
::
CreateRepositoryError
)
end
end
it
'returns 400'
do
post
api
(
"/projects/
#{
project
.
id
}
/snippets"
,
admin
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
end
context
'when the snippet is spam'
do
def
create_snippet
(
project
,
snippet_params
=
{})
project
.
add_developer
(
user
)
...
...
spec/requests/api/snippets_spec.rb
View file @
ac3438f5
...
...
@@ -267,6 +267,28 @@ describe API::Snippets do
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
it
'returns 400 for validation errors'
do
params
[
:title
]
=
''
post
api
(
"/snippets/"
,
user
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
context
'when save fails because the repository could not be created'
do
before
do
allow_next_instance_of
(
Snippets
::
CreateService
)
do
|
instance
|
allow
(
instance
).
to
receive
(
:create_repository
).
and_raise
(
Snippets
::
CreateService
::
CreateRepositoryError
)
end
end
it
'returns 400'
do
post
api
(
"/snippets/"
,
user
),
params:
params
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
end
context
'when the snippet is spam'
do
def
create_snippet
(
snippet_params
=
{})
post
api
(
'/snippets'
,
user
),
params:
params
.
merge
(
snippet_params
)
...
...
@@ -356,6 +378,12 @@ describe API::Snippets do
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
it
'returns 400 for validation errors'
do
update_snippet
(
params:
{
title:
''
})
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
it_behaves_like
'update with repository actions'
do
let
(
:snippet_without_repo
)
{
create
(
:personal_snippet
,
author:
user
,
visibility_level:
visibility_level
)
}
end
...
...
spec/support/shared_examples/requests/snippet_shared_examples.rb
View file @
ac3438f5
...
...
@@ -48,6 +48,28 @@ RSpec.shared_examples 'update with repository actions' do
expect
(
blob
).
not_to
be_nil
expect
(
blob
.
data
).
to
eq
content
end
context
'when save fails due to a repository creation error'
do
let
(
:content
)
{
'File content'
}
let
(
:file_name
)
{
'test.md'
}
before
do
allow_next_instance_of
(
Snippets
::
UpdateService
)
do
|
instance
|
allow
(
instance
).
to
receive
(
:create_repository_for
).
with
(
snippet
).
and_raise
(
Snippets
::
UpdateService
::
CreateRepositoryError
)
end
update_snippet
(
snippet_id:
snippet
.
id
,
params:
{
content:
content
,
file_name:
file_name
})
end
it
'returns 400'
do
expect
(
response
).
to
have_gitlab_http_status
(
:bad_request
)
end
it
'does not save the changes to the snippet object'
do
expect
(
snippet
.
content
).
not_to
eq
(
content
)
expect
(
snippet
.
file_name
).
not_to
eq
(
file_name
)
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment