Commit 1d7e543c authored by Jonas Wälter's avatar Jonas Wälter Committed by Marcin Sedlak-Jakubowski

Topic API: allow to remove avatar by update

parent a4cb0770
......@@ -188,3 +188,18 @@ curl --request PUT \
"https://gitlab.example.com/api/v4/topics/1" \
--form "avatar=@/tmp/example.png"
```
### Remove a topic avatar
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/348148) in GitLab 14.6.
To remove a topic avatar, use a blank value for the `avatar` attribute.
Example request:
```shell
curl --request PUT \
--data "avatar=" \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/topics/1"
```
......@@ -69,6 +69,8 @@ module API
topic = ::Projects::Topic.find(params[:id])
topic.remove_avatar! if params.key?(:avatar) && params[:avatar].nil?
if topic.update(declared_params(include_missing: false))
present topic, with: Entities::Projects::Topic
else
......
......@@ -5,6 +5,7 @@ module API
module Types
class WorkhorseFile
def self.parse(value)
return if value.blank?
raise "#{value.class} is not an UploadedFile type" unless parsed?(value)
value
......
......@@ -5,15 +5,15 @@ require 'spec_helper'
RSpec.describe API::Topics do
include WorkhorseHelpers
let_it_be(:topic_1) { create(:topic, name: 'Git', total_projects_count: 1) }
let_it_be(:file) { fixture_file_upload('spec/fixtures/dk.png') }
let_it_be(:topic_1) { create(:topic, name: 'Git', total_projects_count: 1, avatar: file) }
let_it_be(:topic_2) { create(:topic, name: 'GitLab', total_projects_count: 2) }
let_it_be(:topic_3) { create(:topic, name: 'other-topic', total_projects_count: 3) }
let_it_be(:admin) { create(:user, :admin) }
let_it_be(:user) { create(:user) }
let(:file) { fixture_file_upload('spec/fixtures/dk.png') }
describe 'GET /topics', :aggregate_failures do
it 'returns topics ordered by total_projects_count' do
get api('/topics')
......@@ -184,6 +184,14 @@ RSpec.describe API::Topics do
expect(json_response['avatar_url']).to end_with('dk.png')
end
it 'keeps avatar when updating other fields' do
put api("/topics/#{topic_1.id}", admin), params: { name: 'my-topic' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq('my-topic')
expect(topic_1.reload.avatar_url).not_to be_nil
end
it 'returns 404 for non existing id' do
put api("/topics/#{non_existing_record_id}", admin), params: { name: 'my-topic' }
......@@ -196,6 +204,32 @@ RSpec.describe API::Topics do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['error']).to eql('id is invalid')
end
context 'with blank avatar' do
it 'removes avatar' do
put api("/topics/#{topic_1.id}", admin), params: { avatar: '' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['avatar_url']).to be_nil
expect(topic_3.reload.avatar_url).to be_nil
end
it 'removes avatar besides other changes' do
put api("/topics/#{topic_1.id}", admin), params: { name: 'new-topic-name', avatar: '' }
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq('new-topic-name')
expect(json_response['avatar_url']).to be_nil
expect(topic_1.reload.avatar_url).to be_nil
end
it 'does not remove avatar in case of other errors' do
put api("/topics/#{topic_1.id}", admin), params: { name: topic_2.name, avatar: '' }
expect(response).to have_gitlab_http_status(:bad_request)
expect(topic_1.reload.avatar_url).not_to be_nil
end
end
end
context 'as normal user' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment