From 2c56aca6b9301547568bc0e4177044f1d64cfe02 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 6 Mar 2020 15:09:54 +0000 Subject: [PATCH] Handle old-style /goverment/uploads routes This is currently handled by Whitehall but we're trying to move away from Whitehall being a frontend application. The next best place for this bit of code seems to be Government Frontend. https://github.com/alphagov/whitehall/blob/master/test/functional/asset_manager_redirect_controller_test.rb The code itself is used to handle assets which are still being requested on their own URL before we migrated all assets over to Asset Manager. --- .../asset_manager_redirect_controller.rb | 10 +++++++++ config/routes.rb | 2 ++ .../asset_manager_redirect_controller_test.rb | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 app/controllers/asset_manager_redirect_controller.rb create mode 100644 test/controllers/asset_manager_redirect_controller_test.rb diff --git a/app/controllers/asset_manager_redirect_controller.rb b/app/controllers/asset_manager_redirect_controller.rb new file mode 100644 index 000000000..7bba68370 --- /dev/null +++ b/app/controllers/asset_manager_redirect_controller.rb @@ -0,0 +1,10 @@ +class AssetManagerRedirectController < ApplicationController + def show + asset_url = Plek.new.public_asset_host + if request.host.start_with?("draft-") + asset_url = Plek.new.external_url_for("draft-assets") + end + + redirect_to host: URI.parse(asset_url).host, status: :moved_permanently + end +end diff --git a/config/routes.rb b/config/routes.rb index 0eca6af76..221a1999f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,8 @@ get "healthcheck", to: proc { [200, {}, [""]] } + get "/government/uploads/*path" => "asset_manager_redirect#show", format: true + get "*path/:variant" => "content_items#show", constraints: { variant: /print/, diff --git a/test/controllers/asset_manager_redirect_controller_test.rb b/test/controllers/asset_manager_redirect_controller_test.rb new file mode 100644 index 000000000..61a4c15f3 --- /dev/null +++ b/test/controllers/asset_manager_redirect_controller_test.rb @@ -0,0 +1,22 @@ +require "test_helper" + +class AssetManagerRedirectControllerTest < ActionController::TestCase + setup do + Plek.any_instance.stubs(:public_asset_host).returns("http://asset-host.com") + Plek.any_instance.stubs(:external_url_for).returns("http://draft-asset-host.com") + end + + test "redirects asset requests made via public host to the public asset host" do + request.host = "some-host.com" + get :show, params: { path: "asset", format: "txt" } + + assert_redirected_to "http://asset-host.com/government/uploads/asset.txt" + end + + test "redirects asset requests made via draft host to the draft asset host" do + request.host = "draft-some-host.com" + get :show, params: { path: "asset", format: "txt" } + + assert_redirected_to "http://draft-asset-host.com/government/uploads/asset.txt" + end +end