From d9d218f6613e775225d1202ddc98e91eef490268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 14 Apr 2020 14:28:19 +0200 Subject: [PATCH 01/15] Add DFC Provider engine --- Gemfile | 1 + Gemfile.lock | 6 ++++ config/routes.rb | 1 + .../api/dfc_provider/products_controller.rb | 26 +++++++++++++++++ .../dfc_provider/product_serializer.rb | 28 +++++++++++++++++++ engines/dfc_provider/config/routes.rb | 9 ++++++ engines/dfc_provider/dfc_provider.gemspec | 16 +++++++++++ engines/dfc_provider/lib/dfc_provider.rb | 4 +++ .../dfc_provider/lib/dfc_provider/engine.rb | 5 ++++ .../dfc_provider/lib/dfc_provider/version.rb | 3 ++ 10 files changed, 99 insertions(+) create mode 100644 engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb create mode 100644 engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb create mode 100644 engines/dfc_provider/config/routes.rb create mode 100644 engines/dfc_provider/dfc_provider.gemspec create mode 100644 engines/dfc_provider/lib/dfc_provider.rb create mode 100644 engines/dfc_provider/lib/dfc_provider/engine.rb create mode 100644 engines/dfc_provider/lib/dfc_provider/version.rb diff --git a/Gemfile b/Gemfile index dcf202d4f3..53130494e0 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'rails_safe_tasks', '~> 1.0' gem "activerecord-import" gem "catalog", path: "./engines/catalog" +gem 'dfc_provider', path: './engines/dfc_provider' gem "order_management", path: "./engines/order_management" gem 'web', path: './engines/web' diff --git a/Gemfile.lock b/Gemfile.lock index e5ebbf35a7..4dc1791427 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,11 @@ GIT rails-i18n spree_core (>= 1.1) +PATH + remote: engines/dfc_provider + specs: + dfc_provider (0.0.1) + PATH remote: engines/catalog specs: @@ -710,6 +715,7 @@ DEPENDENCIES delayed_job_web devise (~> 2.2.5) devise-encryptable (= 0.2.0) + dfc_provider! diffy eventmachine (>= 1.2.3) factory_bot_rails diff --git a/config/routes.rb b/config/routes.rb index dcb828b3be..98897ffdf4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -91,4 +91,5 @@ Openfoodnetwork::Application.routes.draw do # Mount Spree's routes mount Spree::Core::Engine, :at => '/' + mount DfcProvider::Engine, at: '/' end diff --git a/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb b/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb new file mode 100644 index 0000000000..c0e24eb6f9 --- /dev/null +++ b/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb @@ -0,0 +1,26 @@ +module Api::DfcProvider + class ProductsController < ActionController::Base + before_filter :set_enterprise + + def index + products = @enterprise.inventory_variants + .includes(:product, :inventory_items) + + products_json = DfcProvider::ProductSerializer + .new(@enterprise, products, base_url) + .serialized_json + + render json: products_json + end + + private + + def set_enterprise + @enterprise = ::Enterprise.find(params[:enterprise_id]) + end + + def base_url + "#{root_url}api/dfc_provider" + end + end +end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb new file mode 100644 index 0000000000..1422f72f02 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb @@ -0,0 +1,28 @@ +module DfcProvider + class ProductSerializer + def initialize(enterprise, products, base_url) + @enterprise = enterprise + @products = products + @base_url = base_url + end + + def serialized_json + { + "@context" => + { + "DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", + "@base" => @base_url + }, + "@id" => "/enterprises/#{@enterprise.id}/products", + "DFC:supplies" => @products.map do |variant| + { + "DFC:description" => variant.name, + "DFC:quantity" => variant.total_on_hand, + "@id" => variant.id, + "DFC:hasUnit" => {"@id" => "/unit/#{variant.unit_description.presence || 'piece' }"} + } + end + }.to_json + end + end +end diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb new file mode 100644 index 0000000000..800435932a --- /dev/null +++ b/engines/dfc_provider/config/routes.rb @@ -0,0 +1,9 @@ +DfcProvider::Engine.routes.draw do + namespace :api do + namespace :dfc_provider do + resources :enterprises, only: :none do + resources :products, only: %i[index] + end + end + end +end diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec new file mode 100644 index 0000000000..d14ab9db5f --- /dev/null +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -0,0 +1,16 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "dfc_provider/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = 'dfc_provider' + s.version = DfcProvider::VERSION + s.authors = ['Admin OFF'] + s.email = ['admin@openfoodfrance.org'] + s.summary = 'Provdes an API stack implementing DFC semantic specifications' + + s.files = Dir["{app,config,db,lib}/**/*"] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc'] + s.test_files = Dir['test/**/*'] +end diff --git a/engines/dfc_provider/lib/dfc_provider.rb b/engines/dfc_provider/lib/dfc_provider.rb new file mode 100644 index 0000000000..feda25b81c --- /dev/null +++ b/engines/dfc_provider/lib/dfc_provider.rb @@ -0,0 +1,4 @@ +require "dfc_provider/engine" + +module DfcProvider +end diff --git a/engines/dfc_provider/lib/dfc_provider/engine.rb b/engines/dfc_provider/lib/dfc_provider/engine.rb new file mode 100644 index 0000000000..9ad5163c30 --- /dev/null +++ b/engines/dfc_provider/lib/dfc_provider/engine.rb @@ -0,0 +1,5 @@ +module DfcProvider + class Engine < ::Rails::Engine + isolate_namespace DfcProvider + end +end diff --git a/engines/dfc_provider/lib/dfc_provider/version.rb b/engines/dfc_provider/lib/dfc_provider/version.rb new file mode 100644 index 0000000000..bfe289fbeb --- /dev/null +++ b/engines/dfc_provider/lib/dfc_provider/version.rb @@ -0,0 +1,3 @@ +module DfcProvider + VERSION = "0.0.1" +end From b152e532d25c2f5e442ca5e6021f2e043aa097c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Wed, 15 Apr 2020 22:43:23 +0200 Subject: [PATCH 02/15] Cosmetics --- .../api/dfc_provider/products_controller.rb | 40 +++++++++++-------- .../dfc_provider/product_serializer.rb | 38 +++++++++++------- engines/dfc_provider/config/routes.rb | 2 + engines/dfc_provider/dfc_provider.gemspec | 6 ++- engines/dfc_provider/lib/dfc_provider.rb | 2 + .../dfc_provider/lib/dfc_provider/engine.rb | 2 + .../dfc_provider/lib/dfc_provider/version.rb | 4 +- 7 files changed, 60 insertions(+), 34 deletions(-) diff --git a/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb b/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb index c0e24eb6f9..a3b9e2e399 100644 --- a/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb +++ b/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb @@ -1,26 +1,32 @@ -module Api::DfcProvider - class ProductsController < ActionController::Base - before_filter :set_enterprise +# frozen_string_literal: true - def index - products = @enterprise.inventory_variants - .includes(:product, :inventory_items) +# Controller used to provide the API products for the DFC application +module Api + module DfcProvider + class ProductsController < Api::BaseController + before_filter :set_enterprise - products_json = DfcProvider::ProductSerializer - .new(@enterprise, products, base_url) - .serialized_json + def index + products = @enterprise. + inventory_variants. + includes(:product, :inventory_items) - render json: products_json - end + products_json = ::DfcProvider::ProductSerializer. + new(@enterprise, products, base_url). + serialized_json - private + render json: products_json + end - def set_enterprise - @enterprise = ::Enterprise.find(params[:enterprise_id]) - end + private - def base_url - "#{root_url}api/dfc_provider" + def set_enterprise + @enterprise = ::Enterprise.find(params[:enterprise_id]) + end + + def base_url + "#{root_url}api/dfc_provider" + end end end end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb index 1422f72f02..8bd33abed6 100644 --- a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb +++ b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +# Serializer used to render the products passed +# into JSON-LD format based on DFC ontology module DfcProvider class ProductSerializer def initialize(enterprise, products, base_url) @@ -9,20 +13,26 @@ module DfcProvider def serialized_json { "@context" => - { - "DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", - "@base" => @base_url - }, - "@id" => "/enterprises/#{@enterprise.id}/products", - "DFC:supplies" => @products.map do |variant| - { - "DFC:description" => variant.name, - "DFC:quantity" => variant.total_on_hand, - "@id" => variant.id, - "DFC:hasUnit" => {"@id" => "/unit/#{variant.unit_description.presence || 'piece' }"} - } - end - }.to_json + { + "DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", + "@base" => @base_url + }, + "@id" => "/enterprises/#{@enterprise.id}/products", + "DFC:supplies" => serialized_products + }.to_json + end + + private + + def serialized_products + @products.map do |variant| + { + "DFC:description" => variant.name, + "DFC:quantity" => variant.total_on_hand, + "@id" => variant.id, + "DFC:hasUnit" => { "@id" => "/unit/#{variant.unit_description.presence || 'piece'}" } + } + end end end end diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb index 800435932a..1d7ab6b87f 100644 --- a/engines/dfc_provider/config/routes.rb +++ b/engines/dfc_provider/config/routes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + DfcProvider::Engine.routes.draw do namespace :api do namespace :dfc_provider do diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec index d14ab9db5f..4bf510bed5 100644 --- a/engines/dfc_provider/dfc_provider.gemspec +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -1,4 +1,6 @@ -$:.push File.expand_path("../lib", __FILE__) +# frozen_string_literal: true + +$LOAD_PATH.push File.expand_path('lib', __dir__) # Maintain your gem's version: require "dfc_provider/version" @@ -9,7 +11,7 @@ Gem::Specification.new do |s| s.version = DfcProvider::VERSION s.authors = ['Admin OFF'] s.email = ['admin@openfoodfrance.org'] - s.summary = 'Provdes an API stack implementing DFC semantic specifications' + s.summary = 'Provides an API stack implementing DFC semantic specifications' s.files = Dir["{app,config,db,lib}/**/*"] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc'] s.test_files = Dir['test/**/*'] diff --git a/engines/dfc_provider/lib/dfc_provider.rb b/engines/dfc_provider/lib/dfc_provider.rb index feda25b81c..b0cf98121a 100644 --- a/engines/dfc_provider/lib/dfc_provider.rb +++ b/engines/dfc_provider/lib/dfc_provider.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "dfc_provider/engine" module DfcProvider diff --git a/engines/dfc_provider/lib/dfc_provider/engine.rb b/engines/dfc_provider/lib/dfc_provider/engine.rb index 9ad5163c30..fedaef351a 100644 --- a/engines/dfc_provider/lib/dfc_provider/engine.rb +++ b/engines/dfc_provider/lib/dfc_provider/engine.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DfcProvider class Engine < ::Rails::Engine isolate_namespace DfcProvider diff --git a/engines/dfc_provider/lib/dfc_provider/version.rb b/engines/dfc_provider/lib/dfc_provider/version.rb index bfe289fbeb..3b891391e3 100644 --- a/engines/dfc_provider/lib/dfc_provider/version.rb +++ b/engines/dfc_provider/lib/dfc_provider/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DfcProvider - VERSION = "0.0.1" + VERSION = '0.0.1' end From 80bbd5d51332f036dc2710804963427730a6dbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 24 Apr 2020 12:12:00 +0200 Subject: [PATCH 03/15] Adjust namespace and scope for routes --- .../api}/products_controller.rb | 13 ++++++++++--- engines/dfc_provider/config/routes.rb | 2 +- engines/dfc_provider/dfc_provider.gemspec | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) rename engines/dfc_provider/app/controllers/{api/dfc_provider => dfc_provider/api}/products_controller.rb (69%) diff --git a/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb similarity index 69% rename from engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb rename to engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index a3b9e2e399..f6fc6fcc3c 100644 --- a/engines/dfc_provider/app/controllers/api/dfc_provider/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -1,10 +1,13 @@ # frozen_string_literal: true # Controller used to provide the API products for the DFC application -module Api - module DfcProvider - class ProductsController < Api::BaseController +module DfcProvider + module Api + class ProductsController < ::Api::BaseController + skip_before_filter :authenticate_user before_filter :set_enterprise + before_filter :authenticate_user + skip_authorization_check def index products = @enterprise. @@ -20,6 +23,10 @@ module Api private + def authenticate_user + @current_api_user = @enterprise.owner + end + def set_enterprise @enterprise = ::Enterprise.find(params[:enterprise_id]) end diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb index 1d7ab6b87f..d716c6100d 100644 --- a/engines/dfc_provider/config/routes.rb +++ b/engines/dfc_provider/config/routes.rb @@ -2,7 +2,7 @@ DfcProvider::Engine.routes.draw do namespace :api do - namespace :dfc_provider do + scope :dfc_provider, as: :dfc_provider, path: '/dfc_provider' do resources :enterprises, only: :none do resources :products, only: %i[index] end diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec index 4bf510bed5..bd3804e257 100644 --- a/engines/dfc_provider/dfc_provider.gemspec +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -13,6 +13,6 @@ Gem::Specification.new do |s| s.email = ['admin@openfoodfrance.org'] s.summary = 'Provides an API stack implementing DFC semantic specifications' - s.files = Dir["{app,config,db,lib}/**/*"] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc'] + s.files = Dir["{app,config,db,lib}/**/*"] + ['README.rdoc'] s.test_files = Dir['test/**/*'] end From 496174255f193ef5aa2e5f4fd1b76dc558a52d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 24 Apr 2020 12:13:49 +0200 Subject: [PATCH 04/15] Add REAME.md file --- engines/dfc_provider/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 engines/dfc_provider/README.md diff --git a/engines/dfc_provider/README.md b/engines/dfc_provider/README.md new file mode 100644 index 0000000000..dda1304a56 --- /dev/null +++ b/engines/dfc_provider/README.md @@ -0,0 +1,8 @@ +# DfcProvider + +This engine is implementing the Data Food Consortium specifications in order to serve semantic data. +You can find more details about this on https://github.com/datafoodconsortium. + +Basically, it allows an OFN user: +* to retrieve an Access token from DFC Authorization server (using OIDC implemntation) +* to serve his Products Catalog through a dedicated API using JSON-LD format, structured by the DFC Ontology. From a8fb0594826d5e359c39ff1d68cff8d3bd87d5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 24 Apr 2020 12:14:17 +0200 Subject: [PATCH 05/15] Add basic spec --- .../api/products_controller_spec.rb | 34 +++++++++++++++++++ engines/dfc_provider/spec/spec_helper.rb | 3 ++ 2 files changed, 37 insertions(+) create mode 100644 engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb create mode 100644 engines/dfc_provider/spec/spec_helper.rb diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb new file mode 100644 index 0000000000..9cc26b5e81 --- /dev/null +++ b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe DfcProvider::Api::ProductsController, type: :controller do + render_views + + let(:enterprise) { create(:distributor_enterprise) } + let(:product) do + create(:simple_product, supplier: enterprise ) + end + let!(:visible_inventory_item) do + create(:inventory_item, + enterprise: enterprise, + variant: product.variants.first, + visible: true) + end + + describe('.index') do + before do + allow(controller) + .to receive(:spree_current_user) { enterprise.owner } + + get :index, enterprise_id: enterprise.id + end + + it 'is successful' do + expect(response.status).to eq 200 + end + + it 'renders the related product' do + expect(response.body) + .to include("\"DFC:description\":\"#{product.variants.first.name}\"") + end + end +end diff --git a/engines/dfc_provider/spec/spec_helper.rb b/engines/dfc_provider/spec/spec_helper.rb new file mode 100644 index 0000000000..9cfd0bc717 --- /dev/null +++ b/engines/dfc_provider/spec/spec_helper.rb @@ -0,0 +1,3 @@ +require "../../spec/spec_helper.rb" + +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } From c1d173d601504f577e4eb1c2061c8b4ff3f51112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 28 Apr 2020 12:12:04 +0200 Subject: [PATCH 06/15] Add the access token logic, light version --- Gemfile.lock | 12 +-- .../dfc_provider/api/products_controller.rb | 59 +++++++++++-- .../dfc_provider/authorization_control.rb | 31 +++++++ engines/dfc_provider/dfc_provider.gemspec | 20 +++-- .../api/products_controller_spec.rb | 82 ++++++++++++++++--- engines/dfc_provider/spec/spec_helper.rb | 4 +- 6 files changed, 175 insertions(+), 33 deletions(-) create mode 100644 engines/dfc_provider/app/services/dfc_provider/authorization_control.rb diff --git a/Gemfile.lock b/Gemfile.lock index 4dc1791427..cf7b88e370 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,16 +56,18 @@ GIT rails-i18n spree_core (>= 1.1) -PATH - remote: engines/dfc_provider - specs: - dfc_provider (0.0.1) - PATH remote: engines/catalog specs: catalog (0.0.1) +PATH + remote: engines/dfc_provider + specs: + dfc_provider (0.0.1) + jwt (~> 2.2) + rspec (~> 3.9) + PATH remote: engines/order_management specs: diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index f6fc6fcc3c..ce33ba1c72 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -3,11 +3,24 @@ # Controller used to provide the API products for the DFC application module DfcProvider module Api - class ProductsController < ::Api::BaseController - skip_before_filter :authenticate_user - before_filter :set_enterprise - before_filter :authenticate_user - skip_authorization_check + class ProductsController < ::ActionController::Metal + include ActionController::Head + include AbstractController::Rendering + include ActionController::Rendering + include ActionController::Renderers::All + include ActionController::MimeResponds + include ActionController::ImplicitRender + include AbstractController::Callbacks + # To access 'base_url' helper + include ActionController::UrlFor + include Rails.application.routes.url_helpers + + before_filter :check_authorization, + :check_enterprise, + :check_user, + :check_accessibility + + respond_to :json def index products = @enterprise. @@ -23,17 +36,45 @@ module DfcProvider private - def authenticate_user - @current_api_user = @enterprise.owner + def check_enterprise + @enterprise = ::Enterprise.where(id: params[:enterprise_id]).first + + return if @enterprise.present? + + head :not_found end - def set_enterprise - @enterprise = ::Enterprise.find(params[:enterprise_id]) + def check_authorization + return if access_token.present? + + head :unauthorized + end + + def check_user + @user = authorization_control.process + + return if @user.present? + + head :unprocessable_entity + end + + def check_accessibility + return if @enterprise.owner == @user + + head :forbidden end def base_url "#{root_url}api/dfc_provider" end + + def access_token + request.headers['Authorization'].to_s.split(' ').last + end + + def authorization_control + DfcProvider::AuthorizationControl.new(access_token) + end end end end diff --git a/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb b/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb new file mode 100644 index 0000000000..beb1885816 --- /dev/null +++ b/engines/dfc_provider/app/services/dfc_provider/authorization_control.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# Service used to authorize the user on DCF Provider API +# It controls an OICD Access token and an enterprise. +module DfcProvider + class AuthorizationControl + def initialize(access_token) + @access_token = access_token + end + + def process + decode_token + find_ofn_user + end + + def decode_token + data = JWT.decode( + @access_token, + nil, + false + ) + + @header = data.last + @payload = data.first + end + + def find_ofn_user + Spree::User.where(email: @payload['email']).first + end + end +end diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec index bd3804e257..b00bdf0c33 100644 --- a/engines/dfc_provider/dfc_provider.gemspec +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -6,13 +6,17 @@ $LOAD_PATH.push File.expand_path('lib', __dir__) require "dfc_provider/version" # Describe your gem and declare its dependencies: -Gem::Specification.new do |s| - s.name = 'dfc_provider' - s.version = DfcProvider::VERSION - s.authors = ['Admin OFF'] - s.email = ['admin@openfoodfrance.org'] - s.summary = 'Provides an API stack implementing DFC semantic specifications' +Gem::Specification.new do |spec| + spec.name = 'dfc_provider' + spec.version = DfcProvider::VERSION + spec.authors = ['Admin OFF'] + spec.email = ['admin@openfoodfrance.org'] + spec.summary = 'Provides an API stack implementing DFC semantic ' \ + 'specifications' - s.files = Dir["{app,config,db,lib}/**/*"] + ['README.rdoc'] - s.test_files = Dir['test/**/*'] + spec.files = Dir["{app,config,lib}/**/*"] + ['README.md'] + spec.test_files = Dir['spec/**/*'] + + spec.add_dependency 'jwt', '~> 2.2' + spec.add_dependency 'rspec', '~> 3.9' end diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb index 9cc26b5e81..754b81fd94 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe DfcProvider::Api::ProductsController, type: :controller do @@ -13,22 +15,82 @@ describe DfcProvider::Api::ProductsController, type: :controller do variant: product.variants.first, visible: true) end + let(:user) { enterprise.owner } describe('.index') do - before do - allow(controller) - .to receive(:spree_current_user) { enterprise.owner } + context 'with authorization token' do + before do + request.env['Authorization'] = 'Bearer 123456.abcdef.123456' + end - get :index, enterprise_id: enterprise.id + context 'with an enterprise' do + context 'with a linked user' do + before do + allow_any_instance_of(DfcProvider::AuthorizationControl) + .to receive(:process) + .and_return(user) + end + + context 'with valid accessibility' do + before do + get :index, enterprise_id: enterprise.id + end + + it 'is successful' do + expect(response.status).to eq 200 + end + + it 'renders the related product' do + expect(response.body) + .to include("\"DFC:description\":\"#{product.variants.first.name}\"") + end + end + + context 'without valid accessibility' do + before do + get :index, enterprise_id: create(:enterprise).id + end + + it 'returns unauthorized head' do + expect(response.status).to eq 403 + end + end + end + + context 'without a linked user' do + before do + allow_any_instance_of(DfcProvider::AuthorizationControl) + .to receive(:process) + .and_return(nil) + end + + before do + get :index, enterprise_id: enterprise.id + end + + it 'returns unprocessable_entity head' do + expect(response.status).to eq 422 + end + end + end + + context 'without a recorded enterprise' do + before do + get :index, enterprise_id: '123456' + end + + it 'returns not_found head' do + expect(response.status).to eq 404 + end + end end - it 'is successful' do - expect(response.status).to eq 200 - end + context 'without an authorization token' do + before { get :index, enterprise_id: enterprise.id } - it 'renders the related product' do - expect(response.body) - .to include("\"DFC:description\":\"#{product.variants.first.name}\"") + it 'returns unauthorized head' do + expect(response.status).to eq 401 + end end end end diff --git a/engines/dfc_provider/spec/spec_helper.rb b/engines/dfc_provider/spec/spec_helper.rb index 9cfd0bc717..3492f4f944 100644 --- a/engines/dfc_provider/spec/spec_helper.rb +++ b/engines/dfc_provider/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "../../spec/spec_helper.rb" -Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f } From dd9c994f9ddc6c8f82a801c0a2d7f1036955428f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 28 Apr 2020 16:08:11 +0200 Subject: [PATCH 07/15] Use spree_api_controller_setup layer --- .../dfc_provider/api/products_controller.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index ce33ba1c72..52c7f49121 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -4,13 +4,8 @@ module DfcProvider module Api class ProductsController < ::ActionController::Metal - include ActionController::Head - include AbstractController::Rendering - include ActionController::Rendering - include ActionController::Renderers::All - include ActionController::MimeResponds - include ActionController::ImplicitRender - include AbstractController::Callbacks + include Spree::Api::ControllerSetup + # To access 'base_url' helper include ActionController::UrlFor include Rails.application.routes.url_helpers @@ -20,8 +15,6 @@ module DfcProvider :check_user, :check_accessibility - respond_to :json - def index products = @enterprise. inventory_variants. From 830bf796d07e7f3b0115a98921d2000e7beabb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 28 Apr 2020 16:08:39 +0200 Subject: [PATCH 08/15] Update authors and remove emails --- engines/dfc_provider/dfc_provider.gemspec | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec index b00bdf0c33..4a44fede6b 100644 --- a/engines/dfc_provider/dfc_provider.gemspec +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -9,8 +9,7 @@ require "dfc_provider/version" Gem::Specification.new do |spec| spec.name = 'dfc_provider' spec.version = DfcProvider::VERSION - spec.authors = ['Admin OFF'] - spec.email = ['admin@openfoodfrance.org'] + spec.authors = ["developers@ofn"] spec.summary = 'Provides an API stack implementing DFC semantic ' \ 'specifications' From 013198b33bfcec6f283da98327ef08e1aff4ae07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 28 Apr 2020 17:28:22 +0200 Subject: [PATCH 09/15] Update routes --- config/routes.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 98897ffdf4..fcb633f9e3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,7 +89,9 @@ Openfoodnetwork::Application.routes.draw do get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' } + # Mount DFC API endpoints + mount DfcProvider::Engine, at: '/' + # Mount Spree's routes mount Spree::Core::Engine, :at => '/' - mount DfcProvider::Engine, at: '/' end From f0f82249344c035bf9ea70aa25d0e688d6ee5d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 28 Apr 2020 20:12:40 +0200 Subject: [PATCH 10/15] Put back former lighter inclusions --- .../dfc_provider/api/products_controller.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index 52c7f49121..ce33ba1c72 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -4,8 +4,13 @@ module DfcProvider module Api class ProductsController < ::ActionController::Metal - include Spree::Api::ControllerSetup - + include ActionController::Head + include AbstractController::Rendering + include ActionController::Rendering + include ActionController::Renderers::All + include ActionController::MimeResponds + include ActionController::ImplicitRender + include AbstractController::Callbacks # To access 'base_url' helper include ActionController::UrlFor include Rails.application.routes.url_helpers @@ -15,6 +20,8 @@ module DfcProvider :check_user, :check_accessibility + respond_to :json + def index products = @enterprise. inventory_variants. From 43293f00df974df6441bb4fecb3d124dda9beb6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Sat, 2 May 2020 18:30:01 +0200 Subject: [PATCH 11/15] Make the endpoint unrelated to enterprise id Better to avoid to have endpoint specific to an enterprise as we don't know the enterprise id yet on DFC side --- engines/dfc_provider/README.md | 8 +- .../dfc_provider/api/products_controller.rb | 17 ++-- .../dfc_provider/product_serializer.rb | 5 +- engines/dfc_provider/config/routes.rb | 2 +- engines/dfc_provider/dfc_provider.gemspec | 2 +- .../api/products_controller_spec.rb | 80 ++++++++----------- 6 files changed, 46 insertions(+), 68 deletions(-) diff --git a/engines/dfc_provider/README.md b/engines/dfc_provider/README.md index dda1304a56..8e73633c46 100644 --- a/engines/dfc_provider/README.md +++ b/engines/dfc_provider/README.md @@ -3,6 +3,8 @@ This engine is implementing the Data Food Consortium specifications in order to serve semantic data. You can find more details about this on https://github.com/datafoodconsortium. -Basically, it allows an OFN user: -* to retrieve an Access token from DFC Authorization server (using OIDC implemntation) -* to serve his Products Catalog through a dedicated API using JSON-LD format, structured by the DFC Ontology. +Basically, it allows an OFN user linked to an enterprise: +* to serve his Products Catalog through a dedicated API using JSON-LD format, structured by the DFC Ontology +* to be authenticated thanks to an Access Token from DFC Authorization server (using an OIDC implementation) + +The API endpoint for the catalog is `/api/dfc_provider/enterprise/prodcuts.json` and you need to pass the token inside an authentication header (`Authentication: Bearer 123mytoken456`). diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index ce33ba1c72..d31aa65b75 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -16,9 +16,8 @@ module DfcProvider include Rails.application.routes.url_helpers before_filter :check_authorization, - :check_enterprise, :check_user, - :check_accessibility + :check_enterprise respond_to :json @@ -28,7 +27,7 @@ module DfcProvider includes(:product, :inventory_items) products_json = ::DfcProvider::ProductSerializer. - new(@enterprise, products, base_url). + new(products, base_url). serialized_json render json: products_json @@ -37,7 +36,7 @@ module DfcProvider private def check_enterprise - @enterprise = ::Enterprise.where(id: params[:enterprise_id]).first + @enterprise = @user.enterprises.first return if @enterprise.present? @@ -47,7 +46,7 @@ module DfcProvider def check_authorization return if access_token.present? - head :unauthorized + head :unprocessable_entity end def check_user @@ -55,13 +54,7 @@ module DfcProvider return if @user.present? - head :unprocessable_entity - end - - def check_accessibility - return if @enterprise.owner == @user - - head :forbidden + head :unauthorized end def base_url diff --git a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb index 8bd33abed6..35a840c6e1 100644 --- a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb +++ b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb @@ -4,8 +4,7 @@ # into JSON-LD format based on DFC ontology module DfcProvider class ProductSerializer - def initialize(enterprise, products, base_url) - @enterprise = enterprise + def initialize(products, base_url) @products = products @base_url = base_url end @@ -17,7 +16,7 @@ module DfcProvider "DFC" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", "@base" => @base_url }, - "@id" => "/enterprises/#{@enterprise.id}/products", + "@id" => "/enterprise/products", "DFC:supplies" => serialized_products }.to_json end diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb index d716c6100d..c9579cd4d1 100644 --- a/engines/dfc_provider/config/routes.rb +++ b/engines/dfc_provider/config/routes.rb @@ -3,7 +3,7 @@ DfcProvider::Engine.routes.draw do namespace :api do scope :dfc_provider, as: :dfc_provider, path: '/dfc_provider' do - resources :enterprises, only: :none do + resource :enterprise, only: :none do resources :products, only: %i[index] end end diff --git a/engines/dfc_provider/dfc_provider.gemspec b/engines/dfc_provider/dfc_provider.gemspec index 4a44fede6b..4684aaaa18 100644 --- a/engines/dfc_provider/dfc_provider.gemspec +++ b/engines/dfc_provider/dfc_provider.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |spec| spec.version = DfcProvider::VERSION spec.authors = ["developers@ofn"] spec.summary = 'Provides an API stack implementing DFC semantic ' \ - 'specifications' + 'specifications' spec.files = Dir["{app,config,lib}/**/*"] + ['README.md'] spec.test_files = Dir['spec/**/*'] diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb index 754b81fd94..c12ef26bc1 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb @@ -5,7 +5,8 @@ require 'spec_helper' describe DfcProvider::Api::ProductsController, type: :controller do render_views - let(:enterprise) { create(:distributor_enterprise) } + let(:user) { create(:user) } + let(:enterprise) { create(:distributor_enterprise, owner: user) } let(:product) do create(:simple_product, supplier: enterprise ) end @@ -15,7 +16,6 @@ describe DfcProvider::Api::ProductsController, type: :controller do variant: product.variants.first, visible: true) end - let(:user) { enterprise.owner } describe('.index') do context 'with authorization token' do @@ -23,64 +23,48 @@ describe DfcProvider::Api::ProductsController, type: :controller do request.env['Authorization'] = 'Bearer 123456.abcdef.123456' end - context 'with an enterprise' do - context 'with a linked user' do - before do - allow_any_instance_of(DfcProvider::AuthorizationControl) - .to receive(:process) - .and_return(user) + context 'with an authenticated user' do + before do + allow_any_instance_of(DfcProvider::AuthorizationControl) + .to receive(:process) + .and_return(user) + end + + context 'with an enterprise' do + before { get :index } + + it 'is successful' do + expect(response.status).to eq 200 end - context 'with valid accessibility' do - before do - get :index, enterprise_id: enterprise.id - end - - it 'is successful' do - expect(response.status).to eq 200 - end - - it 'renders the related product' do - expect(response.body) - .to include("\"DFC:description\":\"#{product.variants.first.name}\"") - end - end - - context 'without valid accessibility' do - before do - get :index, enterprise_id: create(:enterprise).id - end - - it 'returns unauthorized head' do - expect(response.status).to eq 403 - end + it 'renders the related product' do + expect(response.body) + .to include("\"DFC:description\":\"#{product.variants.first.name}\"") end end - context 'without a linked user' do - before do - allow_any_instance_of(DfcProvider::AuthorizationControl) - .to receive(:process) - .and_return(nil) - end + context 'without a recorded enterprise' do + let(:enterprise) { create(:enterprise) } - before do - get :index, enterprise_id: enterprise.id - end + before { get :index } - it 'returns unprocessable_entity head' do - expect(response.status).to eq 422 + it 'returns not_found head' do + expect(response.status).to eq 404 end end end - context 'without a recorded enterprise' do + context 'without an authenticated user' do before do - get :index, enterprise_id: '123456' + allow_any_instance_of(DfcProvider::AuthorizationControl) + .to receive(:process) + .and_return(nil) end - it 'returns not_found head' do - expect(response.status).to eq 404 + before { get :index } + + it 'returns unauthorized head' do + expect(response.status).to eq 401 end end end @@ -88,8 +72,8 @@ describe DfcProvider::Api::ProductsController, type: :controller do context 'without an authorization token' do before { get :index, enterprise_id: enterprise.id } - it 'returns unauthorized head' do - expect(response.status).to eq 401 + it 'returns unprocessable_entity head' do + expect(response.status).to eq 422 end end end From 99798b010f1495f4d2589d7dea6e8e666731478c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Wed, 6 May 2020 21:44:59 +0200 Subject: [PATCH 12/15] Use ActionController::Base --- .../dfc_provider/api/products_controller.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index d31aa65b75..6230d25355 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -3,16 +3,8 @@ # Controller used to provide the API products for the DFC application module DfcProvider module Api - class ProductsController < ::ActionController::Metal - include ActionController::Head - include AbstractController::Rendering - include ActionController::Rendering - include ActionController::Renderers::All - include ActionController::MimeResponds - include ActionController::ImplicitRender - include AbstractController::Callbacks + class ProductsController < ::ActionController::Base # To access 'base_url' helper - include ActionController::UrlFor include Rails.application.routes.url_helpers before_filter :check_authorization, From f6e06b565825150ce000eaeed4fe54699d46302c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Wed, 6 May 2020 21:45:35 +0200 Subject: [PATCH 13/15] Use default enterprise_id to get the entreprise --- .../dfc_provider/api/products_controller.rb | 7 ++- engines/dfc_provider/config/routes.rb | 4 +- .../api/products_controller_spec.rb | 47 ++++++++++++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index 6230d25355..c5f086bbfd 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -28,7 +28,12 @@ module DfcProvider private def check_enterprise - @enterprise = @user.enterprises.first + @enterprise = + if params[:enterprise_id] == 'default' + @user.enterprises.first + else + @user.enterprises.where(id: params[:enterprise_id]).first + end return if @enterprise.present? diff --git a/engines/dfc_provider/config/routes.rb b/engines/dfc_provider/config/routes.rb index c9579cd4d1..b88e13e768 100644 --- a/engines/dfc_provider/config/routes.rb +++ b/engines/dfc_provider/config/routes.rb @@ -3,8 +3,8 @@ DfcProvider::Engine.routes.draw do namespace :api do scope :dfc_provider, as: :dfc_provider, path: '/dfc_provider' do - resource :enterprise, only: :none do - resources :products, only: %i[index] + resources :enterprises, only: :none do + resources :products, only: [:index] end end end diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb index c12ef26bc1..6a2f90099f 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb @@ -7,9 +7,7 @@ describe DfcProvider::Api::ProductsController, type: :controller do let(:user) { create(:user) } let(:enterprise) { create(:distributor_enterprise, owner: user) } - let(:product) do - create(:simple_product, supplier: enterprise ) - end + let(:product) { create(:simple_product, supplier: enterprise ) } let!(:visible_inventory_item) do create(:inventory_item, enterprise: enterprise, @@ -31,22 +29,49 @@ describe DfcProvider::Api::ProductsController, type: :controller do end context 'with an enterprise' do - before { get :index } + context 'given with an id' do + context 'related to the user' do + before { get :index, enterprise_id: enterprise.id } - it 'is successful' do - expect(response.status).to eq 200 + it 'is successful' do + expect(response.status).to eq 200 + end + + it 'renders the related product' do + expect(response.body) + .to include(product.variants.first.name) + end + end + + context 'not related to the user' do + let(:enterprise) { create(:enterprise) } + + before { get :index, enterprise_id: enterprise.id } + + it 'returns not_found head' do + expect(response.status).to eq 404 + end + end end - it 'renders the related product' do - expect(response.body) - .to include("\"DFC:description\":\"#{product.variants.first.name}\"") + context 'as default' do + before { get :index, enterprise_id: 'default' } + + it 'is successful' do + expect(response.status).to eq 200 + end + + it 'renders the related product' do + expect(response.body) + .to include(product.variants.first.name) + end end end context 'without a recorded enterprise' do let(:enterprise) { create(:enterprise) } - before { get :index } + before { get :index, enterprise_id: 'default' } it 'returns not_found head' do expect(response.status).to eq 404 @@ -61,7 +86,7 @@ describe DfcProvider::Api::ProductsController, type: :controller do .and_return(nil) end - before { get :index } + before { get :index, enterprise_id: 'default' } it 'returns unauthorized head' do expect(response.status).to eq 401 From 13f00480ce175fa613d0e6f8404e8259dd49d7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Wed, 6 May 2020 21:55:04 +0200 Subject: [PATCH 14/15] Use serialized_data --- .../app/controllers/dfc_provider/api/products_controller.rb | 6 +++--- .../app/serializers/dfc_provider/product_serializer.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index c5f086bbfd..54c5bd4556 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -18,11 +18,11 @@ module DfcProvider inventory_variants. includes(:product, :inventory_items) - products_json = ::DfcProvider::ProductSerializer. + serialized_data = ::DfcProvider::ProductSerializer. new(products, base_url). - serialized_json + serialized_data - render json: products_json + render json: serialized_data end private diff --git a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb index 35a840c6e1..42749a3d8d 100644 --- a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb +++ b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb @@ -9,7 +9,7 @@ module DfcProvider @base_url = base_url end - def serialized_json + def serialized_data { "@context" => { @@ -18,7 +18,7 @@ module DfcProvider }, "@id" => "/enterprise/products", "DFC:supplies" => serialized_products - }.to_json + } end private From d5b69ec90c808f8712159739ad6e8d23f7e2ca6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Wed, 6 May 2020 22:04:12 +0200 Subject: [PATCH 15/15] Remove before call when no factorization --- .../api/products_controller_spec.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb index 6a2f90099f..c3f40a16a0 100644 --- a/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb +++ b/engines/dfc_provider/spec/controllers/dfc_provider/api/products_controller_spec.rb @@ -31,7 +31,7 @@ describe DfcProvider::Api::ProductsController, type: :controller do context 'with an enterprise' do context 'given with an id' do context 'related to the user' do - before { get :index, enterprise_id: enterprise.id } + before { get :index, enterprise_id: 'default' } it 'is successful' do expect(response.status).to eq 200 @@ -46,9 +46,8 @@ describe DfcProvider::Api::ProductsController, type: :controller do context 'not related to the user' do let(:enterprise) { create(:enterprise) } - before { get :index, enterprise_id: enterprise.id } - it 'returns not_found head' do + get :index, enterprise_id: enterprise.id expect(response.status).to eq 404 end end @@ -71,33 +70,28 @@ describe DfcProvider::Api::ProductsController, type: :controller do context 'without a recorded enterprise' do let(:enterprise) { create(:enterprise) } - before { get :index, enterprise_id: 'default' } - it 'returns not_found head' do + get :index, enterprise_id: 'default' expect(response.status).to eq 404 end end end context 'without an authenticated user' do - before do + it 'returns unauthorized head' do allow_any_instance_of(DfcProvider::AuthorizationControl) .to receive(:process) .and_return(nil) - end - before { get :index, enterprise_id: 'default' } - - it 'returns unauthorized head' do + get :index, enterprise_id: 'default' expect(response.status).to eq 401 end end end context 'without an authorization token' do - before { get :index, enterprise_id: enterprise.id } - it 'returns unprocessable_entity head' do + get :index, enterprise_id: enterprise.id expect(response.status).to eq 422 end end