Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUBY-3220] Add project name validation logic to submit proposal #916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 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,18 @@ 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

other_projects_with_same_name = PafsCore::Project.where.not(id: project.id).where(name: name)

if other_projects_with_same_name.present?
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
Loading