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

Validate landing page base paths must start with / #9629

Merged
merged 1 commit into from
Nov 19, 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
2 changes: 1 addition & 1 deletion app/models/landing_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class LandingPage < Edition
include Edition::Images

skip_callback :validation, :before, :update_document_slug
validates :base_path, presence: true
validates :base_path, presence: true, format: { with: /\A\/.*\z/, message: "must start with a slash (/)" }
validate :base_path_must_not_be_taken
validate :body_must_be_valid_yaml

Expand Down
9 changes: 6 additions & 3 deletions test/functional/admin/landing_pages_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Admin::LandingPagesControllerTest < ActionController::TestCase
end

test "GET :edit fetches the supplied instance" do
page = create(:landing_page, organisations: [@organisation])
document = create(:document, slug: "/some-slug-starting-with-slash")
page = create(:landing_page, organisations: [@organisation], document:)

get :edit, params: { id: page }

Expand All @@ -58,7 +59,8 @@ class Admin::LandingPagesControllerTest < ActionController::TestCase

test "PUT :update changes the supplied instance with the supplied params" do
attrs = attributes_for(:landing_page, title: "Hello there")
page = create(:landing_page, organisations: [@organisation], title: "Goodbye")
document = create(:document, slug: "/some-slug-starting-with-slash")
page = create(:landing_page, organisations: [@organisation], title: "Goodbye", document:)

post :update, params: {
id: page,
Expand All @@ -72,7 +74,8 @@ class Admin::LandingPagesControllerTest < ActionController::TestCase

test "PUT :update doesn't save the new instance when the supplied params are invalid" do
attrs = attributes_for(:landing_page, title: "")
page = create(:landing_page, organisations: [@organisation], title: "Goodbye")
document = create(:document, slug: "/some-slug-starting-with-slash")
page = create(:landing_page, organisations: [@organisation], title: "Goodbye", document:)

post :update, params: { id: page, edition: attrs }

Expand Down
6 changes: 6 additions & 0 deletions test/unit/app/models/landing_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class LandingPageTest < ActiveSupport::TestCase
assert_equal :base_path, landing_page.errors.first.attribute
end

test "landing-page is not valid if base_path does not start with a slash" do
document = build(:document, slug: "landing-page/test")
landing_page = build(:landing_page, document:, body: "blocks: []")
assert_not landing_page.valid?
end

test "landing-page is valid if body is YAML with at least the blocks: element" do
document = build(:document, slug: "/landing-page/test")
landing_page = build(:landing_page, document:, body: "blocks:\nother:\n")
Expand Down
Loading