Skip to content

Commit

Permalink
Validate that host hostname is lowercase
Browse files Browse the repository at this point in the history
Currently the site importer (`lib/transition/import/site_yaml_file.rb`)
downcases any hostnames.

This adds validation to the host model which will be surfaced on the new site
form.
  • Loading branch information
jkempster34 committed Sep 5, 2023
1 parent b19808a commit fdafcd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Host < ApplicationRecord
validates :hostname, hostname: true
validates :site, presence: true
validate :canonical_host_id_xor_aka_present, if: -> { hostname.present? }
validate :hostname_is_downcased, if: -> { hostname.present? }

after_update :update_hits_relations, if: :saved_change_to_site_id?

Expand Down Expand Up @@ -71,4 +72,10 @@ def update_hits_relations
hits.update_all(mapping_id: nil)
Transition::Import::HitsMappingsRelations.refresh!(site)
end

def hostname_is_downcased
if hostname != hostname.downcase
errors.add(:hostname, "must be lowercase")
end
end
end
12 changes: 12 additions & 0 deletions spec/models/host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@
expect(host.errors_on(:hostname)).to include("is an invalid hostname")
end
end

context "is downcased" do
subject(:host) { build :host, hostname: "AB.com" }

describe "#valid?" do
subject { super().valid? }
it { is_expected.to be_falsey }
end
it "should have an error for invalid hostname" do
expect(host.errors_on(:hostname)).to include("must be lowercase")
end
end
end
end

Expand Down

0 comments on commit fdafcd0

Please sign in to comment.