Merge pull request #12729 from mkllnk/fdc-update

Remove now unneeded FDC compatibility code from product import
This commit is contained in:
Gaetan Craig-Riou
2024-08-12 11:14:44 +10:00
committed by GitHub
8 changed files with 269 additions and 336 deletions

View File

@@ -35,13 +35,7 @@ module Admin
private
def fetch_catalog(url)
if url =~ /food-data-collaboration/
fdc_json = FdcRequest.new(spree_current_user).call(url)
fdc_message = JSON.parse(fdc_json)
fdc_message["products"]
else
DfcRequest.new(spree_current_user).call(url)
end
DfcRequest.new(spree_current_user).call(url)
end
# Most of this code is the same as in the DfcProvider::SuppliedProductsController.

View File

@@ -1,33 +0,0 @@
# frozen_string_literal: true
require "private_address_check"
require "private_address_check/tcpsocket_ext"
# Request a JSON document from the FDC API with authentication.
#
# Currently, the API doesn't quite comply with the DFC standard and we need
# to authenticate a little bit differently.
#
# And then we get slightly different data as well.
class FdcRequest < DfcRequest
# Override main method to POST authorization data.
def call(url, data = {})
response = request(url, auth_data.merge(data).to_json)
if response.status >= 400 && token_stale?
refresh_access_token!
response = request(url, auth_data.merge(data).to_json)
end
response.body
end
private
def auth_data
{
userId: @user.oidc_account.uid,
accessToken: @user.oidc_account.token,
}
end
end

View File

@@ -59,4 +59,28 @@ RSpec.describe DfcRequest do
# would raise errors because we didn't setup Webmock or VCR.
# The absence of errors makes this test pass.
end
it "refreshes the access token and retrieves the FDC catalog", vcr: true do
# A refresh is only attempted if the token is stale.
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago
response = nil
expect {
response = api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to change {
account.token
}.and change {
account.refresh_token
}
json = JSON.parse(response)
graph = DfcIo.import(json)
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
end
end

View File

@@ -1,36 +0,0 @@
# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe FdcRequest do
subject(:api) { FdcRequest.new(user) }
let(:user) { build(:oidc_user) }
let(:account) { user.oidc_account }
let(:url) {
"https://food-data-collaboration-produc-fe870152f634.herokuapp.com/fdc/products?shop=test-hodmedod.myshopify.com"
}
it "refreshes the access token and retrieves a catalog", vcr: true do
# A refresh is only attempted if the token is stale.
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago
response = nil
expect {
response = api.call(url)
}.to change {
account.token
}.and change {
account.refresh_token
}
json = JSON.parse(response)
expect(json["message"]).to eq "Products retrieved successfully"
graph = DfcIo.import(json["products"])
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
end
end

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -53,7 +53,7 @@ RSpec.describe "DFC Product Import" do
select enterprise.name, from: "Enterprise"
url = "https://food-data-collaboration-produc-fe870152f634.herokuapp.com/fdc/products?shop=test-hodmedod.myshopify.com"
url = "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
fill_in "catalog_url", with: url
expect {