From ffa8a8c7d685e04e39b445d1abc840ae8628123f Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 27 Apr 2018 16:04:58 +1000 Subject: [PATCH] Create Api::BaseController to allow use of ActiveModelSerializers Also add index action to Api::CustomersController --- app/controllers/api/base_controller.rb | 13 +++++++++ app/controllers/api/customers_controller.rb | 9 ++++--- app/controllers/api/statuses_controller.rb | 2 +- app/serializers/api/customer_serializer.rb | 5 ++++ config/routes.rb | 2 +- .../api/customers_controller_spec.rb | 27 ++++++++++++++++--- spec/support/api_helper.rb | 15 +++++++++++ 7 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 app/controllers/api/base_controller.rb create mode 100644 app/serializers/api/customer_serializer.rb create mode 100644 spec/support/api_helper.rb diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb new file mode 100644 index 0000000000..f25c47417d --- /dev/null +++ b/app/controllers/api/base_controller.rb @@ -0,0 +1,13 @@ +# Base controller for OFN's API +# Includes the minimum machinery required by ActiveModelSerializers +module Api + class BaseController < Spree::Api::BaseController + # Need to include these because Spree::Api::BaseContoller inherits + # from ActionController::Metal rather than ActionController::Base + # and they are required by ActiveModelSerializers + include ActionController::Serialization + include ActionController::UrlFor + include Rails.application.routes.url_helpers + use_renderers :json + end +end diff --git a/app/controllers/api/customers_controller.rb b/app/controllers/api/customers_controller.rb index 9df7534259..cbbe4ce35f 100644 --- a/app/controllers/api/customers_controller.rb +++ b/app/controllers/api/customers_controller.rb @@ -1,13 +1,16 @@ module Api - class CustomersController < Spree::Api::BaseController - respond_to :json + class CustomersController < BaseController + def index + @customers = current_api_user.customers + render json: @customers, each_serializer: CustomerSerializer + end def update @customer = Customer.find(params[:id]) authorize! :update, @customer if @customer.update_attributes(params[:customer]) - render text: @customer.id, :status => 200 + render json: @customer, serializer: CustomerSerializer, status: 200 else invalid_resource!(@customer) end diff --git a/app/controllers/api/statuses_controller.rb b/app/controllers/api/statuses_controller.rb index c8844b868b..49a6f991ff 100644 --- a/app/controllers/api/statuses_controller.rb +++ b/app/controllers/api/statuses_controller.rb @@ -1,5 +1,5 @@ module Api - class StatusesController < BaseController + class StatusesController < ::BaseController respond_to :json def job_queue diff --git a/app/serializers/api/customer_serializer.rb b/app/serializers/api/customer_serializer.rb new file mode 100644 index 0000000000..44914e0a49 --- /dev/null +++ b/app/serializers/api/customer_serializer.rb @@ -0,0 +1,5 @@ +module Api + class CustomerSerializer < ActiveModel::Serializer + attributes :id, :enterprise_id, :name, :code, :email, :allow_charges + end +end diff --git a/config/routes.rb b/config/routes.rb index f0f6bd4488..ba4da8ae2e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -217,7 +217,7 @@ Openfoodnetwork::Application.routes.draw do get :job_queue end - resources :customers, only: [:update] + resources :customers, only: [:index, :update] post '/product_images/:product_id', to: 'product_images#update_product_image' end diff --git a/spec/controllers/api/customers_controller_spec.rb b/spec/controllers/api/customers_controller_spec.rb index 764f767549..f6c8f4e0a5 100644 --- a/spec/controllers/api/customers_controller_spec.rb +++ b/spec/controllers/api/customers_controller_spec.rb @@ -3,13 +3,32 @@ require 'spec_helper' module Api describe CustomersController, type: :controller do include AuthenticationWorkflow + include OpenFoodNetwork::ApiHelper render_views let(:user) { create(:user) } - let(:customer) { create(:customer, user: user) } - let(:params) { { format: :json, id: customer.id, customer: { code: '123' } } } + + describe "index" do + let!(:customer1) { create(:customer) } + let!(:customer2) { create(:customer) } + + before do + user.customers << customer1 + allow(controller).to receive(:spree_current_user) { user } + end + + it "lists customers associated with the current user" do + spree_get :index + expect(response.status).to eq 200 + expect(json_response.length).to eq 1 + expect(json_response.first[:id]).to eq customer1.id + end + end describe "#update" do + let(:customer) { create(:customer, user: user) } + let(:params) { { format: :json, id: customer.id, customer: { code: '123' } } } + context "as a user who is not associated with the customer" do before do allow(controller).to receive(:spree_current_user) { create(:user) } @@ -30,7 +49,7 @@ module Api it "returns the id of the updated customer" do spree_post :update, params expect(response.status).to eq 200 - expect(response.body).to eq customer.id.to_s + expect(json_response[:id]).to eq customer.id end end @@ -40,7 +59,7 @@ module Api it "returns a 422, with an error message" do spree_post :update, params expect(response.status).to be 422 - expect(JSON.parse(response.body)['error']).to be + expect(json_response[:error]).to be end end end diff --git a/spec/support/api_helper.rb b/spec/support/api_helper.rb new file mode 100644 index 0000000000..08918fb761 --- /dev/null +++ b/spec/support/api_helper.rb @@ -0,0 +1,15 @@ +module OpenFoodNetwork + module ApiHelper + def json_response + json_response = JSON.parse(response.body) + case json_response + when Hash + json_response.with_indifferent_access + when Array + json_response.map(&:with_indifferent_access) + else + json_response + end + end + end +end