mirror of
https://github.com/discourse/discourse.git
synced 2025-06-13 01:46:29 +08:00
FIX: Form template limit validation (#28791)
This commit is contained in:
@ -5,14 +5,14 @@ class FormTemplate < ActiveRecord::Base
|
|||||||
presence: true,
|
presence: true,
|
||||||
uniqueness: true,
|
uniqueness: true,
|
||||||
length: {
|
length: {
|
||||||
maximum: SiteSetting.max_form_template_title_length,
|
maximum: -> { SiteSetting.max_form_template_title_length },
|
||||||
}
|
}
|
||||||
validates :template,
|
validates :template,
|
||||||
presence: true,
|
presence: true,
|
||||||
length: {
|
length: {
|
||||||
maximum: SiteSetting.max_form_template_content_length,
|
maximum: -> { SiteSetting.max_form_template_content_length },
|
||||||
}
|
}
|
||||||
validates_with FormTemplateYamlValidator
|
validates_with FormTemplateYamlValidator, if: ->(ft) { ft.template }
|
||||||
|
|
||||||
has_many :category_form_templates, dependent: :destroy
|
has_many :category_form_templates, dependent: :destroy
|
||||||
has_many :categories, through: :category_form_templates
|
has_many :categories, through: :category_form_templates
|
||||||
@ -26,7 +26,7 @@ end
|
|||||||
# Table name: form_templates
|
# Table name: form_templates
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# name :string(100) not null
|
# name :string not null
|
||||||
# template :text not null
|
# template :text not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class RemoveLimitsFromFormTemplates < ActiveRecord::Migration[7.1]
|
||||||
|
def change
|
||||||
|
change_column :form_templates, :name, :string, limit: nil
|
||||||
|
change_column :form_templates, :template, :text, limit: nil
|
||||||
|
end
|
||||||
|
end
|
@ -11,6 +11,48 @@ RSpec.describe FormTemplate, type: :model do
|
|||||||
expect(described_class.count).to eq(1)
|
expect(described_class.count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when length validations set in SiteSetting" do
|
||||||
|
before do
|
||||||
|
SiteSetting.max_form_template_content_length = 50
|
||||||
|
SiteSetting.max_form_template_title_length = 5
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can't exceed max title length" do
|
||||||
|
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
|
||||||
|
expect(t.save).to eq(false)
|
||||||
|
expect(t.errors.full_messages.first).to include(
|
||||||
|
"Name #{I18n.t("errors.messages.too_long", count: SiteSetting.max_form_template_title_length)}",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can't exceed max content length" do
|
||||||
|
t =
|
||||||
|
Fabricate.build(
|
||||||
|
:form_template,
|
||||||
|
name: "Bug",
|
||||||
|
template: "- type: input\n id: name-that-is-really-long-to-make-the-template-longer",
|
||||||
|
)
|
||||||
|
expect(t.save).to eq(false)
|
||||||
|
expect(t.errors.full_messages.first).to include(
|
||||||
|
"Template #{I18n.t("errors.messages.too_long", count: SiteSetting.max_form_template_content_length)}",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should update validation limits when the site setting has been changed" do
|
||||||
|
SiteSetting.max_form_template_content_length = 100
|
||||||
|
SiteSetting.max_form_template_title_length = 100
|
||||||
|
|
||||||
|
t =
|
||||||
|
Fabricate.build(
|
||||||
|
:form_template,
|
||||||
|
name: "Bug Report",
|
||||||
|
template: "- type: input\n id: name-that-is-really-long-to-make-the-template-longer",
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(t.save).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "can't have an invalid yaml template" do
|
it "can't have an invalid yaml template" do
|
||||||
template = "- type: checkbox\nattributes; bad"
|
template = "- type: checkbox\nattributes; bad"
|
||||||
t = Fabricate.build(:form_template, name: "Feature Request", template: template)
|
t = Fabricate.build(:form_template, name: "Feature Request", template: template)
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
RSpec.describe Admin::FormTemplatesController do
|
RSpec.describe Admin::FormTemplatesController do
|
||||||
fab!(:admin)
|
fab!(:admin)
|
||||||
fab!(:user)
|
fab!(:user)
|
||||||
|
fab!(:form_template)
|
||||||
|
|
||||||
before { SiteSetting.experimental_form_templates = true }
|
before { SiteSetting.experimental_form_templates = true }
|
||||||
|
|
||||||
describe "#index" do
|
describe "#index" do
|
||||||
fab!(:form_template)
|
|
||||||
|
|
||||||
context "when logged in as an admin" do
|
context "when logged in as an admin" do
|
||||||
before { sign_in(admin) }
|
before { sign_in(admin) }
|
||||||
|
|
||||||
@ -46,8 +45,6 @@ RSpec.describe Admin::FormTemplatesController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "#show" do
|
describe "#show" do
|
||||||
fab!(:form_template)
|
|
||||||
|
|
||||||
context "when logged in as an admin" do
|
context "when logged in as an admin" do
|
||||||
before { sign_in(admin) }
|
before { sign_in(admin) }
|
||||||
|
|
||||||
@ -102,8 +99,6 @@ RSpec.describe Admin::FormTemplatesController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "#update" do
|
describe "#update" do
|
||||||
fab!(:form_template)
|
|
||||||
|
|
||||||
context "when logged in as an admin" do
|
context "when logged in as an admin" do
|
||||||
before { sign_in(admin) }
|
before { sign_in(admin) }
|
||||||
|
|
||||||
@ -111,13 +106,13 @@ RSpec.describe Admin::FormTemplatesController do
|
|||||||
put "/admin/customize/form-templates/#{form_template.id}.json",
|
put "/admin/customize/form-templates/#{form_template.id}.json",
|
||||||
params: {
|
params: {
|
||||||
id: form_template.id,
|
id: form_template.id,
|
||||||
name: "Updated Template",
|
name: "Bugs",
|
||||||
template: "- type: checkbox\n id: checkbox",
|
template: "- type: checkbox\n id: checkbox",
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
expect(response.status).to eq(200)
|
||||||
form_template.reload
|
form_template.reload
|
||||||
expect(form_template.name).to eq("Updated Template")
|
expect(form_template.name).to eq("Bugs")
|
||||||
expect(form_template.template).to eq("- type: checkbox\n id: checkbox")
|
expect(form_template.template).to eq("- type: checkbox\n id: checkbox")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -145,8 +140,6 @@ RSpec.describe Admin::FormTemplatesController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "#destroy" do
|
describe "#destroy" do
|
||||||
fab!(:form_template)
|
|
||||||
|
|
||||||
context "when logged in as an admin" do
|
context "when logged in as an admin" do
|
||||||
before { sign_in(admin) }
|
before { sign_in(admin) }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user