From 33ee03388fdd794f53165843bcbd1b4e326a1140 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 8 Nov 2022 15:40:31 +1100 Subject: [PATCH] Add DFC product update action, not implemented --- .../supplied_products_controller.rb | 7 ++ engines/dfc_provider/config/routes.rb | 2 +- .../supplied_products_controller_spec.rb | 30 ++++++- .../spec/support/patch_product.json | 88 +++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 engines/dfc_provider/spec/support/patch_product.json diff --git a/engines/dfc_provider/app/controllers/dfc_provider/supplied_products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/supplied_products_controller.rb index 188ba2093e..f1ac6c08c0 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/supplied_products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/supplied_products_controller.rb @@ -10,6 +10,13 @@ module DfcProvider render json: variant, serializer: DfcProvider::SuppliedProductSerializer end + def update + dfc_request = JSON.parse(request.body.read) + return unless dfc_request.key?("dfc-b:description") + + variant.product.update!(name: dfc_request["dfc-b:description"]) + end + private def variant diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb index afa2f8e638..a551e620a1 100644 --- a/engines/dfc_provider/config/routes.rb +++ b/engines/dfc_provider/config/routes.rb @@ -3,7 +3,7 @@ DfcProvider::Engine.routes.draw do resources :enterprises, only: [:show] do resources :catalog_items, only: [:index, :show] - resources :supplied_products, only: [:show] + resources :supplied_products, only: [:show, :update] end resources :persons, only: [:show] end diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb index 0e2122991d..a0312a7cec 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb @@ -1,8 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' +require DfcProvider::Engine.root.join("spec/spec_helper") describe DfcProvider::SuppliedProductsController, type: :controller do + include AuthorizationHelper + render_views let!(:user) { create(:user) } @@ -49,4 +51,30 @@ describe DfcProvider::SuppliedProductsController, type: :controller do end end end + + describe "#update" do + routes { DfcProvider::Engine.routes } + + it "requires authorisation" do + api_put :update, enterprise_id: "default", id: "0" + expect(response).to have_http_status :unauthorized + end + + describe "with authorisation" do + before { authorise user.email } + + it "updates the variant's name" do + params = { enterprise_id: enterprise.id, id: variant.id } + request_body = File.read(File.join(__dir__, "../../support/patch_product.json")) + + expect { + put(:update, params: params, body: request_body) + expect(response).to have_http_status :success + variant.reload + }.to change { + variant.name + } + end + end + end end diff --git a/engines/dfc_provider/spec/support/patch_product.json b/engines/dfc_provider/spec/support/patch_product.json new file mode 100644 index 0000000000..9e415bec9a --- /dev/null +++ b/engines/dfc_provider/spec/support/patch_product.json @@ -0,0 +1,88 @@ +{ + "@context": { + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", + "skos": "http://www.w3.org/2004/02/skos/core#", + "dfc": "http://static.datafoodconsortium.org/ontologies/DFC_FullModel.owl#", + "dc": "http://purl.org/dc/elements/1.1/#", + "dfc-b": "http://static.datafoodconsortium.org/ontologies/DFC_BusinessOntology.owl#", + "dfc-p": "http://static.datafoodconsortium.org/ontologies/DFC_ProductOntology.owl#", + "dfc-t": "http://static.datafoodconsortium.org/ontologies/DFC_TechnicalOntology.owl#", + "dfc-u": "http://static.datafoodconsortium.org/data/units.rdf#", + "dfc-pt": "http://static.datafoodconsortium.org/data/productTypes.rdf#", + "dfc-a": "http://static.datafoodconsortium.org/data/claims.rdf#", + "dfc-d": "http://static.datafoodconsortium.org/data/dimensions.rdf#", + "dfc-c": "http://static.datafoodconsortium.org/data/certifications.rdf#", + "dfc-g": "http://static.datafoodconsortium.org/data/geoOrigin.rdf#", + "dfc-p:hasUnit": { + "@type": "@id" + }, + "dfc-b:hasUnit": { + "@type": "@id" + }, + "dfc-b:hasQuantity": { + "@type": "@id" + }, + "dfc-p:hasType": { + "@type": "@id" + }, + "dfc-b:hasType": { + "@type": "@id" + }, + "dfc-b:references": { + "@type": "@id" + }, + "dfc-b:referencedBy": { + "@type": "@id" + }, + "dfc-b:offeres": { + "@type": "@id" + }, + "dfc-b:supplies": { + "@type": "@id" + }, + "dfc-b:defines": { + "@type": "@id" + }, + "dfc-b:affiliates": { + "@type": "@id" + }, + "dfc-b:manages": { + "@type": "@id" + }, + "dfc-b:offeredThrough": { + "@type": "@id" + }, + "dfc-b:hasBrand": { + "@type": "@id" + }, + "dfc-b:hasGeographicalOrigin": { + "@type": "@id" + }, + "dfc-b:hasClaim": { + "@type": "@id" + }, + "dfc-b:hasAllergenDimension": { + "@type": "@id" + }, + "dfc-b:hasNutrimentDimension": { + "@type": "@id" + }, + "dfc-b:hasPhysicalDimension": { + "@type": "@id" + }, + "dfc:owner": { + "@type": "@id" + }, + "dfc-t:hostedBy": { + "@type": "@id" + }, + "dfc-t:hasPivot": { + "@type": "@id" + }, + "dfc-t:represent": { + "@type": "@id" + } + }, + "dfc-b:description": "DFC-Pesto updated", + "dfc-b:quantity": 0 +}