diff --git a/app/models/host.rb b/app/models/host.rb index 397d132c5..35cd1fcd1 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -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? @@ -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 diff --git a/spec/models/host_spec.rb b/spec/models/host_spec.rb index f9578fea1..9482f9520 100644 --- a/spec/models/host_spec.rb +++ b/spec/models/host_spec.rb @@ -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