Files
openfoodnetwork/engines/dfc_provider/spec/services/fdc_request_spec.rb
Maikel Linke 1f00662709 Add service to access FDC API
The current implementation of the FDC is not adhering to the DFC
standard. The difference is added in this compatibility layer.

This should be temporary code. The FDC dev team should change their API
in their next development cycle.
2024-05-28 17:00:06 +10:00

37 lines
1.0 KiB
Ruby

# 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