Skip to content

Commit

Permalink
[RUBY-3220] Add project name validation logic to submit proposal
Browse files Browse the repository at this point in the history
- Updated `project_name_complete?` method to check for blank names, valid characters, and uniqueness.
- Introduced `PROJECT_NAME_REGEX` constant in `ProjectNameStep` for name format validation.
- Added comprehensive RSpec tests for various project name validation scenarios.
  • Loading branch information
jjromeo committed Sep 17, 2024
1 parent 673169d commit 70771ed
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/presenters/pafs_core/validation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ def complete?
end

def project_name_complete?
# NOTE: currently has to be present before creation
return add_error(:project_name, "Tell us the project name") if name.blank?

unless name =~ PafsCore::ProjectNameStep::PROJECT_NAME_REGEX
return add_error(:project_name, "The project name must only contain letters, underscores, hyphens and numbers")
end

if PafsCore::Project.where.not(id: project.id).exists?(name: name)
return add_error(:project_name, "The project name already exists. Your project must have a unique name.")
end

true
end

Expand Down
3 changes: 2 additions & 1 deletion app/steps/pafs_core/project_name_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

module PafsCore
class ProjectNameStep < BasicStep
PROJECT_NAME_REGEX = /\A[A-Za-z0-9 _-]+\z/
delegate :name, :name=, to: :project

validates :name, presence: { message: "Tell us the project name" }

validates :name, format: {
with: /\A[A-Za-z0-9 _-]+\z/,
with: PROJECT_NAME_REGEX,
message: "The project name must only contain letters, underscores, hyphens and numbers"
}, if: -> { name.present? }

Expand Down
54 changes: 52 additions & 2 deletions spec/presenters/pafs_core/validation_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,58 @@
end

describe "#project_name_complete?" do
it "always returns true" do
expect(subject.project_name_complete?).to be true
context "when the project name is blank" do
before { subject.name = "" }

it_behaves_like "failed validation example", :project_name_complete?, :project_name, "Tell us the project name"
end

context "when the project name contains invalid characters" do
invalid_names = ["Project/123", "Project@Name", "Project#ABC"]

invalid_names.each do |invalid_name|
context "with name '#{invalid_name}'" do
before { subject.name = invalid_name }

it_behaves_like "failed validation example", :project_name_complete?, :project_name, "The project name must only contain letters, underscores, hyphens and numbers"
end
end
end

context "when the project name is not unique" do
before do
existing_project = create(:project, name: "Existing Project")
subject.name = existing_project.name
end

it_behaves_like "failed validation example", :project_name_complete?, :project_name, "The project name already exists. Your project must have a unique name."
end

context "when the project name is valid and unique" do
valid_names = ["Project 123", "PROJECT_ABC", "project-xyz", "Simple Project Name"]

valid_names.each do |valid_name|
context "with name '#{valid_name}'" do
before { subject.name = valid_name }

it_behaves_like "successful validation example", :project_name_complete?
end
end
end

context "when updating an existing project" do
subject { described_class.new(existing_project) }

let(:existing_project) { create(:project, name: "Existing Project") }

it "allows the project to keep its current name" do
expect(subject.project_name_complete?).to be true
end

it "allows the project to change its name" do
subject.name = "Updated Project Name"
expect(subject.project_name_complete?).to be true
end
end
end

Expand Down

0 comments on commit 70771ed

Please sign in to comment.