Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail gracefully on DFC product import errors #12906

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/admin/dfc_product_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def index
end

@count = imported.compact.count
rescue Faraday::Error,
Addressable::URI::InvalidURIError,
ActionController::ParameterMissing => e
flash[:error] = e.message
redirect_to admin_product_import_path
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@
end
end

describe ".update_product" do
let(:subject) { builder.update_product(product, variant) }
let(:product) {
DfcIo.import(product_json).find do |subject|
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
end
}
let(:product_json) { ExampleJson.read("product.GET") }

it "updates a variant" do
variant # Create test data first

expect { subject }.not_to change {
Spree::Variant.count
}

expect(subject).to eq variant
expect(subject.display_name).to eq "Fillet Steak - 201g x 1 Steak"
expect(subject.variant_unit).to eq "items"
expect(subject.unit_value).to eq 1
expect(subject.on_demand).to eq false
expect(subject.on_hand).to eq 11
end
end

describe ".import_product" do
let(:supplied_product) do
DfcProvider::SuppliedProduct.new(
Expand Down
31 changes: 30 additions & 1 deletion spec/system/admin/dfc_product_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,41 @@
}.to change { enterprise.supplied_products.count }
.and change { linked_variant.display_name }
.and change { linked_variant.unit_value }
.and change { linked_variant.price }
.and change { linked_variant.price }.to(2.09)
.and change { linked_variant.on_demand }.to(true)
.and change { linked_variant.on_hand }.by(0)

expect(page).to have_content "Importing a DFC product catalog"

product = Spree::Product.last
expect(product.variants[0].semantic_links).to be_present
expect(product.image).to be_present
end

it "fails gracefully" do
user.oidc_account.update!(
uid: "anonymous@example.net",
updated_at: 1.minute.ago,
)
url = "https://example.net/unauthorized"
stub_request(:get, url).to_return(status: [401, "Unauthorized"])

visit admin_product_import_path
select enterprise.name, from: "Enterprise"
fill_in "catalog_url", with: url

expect { click_button "Import" }.not_to change { Spree::Variant.count }

expect(page).to have_content "the server responded with status 401"

select enterprise.name, from: "Enterprise"
fill_in "catalog_url", with: "badurl"
click_button "Import"
expect(page).to have_content "Absolute URI missing hierarchical segment: 'http://:80'"

select enterprise.name, from: "Enterprise"
fill_in "catalog_url", with: ""
click_button "Import"
expect(page).to have_content "param is missing or the value is empty: catalog_url"
end
end
Loading