2023-02-08 13:21:39 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe FormTemplate, type: :model do
|
|
|
|
it "can't have duplicate names" do
|
2023-08-29 16:41:33 -05:00
|
|
|
Fabricate(:form_template, name: "Bug Report", template: "- type: input\n id: name")
|
|
|
|
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
|
2023-02-08 13:21:39 -06:00
|
|
|
expect(t.save).to eq(false)
|
2023-08-29 16:41:33 -05:00
|
|
|
t = Fabricate.build(:form_template, name: "Bug Report", template: "- type: input\n id: name")
|
2023-02-08 13:21:39 -06:00
|
|
|
expect(t.save).to eq(false)
|
2023-08-29 16:41:33 -05:00
|
|
|
expect(t.errors.full_messages.first).to include(I18n.t("errors.messages.taken"))
|
2023-02-08 13:21:39 -06:00
|
|
|
expect(described_class.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2024-09-10 10:11:44 -05:00
|
|
|
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
|
|
|
|
|
2023-02-08 13:21:39 -06:00
|
|
|
it "can't have an invalid yaml template" do
|
2023-03-01 13:07:13 -06:00
|
|
|
template = "- type: checkbox\nattributes; bad"
|
2023-02-08 13:21:39 -06:00
|
|
|
t = Fabricate.build(:form_template, name: "Feature Request", template: template)
|
|
|
|
expect(t.save).to eq(false)
|
2023-08-29 16:41:33 -05:00
|
|
|
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.invalid_yaml"))
|
2023-02-08 13:21:39 -06:00
|
|
|
end
|
2023-03-01 13:07:13 -06:00
|
|
|
|
|
|
|
it "must have a supported type" do
|
2023-08-29 16:41:33 -05:00
|
|
|
template = "- type: fancy\n id: something"
|
2023-03-01 13:07:13 -06:00
|
|
|
t = Fabricate.build(:form_template, name: "Fancy Template", template: template)
|
|
|
|
expect(t.save).to eq(false)
|
2023-08-29 16:41:33 -05:00
|
|
|
expect(t.errors.full_messages.first).to include(
|
|
|
|
I18n.t(
|
|
|
|
"form_templates.errors.invalid_type",
|
|
|
|
type: "fancy",
|
|
|
|
valid_types: FormTemplateYamlValidator::ALLOWED_TYPES.join(", "),
|
|
|
|
),
|
|
|
|
)
|
2023-03-01 13:07:13 -06:00
|
|
|
end
|
|
|
|
|
2023-06-13 15:02:21 -05:00
|
|
|
it "must have a type property" do
|
2023-08-29 16:41:33 -05:00
|
|
|
template = "- hello: world\n id: something"
|
2023-03-01 13:07:13 -06:00
|
|
|
t = Fabricate.build(:form_template, name: "Basic Template", template: template)
|
|
|
|
expect(t.save).to eq(false)
|
2023-08-29 16:41:33 -05:00
|
|
|
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.missing_type"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "must have a id property" do
|
|
|
|
template = "- type: checkbox"
|
|
|
|
t = Fabricate.build(:form_template, name: "Basic Template", template: template)
|
|
|
|
expect(t.save).to eq(false)
|
|
|
|
expect(t.errors.full_messages.first).to include(I18n.t("form_templates.errors.missing_id"))
|
2023-03-01 13:07:13 -06:00
|
|
|
end
|
2023-02-08 13:21:39 -06:00
|
|
|
end
|