FIX: Update only present fields in request (#14310)

Some category fields were always updated, even if they were not present
in the request. When this happened, these field were erased.
This commit is contained in:
Bianca Nenciu
2021-09-14 15:04:54 +03:00
committed by GitHub
parent 581482003a
commit dde66b9e16
3 changed files with 52 additions and 10 deletions

View File

@ -437,13 +437,47 @@ describe CategoriesController do
color: category.color,
text_color: category.text_color,
allow_global_tags: 'false',
min_tags_from_required_group: 1
min_tags_from_required_group: 1,
required_tag_group_name: ''
}
expect(response.status).to eq(200)
category.reload
expect(category.required_tag_group).to be_nil
end
it "does not update other fields" do
SiteSetting.tagging_enabled = true
tag_group_1 = Fabricate(:tag_group)
tag_group_2 = Fabricate(:tag_group)
category.update!(
allowed_tags: ["hello", "world"],
allowed_tag_groups: [tag_group_1.name],
required_tag_group_name: tag_group_2.name
)
put "/categories/#{category.id}.json"
expect(response.status).to eq(200)
category.reload
expect(category.tags.pluck(:name)).to contain_exactly("hello", "world")
expect(category.tag_groups.pluck(:name)).to contain_exactly(tag_group_1.name)
expect(category.required_tag_group).to eq(tag_group_2)
put "/categories/#{category.id}.json", params: { allowed_tags: [] }
expect(response.status).to eq(200)
category.reload
expect(category.tags).to be_blank
expect(category.tag_groups.pluck(:name)).to contain_exactly(tag_group_1.name)
expect(category.required_tag_group).to eq(tag_group_2)
put "/categories/#{category.id}.json", params: { allowed_tags: [], allowed_tag_groups: [], required_tag_group_name: nil }
expect(response.status).to eq(200)
category.reload
expect(category.tags).to be_blank
expect(category.tag_groups).to be_blank
expect(category.required_tag_group).to eq(nil)
end
end
end
end