From 635234a889fe498407c7fdf3f8ecb3dea5ec561c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 22 Mar 2024 14:21:14 +1100 Subject: [PATCH] Enable POSTing DFC data --- .../admin/dfc_product_imports_controller.rb | 2 +- engines/dfc_provider/app/services/dfc_request.rb | 16 ++++++++++------ .../spec/services/dfc_request_spec.rb | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index 1b3f8dfb88..8dc580afbd 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -20,7 +20,7 @@ module Admin catalog_url = params.require(:catalog_url) - json_catalog = DfcRequest.new(spree_current_user).get(catalog_url) + json_catalog = DfcRequest.new(spree_current_user).call(catalog_url) graph = DfcIo.import(json_catalog) # * First step: import all products for given enterprise. diff --git a/engines/dfc_provider/app/services/dfc_request.rb b/engines/dfc_provider/app/services/dfc_request.rb index 76ff6e2064..949ec23bad 100644 --- a/engines/dfc_provider/app/services/dfc_request.rb +++ b/engines/dfc_provider/app/services/dfc_request.rb @@ -13,12 +13,12 @@ class DfcRequest @user = user end - def get(url) - response = request(url) + def call(url, data = nil) + response = request(url, data) - if response.status != 200 && token_stale? + if response.status >= 400 && token_stale? refresh_access_token! - response = request(url) + response = request(url, data) end response.body @@ -26,9 +26,13 @@ class DfcRequest private - def request(url) + def request(url, data = nil) only_public_connections do - connection.get(url) + if data + connection.post(url, data) + else + connection.get(url) + end end end diff --git a/engines/dfc_provider/spec/services/dfc_request_spec.rb b/engines/dfc_provider/spec/services/dfc_request_spec.rb index d1e82d17ef..ea17ae02af 100644 --- a/engines/dfc_provider/spec/services/dfc_request_spec.rb +++ b/engines/dfc_provider/spec/services/dfc_request_spec.rb @@ -12,7 +12,16 @@ RSpec.describe DfcRequest do stub_request(:get, "http://example.net/api"). to_return(status: 200, body: '{"@context":"/"}') - expect(api.get("http://example.net/api")).to eq '{"@context":"/"}' + expect(api.call("http://example.net/api")).to eq '{"@context":"/"}' + end + + it "posts a DFC document" do + json = '{"name":"new season apples"}' + stub_request(:post, "http://example.net/api"). + with(body: json). + to_return(status: 201) # Created + + expect(api.call("http://example.net/api", json)).to eq "" end it "refreshes the access token on fail", vcr: true do @@ -30,7 +39,7 @@ RSpec.describe DfcRequest do account.updated_at = 1.day.ago expect { - api.get("http://example.net/api") + api.call("http://example.net/api") }.to change { account.token }.and change { @@ -44,7 +53,7 @@ RSpec.describe DfcRequest do user.oidc_account.updated_at = 1.minute.ago - expect(api.get("http://example.net/api")).to eq "" + expect(api.call("http://example.net/api")).to eq "" # Trying to reach the OIDC server via network request to refresh the token # would raise errors because we didn't setup Webmock or VCR.