FIX: prevent minimum_required_tags on category being set to null (#6703)

* FIX: prevent minimum_required_tags on category being set to null

* add migration for NOT_NULL constraint for minimum_required_tags

* add specs
This commit is contained in:
Maja Komel 2018-11-29 18:10:14 +01:00 committed by Régis Hanol
parent 5b6c1b1670
commit 4a8f21d387
4 changed files with 11 additions and 4 deletions

View File

@ -169,7 +169,7 @@
<label for="category-minimum-tags"> <label for="category-minimum-tags">
{{i18n 'category.minimum_required_tags'}} {{i18n 'category.minimum_required_tags'}}
</label> </label>
{{text-field value=category.minimum_required_tags id="category-minimum-tags" type="number"}} {{text-field value=category.minimum_required_tags id="category-minimum-tags" type="number" min="0"}}
</section> </section>
{{/if}} {{/if}}

View File

@ -149,9 +149,8 @@ class CategoriesController < ApplicationController
category_params.delete(:position) category_params.delete(:position)
# properly null the value so the database constraint doesn't catch us # properly null the value so the database constraint doesn't catch us
if category_params.has_key?(:email_in) && category_params[:email_in].blank? category_params[:email_in] = nil if category_params[:email_in]&.blank?
category_params[:email_in] = nil category_params[:minimum_required_tags] = 0 if category_params[:minimum_required_tags]&.blank?
end
old_permissions = cat.permissions_params old_permissions = cat.permissions_params

View File

@ -0,0 +1,5 @@
class AddNotNullMinimumRequiredTagsOnCategories < ActiveRecord::Migration[5.2]
def change
change_column_null :categories, :minimum_required_tags, false, 0
end
end

View File

@ -91,6 +91,7 @@ describe CategoriesController do
it "raises an exception when the text color is missing" do it "raises an exception when the text color is missing" do
post "/categories.json", params: { name: "hello", color: "ff0" } post "/categories.json", params: { name: "hello", color: "ff0" }
expect(response.status).to eq(400)
end end
describe "failure" do describe "failure" do
@ -299,6 +300,7 @@ describe CategoriesController do
custom_fields: { custom_fields: {
"dancing" => "frogs" "dancing" => "frogs"
}, },
minimum_required_tags: ""
} }
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -311,6 +313,7 @@ describe CategoriesController do
expect(category.color).to eq("ff0") expect(category.color).to eq("ff0")
expect(category.auto_close_hours).to eq(72) expect(category.auto_close_hours).to eq(72)
expect(category.custom_fields).to eq("dancing" => "frogs") expect(category.custom_fields).to eq("dancing" => "frogs")
expect(category.minimum_required_tags).to eq(0)
end end
it 'logs the changes correctly' do it 'logs the changes correctly' do