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

NACS Site naming validation #710

Merged
merged 2 commits into from
Jun 18, 2024
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
9 changes: 9 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class Site < ApplicationRecord
paginates_per 50

# Removing blank spaces from Site name entered
before_validation :strip_whitespace, if: -> { name.present? && name.start_with?("FITS") }

validates :name, presence: true
validates :name, uniqueness: { case_sensitive: false }, unless: :skip_uniqueness_validation?
validates :name, format: { with: /\AFITS-\d{4}-\w+-\w+\z/, message: "Site Name not in expected format : 'FITS-XXXX-TYPE-LOCATION'" }, if: -> { name.present? && name.start_with?("FITS") }

has_many :clients, dependent: :destroy
has_many :mac_authentication_bypasses, dependent: :destroy
Expand Down Expand Up @@ -67,4 +71,9 @@ def self.ransackable_attributes(_auth_object = nil)
end

# rubocop:enable Lint/IneffectiveAccessModifier

# Removes blank spaces from site name
def strip_whitespace
self.name = name.strip.gsub(/\s+/, "")
end
end
4 changes: 2 additions & 2 deletions app/views/sites/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<div class="govuk-form-group <%= field_error(f.object, :name) %>">
<%= f.label :name, class: "govuk-label" %>
<div id="name-hint" class="govuk-hint">
Must contain the FITS ID, type and location in the following format: "FITS-XXXX - TYPE - LOCATION"<br />
For example: FITS-1234 - Probation - Maidstone
Must contain the FITS ID, type and location in the following format: "FITS-XXXX-TYPE-LOCATION"<br />
For example: FITS-1234-Probation-Maidstone
</div>
<%= f.text_field :name, maxlength: 230, class: "govuk-input" %>
</div>
Expand Down
40 changes: 36 additions & 4 deletions spec/acceptance/sites/create_sites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,59 @@
login_as editor
end

# Test to validate the Site Name format 'FITS-XXXX - TYPE - LOCATION'
it "displays error if site name not in expected format : FITS-XXXX-TYPE-LOCATION" do
visit "/sites"

click_on "Create a new site"

expect(current_path).to eql("/sites/new")

fill_in "Name", with: "FITS-Probation-Maidstone"

click_on "Create"

# Site name has to be in the format 'FITS-XXXX-TYPE-LOCATION'
expect(page).to have_content("Site Name not in expected format : 'FITS-XXXX-TYPE-LOCATION'")
end

it "can create a new site with site name in valid format" do
visit "/sites"

click_on "Create a new site"

expect(current_path).to eql("/sites/new")

fill_in "Name", with: "FITS-9999-Probation-Maidstone"

click_on "Create"

expect(page).to have_content("Successfully created site.")
expect(page).to have_content("This could take up to 10 minutes to apply.")
expect(page).to have_content("FITS-9999-Probation-Maidstone")
end

it "creates a new site with a fallback policy" do
visit "/sites"

click_on "Create a new site"

expect(current_path).to eql("/sites/new")

fill_in "Name", with: "My London Site"
fill_in "Name", with: "FITS-9999-Probation-Maidstone"

click_on "Create"

expect(page).to have_content("Successfully created site.")
expect(page).to have_content("This could take up to 10 minutes to apply.")
expect(page).to have_content("My London Site")
expect(page).to have_content("FITS-9999-Probation-Maidstone")
expect(page).not_to have_content("There are no fallback policies attached to this site.")

click_on "My London Site"
click_on "FITS-9999-Probation-Maidstone"

expect(current_path).to eql("/policies/#{Policy.last.id}")
expect(page).to have_content("Fallback")
expect(page).to have_content("My London Site")
expect(page).to have_content("FITS-9999-Probation-Maidstone")

expect_audit_log_entry_for(editor.email, "create", "Site")
end
Expand Down