From 1594d1c71874f78257f5e933553b33ed24b7dfe7 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 5 Jun 2023 15:25:02 +0200 Subject: [PATCH 1/4] Validate custom tab title --- app/models/custom_tab.rb | 2 ++ config/locales/en.yml | 2 ++ spec/system/admin/enterprises_spec.rb | 10 ++++++++++ 3 files changed, 14 insertions(+) diff --git a/app/models/custom_tab.rb b/app/models/custom_tab.rb index aee8fab087..4f52150940 100644 --- a/app/models/custom_tab.rb +++ b/app/models/custom_tab.rb @@ -2,4 +2,6 @@ class CustomTab < ApplicationRecord belongs_to :enterprise, optional: false + + validates :title, presence: true end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5218012383..476a06da4e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1188,6 +1188,8 @@ en: hide_groups_tab: "Hide groups tab in shopfront" create_custom_tab: "Create custom tab in shopfront" custom_tab_title: "Title for custom tab" + custom_tab_title_error: + blank: "Custom tab title can't be blank" custom_tab_content: "Content for custom tab" actions: edit_profile: Settings diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 2acbccf544..6eee1aab36 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -745,6 +745,16 @@ describe ' expect(distributor1.reload.custom_tab.content).to eq("
Custom tab content
") end + context "managing errors" do + it "can't save custom tab fields if title is blank" do + fill_in "enterprise_custom_tab_attributes_title", with: "" + fill_in_trix_editor "custom_tab_content", with: "Custom tab content" + click_button 'Update' + expect(page).to have_content("Custom tab title can't be blank") + expect(distributor1.reload.custom_tab).to be_nil + end + end + context "when custom tab is already created" do let(:custom_tab) { create(:custom_tab, title: "Custom tab title", From 5938577bd1e4d1b307bfbbb0dd2c718facd45214 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 5 Jun 2023 15:43:11 +0200 Subject: [PATCH 2/4] Update custom_tab_spec.rb --- spec/models/custom_tab_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/models/custom_tab_spec.rb b/spec/models/custom_tab_spec.rb index cad60ef8a2..f710b30461 100644 --- a/spec/models/custom_tab_spec.rb +++ b/spec/models/custom_tab_spec.rb @@ -6,4 +6,8 @@ describe CustomTab do describe 'associations' do it { is_expected.to belong_to(:enterprise).required } end + + describe 'validations' do + it { is_expected.to validate_presence_of(:title) } + end end From 93cf562d596cf47c253e4a476d7329280219f585 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 5 Jun 2023 15:48:02 +0200 Subject: [PATCH 3/4] Update en.yml --- config/locales/en.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 476a06da4e..5218012383 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1188,8 +1188,6 @@ en: hide_groups_tab: "Hide groups tab in shopfront" create_custom_tab: "Create custom tab in shopfront" custom_tab_title: "Title for custom tab" - custom_tab_title_error: - blank: "Custom tab title can't be blank" custom_tab_content: "Content for custom tab" actions: edit_profile: Settings From d1d050e6a41b393603b3fc4204b47be210759426 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 5 Jun 2023 15:59:45 +0200 Subject: [PATCH 4/4] Limit size to 20 char for the custom tab title --- app/models/custom_tab.rb | 2 +- spec/models/custom_tab_spec.rb | 2 ++ spec/system/admin/enterprises_spec.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/custom_tab.rb b/app/models/custom_tab.rb index 4f52150940..22058efcc1 100644 --- a/app/models/custom_tab.rb +++ b/app/models/custom_tab.rb @@ -3,5 +3,5 @@ class CustomTab < ApplicationRecord belongs_to :enterprise, optional: false - validates :title, presence: true + validates :title, presence: true, length: { maximum: 20 } end diff --git a/spec/models/custom_tab_spec.rb b/spec/models/custom_tab_spec.rb index f710b30461..f192068f63 100644 --- a/spec/models/custom_tab_spec.rb +++ b/spec/models/custom_tab_spec.rb @@ -9,5 +9,7 @@ describe CustomTab do describe 'validations' do it { is_expected.to validate_presence_of(:title) } + + it { is_expected.to validate_length_of(:title).is_at_most(20) } end end diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 6eee1aab36..57e674c776 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -753,6 +753,15 @@ describe ' expect(page).to have_content("Custom tab title can't be blank") expect(distributor1.reload.custom_tab).to be_nil end + + it "can't save custom tab fields if title is too long" do + fill_in "enterprise_custom_tab_attributes_title", with: "a" * 21 + fill_in_trix_editor "custom_tab_content", with: "Custom tab content" + click_button 'Update' + expect(page). + to have_content("Custom tab title is too long (maximum is 20 characters)") + expect(distributor1.reload.custom_tab).to be_nil + end end context "when custom tab is already created" do