Skip to content

Commit

Permalink
Validate that global new URL is absent if the global type is "archive"
Browse files Browse the repository at this point in the history
Currently, there is validation in place to prevent the creation of sites which
have a global type of "redirect" without a global new URL, but the opposite
validation is missing.
  • Loading branch information
jkempster34 committed Sep 8, 2023
1 parent cb051c7 commit 7e1e35e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Site < ApplicationRecord
validates :abbr, uniqueness: true, presence: true, format: { with: /\A[a-zA-Z0-9_-]+\z/, message: "can only contain alphanumeric characters, underscores and dashes" }
validates :special_redirect_strategy, inclusion: { in: SPECIAL_REDIRECT_STRATEGY_TYPES, allow_nil: true }
validates :global_new_url, presence: { if: :global_redirect? }
validates :global_new_url, absence: { if: :global_archive? }
validates :global_new_url,
format: { without: /\?/,
message: "cannot contain a query when the path is appended",
Expand Down
9 changes: 9 additions & 0 deletions spec/models/site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
end
end

context "global archive" do
subject(:site) { build(:site, global_type: "archive", global_new_url: "http://a.com/") }

before { expect(site).not_to be_valid }
it "should validate absence of global_new_url" do
expect(site.errors[:global_new_url]).to eq(["must be blank"])
end
end

context "global redirect with path appended" do
subject(:site) { build(:site, global_type: "redirect", global_redirect_append_path: true, global_new_url: "http://a.com/?") }

Expand Down

0 comments on commit 7e1e35e

Please sign in to comment.