Skip to content

Commit

Permalink
Merge pull request #8966 from alphagov/pre-1117-tidy-up
Browse files Browse the repository at this point in the history
Build on republishing work
  • Loading branch information
jkempster34 authored Apr 18, 2024
2 parents b6dd9f3 + 7c06a0c commit f06013c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 41 deletions.
30 changes: 17 additions & 13 deletions app/controllers/admin/republishing_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@ class Admin::RepublishingController < Admin::BaseController
before_action :enforce_permissions!

def index
@republishable_documents = republishable_documents
@republishable_pages = republishable_pages
end

def republish_past_prime_ministers_index
PresentPageToPublishingApiWorker.perform_async("PublishingApi::HistoricalAccountsIndexPresenter")
flash[:notice] = "'Past Prime Ministers' page has been scheduled for republishing"
redirect_to(admin_republishing_index_path)
def confirm_page
page_to_republish = republishable_pages.find { |page| page[:slug] == params[:page_slug] }

return render "admin/errors/not_found", status: :not_found unless page_to_republish

@title = page_to_republish[:title]
@republishing_path = admin_republishing_page_republish_path(page_to_republish[:slug])
end

def confirm
republishable_document = republishable_documents.find { |document| document[:slug] == params[:document_slug] }
def republish_page
page_to_republish = republishable_pages.find { |page| page[:slug] == params[:page_slug] }

return render "admin/errors/not_found", status: :not_found unless republishable_document
return render "admin/errors/not_found", status: :not_found unless page_to_republish

@document_title = republishable_document[:title]
@republishing_path = republishable_document[:republishing_path]
PresentPageToPublishingApiWorker.perform_async(page_to_republish[:presenter])
flash[:notice] = "'#{page_to_republish[:title]}' page has been scheduled for republishing"
redirect_to(admin_republishing_index_path)
end

private

def enforce_permissions!
enforce_permission!(:administer, :republish_documents)
enforce_permission!(:administer, :republish_content)
end

def republishable_documents
def republishable_pages
historical_accounts_index_presenter = PublishingApi::HistoricalAccountsIndexPresenter.new

[{
title: historical_accounts_index_presenter.content[:title],
public_path: historical_accounts_index_presenter.base_path,
republishing_path: admin_republishing_republish_past_prime_ministers_path,
slug: historical_accounts_index_presenter.base_path.split("/").last,
presenter: "PublishingApi::HistoricalAccountsIndexPresenter",
}]
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% content_for :page_title, "Republish '#{@document_title}'" %>
<% content_for :title, "Are you sure you want to republish '#{@document_title}'?" %>
<% content_for :page_title, "Republish '#{@title}'" %>
<% content_for :title, "Are you sure you want to republish '#{@title}'?" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
Expand Down
18 changes: 9 additions & 9 deletions app/views/admin/republishing/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% content_for :page_title, "Republish documents" %>
<% content_for :title, "Republish documents" %>
<% content_for :page_title, "Republish content" %>
<% content_for :title, "Republish content" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
Expand All @@ -15,27 +15,27 @@
<p class="govuk-body">
The following actions will allow you to schedule the republishing of content that was originally published in this application.
Any linked editions will also be republished through dependency resolution.
Try to pick the document type most focused to the scope of what you need to republish to avoid unnecessary server load.
Try to pick the republishing task most focused to the scope of what you need to republish to avoid unnecessary server load.
</p>

