Simplify URL validation in related content
We were using `Setting["url"]` to verify the content belonged to the application URL, but we can use `root_url` instead. Note that means we need to include the port when filling in forms in the tests, since in tests URL helpers like `polymorphic_url` don't include the port, but a port is automatically added when actually making the request.
This commit is contained in:
@@ -18,7 +18,7 @@ class RelatedContentsController < ApplicationController
|
||||
elsif related_content.duplicate?
|
||||
flash[:error] = t("related_content.error_duplicate")
|
||||
else
|
||||
flash[:error] = t("related_content.error", url: Setting["url"])
|
||||
flash[:error] = t("related_content.error", url: root_url)
|
||||
end
|
||||
redirect_to polymorphic_path(related_content.parent_relationable)
|
||||
end
|
||||
@@ -41,7 +41,7 @@ class RelatedContentsController < ApplicationController
|
||||
end
|
||||
|
||||
def valid_url?
|
||||
params[:url].start_with?(Setting["url"])
|
||||
params[:url].start_with?(root_url)
|
||||
end
|
||||
|
||||
def child_relationable_params
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<div class="input-group-field">
|
||||
<%= text_field_tag :url, "",
|
||||
"aria-describedby": "related_content_help_text",
|
||||
placeholder: t("related_content.placeholder", url: setting["url"]) %>
|
||||
placeholder: t("related_content.placeholder", url: root_url) %>
|
||||
|
||||
<%= hidden_field_tag :relationable_klass, relationable.class.name %>
|
||||
<%= hidden_field_tag :relationable_id, relationable.id %>
|
||||
|
||||
@@ -6,7 +6,6 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
|
||||
before do
|
||||
integration_session.host = Capybara.app_host # TODO: remove after upgrading to Rails 6.1
|
||||
Setting["url"] = Capybara.app_host
|
||||
end
|
||||
|
||||
scenario "related contents are listed" do
|
||||
@@ -40,7 +39,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
expect(page).to have_css ".add-related-content[aria-expanded='true']"
|
||||
|
||||
within("#related_content") do
|
||||
fill_in "Link to related content", with: polymorphic_url(related1)
|
||||
fill_in "Link to related content", with: polymorphic_url(related1, port: app_port)
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
@@ -57,7 +56,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
click_button "Add related content"
|
||||
|
||||
within("#related_content") do
|
||||
fill_in "Link to related content", with: polymorphic_url(related2)
|
||||
fill_in "Link to related content", with: polymorphic_url(related2, port: app_port)
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
@@ -77,7 +76,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
expect(page).to have_content "Link not valid. Remember to start with #{Capybara.app_host}."
|
||||
expect(page).to have_content "Link not valid. Remember to start with #{app_host}/."
|
||||
end
|
||||
|
||||
scenario "returns error when relating content URL to itself" do
|
||||
@@ -87,7 +86,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
click_button "Add related content"
|
||||
|
||||
within("#related_content") do
|
||||
fill_in "Link to related content", with: polymorphic_url(relationable)
|
||||
fill_in "Link to related content", with: polymorphic_url(relationable, port: app_port)
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
@@ -111,7 +110,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
click_button "Add related content"
|
||||
|
||||
within("#related_content") do
|
||||
fill_in "Link to related content", with: "#{Capybara.app_host}/mypath/#{related.id}"
|
||||
fill_in "Link to related content", with: "#{app_host}/mypath/#{related.id}"
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
@@ -129,7 +128,7 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
click_button "Add related content"
|
||||
|
||||
within("#related_content") do
|
||||
fill_in "url", with: polymorphic_url(related1)
|
||||
fill_in "url", with: polymorphic_url(related1, port: app_port)
|
||||
click_button "Add"
|
||||
end
|
||||
|
||||
|
||||
@@ -19,7 +19,11 @@ module CommonActions
|
||||
include Verifications
|
||||
|
||||
def app_host
|
||||
"#{Capybara.app_host}:#{Capybara::Server.ports.values.last}"
|
||||
"#{Capybara.app_host}:#{app_port}"
|
||||
end
|
||||
|
||||
def app_port
|
||||
Capybara::Server.ports.values.last
|
||||
end
|
||||
|
||||
def fill_in_signup_form(email = "manuela@consul.dev", password = "judgementday")
|
||||
|
||||
Reference in New Issue
Block a user