Skip to content

Commit

Permalink
Update ErrorsController to support ContentBlockManager
Browse files Browse the repository at this point in the history
This adds override templates to ContentBlockManager for the error pages,
if the request path starts with the ContentBlockManager router prefix,
we prepend the view path, so the overridden views are picked up.

Additionally, we also need to do this for the `forbidden!` helper, as
there isn’t a specific error we can raise that will result in a
`forbidden` error
  • Loading branch information
pezholio committed Feb 25, 2025
1 parent b559506 commit a8ba10b
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def product_name
private

def forbidden!
prepend_view_path Rails.root.join("lib/engines/content_block_manager/app/views") if request.path.start_with?(ContentBlockManager.router_prefix)
render "admin/errors/forbidden", status: :forbidden
end

Expand Down
8 changes: 8 additions & 0 deletions app/controllers/admin/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Admin::ErrorsController < Admin::BaseController
before_action :prepend_content_block_manager_view_path, if: -> { request.path.start_with?(ContentBlockManager.router_prefix) }

def bad_request
render(status: :bad_request)
end
Expand All @@ -18,4 +20,10 @@ def unprocessable_entity
def internal_server_error
render(status: :internal_server_error)
end

private

def prepend_content_block_manager_view_path
prepend_view_path Rails.root.join("lib/engines/content_block_manager/app/views")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "Something went wrong" %>
<% content_for :title, "Something went wrong" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">Please try again by selecting the browser&rsquo;s back button or <%= link_to "raise a support request", ContentBlockManager.support_url, class: "govuk-link govuk-link--no-visited-state" %></p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "Down for maintenance" %>
<% content_for :title, "Down for maintenance" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body"><%= ContentBlockManager.product_name %> is currently down for maintenance. We'll be back soon.</p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "Permissions error" %>
<% content_for :title, "Permissions error" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">You do not have permission to access this page.</p>
<p class="govuk-body">If you need to:</p>

<%= render "govuk_publishing_components/components/list", {
visible_counters: true,
items: [
"view the page, request the preview link",
sanitize("edit the page, raise a #{link_to "support ticket", ContentBlockManager.support_url, class: "govuk-link govuk-link--no-visited-state"}"),
],
} %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "Server error" %>
<% content_for :title, "Server error" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">The error has been logged in our system.</p>
<p class="govuk-body">You can also raise a <a href="<%= ContentBlockManager.support_url %>" class="govuk-link">support ticket</a> with the following information:</p>

<%= render "govuk_publishing_components/components/list", {
visible_counters: true,
items: [
"the url of this page",
"the time of the error",
],
} %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "There is a mistake in the URL" %>
<% content_for :title, "There is a mistake in the URL" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">If you typed the URL, check it is correct.</p>
<p class="govuk-body">If you pasted the URL, check you copied the entire address.</p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :product_name, ContentBlockManager.product_name %>
<% content_for :page_title, "Something went wrong" %>
<% content_for :title, "Something went wrong" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row govuk-!-margin-bottom-0">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">Please try again by selecting the browser&rsquo;s back button or <%= link_to "raise a support request", ContentBlockManager.support_url, class: "govuk-link govuk-link--no-visited-state" %>.</p>
</div>
</div>
73 changes: 55 additions & 18 deletions test/functional/admin/errors_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,69 @@
require "test_helper"
require "capybara/rails"

class Admin::ErrorsControllerTest < ActionDispatch::IntegrationTest
include Capybara::DSL
extend Minitest::Spec::DSL

setup do
login_as_admin
end

test "should show the bad request page" do
get "/400"
assert_template :bad_request
end
ERROR_LOOKUPS = {
"400": :bad_request,
"403": :forbidden,
"404": :not_found,
"422": :unprocessable_entity,
"500": :internal_server_error,
}.freeze

test "should show the forbidden page" do
get "/403"
assert_template :forbidden
end
ERROR_LOOKUPS.each do |error_code, error|
it "should show the #{error} page" do
get "/#{error_code}"

test "should show the not found page" do
get "/404"
assert_template :not_found
end
assert_template error
end

it "should render the correct headers" do
get "/#{error_code}"

assert_select ".govuk-header__product-name", text: Whitehall.product_name
refute_select ".govuk-phase-banner__content__tag"
end

it "should render the product name in the title" do
get "/#{error_code}"

test "should show the unprocessable entity page" do
get "/422"
assert_template :unprocessable_entity
assert_select "title", text: /#{Whitehall.product_name}/
end
end

test "should show the internal server error page" do
get "/500"
assert_template :internal_server_error
describe "when request comes from ContentBlockManager" do
before do
path = "some-path"
ActionDispatch::Request.any_instance.stubs(:path).returns(path)
path.stubs(:start_with?).with(ContentBlockManager.router_prefix).returns(true)
end

ERROR_LOOKUPS.each do |error_code, error|
it "should show the #{error} page" do
get "/#{error_code}"

assert_template error
end

it "should render the correct headers" do
get "/#{error_code}"

assert_select ".govuk-header__product-name", text: ContentBlockManager.product_name
assert_select ".govuk-phase-banner__content__tag", text: "Alpha"
end

it "should render the product name in the title" do
get "/#{error_code}"

assert_select "title", text: /#{ContentBlockManager.product_name}/
end
end
end
end

0 comments on commit a8ba10b

Please sign in to comment.