diff --git a/lib/gds_api/asset_manager.rb b/lib/gds_api/asset_manager.rb index ef86bdd1..77fb6b4c 100644 --- a/lib/gds_api/asset_manager.rb +++ b/lib/gds_api/asset_manager.rb @@ -48,98 +48,6 @@ def create_asset(asset) post_multipart("#{base_url}/assets", asset: asset) end - # Creates a Whitehall asset given a hash with +file+ & +legacy_url_path+ - # (required) and +legacy_etag+ & +legacy_last_modified+ (optional) attributes - # - # Makes a +POST+ request to the asset manager api to create a Whitehall asset. - # - # The asset must be provided as a +Hash+ with a +file+ attribute that behaves - # like a +File+ object and a +legacy_url_path+ attribute. The +content-type+ - # that the asset manager will subsequently serve will be based *only* on the - # file's extension (derived from +#path+). If you supply a +content-type+ via, - # for example +ActionDispatch::Http::UploadedFile+ or another multipart - # wrapper, it will be ignored. - # - # The +legacy_url_path+ attribute is used to specify the public URL path at - # which the asset should be served by the Asset Manager. This differs from - # `#create_asset` where Asset Manager itself determines the public URL path to - # be used and returns that to the publishing app in the response. This - # endpoint is intended to be an interim measure which will help us migrate - # assets from Whitehall into Asset Manager without needing to change the URLs. - # The end goal is for Asset Manager to determine the public URL path for all - # assets including Whitehall assets. At that point this endpoint will become - # redundant and should be removed. - # - # There may be restrictions on the format of the `legacy_url_path`. If the - # supplied path is not valid, a `GdsApi::HTTPUnprocessableEntity` exception - # will be raised. - # - # The optional +legacy_etag+ & +legacy_last_modified+ attributes allow the - # client to specify the values that should be used in the `ETag` & - # `Last-Modified` response headers when the asset is requested via its public - # URL. They are only intended to be used for migrating existing Whitehall - # assets to Asset Manager so that we can avoid wholesale cache invalidation. - # New Whitehall assets should not specify values for these attributes; Asset - # Manager will generate suitable values. - # - # Note: this endpoint should only be used by the Whitehall Admin app and not - # by any other publishing apps. - # - # @param asset [Hash] The attributes for the asset to send to the api. Must - # contain +file+, which behaves like a +File+, and +legacy_url_path+, a - # +String+. May contain +legacy_etag+, a +String+, and - # +legacy_last_modified+, a +Time+ object. All other attributes will be - # ignored. - # - # @return [GdsApi::Response] The wrapped http response from the api. Behaves - # both as a +Hash+ and an +OpenStruct+, and responds to the following: - # :id the URL of the asset - # :name the filename of the asset that will be served - # :content_type the content_type of the asset - # :file_url the URL from which the asset will be served when it has - # passed a virus scan - # :state One of 'unscanned', 'clean', or 'infected'. Unless the state is - # 'clean' the asset at the :file_url will redirect to a - # placeholder - # - # @raise [HTTPErrorResponse] if the request returns an error - # - # @example Upload a file from disk - # response = asset_manager.create_asset( - # file: File.new('image.jpg', 'r'), - # legacy_url_path: '/government/uploads/path/to/image.jpg' - # ) - # response['id'] #=> "http://asset-manager.dev.gov.uk/assets/576bbc52759b74196b000012" - # response['content_type'] #=> "image/jpeg" - # @example Upload a file from a Rails param, (typically a multipart wrapper) - # params[:file] #=> # - # - # # Though we sent a file with a +content_type+ of 'application/foofle', - # # this was ignored - # response = asset_manager.create_asset( - # file: params[:file] - # legacy_url_path: '/government/uploads/path/to/cma_case_image.jpg' - # ) - # response['content_type'] #=> "image/jpeg" - def create_whitehall_asset(asset) - post_multipart("#{base_url}/whitehall_assets", asset: asset) - end - - # Fetches a Whitehall asset's metadata given the legacy URL path - # - # @param legacy_url_path [String] The Whitehall asset identifier. - # @return [GdsApi::Response] A response object containing the parsed JSON - # response. If the asset cannot be found, +GdsApi::HTTPNotFound+ will be - # raised. - # - # @raise [HTTPErrorResponse] if the request returns an error - def whitehall_asset(legacy_url_path) - get_json("#{base_url}/whitehall_assets/#{uri_encode(legacy_url_path)}") - end - # Updates an asset given a hash with one +file+ attribute # # Makes a +PUT+ request to the asset manager api to update an asset. diff --git a/lib/gds_api/test_helpers/asset_manager.rb b/lib/gds_api/test_helpers/asset_manager.rb index 0e250cf9..3afba053 100644 --- a/lib/gds_api/test_helpers/asset_manager.rb +++ b/lib/gds_api/test_helpers/asset_manager.rb @@ -31,14 +31,9 @@ def stub_asset_manager_has_an_asset(id, atts, filename = "") .to_return(body: "Some file content", status: 200) end - def stub_asset_manager_has_a_whitehall_asset(legacy_url_path, atts) - response = atts.merge("_response_info" => { "status" => "ok" }) - - stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/whitehall_assets/#{legacy_url_path}") - .to_return(body: response.to_json, status: 200) - + def stub_asset_manager_has_a_whitehall_media(legacy_url_path, content) stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/#{legacy_url_path}") - .to_return(body: "Some file content", status: 200) + .to_return(body: content, status: 200) end def stub_asset_manager_does_not_have_an_asset(id) @@ -50,15 +45,6 @@ def stub_asset_manager_does_not_have_an_asset(id) .to_return(body: response.to_json, status: 404) end - def stub_asset_manager_does_not_have_a_whitehall_asset(legacy_url_path) - response = { - "_response_info" => { "status" => "not found" }, - } - - stub_request(:get, "#{ASSET_MANAGER_ENDPOINT}/whitehall_assets/#{legacy_url_path}") - .to_return(body: response.to_json, status: 404) - end - # This can take a string of an exact url or a hash of options # # with a string: diff --git a/test/asset_manager_test.rb b/test/asset_manager_test.rb index 67483834..9678b5ad 100644 --- a/test/asset_manager_test.rb +++ b/test/asset_manager_test.rb @@ -34,18 +34,6 @@ assert_requested(req) end - it "creates a Whitehall asset with a file" do - req = stub_request(:post, "#{base_api_url}/whitehall_assets") - .with { |request| - request.body =~ %r{Content-Disposition: form-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent-Type: text/plain} - }.to_return(body: JSON.dump(stub_asset_manager_response), status: 201) - - response = api.create_whitehall_asset(file: file_fixture, legacy_url_path: "/government/uploads/path/to/hello.txt") - - assert_equal asset_url, response["asset"]["id"] - assert_requested(req) - end - it "returns not found when the asset does not exist" do stub_asset_manager_does_not_have_an_asset("not-really-here") @@ -58,14 +46,6 @@ end end - it "raises not found when a Whitehall asset does not exist" do - stub_asset_manager_does_not_have_a_whitehall_asset("/path/to/non-existent-asset.png") - - assert_raises GdsApi::HTTPNotFound do - api.whitehall_asset("/path/to/non-existent-asset.png") - end - end - describe "the asset exists" do before do stub_asset_manager_has_an_asset( @@ -108,18 +88,12 @@ describe "a Whitehall asset exists" do before do - stub_asset_manager_has_a_whitehall_asset( + stub_asset_manager_has_a_whitehall_media( "/government/uploads/photo.jpg", - "id" => "asset-id", + "Some file content", ) end - it "retrieves the asset's metadata" do - asset = api.whitehall_asset("/government/uploads/photo.jpg") - - assert_equal "asset-id", asset["id"] - end - it "retrieves the asset" do asset = api.whitehall_media("/government/uploads/photo.jpg") @@ -129,16 +103,16 @@ describe "a Whitehall asset with a legacy_url_path containing non-ascii characters exists" do before do - stub_asset_manager_has_a_whitehall_asset( + stub_asset_manager_has_a_whitehall_media( "/government/uploads/phot%C3%B8.jpg", - "id" => "asset-id", + "Some file content", ) end it "retrieves the asset's metadata" do - asset = api.whitehall_asset("/government/uploads/photø.jpg") + asset = api.whitehall_media("/government/uploads/photø.jpg") - assert_equal "asset-id", asset["id"] + assert_equal "Some file content", asset.body end end diff --git a/test/pacts/asset_manager_pact_test.rb b/test/pacts/asset_manager_pact_test.rb index c5824cc6..f55a08a1 100644 --- a/test/pacts/asset_manager_pact_test.rb +++ b/test/pacts/asset_manager_pact_test.rb @@ -184,46 +184,6 @@ let(:url_for_asset) { "http://static.dev.gov.uk#{legacy_url_path}" } let(:existing_asset_response_body) { existing_asset_body(file_url: url_for_asset) } - describe "#create whitehall asset" do - it "creates a whitehall asset" do - asset_manager - .upon_receiving("a create whitehall asset request") - .with( - method: :post, - path: "/whitehall_assets", - body: a_whitehall_multipart_request_body, - headers: multipart_headers, - ).will_respond_with( - status: 201, - body: created_asset_body( - id: an_asset_id_string, - file_url: url_for_asset, - ), - headers: json_content_type, - ) - - api_client.create_whitehall_asset(file: file_fixture, legacy_url_path: legacy_url_path) - end - end - - describe "#get whitehall asset metadata" do - it "gets a whitehall asset's metadata" do - asset_manager - .given("a whitehall asset exists with legacy url path #{legacy_url_path} and id #{content_id}") - .upon_receiving("a get whitehall asset metadata request") - .with( - method: :get, - path: "/whitehall_assets/#{legacy_url_path}", - ).will_respond_with( - status: 200, - body: existing_asset_response_body, - headers: json_content_type, - ) - - api_client.whitehall_asset(legacy_url_path) - end - end - describe "#get whitehall asset" do it "gets a whitehall asset" do asset_manager @@ -240,30 +200,6 @@ end end - describe "#update_asset" do - it "updates a whitehall asset" do - asset_manager - .given("a whitehall asset exists with legacy url path #{legacy_url_path} and id #{content_id}") - .upon_receiving("an update asset request") - .with( - method: :put, - path: "/assets/#{content_id}", - body: a_multipart_request_body, - headers: multipart_headers, - ) - .will_respond_with( - status: 200, - body: created_asset_body( - id: "http://example.org/assets/#{content_id}", - file_url: "http://static.dev.gov.uk#{legacy_url_path}", - ), - headers: json_content_type, - ) - - api_client.update_asset(content_id, file: file_fixture) - end - end - describe "#delete_asset" do it "deletes a whitehall asset" do asset_manager @@ -312,15 +248,6 @@ def a_multipart_request_body ) end - def a_whitehall_multipart_request_body - legacy_url_details = "\r\nContent-Disposition: form-data; name=\"asset[legacy_url_path]\"\r\n\r\n/government/uploads/some-edition/hello.txt\r\n" - legacy_url_details_regex = /\s+Content-Disposition: form-data; name="asset\[legacy_url_path\]"\s+\/government\/uploads\/some-edition\/hello.txt\s+/ - Pact.term( - generate: construct_multipart_string([asset_details, legacy_url_details]), - matcher: construct_multipart_regex([asset_details_regex, legacy_url_details_regex]), - ) - end - def a_file_url_string Pact.term( generate: "http://static.dev.gov.uk/media/62b418d7c7d6b700ce9fa93d/hello.txt",