From a7b674813e236ab5fe6cccaeeb7af59861d18764 Mon Sep 17 00:00:00 2001 From: Jerome Pratt Date: Mon, 2 Sep 2024 16:24:57 +0100 Subject: [PATCH] [RUBY-3212] Add uniqueness validation for project name - Updated `PafsCore::ProjectNameStep` to validate uniqueness of the project name with a custom error message. - Added corresponding tests in `project_name_step_spec.rb` to ensure validation fails for non-unique project names. --- app/steps/pafs_core/project_name_step.rb | 2 ++ spec/steps/pafs_core/project_name_step_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/app/steps/pafs_core/project_name_step.rb b/app/steps/pafs_core/project_name_step.rb index 2bee9702..e48b3972 100644 --- a/app/steps/pafs_core/project_name_step.rb +++ b/app/steps/pafs_core/project_name_step.rb @@ -11,6 +11,8 @@ class ProjectNameStep < BasicStep message: "The project name must only contain letters, underscores, hyphens and numbers" } + validates :name, uniqueness: { message: "The project name already exists. Your project must have a unique name." } + private def step_params(params) diff --git a/spec/steps/pafs_core/project_name_step_spec.rb b/spec/steps/pafs_core/project_name_step_spec.rb index 4c0d2e37..cda379ec 100644 --- a/spec/steps/pafs_core/project_name_step_spec.rb +++ b/spec/steps/pafs_core/project_name_step_spec.rb @@ -41,6 +41,18 @@ end end end + + context "when name is not unique" do + before do + create(:project_name_step, name: "Existing Project") + end + + it "returns false when validation fails due to non-unique name" do + duplicate_params = ActionController::Parameters.new({ project_name_step: { name: "Existing Project" } }) + expect(subject.update(duplicate_params)).to be false + expect(subject.errors[:name].join).to eq("The project name already exists. Your project must have a unique name.") + end + end end describe "#update" do