FIX: Form template limit validation (#28791)

This commit is contained in:
Keegan George 2024-09-10 08:11:44 -07:00 committed by GitHub
parent 0a994a9221
commit f2059bf15f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 14 deletions

View File

@ -5,14 +5,14 @@ class FormTemplate < ActiveRecord::Base
presence: true,
uniqueness: true,
length: {
maximum: SiteSetting.max_form_template_title_length,
maximum: -> { SiteSetting.max_form_template_title_length },
}
validates :template,
presence: true,
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 :categories, through: :category_form_templates
@ -26,7 +26,7 @@ end
# Table name: form_templates
#
# id :bigint not null, primary key
# name :string(100) not null
# name :string not null
# template :text not null
# created_at :datetime not null
# updated_at :datetime not null

View File

@ -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

View File

@ -11,6 +11,48 @@ RSpec.describe FormTemplate, type: :model do
expect(described_class.count).to eq(1)
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
template = "- type: checkbox\nattributes; bad"
t = Fabricate.build(:form_template, name: "Feature Request", template: template)

View File

@ -3,12 +3,11 @@
RSpec.describe Admin::FormTemplatesController do
fab!(:admin)
fab!(:user)
fab!(:form_template)
before { SiteSetting.experimental_form_templates = true }
describe "#index" do
fab!(:form_template)
context "when logged in as an admin" do
before { sign_in(admin) }
@ -46,8 +45,6 @@ RSpec.describe Admin::FormTemplatesController do
end
describe "#show" do
fab!(:form_template)
context "when logged in as an admin" do
before { sign_in(admin) }
@ -102,8 +99,6 @@ RSpec.describe Admin::FormTemplatesController do
end
describe "#update" do
fab!(:form_template)
context "when logged in as an admin" do
before { sign_in(admin) }
@ -111,13 +106,13 @@ RSpec.describe Admin::FormTemplatesController do
put "/admin/customize/form-templates/#{form_template.id}.json",
params: {
id: form_template.id,
name: "Updated Template",
name: "Bugs",
template: "- type: checkbox\n id: checkbox",
}
expect(response.status).to eq(200)
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")
end
end
@ -145,8 +140,6 @@ RSpec.describe Admin::FormTemplatesController do
end
describe "#destroy" do
fab!(:form_template)
context "when logged in as an admin" do
before { sign_in(admin) }