mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add SuppliedProducts controller
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Controller used to provide the SuppliedProducts API for the DFC application
|
||||
module DfcProvider
|
||||
module Api
|
||||
class SuppliedProductsController < BaseController
|
||||
def show
|
||||
@product = @enterprise.supplied_products.find(params[:id])
|
||||
|
||||
render json: @product, serializer: DfcProvider::SuppliedProductSerializer
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -42,6 +42,14 @@ module DfcProvider
|
||||
|
||||
private
|
||||
|
||||
def reference_id
|
||||
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
|
||||
enterprise_id: object.product.supplier_id,
|
||||
id: object.product_id,
|
||||
host: root_url
|
||||
)
|
||||
end
|
||||
|
||||
def dfc_provider_routes
|
||||
DfcProvider::Engine.routes.url_helpers
|
||||
end
|
||||
|
||||
@@ -17,7 +17,11 @@ module DfcProvider
|
||||
has_many :physical_characteristics, key: 'dfc:physicalCharacterisctics'
|
||||
|
||||
def id
|
||||
"/supplied_products/#{object.id}"
|
||||
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
|
||||
enterprise_id: object.supplier_id,
|
||||
id: object.id,
|
||||
host: root_url
|
||||
)
|
||||
end
|
||||
|
||||
def type
|
||||
@@ -60,5 +64,9 @@ module DfcProvider
|
||||
def unit_name
|
||||
object.unit_description.presence || 'piece'
|
||||
end
|
||||
|
||||
def dfc_provider_routes
|
||||
DfcProvider::Engine.routes.url_helpers
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,6 +5,7 @@ DfcProvider::Engine.routes.draw do
|
||||
scope :dfc_provider, as: :dfc_provider, path: '/dfc_provider' do
|
||||
resources :enterprises, only: [:show] do
|
||||
resources :catalog_items, only: [:index, :show]
|
||||
resources :supplied_products, only: [:show]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe DfcProvider::Api::SuppliedProductsController, type: :controller do
|
||||
render_views
|
||||
|
||||
let!(:user) { create(:user) }
|
||||
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
|
||||
let!(:product) { create(:simple_product, supplier: enterprise ) }
|
||||
|
||||
describe('.show') do
|
||||
context 'with authorization token' do
|
||||
before do
|
||||
request.headers['Authorization'] = 'Bearer 123456.abcdef.123456'
|
||||
end
|
||||
|
||||
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
|
||||
context 'given with an id' do
|
||||
before do
|
||||
api_get :show,
|
||||
enterprise_id: 'default',
|
||||
id: product.id
|
||||
end
|
||||
|
||||
it 'is successful' do
|
||||
expect(response.status).to eq 200
|
||||
end
|
||||
|
||||
it 'renders the required content' do
|
||||
expect(response.body)
|
||||
.to include(product.name)
|
||||
end
|
||||
end
|
||||
|
||||
context 'given with a wrong id' do
|
||||
before { api_get :show, id: 999 }
|
||||
|
||||
it 'returns 404' do
|
||||
expect(response.status).to eq 404
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user