Skip to content

Commit

Permalink
Redirect to uploads url
Browse files Browse the repository at this point in the history
This is the first step in adding signed cookies.
  • Loading branch information
pixeltrix committed Dec 20, 2024
1 parent 17e9c91 commit 7711eea
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DOMAIN=bops.localhost:3000
PAAPI_HOST=paapi.services
OS_VECTOR_TILES_API_KEY=xxxxx
OTP_SECRET_ENCRYPTION_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DOMAIN="example.com"
STAGING_API_URL="bops-staging.services"
STAGING_API_BEARER="fjisdfjsdiofjdsoi"
OS_VECTOR_TILES_API_KEY="testtest"
Expand Down
1 change: 0 additions & 1 deletion app/helpers/document_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def link_to_document(link_text, document, **args)
link_text,
url_for_document(document),
new_tab:,
download: reference_or_file_name(document),
**args
)
end
Expand Down
19 changes: 18 additions & 1 deletion app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ class Document < ApplicationRecord
class Routing
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers

def initialize(subdomain)
@subdomain = subdomain
end

def default_url_options
{host: "#{subdomain}.#{domain}"}
end

private

attr_reader :subdomain

def domain
Rails.configuration.domain
end
end

class NotArchiveableError < StandardError; end
Expand All @@ -29,6 +45,7 @@ class NotArchiveableError < StandardError; end
inverse_of: false

delegate :audits, to: :planning_application
delegate :local_authority, to: :planning_application
delegate :blob, :representable?, to: :file

include Auditable
Expand Down Expand Up @@ -359,7 +376,7 @@ def representation_url(transformations = {resize_to_limit: [1000, 1000]})
private

def routes
@_routes ||= Routing.new
@_routes ||= Routing.new(local_authority.subdomain)
end

def no_open_replacement_request
Expand Down
1 change: 1 addition & 0 deletions config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ shared:
staging_api_url: <%= ENV["STAGING_API_URL"] %>
uploads_hostname: <%= ENV.fetch("UPLOADS_HOSTNAME", "uploads.bops.localhost:3000") %>
uploads_base_url: <%= ENV.fetch("UPLOADS_BASE_URL", "http://uploads.bops.localhost:3000") %>
domain: <%= ENV.fetch("DOMAIN", "bops.localhost:3000") %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module BopsUploads
class RedirectsController < ApplicationController
def show
redirect_to redirect_url, allow_other_host: true
end

private

def redirect_url
file_url(params[:key], host: uploads_base_url)
end

def uploads_base_url
Rails.configuration.uploads_base_url
end
end
end
6 changes: 5 additions & 1 deletion engines/bops_uploads/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
BopsUploads::Engine.routes.draw do
extend BopsCore::Routing

local_authority_subdomain do
get "/files/:key", to: "redirects#show", as: "redirect"
end

uploads_subdomain do
get "/:key", to: "files#show", as: "file"
end
Expand All @@ -12,7 +16,7 @@
direct :uploaded_file do |blob, options|
next "" if blob.blank?

bops_uploads.file_url(blob.key, host: Rails.configuration.uploads_base_url)
bops_uploads.redirect_url(blob.key)
end

resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:uploaded_file, attachment.blob, options) }
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/document_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

it "redirects to blob url" do
get "/api/v1/planning_applications/#{planning_application.reference}/documents/#{document.id}"
expect(response).to redirect_to("http://uploads.example.com/#{document.blob.key}")
expect(response).to redirect_to("http://planx.example.com/files/#{document.blob.key}")
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/api/oas3_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def example_response_hash_for(*)
expected_response = example_response_hash_for("/api/v1/planning_applications/{id}", "get", 200, "ldc_proposed")
expected_response["status"] = "in_assessment"
expected_response["documents"].first["url"] = "http://planx.example.com/api/v1/planning_applications/#{planning_application.reference}/documents/#{planning_application_document.id}"
expected_response["documents"].first["blob_url"] = "http://uploads.example.com/#{planning_application_document.representation.key}"
expected_response["documents"].first["blob_url"] = "http://planx.example.com/files/#{planning_application_document.representation.key}"

get("/api/v1/planning_applications/#{planning_application_hash["id"]}", headers: {"CONTENT-TYPE": "application/json", Authorization: "Bearer #{api_user.token}"})
expect(JSON.parse(response.body)).to eq(expected_response)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/planning_application_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

planning_application_json["documents"].first.tap do |document_json|
expect(document_json["url"]).to eq("http://planx.example.com/api/v1/planning_applications/#{planning_application.reference}/documents/#{document_with_number.id}")
expect(document_json["blob_url"]).to eq("http://uploads.example.com/#{document_with_number.representation.key}")
expect(document_json["blob_url"]).to eq("http://planx.example.com/files/#{document_with_number.representation.key}")
expect(document_json["created_at"]).to eq(json_time_format(document_with_number.created_at))
expect(document_json["archived_at"]).to eq(json_time_format(document_with_number.archived_at))
expect(document_json["archive_reason"]).to eq(document_with_number.archive_reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let!(:default_local_authority) { create(:local_authority, :default) }
let!(:api_user) { create(:api_user, local_authority: default_local_authority) }
let!(:planning_application) { create(:planning_application, :invalidated, local_authority: default_local_authority) }
let(:old_document) { create(:document) }
let(:old_document) { create(:document, planning_application:) }

let!(:replacement_document_validation_request) do
create(
Expand All @@ -20,8 +20,8 @@
let(:token) { "Bearer #{api_user.token}" }

describe "#index" do
let(:old_document2) { create(:document) }
let(:old_document3) { create(:document) }
let(:old_document2) { create(:document, planning_application:) }
let(:old_document3) { create(:document, planning_application:) }

let(:path) do
api_v1_planning_application_replacement_document_validation_requests_path(
Expand Down Expand Up @@ -50,7 +50,7 @@
)
end

let!(:new_document) { create(:document, owner: replacement_document_validation_request2) }
let!(:new_document) { create(:document, planning_application:, owner: replacement_document_validation_request2) }

context "when the request is valid" do
it "is successful" do
Expand Down Expand Up @@ -83,7 +83,7 @@
old_document: {
name: "proposed-floorplan.png",
invalid_document_reason: "Document is invalid",
url: "http://uploads.example.com/#{old_document.representation.key}"
url: "http://planx.example.com/files/#{old_document.representation.key}"
}
}.deep_stringify_keys,
{
Expand All @@ -96,11 +96,11 @@
old_document: {
name: "proposed-floorplan.png",
invalid_document_reason: "Document is invalid",
url: "http://uploads.example.com/#{old_document2.representation.key}"
url: "http://planx.example.com/files/#{old_document2.representation.key}"
},
new_document: {
name: "proposed-floorplan.png",
url: "http://uploads.example.com/#{new_document.representation.key}"
url: "http://planx.example.com/files/#{new_document.representation.key}"
}
}.deep_stringify_keys,
{
Expand All @@ -113,7 +113,7 @@
old_document: {
name: "proposed-floorplan.png",
invalid_document_reason: nil,
url: "http://uploads.example.com/#{old_document3.representation.key}"
url: "http://planx.example.com/files/#{old_document3.representation.key}"
}
}.deep_stringify_keys
)
Expand Down Expand Up @@ -172,7 +172,7 @@
old_document: {
name: "proposed-floorplan.png",
invalid_document_reason: "Document is invalid",
url: "http://uploads.example.com/#{old_document.representation.key}"
url: "http://planx.example.com/files/#{old_document.representation.key}"
}
}.deep_stringify_keys
)
Expand Down

0 comments on commit 7711eea

Please sign in to comment.