<%= render "govuk_publishing_components/components/table", {
head: [
{
text: "Document",
text: "Page",
},
{
text: "Action",
},
],
rows: @republishable_documents.map do |document|
rows: @republishable_pages.map do |page|
[
{
text: link_to(document[:title], Plek.website_root + document[:public_path], class:"govuk-link"),
text: link_to(page[:title], Plek.website_root + page[:public_path], class:"govuk-link"),
},
{
text: link_to(sanitize("Republish #{tag.span('\'' + document[:title] + '\' page', class: 'govuk-visually-hidden')}"),
admin_confirm_republishing_path(document[:slug]),
id: "republish-past-prime-ministers-index",
text: link_to(sanitize("Republish #{tag.span('\'' + page[:title] + '\' page', class: 'govuk-visually-hidden')}"),
admin_republishing_page_confirm_path(page[:slug]),
id: "republish-" + page[:slug],
class: "govuk-link",
),
},
Expand Down
10 changes: 7 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })

resources :users, only: %i[index show edit update]

get "republishing" => "republishing#index", as: :republishing_index
get "republishing/:document_slug/confirm" => "republishing#confirm", as: :confirm_republishing
post "republishing/republish-past-prime-ministers" => "republishing#republish_past_prime_ministers_index"
scope :republishing do
root to: "republishing#index", as: :republishing_index, via: :get
scope :page do
get "/:page_slug/confirm" => "republishing#confirm_page", as: :republishing_page_confirm
post "/:page_slug/republish" => "republishing#republish_page", as: :republishing_page_republish
end
end

resources :documents, only: [] do
resources :review_reminders, only: %i[new create edit update]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

When(/^I request a republish of the "Past prime ministers" page$/) do
visit admin_republishing_index_path
find("#republish-past-prime-ministers-index").click
find("#republish-past-prime-ministers").click
click_button("Confirm republishing")
end

Expand Down
2 changes: 1 addition & 1 deletion lib/whitehall/authority/rules/miscellaneous_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def can?(action)
end
end

def can_for_republish_documents?(_action)
def can_for_republish_content?(_action)
actor.gds_admin?
end

Expand Down
31 changes: 19 additions & 12 deletions test/functional/admin/republishing_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class Admin::RepublishingControllerTest < ActionController::TestCase

should_be_an_admin_controller

view_test "GDS Admin users should be able to acess the GET :index and see links to republishable documents" do
view_test "GDS Admin users should be able to acess the GET :index and see links to republishable content" do
get :index

assert_select ".govuk-table__cell:nth-child(1) a[href='https://www.test.gov.uk/government/history/past-prime-ministers']", text: "Past Prime Ministers"
assert_select ".govuk-table__cell:nth-child(2) a[href='/government/admin/republishing/past-prime-ministers/confirm']", text: "Republish 'Past Prime Ministers' page"
assert_select ".govuk-table__cell:nth-child(2) a[href='/government/admin/republishing/page/past-prime-ministers/confirm']", text: "Republish 'Past Prime Ministers' page"
assert_response :ok
end

Expand All @@ -23,38 +23,45 @@ class Admin::RepublishingControllerTest < ActionController::TestCase
assert_response :forbidden
end

test "GDS Admin users should be able to access GET :confirm with a republishable document slug" do
get :confirm, params: { document_slug: "past-prime-ministers" }
test "GDS Admin users should be able to access GET :confirm_page with a republishable page slug" do
get :confirm_page, params: { page_slug: "past-prime-ministers" }
assert_response :ok
end

test "GDS Admin users should see a 404 page when trying to republish a document with an unregistered document slug" do
get :confirm, params: { document_slug: "not-republishable" }
test "GDS Admin users should see a 404 page when trying to GET :confirm_page with an unregistered page slug" do
get :confirm_page, params: { page_slug: "not-republishable" }
assert_response :not_found
end

test "Non-GDS Admin users should not be able to access GET :confirm" do
test "Non-GDS Admin users should not be able to access GET :confirm_page" do
login_as :writer

get :confirm, params: { document_slug: "past-prime-ministers" }
get :confirm_page, params: { page_slug: "past-prime-ministers" }
assert_response :forbidden
end

test "GDS Admin users should be able to trigger the PresentPageToPublishingWorker job with the HistoricalAccountsIndexPresenter" do
test "GDS Admin users should be able to access POST :republish_page with a republishable page slug" do
PresentPageToPublishingApiWorker.expects(:perform_async).with("PublishingApi::HistoricalAccountsIndexPresenter").once

post :republish_past_prime_ministers_index
post :republish_page, params: { page_slug: "past-prime-ministers" }

assert_redirected_to admin_republishing_index_path
assert_equal "'Past Prime Ministers' page has been scheduled for republishing", flash[:notice]
end

test "Non-GDS Admin users should not be able to republish the page" do
test "GDS Admin users should see a 404 page when trying to POST :republish_page with an unregistered page slug" do
PresentPageToPublishingApiWorker.expects(:perform_async).with("PublishingApi::HistoricalAccountsIndexPresenter").never

get :republish_page, params: { page_slug: "not-republishable" }
assert_response :not_found
end

test "Non-GDS Admin users should not be able to access POST :republish_page" do
PresentPageToPublishingApiWorker.expects(:perform_async).with("PublishingApi::HistoricalAccountsIndexPresenter").never

login_as :writer

post :republish_past_prime_ministers_index
post :republish_page, params: { page_slug: "past-prime-ministers" }
assert_response :forbidden
end
end

0 comments on commit f06013c

Please sign in to comment.