Skip to content

Commit

Permalink
Merge pull request #2386 from alphagov/browser-extension
Browse files Browse the repository at this point in the history
Add route for browser extension to get routed to publisher
  • Loading branch information
syed-ali-tw authored Oct 22, 2024
2 parents 25fcb87 + b9233cb commit 87db0ba
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/controllers/content_item_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ContentItemController < ApplicationController
def by_content_id
artefact =
Artefact.find_by(content_id: params[:content_id])

if artefact
redirect_to edition_path(artefact.latest_edition)
else
redirect_to_root_path_with_error
end
rescue StandardError
redirect_to_root_path_with_error
end

private

def redirect_to_root_path_with_error
flash[:danger] = "The requested content was not found"
redirect_to root_path
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
delete "/homepage/popular-links/:id" => "homepage#destroy", as: "delete_popular_links"
get "homepage/popular-links/:id/confirm-destroy" => "homepage#confirm_destroy", as: "confirm_destroy_popular_links"

get "by-content-id/:content_id" => "content_item#by_content_id"

mount GovukAdminTemplate::Engine, at: "/style-guide"
mount Flipflop::Engine => "/flipflop"
mount GovukPublishingComponents::Engine, at: "/component-guide"
Expand Down
30 changes: 30 additions & 0 deletions test/functional/content_item_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "test_helper"

class ContentItemControllerTest < ActionController::TestCase
def setup
login_as_stub_user
@edition = FactoryBot.create(:edition)
end

should "redirect to content by content_id" do
get :by_content_id, params: { content_id: @edition.content_id }

assert_redirected_to edition_path(@edition.artefact.latest_edition)
end

should "redirect to root with error message if content_id is not found" do
get :by_content_id, params: { content_id: "#{@edition.artefact.content_id}wrong-id" }

assert_redirected_to root_path
assert_equal "The requested content was not found", flash[:danger]
end

should "redirect to root with error message if any error" do
Artefact.any_instance.stubs(:find_by).raises(StandardError)

get :by_content_id, params: { content_id: "#{@edition.artefact.content_id}wrong-id" }

assert_equal "The requested content was not found", flash[:danger]
assert_redirected_to root_path
end
end

0 comments on commit 87db0ba

Please sign in to comment.