Skip to content

Commit

Permalink
[RUBY-3220] Add validation for project name format (#902)
Browse files Browse the repository at this point in the history
* [RUBY-3220] Add format validation for project name and update tests

- Add regex format validation to `name` attribute in `ProjectNameStep` to ensure it only contains letters, underscores, hyphens, and numbers.
- Update project factory to remove periods from generated project names.
- Enhance `ProjectNameStep` spec to test valid and invalid name formats, ensuring proper validation messages are displayed.

* [RUBY-3220] Add conditional validation for project name and refactor tests

- Updated `project_name_step.rb` to include a conditional validation for `name` presence.
- Refactored `project_name_step_spec.rb` to improve readability and organization.
- Grouped test cases logically and used more descriptive variable names.

* [RUBY-3220] Add back shared example to `project_name_step_spec.rb`
  • Loading branch information
jjromeo authored Sep 4, 2024
1 parent e6ecc1f commit 6e93e31
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
5 changes: 5 additions & 0 deletions app/steps/pafs_core/project_name_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class ProjectNameStep < BasicStep

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

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

private

def step_params(params)
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FactoryBot.define do
factory :project, class: "PafsCore::Project" do
reference_number { PafsCore::ProjectService.generate_reference_number("TH") }
name { Faker::Lorem.sentence }
name { Faker::Lorem.sentence.remove(".") }
version { 0 }
created_at { 1.day.ago }
updated_at { 1.day.ago }
Expand Down
50 changes: 34 additions & 16 deletions spec/steps/pafs_core/project_name_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,46 @@
require "rails_helper"

RSpec.describe PafsCore::ProjectNameStep, type: :model do
describe "attributes" do
subject { build(:project_name_step) }

it_behaves_like "a project step"
describe "#update" do
subject(:project_name_step) { create(:project_name_step) }

it { is_expected.to validate_presence_of(:name).with_message("Tell us the project name") }
end
let(:valid_params) { ActionController::Parameters.new({ project_name_step: { name: "Wigwam waste water" } }) }
let(:blank_name_params) { ActionController::Parameters.new({ project_name_step: { name: nil } }) }
let(:error_message) { "The project name must only contain letters, underscores, hyphens and numbers" }

describe "#update" do
subject { create(:project_name_step) }
it_behaves_like "a project step"

let(:params) { ActionController::Parameters.new({ project_name_step: { name: "Wigwam waste water" } }) }
let(:error_params) { ActionController::Parameters.new({ project_name_step: { name: nil } }) }
context "when name is valid" do
it "returns true and saves the name" do
expect(project_name_step.name).not_to eq "Wigwam waste water"
expect(project_name_step.update(valid_params)).to be true
expect(project_name_step.name).to eq "Wigwam waste water"
end
end

it "saves the :name when valid" do
expect(subject.name).not_to eq "Wigwam waste water"
expect(subject.update(params)).to be true
expect(subject.name).to eq "Wigwam waste water"
context "when name is blank" do
it "returns false and sets the correct error message" do
expect(project_name_step.update(blank_name_params)).to be false
expect(project_name_step.errors[:name].first).to eq("Tell us the project name")
end
end

it "returns false when validation fails" do
expect(subject.update(error_params)).to be false
context "when name has an invalid format" do
invalid_names = {
"slash" => "Invalid/Name",
"backslash" => "Invalid\\Name",
"ampersand" => "Invalid&Name",
"at symbol" => "Invalid@Name",
"hash symbol" => "Invalid#Name"
}

invalid_names.each do |description, name|
it "returns false and sets the correct error message for #{description} character" do
project_name_step.name = name
expect(project_name_step).not_to be_valid
expect(project_name_step.errors[:name].first).to eq(error_message)
end
end
end
end
end

0 comments on commit 6e93e31

Please sign in to comment.